Use Sequence instead of list for path_or_paths typing#8361
Open
Par-star wants to merge 3 commits into
Open
Conversation
Fixes huggingface#5354 ## What Changes the type annotation of `path_or_paths` from `Union[PathLike, list[PathLike]]` to `Union[PathLike, Sequence[PathLike]]` in: - `Dataset.from_csv` - `Dataset.from_json` - `Dataset.from_parquet` - `Dataset.from_text` ## Why `list` is invariant in mypy, so passing a `List[Union[str, bytes, PathLike]]` (or any other list subtype) to these functions raises a mypy error, even though it works correctly at runtime: error: Argument 1 to "from_parquet" has incompatible type "List[str]"; expected "Union[..., List[Union[str, bytes, PathLike[Any]]]]" [arg-type] note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance note: Consider using "Sequence" instead, which is covariant Since these functions only read/iterate over `path_or_paths` and never mutate it, `Sequence` is the more accurate and mypy-friendly annotation, matching the standard `typing` recommendation for read-only sequence arguments. ## How I tested - Ran `mypy src/datasets/arrow_dataset.py` to confirm no new typing errors. - Ran existing tests: `pytest tests/test_arrow_dataset.py -k "parquet or csv or json or text"` - Confirmed the reported reproduction case from the issue no longer raises a mypy error. ## Backward compatibility No runtime behavior change — `Sequence` still accepts lists, tuples, etc., so this is purely a typing improvement and fully backward compatible.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5354
What
Changes the type annotation of
path_or_pathsfromUnion[PathLike, list[PathLike]]to
Union[PathLike, Sequence[PathLike]]in:Dataset.from_csvDataset.from_jsonDataset.from_parquetDataset.from_textWhy
listis invariant in mypy, so passing aList[Union[str, bytes, PathLike]](or anyother list subtype) to these functions raises a mypy error, even though it works
correctly at runtime:
Since these functions only read/iterate over
path_or_pathsand never mutate it,Sequenceis the more accurate and mypy-friendly annotation, matching the standardtypingrecommendation for read-only sequence arguments.Backward compatibility
No runtime behavior change —
Sequencestill accepts lists, tuples, etc., so this ispurely a typing improvement and fully backward compatible.