Skip to content

Commit ec0efad

Browse files
authored
fix open_array for mode r+ (#2494)
* fix open_array for mode r+ * fix v2 fill value test * use zarr.create instead of zarr.open_array
1 parent f74e53a commit ec0efad

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/zarr/api/asynchronous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ async def open_array(
10971097
try:
10981098
return await AsyncArray.open(store_path, zarr_format=zarr_format)
10991099
except FileNotFoundError:
1100-
if not store_path.read_only:
1100+
if not store_path.read_only and mode in _CREATE_MODES:
11011101
exists_ok = _infer_exists_ok(mode)
11021102
_zarr_format = zarr_format or _default_zarr_version()
11031103
return await create(

tests/test_api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,3 +1030,19 @@ async def test_metadata_validation_error() -> None:
10301030
match="Invalid value for 'zarr_format'. Expected '2, 3, or None'. Got '3.0'.",
10311031
):
10321032
await zarr.api.asynchronous.open_array(shape=(1,), zarr_format="3.0") # type: ignore[arg-type]
1033+
1034+
1035+
@pytest.mark.parametrize(
1036+
"store",
1037+
["local", "memory", "zip"],
1038+
indirect=True,
1039+
)
1040+
def test_open_array_with_mode_r_plus(store: Store) -> None:
1041+
# 'r+' means read/write (must exist)
1042+
with pytest.raises(FileNotFoundError):
1043+
zarr.open_array(store=store, mode="r+")
1044+
zarr.ones(store=store, shape=(3, 3))
1045+
z2 = zarr.open_array(store=store, mode="r+")
1046+
assert isinstance(z2, Array)
1047+
assert (z2[:] == 1).all()
1048+
z2[:] = 3

tests/test_v2.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_simple(store: StorePath) -> None:
3636
assert np.array_equal(data, a[:, :])
3737

3838

39+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
3940
@pytest.mark.parametrize(
4041
("dtype", "fill_value"),
4142
[
@@ -48,8 +49,8 @@ def test_simple(store: StorePath) -> None:
4849
(str, ""),
4950
],
5051
)
51-
def test_implicit_fill_value(store: StorePath, dtype: str, fill_value: Any) -> None:
52-
arr = zarr.open_array(store=store, shape=(4,), fill_value=None, zarr_format=2, dtype=dtype)
52+
def test_implicit_fill_value(store: MemoryStore, dtype: str, fill_value: Any) -> None:
53+
arr = zarr.create(store=store, shape=(4,), fill_value=None, zarr_format=2, dtype=dtype)
5354
assert arr.metadata.fill_value is None
5455
assert arr.metadata.to_dict()["fill_value"] is None
5556
result = arr[:]

0 commit comments

Comments
 (0)