Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions changes/3352.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix `zarr.api.asynchronous.open_like` so it can create a new array by default when the
target path does not already exist. It now defaults to `mode="a"`; when using a read-only
store to open an existing array, pass `mode="r"` explicitly.
6 changes: 5 additions & 1 deletion src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,14 +1292,18 @@ async def open_like(a: ArrayLike, path: str, **kwargs: Any) -> AnyAsyncArray:
path : str
The path to the new array.
**kwargs
Any keyword arguments to pass to the array constructor.
Additional keyword arguments passed to `open_array`.
If `mode` is omitted or `None`, it defaults to `"a"`. Pass `mode="r"` when
opening an existing array from a read-only store.

Returns
-------
AsyncArray
The opened array.
"""
like_kwargs = _like_args(a) | kwargs
if like_kwargs.get("mode") is None:
like_kwargs["mode"] = "a"
return await open_array(path=path, **like_kwargs) # type: ignore[arg-type]


Expand Down
6 changes: 4 additions & 2 deletions src/zarr/api/synchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,11 +1399,13 @@ def open_like(a: ArrayLike, path: str, **kwargs: Any) -> AnyArray:
path : str
The path to the new array.
**kwargs
Any keyword arguments to pass to the array constructor.
Additional keyword arguments passed to `open_array`.
If `mode` is omitted or `None`, it defaults to `"a"`. Pass `mode="r"` when
opening an existing array from a read-only store.

Returns
-------
AsyncArray
Array
The opened array.
"""
return Array(sync(async_api.open_like(a, path=path, **kwargs)))
Expand Down
47 changes: 47 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,53 @@ async def test_array_like_creation(
assert np.all(Array(new_arr)[:] == expect_fill)


@pytest.mark.parametrize("mode_kwargs", [{}, {"mode": None}])
async def test_open_like_creates_array_by_default(
zarr_format: ZarrFormat, mode_kwargs: dict[str, None]
) -> None:
ref_arr = zarr.create_array(
store={},
shape=(11, 12),
dtype="uint8",
chunks=(11, 12),
zarr_format=zarr_format,
fill_value=100,
)

new_arr = await zarr.api.asynchronous.open_like(
ref_arr,
path="foo",
store={},
zarr_format=zarr_format,
**mode_kwargs,
)

assert new_arr.shape == ref_arr.shape
assert new_arr.chunks == ref_arr.chunks
assert new_arr.dtype == ref_arr.dtype
assert np.all(Array(new_arr)[:] == ref_arr.fill_value)


async def test_open_like_default_mode_rejects_read_only_store(
zarr_format: ZarrFormat,
) -> None:
ref_arr = zarr.create_array(
store={},
shape=(11, 12),
dtype="uint8",
chunks=(11, 12),
zarr_format=zarr_format,
)

with pytest.raises(ValueError, match="Store is read-only but mode is 'a'"):
await zarr.api.asynchronous.open_like(
ref_arr,
path="foo",
store=MemoryStore(read_only=True),
zarr_format=zarr_format,
)


# TODO: parametrize over everything this function takes
@pytest.mark.parametrize("store", ["memory"], indirect=True)
def test_create_array(store: Store, zarr_format: ZarrFormat) -> None:
Expand Down
Loading