4141from zarr .core .metadata .v3 import ArrayV3Metadata
4242from zarr .core .sync import _collect_aiterator , sync
4343from zarr .errors import (
44+ ArrayNotFoundError ,
4445 ContainsArrayError ,
4546 ContainsGroupError ,
47+ GroupNotFoundError ,
4648 MetadataValidationError ,
4749 ZarrUserWarning ,
4850)
@@ -101,7 +103,7 @@ async def test_create_creates_parents(store: Store, zarr_format: ZarrFormat) ->
101103 root = await zarr .api .asynchronous .open_group (
102104 store = store ,
103105 )
104- agroup = await root .getitem ("a" )
106+ agroup = await root .get_group ("a" )
105107 assert agroup .attrs == {"key" : "value" }
106108
107109 # create a child node with a couple intermediates
@@ -446,6 +448,77 @@ def test_group_get_with_default(store: Store, zarr_format: ZarrFormat) -> None:
446448 assert result .attrs ["foo" ] == "bar"
447449
448450
451+ def test_group_get_array (store : Store , zarr_format : ZarrFormat ) -> None :
452+ """
453+ `Group.get_array` returns the array at the given path, for both direct child names
454+ and nested paths, and the result is statically typed as an Array.
455+ """
456+ group = Group .from_store (store , zarr_format = zarr_format )
457+ subgroup = group .create_group (name = "subgroup" )
458+ subarray = group .create_array (name = "subarray" , shape = (10 ,), chunks = (10 ,), dtype = "uint8" )
459+ subsubarray = subgroup .create_array (name = "subarray" , shape = (10 ,), chunks = (10 ,), dtype = "uint8" )
460+
461+ observed = group .get_array ("subarray" )
462+ assert isinstance (observed , Array )
463+ assert observed == subarray
464+ assert group .get_array ("subgroup/subarray" ) == subsubarray
465+
466+
467+ def test_group_get_array_missing (store : Store , zarr_format : ZarrFormat ) -> None :
468+ """
469+ `Group.get_array` raises `ArrayNotFoundError` when no node exists at the given path.
470+ """
471+ group = Group .from_store (store , zarr_format = zarr_format )
472+ with pytest .raises (ArrayNotFoundError , match = "No array found in store" ):
473+ group .get_array ("missing" )
474+
475+
476+ def test_group_get_array_wrong_node_type (store : Store , zarr_format : ZarrFormat ) -> None :
477+ """
478+ `Group.get_array` raises `ContainsGroupError` when the node at the given path is a
479+ group rather than an array.
480+ """
481+ group = Group .from_store (store , zarr_format = zarr_format )
482+ group .create_group (name = "subgroup" )
483+ with pytest .raises (ContainsGroupError , match = "A group exists in store" ):
484+ group .get_array ("subgroup" )
485+
486+
487+ def test_group_get_group (store : Store , zarr_format : ZarrFormat ) -> None :
488+ """
489+ `Group.get_group` returns the group at the given path, for both direct child names
490+ and nested paths, and the result is statically typed as a Group.
491+ """
492+ group = Group .from_store (store , zarr_format = zarr_format )
493+ subgroup = group .create_group (name = "subgroup" )
494+ subsubgroup = subgroup .create_group (name = "subsubgroup" )
495+
496+ observed = group .get_group ("subgroup" )
497+ assert isinstance (observed , Group )
498+ assert observed == subgroup
499+ assert group .get_group ("subgroup/subsubgroup" ) == subsubgroup
500+
501+
502+ def test_group_get_group_missing (store : Store , zarr_format : ZarrFormat ) -> None :
503+ """
504+ `Group.get_group` raises `GroupNotFoundError` when no node exists at the given path.
505+ """
506+ group = Group .from_store (store , zarr_format = zarr_format )
507+ with pytest .raises (GroupNotFoundError , match = "No group found in store" ):
508+ group .get_group ("missing" )
509+
510+
511+ def test_group_get_group_wrong_node_type (store : Store , zarr_format : ZarrFormat ) -> None :
512+ """
513+ `Group.get_group` raises `ContainsArrayError` when the node at the given path is an
514+ array rather than a group.
515+ """
516+ group = Group .from_store (store , zarr_format = zarr_format )
517+ group .create_array (name = "subarray" , shape = (10 ,), chunks = (10 ,), dtype = "uint8" )
518+ with pytest .raises (ContainsArrayError , match = "An array exists in store" ):
519+ group .get_group ("subarray" )
520+
521+
449522@pytest .mark .parametrize ("consolidated" , [True , False ])
450523def test_group_delitem (store : Store , zarr_format : ZarrFormat , consolidated : bool ) -> None :
451524 """
@@ -1469,7 +1542,7 @@ async def test_group_getitem_consolidated(self, store: Store) -> None:
14691542
14701543 # On disk, we've consolidated all the metadata in the root zarr.json
14711544 group = await zarr .api .asynchronous .open (store = store )
1472- rg0 = await group .getitem ("g0" )
1545+ rg0 = await group .get_group ("g0" )
14731546
14741547 expected = ConsolidatedMetadata (
14751548 metadata = {
@@ -1490,10 +1563,10 @@ async def test_group_getitem_consolidated(self, store: Store) -> None:
14901563 )
14911564 assert rg0 .metadata .consolidated_metadata == expected
14921565
1493- rg1 = await rg0 .getitem ("g1" )
1566+ rg1 = await rg0 .get_group ("g1" )
14941567 assert rg1 .metadata .consolidated_metadata == expected .metadata ["g1" ].consolidated_metadata
14951568
1496- rg2 = await rg1 .getitem ("g2" )
1569+ rg2 = await rg1 .get_group ("g2" )
14971570 assert rg2 .metadata .consolidated_metadata == ConsolidatedMetadata (metadata = {})
14981571
14991572 async def test_group_delitem_consolidated (self , store : Store ) -> None :
0 commit comments