Skip to content

Only warn if opening writeable store with mode='r' #3155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion changes/3068.bugfix.rst
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Trying to open an array with ``mode='r'`` when the store is not read-only now raises an error.
Opening an array or group with ``mode='r'`` when the store is not read-only now raises a warning.
If you do this the group or array will still be writeable.
We intend to fix this behaviour in a future zarr-python release.
7 changes: 6 additions & 1 deletion src/zarr/storage/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import importlib.util
import json
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, Self, TypeAlias

Expand Down Expand Up @@ -87,7 +88,11 @@
if store.read_only and mode != "r":
raise ValueError(f"Store is read-only but mode is '{mode}'")
if not store.read_only and mode == "r":
raise ValueError(f"Store is not read-only but mode is '{mode}'")
warnings.warn(

Check warning on line 91 in src/zarr/storage/_common.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/storage/_common.py#L91

Added line #L91 was not covered by tests
f"Store is not read-only but mode is '{mode}'. You will still be able to write to arrays and groups within this store.",
UserWarning,
stacklevel=2,
)

match mode:
case "w-":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ def test_no_overwrite_open(tmp_path: Path, open_func: Callable, mode: str) -> No
existing_fpath = add_empty_file(tmp_path)

assert existing_fpath.exists()
with contextlib.suppress(FileExistsError, FileNotFoundError, ValueError):
with contextlib.suppress(FileExistsError, FileNotFoundError, UserWarning):
open_func(store=store, mode=mode)
if mode == "w":
assert not existing_fpath.exists()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_store/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ def test_relativize_path_invalid() -> None:
_relativize_path(path="a/b/c", prefix="b")


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