Skip to content

Commit dee2188

Browse files
committed
Expose get_node() in the public API
1 parent 9e8b50a commit dee2188

File tree

6 files changed

+54
-28
lines changed

6 files changed

+54
-28
lines changed

changes/xxxx.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added migration guide for ``zarr.storage.contains_array()`` and ``zarr.storage.contains_group()``.

changes/xxxx.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `zarr.storage.get_node` to get a node within a store.

docs/user-guide/v3_migration.rst

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ The Group class
121121
The Store class
122122
~~~~~~~~~~~~~~~
123123

124-
The Store API has changed significant in Zarr-Python 3. The most notable changes to the
125-
Store API are:
124+
The Store API has changed significant in Zarr-Python 3.
126125

127126
Store Import Paths
128127
^^^^^^^^^^^^^^^^^^
@@ -154,8 +153,30 @@ Common replacements:
154153
change ensures that all store methods are non-blocking and are as performant as
155154
possible.
156155

157-
Beyond the changes store interface, a number of deprecated stores were also removed in
158-
Zarr-Python 3. See :issue:`1274` for more details on the removal of these stores.
156+
Removed store API
157+
^^^^^^^^^^^^^^^^^
158+
159+
``zarr.storage.contains_array()`` and ``zarr.storage.contains_group()`` have been removed.
160+
Instead, use `zarr.storage.get_node`, and check the return type, e.g.,
161+
162+
>>> import zarr
163+
>>> import zarr.storage
164+
>>>
165+
>>> store = zarr.storage.MemoryStore()
166+
>>> root = zarr.create_group(store=store)
167+
>>> isinstance(zarr.storage.get_node(store, path="", zarr_format=3), zarr.Group)
168+
True
169+
>>>
170+
>>> root.create_array("b", shape=(1,), dtype=int)
171+
<Array memory://.../b shape=(1,) dtype=int64>
172+
>>> isinstance(zarr.storage.get_node(store, path="b", zarr_format=3), zarr.Array)
173+
True
174+
175+
Removed stores
176+
^^^^^^^^^^^^^^
177+
178+
A number of deprecated stores were removed in Zarr-Python 3. See :issue:`1274` for more
179+
details on the removal of these stores.
159180

160181
- ``N5Store`` - see https://github.com/zarr-developers/n5py for an alternative interface to
161182
N5 formatted data.

src/zarr/core/sync_group.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
from zarr.core.group import create_hierarchy as create_hierarchy_async
77
from zarr.core.group import create_nodes as create_nodes_async
88
from zarr.core.group import create_rooted_hierarchy as create_rooted_hierarchy_async
9-
from zarr.core.group import get_node as get_node_async
109
from zarr.core.sync import _collect_aiterator, sync
1110

1211
if TYPE_CHECKING:
1312
from collections.abc import Iterator
1413

1514
from zarr.abc.store import Store
1615
from zarr.core.array import Array
17-
from zarr.core.common import ZarrFormat
1816
from zarr.core.metadata import ArrayV2Metadata, ArrayV3Metadata
1917

2018

@@ -138,24 +136,3 @@ def create_rooted_hierarchy(
138136
"""
139137
async_node = sync(create_rooted_hierarchy_async(store=store, nodes=nodes, overwrite=overwrite))
140138
return _parse_async_node(async_node)
141-
142-
143-
def get_node(store: Store, path: str, zarr_format: ZarrFormat) -> Array | Group:
144-
"""
145-
Get an Array or Group from a path in a Store.
146-
147-
Parameters
148-
----------
149-
store : Store
150-
The store-like object to read from.
151-
path : str
152-
The path to the node to read.
153-
zarr_format : {2, 3}
154-
The zarr format of the node to read.
155-
156-
Returns
157-
-------
158-
Array | Group
159-
"""
160-
161-
return _parse_async_node(sync(get_node_async(store=store, path=path, zarr_format=zarr_format)))

src/zarr/storage/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from types import ModuleType
44
from typing import Any
55

6-
from zarr.storage._common import StoreLike, StorePath
6+
from zarr.storage._common import StoreLike, StorePath, get_node
77
from zarr.storage._fsspec import FsspecStore
88
from zarr.storage._local import LocalStore
99
from zarr.storage._logging import LoggingStore
@@ -23,6 +23,7 @@
2323
"StorePath",
2424
"WrapperStore",
2525
"ZipStore",
26+
"get_node",
2627
]
2728

2829

src/zarr/storage/_common.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
from zarr.abc.store import ByteRequest, Store
88
from zarr.core.buffer import Buffer, default_buffer_prototype
99
from zarr.core.common import ZARR_JSON, ZARRAY_JSON, ZGROUP_JSON, AccessModeLiteral, ZarrFormat
10+
from zarr.core.sync import sync
1011
from zarr.errors import ContainsArrayAndGroupError, ContainsArrayError, ContainsGroupError
1112
from zarr.storage._local import LocalStore
1213
from zarr.storage._memory import MemoryStore
1314
from zarr.storage._utils import normalize_path
1415

1516
if TYPE_CHECKING:
17+
from zarr import Array, Group
1618
from zarr.core.buffer import BufferPrototype
1719

1820

@@ -505,3 +507,26 @@ async def contains_group(store_path: StorePath, zarr_format: ZarrFormat) -> bool
505507
return await (store_path / ZGROUP_JSON).exists()
506508
msg = f"Invalid zarr_format provided. Got {zarr_format}, expected 2 or 3" # type: ignore[unreachable]
507509
raise ValueError(msg)
510+
511+
512+
def get_node(store: Store, path: str, zarr_format: ZarrFormat) -> Array | Group:
513+
"""
514+
Get an Array or Group from a path in a Store.
515+
516+
Parameters
517+
----------
518+
store : Store
519+
The store-like object to read from.
520+
path : str
521+
The path to the node to read.
522+
zarr_format : {2, 3}
523+
The zarr format of the node to read.
524+
525+
Returns
526+
-------
527+
Array | Group
528+
"""
529+
from zarr.core.group import _parse_async_node
530+
from zarr.core.group import get_node as get_node_async
531+
532+
return _parse_async_node(sync(get_node_async(store=store, path=path, zarr_format=zarr_format)))

0 commit comments

Comments
 (0)