Skip to content

Fix open_mfdataset for list of fsspec files #9785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions xarray/backends/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ def _find_absolute_paths(
def _normalize_path_list(
lpaths: NestedSequence[str | os.PathLike],
) -> NestedSequence[str]:
return [
(
_normalize_path(p)
if isinstance(p, str | os.PathLike)
else _normalize_path_list(p)
)
for p in lpaths
]
paths = []
for p in lpaths:
if isinstance(p, str | os.PathLike):
paths.append(_normalize_path(p))
elif isinstance(p, list):
paths.append(_normalize_path_list(p)) # type: ignore[arg-type]
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you tell me what the expected type here is?
The type hints only "allow" str, os.PathLike or a list of these.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically whatever

fs = fsspec.filesystem("file")
fs.open(my_file)

returns

Copy link
Member

Choose a reason for hiding this comment

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

FYI trying to determine what type fs.open returns could be a massive rabbit hole - see fsspec/filesystem_spec#579 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I guess a minimal protocol similar to how pandas handles it should be the solution then.

paths.append(p) # type: ignore[arg-type]
return paths

return _normalize_path_list(paths)

Expand Down
2 changes: 2 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5511,6 +5511,8 @@ def test_source_encoding_always_present_with_fsspec() -> None:
fs = fsspec.filesystem("file")
with fs.open(tmp) as f, open_dataset(f) as ds:
assert ds.encoding["source"] == tmp
with fs.open(tmp) as f, open_mfdataset([f]) as ds:
assert "foo" in ds


def _assert_no_dates_out_of_range_warning(record):
Expand Down
Loading