Skip to content

Use Sequence instead of list for path_or_paths typing#8361

Open
Par-star wants to merge 3 commits into
huggingface:mainfrom
Par-star:Par-star-patch-1
Open

Use Sequence instead of list for path_or_paths typing#8361
Par-star wants to merge 3 commits into
huggingface:mainfrom
Par-star:Par-star-patch-1

Conversation

@Par-star

Copy link
Copy Markdown

Fixes #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.

Backward compatibility

No runtime behavior change — Sequence still accepts lists, tuples, etc., so this is
purely a typing improvement and fully backward compatible.

Par-star added 3 commits July 23, 2026 15:18
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider using "Sequence" instead of "List"

1 participant