Skip to content

Commit 88b9583

Browse files
committed
zarr.open should fall back to opening a group
Closes #2309
1 parent c258b27 commit 88b9583

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/zarr/api/asynchronous.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ async def open(
247247

248248
try:
249249
return await open_array(store=store_path, zarr_format=zarr_format, **kwargs)
250-
except KeyError:
250+
except (KeyError, ValueError):
251+
# KeyError for a missing key
252+
# ValueError for failing to parse node metadata as an array when it's
253+
# actually a group
251254
return await open_group(store=store_path, zarr_format=zarr_format, **kwargs)
252255

253256

@@ -580,6 +583,8 @@ async def open_group(
580583
meta_array : array-like, optional
581584
An array instance to use for determining arrays to create and return
582585
to users. Use `numpy.empty(())` by default.
586+
attributes : dict
587+
A dictionary of JSON-serializable values with user-defined attributes.
583588
584589
Returns
585590
-------

src/zarr/api/synchronous.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def open_group(
207207
zarr_version: ZarrFormat | None = None, # deprecated
208208
zarr_format: ZarrFormat | None = None,
209209
meta_array: Any | None = None, # not used in async api
210+
attributes: dict[str, JSON] | None = None,
210211
) -> Group:
211212
return Group(
212213
sync(
@@ -221,6 +222,7 @@ def open_group(
221222
zarr_version=zarr_version,
222223
zarr_format=zarr_format,
223224
meta_array=meta_array,
225+
attributes=attributes,
224226
)
225227
)
226228
)

tests/v3/test_api.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from numpy.testing import assert_array_equal
77

88
import zarr
9+
import zarr.api.asynchronous
910
from zarr import Array, Group
1011
from zarr.abc.store import Store
1112
from zarr.api.synchronous import create, group, load, open, open_group, save, save_array, save_group
@@ -921,3 +922,23 @@ def test_open_group_positional_args_deprecated() -> None:
921922
store = MemoryStore({}, mode="w")
922923
with pytest.warns(FutureWarning, match="pass"):
923924
open_group(store, "w")
925+
926+
927+
def test_open_falls_back_to_open_group() -> None:
928+
# https://github.com/zarr-developers/zarr-python/issues/2309
929+
store = MemoryStore(mode="w")
930+
zarr.open_group(store, attributes={"key": "value"})
931+
932+
group = zarr.open(store)
933+
assert isinstance(group, Group)
934+
assert group.attrs == {"key": "value"}
935+
936+
937+
async def test_open_falls_back_to_open_group_async() -> None:
938+
# https://github.com/zarr-developers/zarr-python/issues/2309
939+
store = MemoryStore(mode="w")
940+
await zarr.api.asynchronous.open_group(store, attributes={"key": "value"})
941+
942+
group = await zarr.api.asynchronous.open(store=store)
943+
assert isinstance(group, zarr.api.asynchronous.AsyncGroup)
944+
assert group.attrs == {"key": "value"}

0 commit comments

Comments
 (0)