Skip to content

Commit 4fe104a

Browse files
jnid-v-b
andauthored
Fix open(..., mode='w') to create a group (#2629)
* Add test for #2490 * Add 'w' to list of valid modes in open to create a group --------- Co-authored-by: Davis Bennett <[email protected]>
1 parent 372995e commit 4fe104a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/zarr/api/asynchronous.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ async def open(
312312
store_path = await make_store_path(store, mode=mode, path=path, storage_options=storage_options)
313313

314314
# TODO: the mode check below seems wrong!
315-
if "shape" not in kwargs and mode in {"a", "r", "r+"}:
315+
if "shape" not in kwargs and mode in {"a", "r", "r+", "w"}:
316316
try:
317317
metadata_dict = await get_array_metadata(store_path, zarr_format=zarr_format)
318318
# TODO: remove this cast when we fix typing for array metadata dicts

tests/test_api.py

+7
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,13 @@ async def test_open_falls_back_to_open_group_async() -> None:
10861086
assert group.attrs == {"key": "value"}
10871087

10881088

1089+
def test_open_mode_write_creates_group(tmp_path: pathlib.Path) -> None:
1090+
# https://github.com/zarr-developers/zarr-python/issues/2490
1091+
zarr_dir = tmp_path / "test.zarr"
1092+
group = zarr.open(zarr_dir, mode="w")
1093+
assert isinstance(group, Group)
1094+
1095+
10891096
async def test_metadata_validation_error() -> None:
10901097
with pytest.raises(
10911098
MetadataValidationError,

0 commit comments

Comments
 (0)