Skip to content

Commit 15c4a7e

Browse files
committed
rename _from_flat to _create_rooted_hierarchy, add sync version
1 parent 63dd07f commit 15c4a7e

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/zarr/core/group.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,14 +3445,16 @@ def _persist_metadata(
34453445
)
34463446

34473447

3448-
async def _from_flat(
3448+
async def _create_rooted_hierarchy(
34493449
store_path: StorePath,
34503450
*,
34513451
nodes: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata],
34523452
overwrite: bool = False,
3453-
) -> AsyncGroup:
3453+
) -> AsyncGroup | AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
34543454
"""
3455-
Create an ``AsyncGroup`` from a store + a dict of nodes.
3455+
Create an ``AsyncGroup`` or ``AsyncArray`` from a store and a dict of metadata documents.
3456+
This function ensures that its input contains a specification of a root node,
3457+
calls ``create_hierarchy`` to create nodes, and returns the root node of the hierarchy.
34563458
"""
34573459
roots = _get_roots(nodes)
34583460
if len(roots) != 1:
@@ -3476,7 +3478,25 @@ async def _from_flat(
34763478
}
34773479
# the names of the created nodes will be relative to the store_path instance
34783480
root_relative_to_store_path = _join_paths([store_path.path, root])
3479-
root_group = nodes_created[root_relative_to_store_path]
3480-
if not isinstance(root_group, AsyncGroup):
3481-
raise TypeError("Invalid root node returned from create_hierarchy.")
3482-
return root_group
3481+
return nodes_created[root_relative_to_store_path]
3482+
3483+
3484+
def _create_rooted_hierarchy_sync(
3485+
store_path: StorePath,
3486+
*,
3487+
nodes: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata],
3488+
overwrite: bool = False,
3489+
) -> Group | Array:
3490+
"""
3491+
Create a ``Group`` from a store and a dict of metadata documents. Calls the async method
3492+
``_create_rooted_hierarchy`` and waits for the result.
3493+
"""
3494+
async_node = sync(
3495+
_create_rooted_hierarchy(store_path=store_path, nodes=nodes, overwrite=overwrite)
3496+
)
3497+
if isinstance(async_node, AsyncGroup):
3498+
return Group(async_node)
3499+
elif isinstance(async_node, AsyncArray):
3500+
return Array(async_node)
3501+
else:
3502+
raise TypeError(f"Unexpected node type: {type(async_node)}")

tests/test_group.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from zarr.core.group import (
2525
ConsolidatedMetadata,
2626
GroupMetadata,
27-
_from_flat,
27+
_create_rooted_hierarchy,
2828
_join_paths,
2929
_normalize_path_keys,
3030
_normalize_paths,
@@ -1648,7 +1648,7 @@ async def test_group_from_flat(store: Store, zarr_format, path: str, root_key: s
16481648

16491649
nodes_create = root_meta | groups_expected_meta | arrays_expected_meta
16501650

1651-
g = await _from_flat(spath, nodes=nodes_create, overwrite=True)
1651+
g = await _create_rooted_hierarchy(spath, nodes=nodes_create, overwrite=True)
16521652
assert g.metadata.attributes == {"path": root_key}
16531653

16541654
members = await _collect_aiterator(g.members(max_depth=None))

0 commit comments

Comments
 (0)