Skip to content

Commit 2c7db29

Browse files
Raise a clearer error when open_dataset is given a list of paths (#11526)
* fix: point at open_mfdataset when open_dataset gets a list of paths Passing a list or tuple of paths to open_dataset failed with the generic "did not find a match in any of xarray's currently installed IO backends" message, which does not hint at the actual mistake. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 466ca4f commit 2c7db29

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎doc/whats-new.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ Bug Fixes
415415
- Preserve NumPy ``StringDType`` variables and coordinates in Zarr format 3
416416
round trips (:issue:`11466`, :pull:`11474`).
417417
By `stanbot8 <https://github.com/stanbot8>`_.
418+
- :py:func:`~xarray.open_dataset` now raises an error pointing at
419+
:py:func:`~xarray.open_mfdataset` when it is given a list or tuple of paths,
420+
instead of reporting that no IO backend matched the input (:issue:`6510`).
421+
By `NoiceHex <https://github.com/NoiceHax>`_.
422+
- Fix async zarr tests using ``wraps`` with ``autospec=True`` on async methods,
423+
which caused ``AsyncMock`` objects to leak through instead of real array data
424+
(:pull:`11232`).
425+
By `Joe Hamman <https://github.com/jhamman>`_.
418426
- Fixed dask-backed bottleneck rolling reductions declaring a dtype that could
419427
differ from the dtype returned by the matching numpy-backed bottleneck path,
420428
notably ``object`` instead of ``float64`` for boolean inputs (:pull:`11449`).

‎xarray/backends/plugins.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ def guess_engine(
214214
if not is_remote_uri(store_spec_str) and not os.path.exists(store_spec_str):
215215
raise FileNotFoundError(f"No such file: '{store_spec_str}'")
216216

217+
if isinstance(store_spec, list | tuple):
218+
raise ValueError(
219+
f"open_dataset opens a single file, but got a {type(store_spec).__name__}. "
220+
"Use open_mfdataset to open multiple files as a single dataset."
221+
)
222+
217223
raise ValueError(error_msg)
218224

219225

‎xarray/tests/backends/test_plugins.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ def test_guess_engine_file_not_found() -> None:
256256
plugins.guess_engine("https://example.com/missing.h5")
257257

258258

259+
@mock.patch(
260+
"xarray.backends.plugins.list_engines",
261+
mock.MagicMock(return_value={"dummy": DummyBackendEntrypointArgs()}),
262+
)
263+
@pytest.mark.parametrize("paths", [["a.nc", "b.nc"], ("a.nc", "b.nc")])
264+
def test_guess_engine_sequence_of_paths(paths) -> None:
265+
with pytest.raises(ValueError, match=r"Use open_mfdataset"):
266+
plugins.guess_engine(paths)
267+
268+
259269
@pytest.mark.parametrize("engine", common.BACKEND_ENTRYPOINTS.keys())
260270
def test_get_backend_fastpath_skips_list_engines(engine: str) -> None:
261271
"""Test that built-in engines skip list_engines (fastpath)."""

0 commit comments

Comments
 (0)