Strategies
Strategies¶
zarr.testing.strategies ¶
array_shapes
module-attribute
¶
array_shapes = array_shapes(
max_dims=4, min_side=3, max_side=5
) | array_shapes(max_dims=4, min_side=0)
attrs
module-attribute
¶
attrs: SearchStrategy[Mapping[str, JSON] | None] = (
none() | dictionaries(_attr_keys, _attr_values)
)
short_node_names
module-attribute
¶
short_node_names = filter(
lambda name: lower() != "zarr.json"
)
zarr_key_chars
module-attribute
¶
zarr_key_chars = sampled_from(
".-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
)
array_metadata ¶
array_metadata(
draw: DrawFn,
*,
array_shapes: Callable[
..., SearchStrategy[tuple[int, ...]]
] = array_shapes,
zarr_formats: SearchStrategy[ZarrFormat] = zarr_formats,
attributes: SearchStrategy[
Mapping[str, JSON] | None
] = attrs,
) -> ArrayV2Metadata | ArrayV3Metadata
Source code in zarr/testing/strategies.py
arrays ¶
arrays(
draw: DrawFn,
*,
shapes: SearchStrategy[tuple[int, ...]] = array_shapes,
compressors: SearchStrategy = compressors,
stores: SearchStrategy[StoreLike] = stores,
paths: SearchStrategy[str] = paths(),
array_names: SearchStrategy = array_names,
arrays: SearchStrategy | None = None,
attrs: SearchStrategy = attrs,
zarr_formats: SearchStrategy = zarr_formats,
open_mode: AccessModeLiteral = "w",
) -> AnyArray
Source code in zarr/testing/strategies.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
basic_indices ¶
basic_indices(
draw: DrawFn,
*,
shape: tuple[int, ...],
min_dims: int = 0,
max_dims: int | None = None,
allow_newaxis: TrueOrFalse = False,
allow_ellipsis: TrueOrFalse = True,
) -> Any
Basic indices without unsupported negative slices.
Source code in zarr/testing/strategies.py
chunk_grids ¶
chunk_grids(
draw: DrawFn, *, shape: tuple[int, ...]
) -> (
RegularChunkGridMetadata | RectilinearChunkGridMetadata
)
Generate either a RegularChunkGridMetadata or RectilinearChunkGridMetadata.
This strategy depends on the global state of the config having rectilinear chunk grids enabled or not. This means that it may be a possible source of a hypothesis FlakyStrategy error due dependence on global state. However, in practice this seems unlikely to happen.
This allows property tests to exercise both chunk grid types.
Source code in zarr/testing/strategies.py
chunk_paths ¶
Source code in zarr/testing/strategies.py
chunk_shapes ¶
Source code in zarr/testing/strategies.py
clear_store ¶
complex_rectilinear_arrays ¶
complex_rectilinear_arrays(
draw: DrawFn,
*,
stores: SearchStrategy[StoreLike] = stores,
paths: SearchStrategy[str] = paths(),
array_names: SearchStrategy = array_names,
attrs: SearchStrategy = attrs,
) -> tuple[NDArray[Any], AnyArray]
Generate a rectilinear array with many small chunks.
The shape is derived from the chunk edges (5-10 chunks per dim,
sizes 1-5), exercising higher chunk counts than rectilinear_arrays.
Source code in zarr/testing/strategies.py
dimension_names ¶
Source code in zarr/testing/strategies.py
dtypes ¶
Source code in zarr/testing/strategies.py
end_slices ¶
A strategy that slices ranges that include the last chunk. This is intended to stress-test handling of a possibly smaller last chunk.
Source code in zarr/testing/strategies.py
is_negative_slice ¶
key_ranges ¶
key_ranges(
keys: SearchStrategy[str] = node_names,
max_size: int = maxsize,
) -> SearchStrategy[list[tuple[str, RangeByteRequest]]]
Function to generate key_ranges strategy for get_partial_values() returns list strategy w/ form::
[(key, (range_start, range_end)),
(key, (range_start, range_end)),...]
Source code in zarr/testing/strategies.py
keys ¶
np_array_and_chunks ¶
np_array_and_chunks(
draw: DrawFn,
*,
arrays: SearchStrategy[NDArray[Any]] = numpy_arrays(),
) -> tuple[ndarray[Any, Any], tuple[int, ...]]
A hypothesis strategy to generate small sized random arrays.
Returns: a tuple of the array and a suitable random chunking for it.
Source code in zarr/testing/strategies.py
numpy_arrays ¶
numpy_arrays(
draw: DrawFn,
*,
shapes: SearchStrategy[tuple[int, ...]] = array_shapes,
dtype: dtype[Any] | None = None,
) -> NDArray[Any]
Generate numpy arrays that can be saved in the provided Zarr format.
Source code in zarr/testing/strategies.py
orthogonal_indices ¶
orthogonal_indices(
draw: DrawFn, *, shape: tuple[int, ...]
) -> tuple[
tuple[ndarray[Any, Any], ...],
tuple[ndarray[Any, Any], ...],
]
Strategy that returns (1) a tuple of integer arrays used for orthogonal indexing of Zarr arrays. (2) a tuple of integer arrays that can be used for equivalent indexing of numpy arrays
Source code in zarr/testing/strategies.py
paths ¶
rectilinear_arrays ¶
rectilinear_arrays(
draw: DrawFn,
*,
shapes: SearchStrategy[
tuple[int, ...]
] = _rectilinear_shapes,
) -> Any
Generate a zarr v3 array with rectilinear (variable) chunk grid.
Source code in zarr/testing/strategies.py
rectilinear_chunks ¶
Generate valid rectilinear chunk shapes for a given array shape.
Uses two modes per dimension: - "expanded": random divider points create arbitrary chunk sizes - "rle": uniform chunks with optional remainder, optionally shuffled
Keeps max chunks per dimension <= 20 to avoid performance issues in property tests. With higher dimensions, the total chunk count grows multiplicatively.
Source code in zarr/testing/strategies.py
safe_unicode_for_dtype ¶
Generate UTF-8-safe text constrained to max_len of dtype.
Source code in zarr/testing/strategies.py
shard_shapes ¶
shard_shapes(
draw: DrawFn,
*,
shape: tuple[int, ...],
chunk_shape: tuple[int, ...],
) -> tuple[int, ...]
Source code in zarr/testing/strategies.py
simple_arrays ¶
simple_arrays(
draw: DrawFn,
*,
shapes: SearchStrategy[tuple[int, ...]] = array_shapes,
) -> Any