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

fix open_array for mode r+ #2494

Merged
merged 3 commits into from
Nov 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ async def open_array(
try:
return await AsyncArray.open(store_path, zarr_format=zarr_format)
except FileNotFoundError:
if not store_path.read_only:
if not store_path.read_only and mode in _CREATE_MODES:
exists_ok = _infer_exists_ok(mode)
_zarr_format = zarr_format or _default_zarr_version()
return await create(
Expand Down
16 changes: 16 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,3 +1030,19 @@ async def test_metadata_validation_error() -> None:
match="Invalid value for 'zarr_format'. Expected '2, 3, or None'. Got '3.0'.",
):
await zarr.api.asynchronous.open_array(shape=(1,), zarr_format="3.0") # type: ignore[arg-type]


@pytest.mark.parametrize(
"store",
["local", "memory", "zip"],
indirect=True,
)
def test_open_array_with_mode_r_plus(store: Store) -> None:
# 'r+' means read/write (must exist)
with pytest.raises(FileNotFoundError):
zarr.open_array(store=store, mode="r+")
zarr.ones(store=store, shape=(3, 3))
z2 = zarr.open_array(store=store, mode="r+")
assert isinstance(z2, Array)
assert (z2[:] == 1).all()
z2[:] = 3
5 changes: 3 additions & 2 deletions tests/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_simple(store: StorePath) -> None:
assert np.array_equal(data, a[:, :])


@pytest.mark.parametrize("store", ["memory"], indirect=True)
@pytest.mark.parametrize(
("dtype", "fill_value"),
[
Expand All @@ -48,8 +49,8 @@ def test_simple(store: StorePath) -> None:
(str, ""),
],
)
def test_implicit_fill_value(store: StorePath, dtype: str, fill_value: Any) -> None:
arr = zarr.open_array(store=store, shape=(4,), fill_value=None, zarr_format=2, dtype=dtype)
def test_implicit_fill_value(store: MemoryStore, dtype: str, fill_value: Any) -> None:
arr = zarr.create(store=store, shape=(4,), fill_value=None, zarr_format=2, dtype=dtype)
assert arr.metadata.fill_value is None
assert arr.metadata.to_dict()["fill_value"] is None
result = arr[:]
Expand Down