Skip to content

Commit 96217d4

Browse files
committed
fix zarr.open default for argument mode when store is read_only
1 parent 6193fd9 commit 96217d4

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/zarr/api/asynchronous.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy.typing as npt
1010
from typing_extensions import deprecated
1111

12+
from zarr.abc.store import Store
1213
from zarr.core.array import (
1314
Array,
1415
AsyncArray,
@@ -289,7 +290,7 @@ async def load(
289290
async def open(
290291
*,
291292
store: StoreLike | None = None,
292-
mode: AccessModeLiteral = "a",
293+
mode: AccessModeLiteral | None = None,
293294
zarr_version: ZarrFormat | None = None, # deprecated
294295
zarr_format: ZarrFormat | None = None,
295296
path: str | None = None,
@@ -324,7 +325,11 @@ async def open(
324325
Return type depends on what exists in the given store.
325326
"""
326327
zarr_format = _handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
327-
328+
if mode is None:
329+
if isinstance(store, Store) and store.read_only:
330+
mode = "r"
331+
else:
332+
mode = "a"
328333
store_path = await make_store_path(store, mode=mode, path=path, storage_options=storage_options)
329334

330335
# TODO: the mode check below seems wrong!

tests/test_api.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
)
4040
from zarr.core.buffer import NDArrayLike
4141
from zarr.errors import MetadataValidationError
42-
from zarr.storage import MemoryStore
42+
from zarr.storage import MemoryStore, ZipStore, LocalStore
4343
from zarr.storage._utils import normalize_path
4444
from zarr.testing.utils import gpu_test
4545

@@ -399,6 +399,37 @@ def test_load_array(memory_store: Store) -> None:
399399
else:
400400
assert_array_equal(bar, array)
401401

402+
@pytest.mark.parametrize("path", ["data", None])
403+
def test_load_zip(tmp_path: pathlib.Path, path: str | None) -> None:
404+
file = tmp_path / "test.zip"
405+
data = np.arange(100).reshape(10, 10)
406+
407+
with ZipStore(file, mode="w", read_only=False) as zs:
408+
save(zs, data, path=path)
409+
with ZipStore(file, mode="r", read_only=False) as zs:
410+
result = zarr.load(store=zs, path=path)
411+
assert np.array_equal(result, data)
412+
with ZipStore(file, mode="r") as zs:
413+
result = zarr.load(store=zs, path=path)
414+
assert np.array_equal(result, data)
415+
with ZipStore(file, read_only=True) as zs:
416+
result = zarr.load(store=zs, path=path)
417+
assert np.array_equal(result, data)
418+
419+
420+
@pytest.mark.parametrize("path", ["data", None])
421+
def test_load_local(tmp_path: pathlib.Path, path: str | None) -> None:
422+
file = tmp_path / "test.zip"
423+
data = np.arange(100).reshape(10, 10)
424+
425+
with LocalStore(file, read_only=False) as zs:
426+
save(zs, data, path=path)
427+
with LocalStore(file, read_only=False) as zs:
428+
result = zarr.load(store=zs, path=path)
429+
assert np.array_equal(result, data)
430+
with LocalStore(file, read_only=True) as zs:
431+
result = zarr.load(store=zs, path=path)
432+
assert np.array_equal(result, data)
402433

403434
def test_tree() -> None:
404435
pytest.importorskip("rich")

0 commit comments

Comments
 (0)