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.
"""Append a 'file_name' column with the source file path, when return_file_name=True."""
if self.config.return_file_name:
pa_table = pa_table.append_column(
"file_name", pa.array([str(file)] * len(pa_table), type=pa.string())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For remote URLs, file here is the DownloadManager cache/extraction path, so return_file_name=True returns a machine-local filename instead of the source URL. The agent-trace branch below uses original_files[shard_idx] for this; should this helper get that value too?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right — for remote files, file was the
DownloadManager's resolved local cache path, not the source path/URL.

I've updated _add_file_name_column to use original_files[shard_idx]
consistently across all call sites (the field-specific path, the pandas
fallback path, and the main JSON-lines path). For the agent-traces branch
I reused the file_path variable that was already being computed the
same way for example["file_path"], so both stay consistent.

Pushed the fix — let me know if this looks right to you.

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"

2 participants