Skip to content

Commit b2d8519

Browse files
authored
QOL: detect both .zarr and .zarr/ as using the zarr engine (#10697)
* QOL: detect both .zarr and .zarr/ as using the zarr engine * test: properly test the opening with a trailing slash
1 parent 7a3cd0c commit b2d8519

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ New Features
2323
- ``compute=False`` is now supported by :py:meth:`DataTree.to_netcdf` and
2424
:py:meth:`DataTree.to_zarr`.
2525
By `Stephan Hoyer <https://github.com/shoyer>`_.
26+
- ``open_dataset`` will now correctly infer a path ending in ``.zarr/`` as zarr
27+
By `Ian Hunt-Isaak <https://github.com/ianhi>`_.
2628

2729
Breaking changes
2830
~~~~~~~~~~~~~~~~

xarray/backends/zarr.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,8 +1618,10 @@ class ZarrBackendEntrypoint(BackendEntrypoint):
16181618

16191619
def guess_can_open(self, filename_or_obj: T_PathFileOrDataStore) -> bool:
16201620
if isinstance(filename_or_obj, str | os.PathLike):
1621-
_, ext = os.path.splitext(filename_or_obj)
1622-
return ext == ".zarr"
1621+
# allow a trailing slash to account for an autocomplete
1622+
# adding it.
1623+
_, ext = os.path.splitext(str(filename_or_obj).rstrip("/"))
1624+
return ext in [".zarr"]
16231625

16241626
return False
16251627

xarray/tests/test_backends.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7149,6 +7149,31 @@ def test_h5netcdf_entrypoint(tmp_path: Path) -> None:
71497149
assert not entrypoint.guess_can_open("not-found-and-no-extension")
71507150

71517151

7152+
@requires_zarr
7153+
def test_zarr_entrypoint(tmp_path: Path) -> None:
7154+
from xarray.backends.zarr import ZarrBackendEntrypoint
7155+
7156+
entrypoint = ZarrBackendEntrypoint()
7157+
ds = create_test_data()
7158+
7159+
path = tmp_path / "foo.zarr"
7160+
ds.to_zarr(path)
7161+
_check_guess_can_open_and_open(entrypoint, path, engine="zarr", expected=ds)
7162+
_check_guess_can_open_and_open(entrypoint, str(path), engine="zarr", expected=ds)
7163+
7164+
# add a trailing slash to the path and check again
7165+
_check_guess_can_open_and_open(
7166+
entrypoint, str(path) + "/", engine="zarr", expected=ds
7167+
)
7168+
7169+
# Test the new functionality: .zarr with trailing slash
7170+
assert entrypoint.guess_can_open("something-local.zarr")
7171+
assert entrypoint.guess_can_open("something-local.zarr/") # With trailing slash
7172+
assert not entrypoint.guess_can_open("something-local.nc")
7173+
assert not entrypoint.guess_can_open("not-found-and-no-extension")
7174+
assert not entrypoint.guess_can_open("something.zarr.txt")
7175+
7176+
71527177
@requires_netCDF4
71537178
@pytest.mark.parametrize("str_type", (str, np.str_))
71547179
def test_write_file_from_np_str(str_type: type[str | np.str_], tmpdir: str) -> None:

0 commit comments

Comments
 (0)