Skip to content

Commit cf2f019

Browse files
committed
Raise helpful errors when opening a group with wrong node type.
1 parent ee9c182 commit cf2f019

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/zarr/core/group.py

Lines changed: 6 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,11 @@ 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+
raise ContainsArrayError(store_path.store, store_path.path)
680+
elif node_type not in ("group", None):
681+
raise GroupNotFoundError(store_path.store, store_path.path)
676682
return cls(
677683
metadata=GroupMetadata.from_dict(data),
678684
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)