Skip to content

Commit 2e878cf

Browse files
d-v-bclaude
andcommitted
test: run the array lifecycle state machine in the slow Hypothesis job
Add it to `just hypothesis`. Re-saving metadata is always enabled and must leave valid metadata unchanged; appends favour an axis stored with chunk size 0, which raises how often a legacy axis grows before it is re-saved. A fixed example pins that a write to a shard kept by a shrink leaves its cells beyond the shape alone. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 192f86c commit 2e878cf

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

‎Justfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ coverage-serve *args:
5757

5858
# Run slow Hypothesis tests and write coverage.xml
5959
hypothesis *args:
60-
hatch run {{ quote(hatch_env) }}:coverage run --source=src -m pytest -nauto --run-slow-hypothesis tests/test_properties.py tests/test_store/test_stateful* "$@"
60+
hatch run {{ quote(hatch_env) }}:coverage run --source=src -m pytest -nauto --run-slow-hypothesis tests/test_properties.py tests/test_store/test_stateful* tests/test_array_stateful.py "$@"
6161
hatch run {{ quote(hatch_env) }}:coverage xml
6262

6363
# Validate executable documentation code blocks

‎tests/test_array.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,18 @@ def test_resize_1d(store: MemoryStore, zarr_format: ZarrFormat) -> None:
732732
assert new_shape == result.shape
733733

734734

735+
@pytest.mark.parametrize("chunks", [(1,), (2,), (4,)])
736+
def test_resize_sharded_keeps_cells_beyond_shape(chunks: tuple[int, ...]) -> None:
737+
"""A shard kept by a shrinking resize keeps its cells beyond the new shape, and a
738+
later write to the shard leaves them alone, so they come back when the array grows."""
739+
arr = zarr.create_array({}, shape=(4,), chunks=chunks, shards=(4,), dtype="int16", fill_value=0)
740+
arr[:] = [1, 2, 3, 4]
741+
arr.resize((2,))
742+
arr[:] = [9, 9]
743+
arr.resize((4,))
744+
np.testing.assert_array_equal(arr[:], [9, 9, 3, 4])
745+
746+
735747
@pytest.mark.parametrize("store", ["memory"], indirect=True)
736748
def test_resize_2d(store: MemoryStore, zarr_format: ZarrFormat) -> None:
737749
z = zarr.create(

‎tests/test_array_stateful.py‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
RuleBasedStateMachine,
3131
initialize,
3232
invariant,
33-
precondition,
3433
rule,
3534
)
3635

@@ -79,8 +78,8 @@ def __init__(self) -> None:
7978
self.fill = 0
8079
# What the store holds, indexed like the array and extending past its shape.
8180
self.stored: np.ndarray[Any, np.dtype[np.int16]] = np.zeros((0,), dtype=DTYPE)
82-
# A store with an invalid stored chunk size warns until its metadata is re-saved.
83-
self.expect_open_warning = False
81+
# Axes stored with chunk size 0; the store warns until its metadata is re-saved.
82+
self.legacy_axes: list[int] = []
8483

8584
# -------------------------------------------------------------- creation
8685
@initialize(data=st.data())
@@ -130,13 +129,12 @@ def create(self, data: st.DataObject) -> None:
130129
# A stored chunk size of 0, as zarr-python wrote for arrays created with a
131130
# zero-length axis; older releases could then grow the axis without storing
132131
# a chunk, so any extent is possible. Sharded arrays store it in the outer grid.
133-
zero_axes = data.draw(
132+
self.legacy_axes = data.draw(
134133
st.lists(st.integers(0, len(shape) - 1), min_size=1, unique=True),
135134
label="axes stored with chunk size 0",
136135
)
137136
stored_zero = data.draw(st.sampled_from([0, False]), label="stored zero")
138-
self._rewrite_stored_chunks(zarr_format, zero_axes, stored_zero)
139-
self.expect_open_warning = True
137+
self._rewrite_stored_chunks(zarr_format, self.legacy_axes, stored_zero)
140138
event("legacy zero chunk size")
141139

142140
def _rewrite_stored_chunks(
@@ -158,7 +156,7 @@ def _open(self) -> zarr.Array[Any]:
158156
warnings.simplefilter("always", ZarrUserWarning)
159157
arr = zarr.open_array(self.store, path=self.path, mode="r+")
160158
warned = any(issubclass(w.category, ZarrUserWarning) for w in record)
161-
assert warned is self.expect_open_warning, [str(w.message) for w in record]
159+
assert warned is bool(self.legacy_axes), [str(w.message) for w in record]
162160
return arr
163161

164162
# ----------------------------------------------------------------- model
@@ -209,13 +207,19 @@ def _model_write(self, arr: zarr.Array[Any], region: tuple[slice, ...], values:
209207
@rule(data=st.data())
210208
def append(self, data: st.DataObject) -> None:
211209
arr = self._open()
212-
axis = data.draw(st.integers(0, len(self.shape) - 1), label="axis")
210+
axes = st.integers(0, len(self.shape) - 1)
211+
if self.legacy_axes:
212+
# What a user of an older release did next: grow an axis stored with chunk size 0.
213+
axes = st.sampled_from(self.legacy_axes) | axes
214+
axis = data.draw(axes, label="axis")
213215
block_shape = list(self.shape)
214216
block_shape[axis] = data.draw(st.integers(0, 4), label="rows")
215217
block = data.draw(npst.arrays(DTYPE, tuple(block_shape)), label="block")
216218
note(f"append {block.shape} along {axis} to {self.shape}")
217219
if self.shape[axis] == 0 and block.shape[axis]:
218220
event("append to a zero-length axis")
221+
if axis in self.legacy_axes and block.shape[axis]:
222+
event("grow an axis stored with chunk size 0")
219223
old_extent = self.shape[axis]
220224
arr.append(block, axis=axis)
221225
self._model_resize(ChunkGrid.from_metadata(arr.metadata), arr.shape)
@@ -224,7 +228,7 @@ def append(self, data: st.DataObject) -> None:
224228
)
225229
self._model_write(arr, region, block)
226230
# Growing the array rewrites its metadata, which stores any correction.
227-
self.expect_open_warning = False
231+
self.legacy_axes = []
228232

229233
@rule(data=st.data())
230234
def resize(self, data: st.DataObject) -> None:
@@ -233,10 +237,12 @@ def resize(self, data: st.DataObject) -> None:
233237
st.tuples(*(st.integers(0, MAX_SIDE) for _ in self.shape)), label="new shape"
234238
)
235239
note(f"resize {self.shape} -> {new_shape}")
240+
if any(new_shape[axis] > self.shape[axis] for axis in self.legacy_axes):
241+
event("grow an axis stored with chunk size 0")
236242
grid = ChunkGrid.from_metadata(arr.metadata)
237243
arr.resize(new_shape)
238244
self._model_resize(grid, new_shape)
239-
self.expect_open_warning = False
245+
self.legacy_axes = []
240246

241247
@rule(data=st.data())
242248
def write(self, data: st.DataObject) -> None:
@@ -251,12 +257,15 @@ def write(self, data: st.DataObject) -> None:
251257
arr[region] = values
252258
self._model_write(arr, region, values)
253259

254-
@precondition(lambda self: self.expect_open_warning)
255260
@rule()
256261
def resave_metadata(self) -> None:
257-
"""What the warning for an invalid stored chunk size tells users to do."""
258-
self._open().update_attributes({})
259-
self.expect_open_warning = False
262+
"""What the warning for an invalid stored chunk size tells users to do. It stores
263+
the metadata as read, which is a no-op for valid metadata."""
264+
arr = self._open()
265+
read = arr.metadata
266+
arr.update_attributes({})
267+
self.legacy_axes = []
268+
assert self._open().metadata == read
260269

261270
def teardown(self) -> None:
262271
self._rectilinear.__exit__(None, None, None)

0 commit comments

Comments
 (0)