Skip to content

Commit 501f151

Browse files
committed
fixup
1 parent efeb440 commit 501f151

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/zarr/abc/store.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,25 @@ async def clear(self) -> None: ...
8484
@abstractmethod
8585
def with_mode(self, mode: AccessModeLiteral) -> Self:
8686
"""
87-
Return a new store pointing to the same location with a new mode.
87+
Return a new store of the same type pointing to the same location with a new mode.
88+
89+
The returned Store is not automatically opened. Call :meth:`Store.open` before
90+
using.
91+
92+
Parameters
93+
----------
94+
mode: AccessModeLiteral
95+
The new mode to use.
96+
97+
Returns
98+
-------
99+
store:
100+
A new store of the same type with the new mode.
101+
102+
Examples
103+
--------
104+
>>> writer = zarr.store.MemoryStore(mode="w")
105+
>>> reader = writer.with_mode("r")
88106
"""
89107
...
90108

src/zarr/store/zip.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ async def empty(self) -> bool:
118118
return True
119119

120120
def with_mode(self, mode: ZipStoreAccessModeLiteral) -> Self: # type: ignore[override]
121-
return type(self)(
122-
path=self.path, mode=mode, compression=self.compression, allowZip64=self.allowZip64
123-
)
121+
raise NotImplementedError("ZipStore cannot be reopened with a new mode.")
124122

125123
def __str__(self) -> str:
126124
return f"zip://{self.path}"

src/zarr/testing/store.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,26 @@ async def test_list_dir(self, store: S) -> None:
254254
assert sorted(keys_expected) == sorted(keys_observed)
255255

256256
async def test_with_mode(self, store: S) -> None:
257-
for mode in ["r", "w"]:
257+
data = b"0000"
258+
self.set(store, "key", self.buffer_cls.from_bytes(data))
259+
assert self.get(store, "key").to_bytes() == data
260+
261+
for mode in ["r", "a"]:
258262
mode = cast(AccessModeLiteral, mode)
259-
result = store.with_mode(mode)
260-
assert result.mode == AccessMode.from_literal(mode)
261-
assert isinstance(result, type(store))
263+
clone = store.with_mode(mode)
264+
# await store.close()
265+
await clone._ensure_open()
266+
assert clone.mode == AccessMode.from_literal(mode)
267+
assert isinstance(clone, type(store))
268+
269+
# earlier writes are visible
270+
assert self.get(clone, "key").to_bytes() == data
271+
272+
# writes to original after with_mode is visible
273+
self.set(store, "key-2", self.buffer_cls.from_bytes(data))
274+
assert self.get(clone, "key-2").to_bytes() == data
275+
276+
if mode == "w":
277+
# writes to clone is visible in the original
278+
self.set(store, "key-3", self.buffer_cls.from_bytes(data))
279+
assert self.get(clone, "key-3").to_bytes() == data

tests/v3/test_store/test_zip.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ def test_api_integration(self, store: ZipStore) -> None:
9696
del root["bar"]
9797

9898
store.close()
99+
100+
async def test_with_mode(self, store: ZipStore) -> None:
101+
with pytest.raises(NotImplementedError, match="new mode"):
102+
await super().test_with_mode(store)

0 commit comments

Comments
 (0)