Skip to content

Commit b483543

Browse files
authored
Fix creating groups with group() (#3431)
* Fix creating groups with group() * Add bugfix entry * Fix import order * Rename 3431.bufix.rst to 3431.bugfix.rst
1 parent 00e7814 commit b483543

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

changes/3431.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Creating a new group with `zarr.group` no longer errors.
2+
This fixes a regression introduced in version 3.1.2.

src/zarr/api/asynchronous.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -670,38 +670,24 @@ async def group(
670670
g : group
671671
The new group.
672672
"""
673-
674-
zarr_format = _handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
675-
676673
mode: AccessModeLiteral
677674
if overwrite:
678675
mode = "w"
679676
else:
680-
mode = "r+"
681-
store_path = await make_store_path(store, path=path, mode=mode, storage_options=storage_options)
682-
683-
if chunk_store is not None:
684-
warnings.warn("chunk_store is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
685-
if cache_attrs is not None:
686-
warnings.warn("cache_attrs is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
687-
if synchronizer is not None:
688-
warnings.warn("synchronizer is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
689-
if meta_array is not None:
690-
warnings.warn("meta_array is not yet implemented", ZarrRuntimeWarning, stacklevel=2)
691-
692-
if attributes is None:
693-
attributes = {}
694-
695-
try:
696-
return await AsyncGroup.open(store=store_path, zarr_format=zarr_format)
697-
except (KeyError, FileNotFoundError):
698-
_zarr_format = zarr_format or _default_zarr_format()
699-
return await AsyncGroup.from_store(
700-
store=store_path,
701-
zarr_format=_zarr_format,
702-
overwrite=overwrite,
703-
attributes=attributes,
704-
)
677+
mode = "a"
678+
return await open_group(
679+
store=store,
680+
mode=mode,
681+
chunk_store=chunk_store,
682+
cache_attrs=cache_attrs,
683+
synchronizer=synchronizer,
684+
path=path,
685+
zarr_version=zarr_version,
686+
zarr_format=zarr_format,
687+
meta_array=meta_array,
688+
attributes=attributes,
689+
storage_options=storage_options,
690+
)
705691

706692

707693
async def create_group(

tests/test_api/test_asynchronous.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import pytest
99

1010
from zarr import create_array
11-
from zarr.api.asynchronous import _get_shape_chunks, _like_args, open
11+
from zarr.api.asynchronous import _get_shape_chunks, _like_args, group, open
1212
from zarr.core.buffer.core import default_buffer_prototype
13+
from zarr.core.group import AsyncGroup
1314

1415
if TYPE_CHECKING:
16+
from pathlib import Path
1517
from typing import Any
1618

1719
import numpy.typing as npt
@@ -103,3 +105,18 @@ async def test_open_no_array() -> None:
103105
TypeError, match=r"open_group\(\) got an unexpected keyword argument 'shape'"
104106
):
105107
await open(store=store, shape=(1,))
108+
109+
110+
async def test_open_group_new_path(tmp_path: Path) -> None:
111+
"""
112+
Test that zarr.api.asynchronous.group properly handles a string representation of a local file
113+
path that does not yet exist.
114+
See https://github.com/zarr-developers/zarr-python/issues/3406
115+
"""
116+
# tmp_path exists, but tmp_path / "test.zarr" will not, which is important for this test
117+
path = tmp_path / "test.zarr"
118+
grp = await group(store=path, attributes={"a": 1})
119+
assert isinstance(grp, AsyncGroup)
120+
# Calling group on an existing store should just open that store
121+
grp = await group(store=path)
122+
assert grp.attrs == {"a": 1}

0 commit comments

Comments
 (0)