Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/zarr/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,18 @@ async def nmembers(
# check if we can use consolidated metadata, which requires that we have non-None
# consolidated metadata at all points in the hierarchy.
if self.metadata.consolidated_metadata is not None:
return len(self.metadata.consolidated_metadata.flattened_metadata)
if max_depth is not None and max_depth < 0:
raise ValueError(f"max_depth must be None or >= 0. Got '{max_depth}' instead")
if max_depth is None:
return len(self.metadata.consolidated_metadata.flattened_metadata)
else:
return len(
[
x
for x in self.metadata.consolidated_metadata.flattened_metadata
if x.count("/") <= max_depth
]
)
# TODO: consider using aioitertools.builtins.sum for this
# return await aioitertools.builtins.sum((1 async for _ in self.members()), start=0)
n = 0
Expand Down
7 changes: 7 additions & 0 deletions tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,13 @@ async def test_group_members_async(store: Store, consolidated_metadata: bool) ->
"consolidated_metadata",
None,
)
# test depth=0
nmembers = await group.nmembers(max_depth=0)
assert nmembers == 2
# test depth=1
nmembers = await group.nmembers(max_depth=1)
assert nmembers == 4
# test depth=None
all_children = sorted(
[x async for x in group.members(max_depth=None)], key=operator.itemgetter(0)
)
Expand Down
Loading