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. diff --git a/src/zarr/core/group.py b/src/zarr/core/group.py index 15a256fb5d..85d83713e4 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,13 @@ def from_dict( store_path: StorePath, data: dict[str, Any], ) -> AsyncGroup: + node_type = data.pop("node_type", None) + if node_type == "array": + 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): + 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, 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)