Skip to content

Commit d087e9b

Browse files
committed
fix open_array for mode r+
1 parent f74e53a commit d087e9b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
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

0 commit comments

Comments
 (0)