Skip to content

Commit

Permalink
fix open_array for mode r+ (#2494)
Browse files Browse the repository at this point in the history
* fix open_array for mode r+

* fix v2 fill value test

* use zarr.create instead of zarr.open_array
  • Loading branch information
d-v-b authored Nov 15, 2024
1 parent f74e53a commit ec0efad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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

0 comments on commit ec0efad

Please sign in to comment.