Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zarr.open should fall back to opening a group #2310

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ async def open(

try:
return await open_array(store=store_path, zarr_format=zarr_format, **kwargs)
except KeyError:
except (KeyError, ValueError):
# KeyError for a missing key
# ValueError for failing to parse node metadata as an array when it's
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should create a specific exception for this case, since "valid document but the wrong node type" is different from "invalid document"

# actually a group
return await open_group(store=store_path, zarr_format=zarr_format, **kwargs)


Expand Down Expand Up @@ -580,6 +583,8 @@ async def open_group(
meta_array : array-like, optional
An array instance to use for determining arrays to create and return
to users. Use `numpy.empty(())` by default.
attributes : dict
A dictionary of JSON-serializable values with user-defined attributes.

Returns
-------
Expand Down
2 changes: 2 additions & 0 deletions src/zarr/api/synchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def open_group(
zarr_version: ZarrFormat | None = None, # deprecated
zarr_format: ZarrFormat | None = None,
meta_array: Any | None = None, # not used in async api
attributes: dict[str, JSON] | None = None,
) -> Group:
return Group(
sync(
Expand All @@ -221,6 +222,7 @@ def open_group(
zarr_version=zarr_version,
zarr_format=zarr_format,
meta_array=meta_array,
attributes=attributes,
)
)
)
Expand Down
21 changes: 21 additions & 0 deletions tests/v3/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from numpy.testing import assert_array_equal

import zarr
import zarr.api.asynchronous
from zarr import Array, Group
from zarr.abc.store import Store
from zarr.api.synchronous import create, group, load, open, open_group, save, save_array, save_group
Expand Down Expand Up @@ -921,3 +922,23 @@ def test_open_group_positional_args_deprecated() -> None:
store = MemoryStore({}, mode="w")
with pytest.warns(FutureWarning, match="pass"):
open_group(store, "w")


def test_open_falls_back_to_open_group() -> None:
# https://github.com/zarr-developers/zarr-python/issues/2309
store = MemoryStore(mode="w")
zarr.open_group(store, attributes={"key": "value"})

group = zarr.open(store)
assert isinstance(group, Group)
assert group.attrs == {"key": "value"}


async def test_open_falls_back_to_open_group_async() -> None:
# https://github.com/zarr-developers/zarr-python/issues/2309
store = MemoryStore(mode="w")
await zarr.api.asynchronous.open_group(store, attributes={"key": "value"})

group = await zarr.api.asynchronous.open(store=store)
assert isinstance(group, zarr.api.asynchronous.AsyncGroup)
assert group.attrs == {"key": "value"}