Repository navigation
fix: refuse writes to a store zarr-python opened read-only #190
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5da648d
fix: refuse writes to a store zarr-python opened read-only
selmanozleyen 522f389
fix: remove store_is_read_only parameter and update usage to reflect …
selmanozleyen 409ba13
fix: update CodecPipelineImpl to use ReadableStorage and handle read-…
selmanozleyen 40deb6e
fix: update documentation to clarify read-only behavior in Rust store…
selmanozleyen ecc20a5
applied @ilan-gold's suggestions
selmanozleyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """A store opened read-only must refuse writes, as zarr-python's own pipeline does. | ||
|
|
||
| zarr-python enforces this in the store itself -- `Store._check_writable`, reached from the | ||
| concrete store's `_set`. This pipeline never gets there: it is handed a `StoreConfig` and | ||
| builds its own Rust store, so nothing zarr-python checks is in the path -- a `mode="r"` write | ||
| SUCCEEDS unless this side refuses it. It refuses by keeping a writable handle only when the | ||
| store is writable; the read handle has no `set`/`erase` in its interface at all. | ||
|
|
||
| Opened STRICT throughout, and that is what makes the assertion mean anything: zarr's own | ||
| refusal message is byte-identical to the Rust one, so with a fallback available these | ||
| tests would pass whether the guard fired or zarr-python served the write. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| import zarr | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def array(tmp_path: Path) -> tuple[Path, np.ndarray]: | ||
| values = np.arange(64, dtype=np.float32) | ||
| path = tmp_path / "a" | ||
| zarr.create_array(path, dtype=values.dtype, shape=values.shape, chunks=(16,))[:] = ( | ||
| values | ||
| ) | ||
| return path, values | ||
|
|
||
|
|
||
| def open_strict(path: Path, mode: str) -> zarr.Array: | ||
| """Open with no fallback. `strict` has to be set BEFORE the open: that is when the | ||
| pipeline decides whether it has one.""" | ||
| with zarr.config.set({"codec_pipeline.strict": True}): | ||
| return zarr.open_array(path, mode=mode) | ||
|
|
||
|
|
||
| def test_write_to_a_read_only_array_raises(array: tuple[Path, np.ndarray]) -> None: | ||
| path, values = array | ||
| z = open_strict(path, "r") | ||
| with pytest.raises(ValueError, match="read-only"): | ||
| z[0:16] = -1.0 | ||
| np.testing.assert_array_equal(zarr.open_array(path, mode="r")[:], values) | ||
|
|
||
|
|
||
| def test_a_writable_array_still_writes(array: tuple[Path, np.ndarray]) -> None: | ||
| path, values = array | ||
| z = open_strict(path, "r+") | ||
| z[0:16] = -1.0 | ||
| expected = values.copy() | ||
| expected[0:16] = -1.0 | ||
| np.testing.assert_array_equal(zarr.open_array(path, mode="r")[:], expected) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.