Skip to content

Commit c5d6f5b

Browse files
authored
Merge branch 'main' into creation-from-other-zarr
2 parents aad5ca1 + 037adf6 commit c5d6f5b

File tree

11 files changed

+84
-21
lines changed

11 files changed

+84
-21
lines changed

.github/workflows/hypothesis.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ jobs:
6969
path: .hypothesis/
7070
key: cache-hypothesis-${{ runner.os }}-${{ github.run_id }}
7171

72+
- name: Upload coverage
73+
uses: codecov/codecov-action@v5
74+
with:
75+
token: ${{ secrets.CODECOV_TOKEN }}
76+
verbose: true # optional (default = false)
77+
7278
- name: Generate and publish the report
7379
if: |
7480
failure()

changes/2804.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:py:class:`LocalStore` learned to ``delete_dir``. This makes array and group deletes more efficient.

changes/2807.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix pickling for ZipStore

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ run = "run-coverage --no-cov"
161161
run-pytest = "run"
162162
run-verbose = "run-coverage --verbose"
163163
run-mypy = "mypy src"
164-
run-hypothesis = "pytest --hypothesis-profile ci tests/test_properties.py tests/test_store/test_stateful*"
164+
run-hypothesis = "run-coverage --hypothesis-profile ci --run-slow-hypothesis tests/test_properties.py tests/test_store/test_stateful*"
165165
list-env = "pip list"
166166

167167
[tool.hatch.envs.doctest]
@@ -398,7 +398,8 @@ filterwarnings = [
398398
"ignore:.*is currently not part in the Zarr format 3 specification.*:UserWarning",
399399
]
400400
markers = [
401-
"gpu: mark a test as requiring CuPy and GPU"
401+
"gpu: mark a test as requiring CuPy and GPU",
402+
"slow_hypothesis: slow hypothesis tests",
402403
]
403404

404405
[tool.repo-review]

src/zarr/storage/_local.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ async def delete(self, key: str) -> None:
208208
else:
209209
await asyncio.to_thread(path.unlink, True) # Q: we may want to raise if path is missing
210210

211+
async def delete_dir(self, prefix: str) -> None:
212+
# docstring inherited
213+
self._check_writable()
214+
path = self.root / prefix
215+
if path.is_dir():
216+
shutil.rmtree(path)
217+
elif path.is_file():
218+
raise ValueError(f"delete_dir was passed a {prefix=!r} that is a file")
219+
else:
220+
# Non-existent directory
221+
# This path is tested by test_group:test_create_creates_parents for one
222+
pass
223+
211224
async def exists(self, key: str) -> bool:
212225
# docstring inherited
213226
path = self.root / key

src/zarr/storage/_zip.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ async def _open(self) -> None:
108108
self._sync_open()
109109

110110
def __getstate__(self) -> dict[str, Any]:
111-
state = self.__dict__
111+
# We need a copy to not modify the state of the original store
112+
state = self.__dict__.copy()
112113
for attr in ["_zf", "_lock"]:
113114
state.pop(attr, None)
114115
return state

src/zarr/testing/stateful.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def can_add(self, path: str) -> bool:
6868
# -------------------- store operations -----------------------
6969
@rule(name=node_names, data=st.data())
7070
def add_group(self, name: str, data: DataObject) -> None:
71+
# Handle possible case-insensitive file systems (e.g. MacOS)
72+
if isinstance(self.store, LocalStore):
73+
name = name.lower()
7174
if self.all_groups:
7275
parent = data.draw(st.sampled_from(sorted(self.all_groups)), label="Group parent")
7376
else:
@@ -90,6 +93,9 @@ def add_array(
9093
name: str,
9194
array_and_chunks: tuple[np.ndarray[Any, Any], tuple[int, ...]],
9295
) -> None:
96+
# Handle possible case-insensitive file systems (e.g. MacOS)
97+
if isinstance(self.store, LocalStore):
98+
name = name.lower()
9399
array, chunks = array_and_chunks
94100
fill_value = data.draw(npst.from_dtype(array.dtype))
95101
if self.all_groups:
@@ -135,6 +141,7 @@ def add_array(
135141
# self.model.rename(from_group, new_path)
136142
# self.repo.store.rename(from_group, new_path)
137143

144+
@precondition(lambda self: self.store.supports_deletes)
138145
@precondition(lambda self: len(self.all_arrays) >= 1)
139146
@rule(data=st.data())
140147
def delete_array_using_del(self, data: DataObject) -> None:
@@ -149,6 +156,7 @@ def delete_array_using_del(self, data: DataObject) -> None:
149156
del group[array_name]
150157
self.all_arrays.remove(array_path)
151158

159+
@precondition(lambda self: self.store.supports_deletes)
152160
@precondition(lambda self: len(self.all_groups) >= 2) # fixme don't delete root
153161
@rule(data=st.data())
154162
def delete_group_using_del(self, data: DataObject) -> None:
@@ -284,6 +292,10 @@ def supports_partial_writes(self) -> bool:
284292
def supports_writes(self) -> bool:
285293
return self.store.supports_writes
286294

295+
@property
296+
def supports_deletes(self) -> bool:
297+
return self.store.supports_deletes
298+
287299

288300
class ZarrStoreStateMachine(RuleBasedStateMachine):
289301
""" "
@@ -366,6 +378,7 @@ def get_partial_values(self, data: DataObject) -> None:
366378
model_vals_ls,
367379
)
368380

381+
@precondition(lambda self: self.store.supports_deletes)
369382
@precondition(lambda self: len(self.model.keys()) > 0)
370383
@rule(data=st.data())
371384
def delete(self, data: DataObject) -> None:

src/zarr/testing/store.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ def test_store_eq(self, store: S, store_kwargs: dict[str, Any]) -> None:
9999
store2 = self.store_cls(**store_kwargs)
100100
assert store == store2
101101

102-
def test_serializable_store(self, store: S) -> None:
102+
async def test_serializable_store(self, store: S) -> None:
103103
new_store: S = pickle.loads(pickle.dumps(store))
104104
assert new_store == store
105105
assert new_store.read_only == store.read_only
106+
# quickly roundtrip data to a key to test that new store works
107+
data_buf = self.buffer_cls.from_bytes(b"\x01\x02\x03\x04")
108+
key = "foo"
109+
await store.set(key, data_buf)
110+
observed = await store.get(key, prototype=default_buffer_prototype())
111+
assert_bytes_equal(observed, data_buf)
106112

107113
def test_store_read_only(self, store: S) -> None:
108114
assert not store.read_only

src/zarr/testing/strategies.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from typing import Any
23

34
import hypothesis.extra.numpy as npst
@@ -209,7 +210,7 @@ def basic_indices(draw: st.DrawFn, *, shape: tuple[int], **kwargs: Any) -> Any:
209210

210211

211212
def key_ranges(
212-
keys: SearchStrategy = node_names, max_size: int | None = None
213+
keys: SearchStrategy = node_names, max_size: int = sys.maxsize
213214
) -> SearchStrategy[list[int]]:
214215
"""
215216
Function to generate key_ranges strategy for get_partial_values()
@@ -218,10 +219,14 @@ def key_ranges(
218219
[(key, (range_start, range_end)),
219220
(key, (range_start, range_end)),...]
220221
"""
222+
223+
def make_request(start: int, length: int) -> RangeByteRequest:
224+
return RangeByteRequest(start, end=min(start + length, max_size))
225+
221226
byte_ranges = st.builds(
222-
RangeByteRequest,
227+
make_request,
223228
start=st.integers(min_value=0, max_value=max_size),
224-
end=st.integers(min_value=0, max_value=max_size),
229+
length=st.integers(min_value=0, max_value=max_size),
225230
)
226231
key_tuple = st.tuples(keys, byte_ranges)
227232
return st.lists(key_tuple, min_size=1, max_size=10)

tests/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ def zarr_format(request: pytest.FixtureRequest) -> ZarrFormat:
155155
raise ValueError(msg)
156156

157157

158+
def pytest_addoption(parser: Any) -> None:
159+
parser.addoption(
160+
"--run-slow-hypothesis",
161+
action="store_true",
162+
default=False,
163+
help="run slow hypothesis tests",
164+
)
165+
166+
167+
def pytest_collection_modifyitems(config: Any, items: Any) -> None:
168+
if config.getoption("--run-slow-hypothesis"):
169+
return
170+
skip_slow_hyp = pytest.mark.skip(reason="need --run-slow-hypothesis option to run")
171+
for item in items:
172+
if "slow_hypothesis" in item.keywords:
173+
item.add_marker(skip_slow_hyp)
174+
175+
158176
settings.register_profile(
159177
"ci",
160178
max_examples=1000,

0 commit comments

Comments
 (0)