From cf2f0195879036dcd5272e8a987c1a93378cc4c2 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 9 Sep 2025 11:26:29 +0100 Subject: [PATCH 1/3] Raise helpful errors when opening a group with wrong node type. --- src/zarr/core/group.py | 6 ++++++ tests/test_group.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/zarr/core/group.py b/src/zarr/core/group.py index 15a256fb5d..0156c20a2d 100644 --- a/src/zarr/core/group.py +++ b/src/zarr/core/group.py @@ -53,6 +53,7 @@ from zarr.errors import ( ContainsArrayError, ContainsGroupError, + GroupNotFoundError, MetadataValidationError, ZarrDeprecationWarning, ZarrUserWarning, @@ -673,6 +674,11 @@ def from_dict( store_path: StorePath, data: dict[str, Any], ) -> AsyncGroup: + node_type = data.pop("node_type", None) + if node_type == "array": + raise ContainsArrayError(store_path.store, store_path.path) + elif node_type not in ("group", None): + raise GroupNotFoundError(store_path.store, store_path.path) return cls( metadata=GroupMetadata.from_dict(data), store_path=store_path, diff --git a/tests/test_group.py b/tests/test_group.py index 2d9070bd67..eb4af7336f 100644 --- a/tests/test_group.py +++ b/tests/test_group.py @@ -2234,3 +2234,9 @@ def test_get_roots(roots: tuple[str, ...]): } data = root_nodes | child_nodes assert set(_get_roots(data)) == set(roots) + + +def test_open_array_as_group(): + z = zarr.create_array(shape=(40, 50), chunks=(10, 10), dtype="f8", store={}) + with pytest.raises(ContainsArrayError): + zarr.open_group(z.store) From e8827e26e5aadacb458435a6ed55c552685ffb91 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 9 Sep 2025 21:39:58 +0100 Subject: [PATCH 2/3] Add changelog --- changes/3444.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/3444.feature.rst diff --git a/changes/3444.feature.rst b/changes/3444.feature.rst new file mode 100644 index 0000000000..d621c428b6 --- /dev/null +++ b/changes/3444.feature.rst @@ -0,0 +1 @@ +Trying to open a group at a path were a array already exists now raises a helpful error. From 0c77e6eb4c1a40d27a3d99a73ac01cce59b07c67 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 10 Sep 2025 09:13:58 +0100 Subject: [PATCH 3/3] Use custom messages --- src/zarr/core/group.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/zarr/core/group.py b/src/zarr/core/group.py index 0156c20a2d..85d83713e4 100644 --- a/src/zarr/core/group.py +++ b/src/zarr/core/group.py @@ -676,9 +676,11 @@ def from_dict( ) -> AsyncGroup: node_type = data.pop("node_type", None) if node_type == "array": - raise ContainsArrayError(store_path.store, store_path.path) + msg = f"An array already exists in store {store_path.store} at path {store_path.path}." + raise ContainsArrayError(msg) elif node_type not in ("group", None): - raise GroupNotFoundError(store_path.store, store_path.path) + msg = f"Node type in metadata ({node_type}) is not 'group'" + raise GroupNotFoundError(msg) return cls( metadata=GroupMetadata.from_dict(data), store_path=store_path,