Skip to content

Commit a830d02

Browse files
dstansbyd-v-b
andauthored
Raise helpful errors when opening a group with wrong node type. (#3444)
* Raise helpful errors when opening a group with wrong node type. * Add changelog * Use custom messages --------- Co-authored-by: Davis Bennett <[email protected]>
1 parent df161b2 commit a830d02

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

changes/3444.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Trying to open a group at a path were a array already exists now raises a helpful error.

src/zarr/core/group.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from zarr.errors import (
5454
ContainsArrayError,
5555
ContainsGroupError,
56+
GroupNotFoundError,
5657
MetadataValidationError,
5758
ZarrDeprecationWarning,
5859
ZarrUserWarning,
@@ -673,6 +674,13 @@ def from_dict(
673674
store_path: StorePath,
674675
data: dict[str, Any],
675676
) -> AsyncGroup:
677+
node_type = data.pop("node_type", None)
678+
if node_type == "array":
679+
msg = f"An array already exists in store {store_path.store} at path {store_path.path}."
680+
raise ContainsArrayError(msg)
681+
elif node_type not in ("group", None):
682+
msg = f"Node type in metadata ({node_type}) is not 'group'"
683+
raise GroupNotFoundError(msg)
676684
return cls(
677685
metadata=GroupMetadata.from_dict(data),
678686
store_path=store_path,

tests/test_group.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,3 +2234,9 @@ def test_get_roots(roots: tuple[str, ...]):
22342234
}
22352235
data = root_nodes | child_nodes
22362236
assert set(_get_roots(data)) == set(roots)
2237+
2238+
2239+
def test_open_array_as_group():
2240+
z = zarr.create_array(shape=(40, 50), chunks=(10, 10), dtype="f8", store={})
2241+
with pytest.raises(ContainsArrayError):
2242+
zarr.open_group(z.store)

0 commit comments

Comments
 (0)