From a1eea782c94ece0bbc53d897cde7adf54cd927d8 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Wed, 23 Oct 2024 13:14:16 -0600 Subject: [PATCH] fix property tests --- src/zarr/testing/strategies.py | 13 ++++++++++--- tests/test_properties.py | 6 ++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/zarr/testing/strategies.py b/src/zarr/testing/strategies.py index 52c5ac5598..c82e168cf1 100644 --- a/src/zarr/testing/strategies.py +++ b/src/zarr/testing/strategies.py @@ -85,7 +85,7 @@ def numpy_arrays( @st.composite # type: ignore[misc] def np_array_and_chunks( draw: st.DrawFn, *, arrays: st.SearchStrategy[np.ndarray] = numpy_arrays -) -> tuple[np.ndarray, tuple[int]]: # type: ignore[type-arg] +) -> tuple[np.ndarray, tuple[int, ...]]: # type: ignore[type-arg] """A hypothesis strategy to generate small sized random arrays. Returns: a tuple of the array and a suitable random chunking for it. @@ -93,9 +93,16 @@ def np_array_and_chunks( array = draw(arrays) # We want this strategy to shrink towards arrays with smaller number of chunks # 1. st.integers() shrinks towards smaller values. So we use that to generate number of chunks - numchunks = draw(st.tuples(*[st.integers(min_value=1, max_value=size) for size in array.shape])) + numchunks = draw( + st.tuples( + *[st.integers(min_value=0 if size == 0 else 1, max_value=size) for size in array.shape] + ) + ) # 2. and now generate the chunks tuple - chunks = tuple(size // nchunks for size, nchunks in zip(array.shape, numchunks, strict=True)) + chunks = tuple( + size // nchunks if nchunks > 0 else 0 + for size, nchunks in zip(array.shape, numchunks, strict=True) + ) return (array, chunks) diff --git a/tests/test_properties.py b/tests/test_properties.py index 380a4d851e..f70753ceb5 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -6,7 +6,7 @@ import hypothesis.extra.numpy as npst # noqa: E402 import hypothesis.strategies as st # noqa: E402 -from hypothesis import given # noqa: E402 +from hypothesis import assume, given # noqa: E402 from zarr.testing.strategies import arrays, basic_indices, numpy_arrays, zarr_formats # noqa: E402 @@ -35,11 +35,13 @@ def test_basic_indexing(data: st.DataObject) -> None: @given(data=st.data()) def test_vindex(data: st.DataObject) -> None: zarray = data.draw(arrays()) + # integer_array_indices can't handle 0-size dimensions. + assume(all(s > 0 for s in zarray.shape)) nparray = zarray[:] indexer = data.draw( npst.integer_array_indices( - shape=nparray.shape, result_shape=npst.array_shapes(max_dims=None) + shape=nparray.shape, result_shape=npst.array_shapes(min_side=1, max_dims=None) ) ) actual = zarray.vindex[indexer]