Skip to content

Commit 70d2f1d

Browse files
oliverwm1dcherianpre-commit-ci[bot]
authored
Ensure KeyError raised for zarr datasets missing dim names (#10025)
* Check that KeyError is raised for missing metadata in zarr v3 * Remove dead code * Use open_group so test works with zarr<3 * Use get method instead of attr access * Special handling for zarr-python 3 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Check error message --------- Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4d8bbee commit 70d2f1d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

xarray/backends/zarr.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ def _get_zarr_dims_and_attrs(zarr_obj, dimension_key, try_nczarr):
375375
pass
376376
else:
377377
attributes = dict(zarr_obj.attrs)
378+
if len(zarr_obj.shape) != len(dimensions):
379+
raise KeyError(
380+
"Zarr object is missing the `dimension_names` metadata which is "
381+
"required for xarray to determine variable dimensions."
382+
)
378383
return dimensions, attributes
379384

380385
# Zarr arrays do not have dimensions. To get around this problem, we add
@@ -393,7 +398,13 @@ def _get_zarr_dims_and_attrs(zarr_obj, dimension_key, try_nczarr):
393398

394399
# NCZarr defines dimensions through metadata in .zarray
395400
zarray_path = os.path.join(zarr_obj.path, ".zarray")
396-
zarray = json.loads(zarr_obj.store[zarray_path])
401+
if _zarr_v3():
402+
import asyncio
403+
404+
zarray_str = asyncio.run(zarr_obj.store.get(zarray_path)).to_bytes()
405+
else:
406+
zarray_str = zarr_obj.store.get(zarray_path)
407+
zarray = json.loads(zarray_str)
397408
try:
398409
# NCZarr uses Fully Qualified Names
399410
dimensions = [

xarray/tests/test_backends.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6153,6 +6153,18 @@ def test_zarr_closing_internal_zip_store():
61536153
assert_identical(original_da, loaded_da)
61546154

61556155

6156+
@requires_zarr
6157+
@pytest.mark.usefixtures("default_zarr_format")
6158+
def test_raises_key_error_on_invalid_zarr_store(tmp_path):
6159+
root = zarr.open_group(tmp_path / "tmp.zarr")
6160+
if Version(zarr.__version__) < Version("3.0.0"):
6161+
root.create_dataset("bar", shape=(3, 5), dtype=np.float32)
6162+
else:
6163+
root.create_array("bar", shape=(3, 5), dtype=np.float32)
6164+
with pytest.raises(KeyError, match=r"xarray to determine variable dimensions"):
6165+
xr.open_zarr(tmp_path / "tmp.zarr", consolidated=False)
6166+
6167+
61566168
@requires_zarr
61576169
@pytest.mark.usefixtures("default_zarr_format")
61586170
class TestZarrRegionAuto:

0 commit comments

Comments
 (0)