Skip to content

Commit 75373d4

Browse files
committed
Only warn if opening writeable store with mode='r'
1 parent 3532d58 commit 75373d4

4 files changed

Lines changed: 12 additions & 5 deletions

File tree

changes/3068.bugfix.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Trying to open an array with ``mode='r'`` when the store is not read-only now raises an error.
1+
Opening an array or group with ``mode='r'`` when the store is not read-only now raises a warning.
2+
If you do this the group or array will still be writeable.
3+
We intend to fix this behaviour in a future zarr-python release.

src/zarr/storage/_common.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import importlib.util
44
import json
5+
import warnings
56
from pathlib import Path
67
from typing import TYPE_CHECKING, Any, Literal, Self, TypeAlias
78

@@ -87,7 +88,11 @@ async def open(cls, store: Store, path: str, mode: AccessModeLiteral | None = No
8788
if store.read_only and mode != "r":
8889
raise ValueError(f"Store is read-only but mode is '{mode}'")
8990
if not store.read_only and mode == "r":
90-
raise ValueError(f"Store is not read-only but mode is '{mode}'")
91+
warnings.warn(
92+
f"Store is not read-only but mode is '{mode}'. You will still be able to write to arrays and groups within this store.",
93+
UserWarning,
94+
stacklevel=2,
95+
)
9196

9297
match mode:
9398
case "w-":

tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ def test_no_overwrite_open(tmp_path: Path, open_func: Callable, mode: str) -> No
13181318
existing_fpath = add_empty_file(tmp_path)
13191319

13201320
assert existing_fpath.exists()
1321-
with contextlib.suppress(FileExistsError, FileNotFoundError, ValueError):
1321+
with contextlib.suppress(FileExistsError, FileNotFoundError, UserWarning):
13221322
open_func(store=store, mode=mode)
13231323
if mode == "w":
13241324
assert not existing_fpath.exists()

tests/test_store/test_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ def test_relativize_path_invalid() -> None:
263263
_relativize_path(path="a/b/c", prefix="b")
264264

265265

266-
def test_invalid_open_mode() -> None:
266+
def test_different_open_mode() -> None:
267267
store = MemoryStore()
268268
zarr.create((100,), store=store, zarr_format=2, path="a")
269-
with pytest.raises(ValueError, match="Store is not read-only but mode is 'r'"):
269+
with pytest.warns(UserWarning, match="Store is not read-only but mode is 'r'"):
270270
zarr.open_array(store=store, path="a", zarr_format=2, mode="r")

0 commit comments

Comments
 (0)