fix: Image Feature in Datasets Library Fails to Handle bytearray Objects from Spark DataFrames (#7517)
CONTRIBUTOR
@lhoestq let me know if you prefer to change the spark iterator so it outputs `bytes`
2025-04-16T06:57:21Z
https://github.com/huggingface/datasets/pull/7521#issuecomment-2808580329
https://api.github.com/repos/huggingface/datasets/issues/7521
IC_kwDODunzps6nZ4jp
https://api.github.com/repos/huggingface/datasets/issues/comments/2808580329/reactions
2025-04-16T06:57:21Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2808580329
https://avatars.githubusercontent.com/u/73196164?v=4
https://api.github.com/users/giraffacarp/events{/privacy}
https://api.github.com/users/giraffacarp/followers
https://api.github.com/users/giraffacarp/following{/other_user}
https://api.github.com/users/giraffacarp/gists{/gist_id}
https://github.com/giraffacarp
giraffacarp
MDQ6VXNlcjczMTk2MTY0
https://api.github.com/users/giraffacarp/orgs
https://api.github.com/users/giraffacarp/received_events
https://api.github.com/users/giraffacarp/repos
https://api.github.com/users/giraffacarp/starred{/owner}{/repo}
https://api.github.com/users/giraffacarp/subscriptions
User
https://api.github.com/users/giraffacarp
public
NONE
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_7521). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.
2025-05-07T14:13:01Z
https://github.com/huggingface/datasets/pull/7521#issuecomment-2858758474
https://api.github.com/repos/huggingface/datasets/issues/7521
IC_kwDODunzps6qZTFK
https://api.github.com/repos/huggingface/datasets/issues/comments/2858758474/reactions
2025-05-07T14:13:01Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2858758474
https://avatars.githubusercontent.com/u/99929124?v=4
https://api.github.com/users/HuggingFaceDocBuilderDev/events{/privacy}
https://api.github.com/users/HuggingFaceDocBuilderDev/followers
https://api.github.com/users/HuggingFaceDocBuilderDev/following{/other_user}
https://api.github.com/users/HuggingFaceDocBuilderDev/gists{/gist_id}
https://github.com/HuggingFaceDocBuilderDev
HuggingFaceDocBuilderDev
U_kgDOBfTMJA
https://api.github.com/users/HuggingFaceDocBuilderDev/orgs
https://api.github.com/users/HuggingFaceDocBuilderDev/received_events
https://api.github.com/users/HuggingFaceDocBuilderDev/repos
https://api.github.com/users/HuggingFaceDocBuilderDev/starred{/owner}{/repo}
https://api.github.com/users/HuggingFaceDocBuilderDev/subscriptions
User
https://api.github.com/users/HuggingFaceDocBuilderDev
public
## Task
Support bytes-like objects (bytes and bytearray) in Features classes

### Description
The `Features` classes only accept `bytes` objects for binary data, but not `bytearray`. This leads to errors when using `IterableDataset.from_spark()` with Spark DataFrames as they contain `bytearray` objects, even though both `bytes` and `bytearray` are valid [*bytes-like objects* in Python](https://docs.python.org/3/glossary.html#term-bytes-like-object).

### Changes
- Updated `Features` classes to accept both `bytes` and `bytearray` types for binary data fields.

### Reasoning
- `bytes` and `bytearray` serve the same purpose for binary data, with the only difference being mutability.
- Modifying the Spark iterator to convert `bytearray` to `bytes` would be a workaround, not a true fix. I think the correct solution is to accept all bytes-like objects as input.
- This approach is more robust and future-proof since Python 3.12+ provides a [standard way to check for buffer protocol](https://docs.python.org/3/c-api/buffer.html#bufferobjects).

### Testing
- Added tests to cover `bytearray` inputs for image features.

### Related Issues
- Fixes: #7517 