Skip to content

Commit 488d57f

Browse files
committed
fixed members
1 parent 05cf652 commit 488d57f

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/zarr/core/group.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,16 @@ async def nmembers(
11241124
-------
11251125
count : int
11261126
"""
1127-
if self.metadata.consolidated_metadata is not None:
1127+
# check if we can use consolidated metadata, which requires that we have non-None
1128+
# consolidated metadata at all points in the hierarchy.
1129+
use_consolidated_metadata = self.metadata.consolidated_metadata is not None and all(
1130+
x.consolidated_metadata is not None
1131+
for x in self.metadata.consolidated_metadata.flattened_metadata.values()
1132+
if isinstance(x, GroupMetadata)
1133+
)
1134+
1135+
if use_consolidated_metadata:
1136+
assert self.metadata.consolidated_metadata is not None # helping mypy
11281137
return len(self.metadata.consolidated_metadata.flattened_metadata)
11291138
# TODO: consider using aioitertools.builtins.sum for this
11301139
# return await aioitertools.builtins.sum((1 async for _ in self.members()), start=0)
@@ -1174,13 +1183,16 @@ async def _members(
11741183
]:
11751184
if self.metadata.consolidated_metadata is not None:
11761185
# we should be able to do members without any additional I/O
1186+
members = self._members_consolidated(max_depth, current_depth)
1187+
11771188
try:
1178-
members = self._members_consolidated(max_depth, current_depth)
1189+
# we already have this in memory, so fine to build this list
1190+
# and catch the exception if needed.
1191+
members_ = list(members)
11791192
except _MixedConsolidatedMetadataException:
1180-
# we've already logged this. We'll fall back to the non-consolidated version.
11811193
pass
11821194
else:
1183-
for member in members:
1195+
for member in members_:
11841196
yield member
11851197
return
11861198

@@ -1238,6 +1250,8 @@ def _members_consolidated(
12381250
]:
12391251
consolidated_metadata = self.metadata.consolidated_metadata
12401252

1253+
if consolidated_metadata is None:
1254+
raise _MixedConsolidatedMetadataException(prefix)
12411255
# we kind of just want the top-level keys.
12421256
if consolidated_metadata is not None:
12431257
for key in consolidated_metadata.metadata.keys():

tests/v3/test_group.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,6 @@ def test_group_getitem(store: Store, zarr_format: ZarrFormat, consolidated: bool
327327

328328
# Now test the mixed case
329329
if consolidated:
330-
group = zarr.api.synchronous.consolidate_metadata(store=store, zarr_format=zarr_format)
331-
# we're going to assume that `group.metadata` is correct, and reuse that to focus
332-
# on indexing in this test. Other tests verify the correctness of group.metadata
333330
object.__setattr__(
334331
group.metadata.consolidated_metadata.metadata["subgroup"],
335332
"consolidated_metadata",
@@ -1036,6 +1033,20 @@ async def test_group_members_async(store: Store, consolidated_metadata: bool) ->
10361033
with pytest.raises(ValueError, match="max_depth"):
10371034
[x async for x in group.members(max_depth=-1)]
10381035

1036+
if consolidated_metadata:
1037+
# test for mixed
1038+
object.__setattr__(
1039+
group.metadata.consolidated_metadata.metadata["g0"].consolidated_metadata.metadata[
1040+
"g1"
1041+
],
1042+
"consolidated_metadata",
1043+
None,
1044+
)
1045+
all_children = sorted([x async for x in group.members(max_depth=None)], key=lambda x: x[0])
1046+
assert len(all_children) == len(expected)
1047+
nmembers = await group.nmembers(max_depth=None)
1048+
assert nmembers == 6
1049+
10391050

10401051
async def test_require_group(store: LocalStore | MemoryStore, zarr_format: ZarrFormat) -> None:
10411052
root = await AsyncGroup.from_store(store=store, zarr_format=zarr_format)

0 commit comments

Comments
 (0)