zarr.testing.strategies
Strategies¶
zarr.testing.strategies ¶
array_shapes
module-attribute
¶
array_shapes = npst.array_shapes(
max_dims=4, min_side=3, max_side=5
) | npst.array_shapes(max_dims=4, min_side=0)
attrs
module-attribute
¶
attrs: SearchStrategy[Mapping[str, JSON] | None] = (
st.none() | st.dictionaries(_attr_keys, _attr_values)
)
node_names
module-attribute
¶
node_names = (
st.text(zarr_key_chars, min_size=1)
.filter(
lambda t: (
t not in (".", "..")
and not t.startswith("__")
)
)
.filter(lambda name: name.lower() != "zarr.json")
)
sharding_inner_codecs
module-attribute
¶
sharding_inner_codecs: SearchStrategy[
list[BytesCodec | ZstdCodec]
] = st.sampled_from(
[[BytesCodec()], [BytesCodec(), ZstdCodec()]]
)
short_node_names
module-attribute
¶
short_node_names = (
st.text(zarr_key_chars, max_size=3, min_size=1)
.filter(
lambda t: (
t not in (".", "..")
and not t.startswith("__")
)
)
.filter(lambda name: name.lower() != "zarr.json")
)
subchunk_write_orders
module-attribute
¶
subchunk_write_orders: SearchStrategy[
SubchunkWriteOrder
] = st.sampled_from(SUBCHUNK_WRITE_ORDER)
zarr_formats
module-attribute
¶
zarr_key_chars
module-attribute
¶
zarr_key_chars = st.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 src/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,
subchunk_write_orders: SearchStrategy[
SubchunkWriteOrder
] = subchunk_write_orders,
open_mode: AccessModeLiteral = "w",
) -> AnyArray
Source code in src/zarr/testing/strategies.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
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 src/zarr/testing/strategies.py
block_indices ¶
block_indices(
draw: DrawFn,
*,
chunk_sizes: tuple[tuple[int, ...], ...],
) -> tuple[tuple[int | slice, ...], tuple[slice, ...]]
Strategy for block-selection indexers over a chunk grid.
Block indexing is basic indexing applied to the block grid (the grid of
chunks), so each axis is drawn with basic_indices over that axis's chunk
count, mirroring how orthogonal_indices reuses basic_indices per
axis. chunk_sizes gives the per-chunk data sizes of the array's outer
(block) grid for every axis — i.e. Array.write_chunk_sizes, the grid that
Array.blocks addresses (the shard grid when sharding is used). For
example (3, 3, 3, 1) for a length-10 axis with a regular chunk size of 3,
or the explicit edges of a rectilinear axis; nchunks for an axis is
len(chunk_sizes[axis]).
The array-space translation uses the cumulative sum of those sizes, matching
BlockIndexer's use of dim_grid.chunk_offset. Because the sizes are
clipped to the array extent, the final offset equals the extent and the
translation is exact for regular (uniform), rectilinear, and sharded grids
alike.
Block indexing only supports integers and step-1 slices whose start references an existing chunk, so strided slices and slices starting at the grid edge are filtered out.
Returns:
-
block_indexer–A per-axis tuple of ints / step-1 slices addressing whole chunks, suitable for
Array.blocks/get_block_selection/set_block_selection. -
array_indexer–The equivalent array-space selection (a tuple of slices) for indexing the corresponding numpy array, used as the comparison oracle.
Source code in src/zarr/testing/strategies.py
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 | |
block_test_arrays ¶
Draw an array for block-indexing property tests, with its source contents.
Two arms, selected with equal probability:
- regular: a regular chunk grid, optionally wrapped in sharding.
- rectilinear: a variable (rectilinear) chunk grid, always unsharded.
Returns (zarray, nparray). The per-axis block sizes the oracle needs are
zarray.write_chunk_sizes — the array's outer (block / shard) grid, which
is exactly the grid Array.blocks addresses; the caller reads it directly.
Source code in src/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 src/zarr/testing/strategies.py
chunk_paths ¶
Source code in src/zarr/testing/strategies.py
chunk_shapes ¶
Source code in src/zarr/testing/strategies.py
chunks_param_from_rectilinear ¶
Convert rectilinear chunk grid metadata into a chunks= argument.
Explicit edge tuples become lists. Bare ints — the spec's step-size shorthand meaning "repeat to cover the axis" — pass through unchanged; wrapping one in a single-element list would instead declare exactly one chunk, which fails normalization whenever the axis needs more than one.
Source code in src/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 src/zarr/testing/strategies.py
dimension_names ¶
Source code in src/zarr/testing/strategies.py
dtypes ¶
Source code in src/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 src/zarr/testing/strategies.py
is_negative_slice ¶
key_ranges ¶
key_ranges(
keys: SearchStrategy[str] = node_names,
max_size: int = maxsize,
) -> SearchStrategy[list[tuple[str, ByteRequest | None]]]
Function to generate key_ranges strategy for get_partial_values() returns list strategy w/ form::
[(key, byte_request),
(key, byte_request),...]
where byte_request is None or any of the concrete ByteRequest
subtypes. The bounds are drawn independently of each value's length, so the
offsets/suffixes routinely exceed the data and exercise the clamping logic
in _normalize_byte_range_index.
Source code in src/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 src/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 src/zarr/testing/strategies.py
orthogonal_indices ¶
orthogonal_indices(
draw: DrawFn, *, shape: tuple[int, ...]
) -> tuple[
tuple[int | slice | ndarray[Any, Any], ...],
tuple[ndarray[Any, Any], ...],
]
Strategy that returns
(1) a tuple of per-axis selectors (integer array, slice, or bare integer) for
orthogonal indexing of Zarr arrays.
(2) a tuple of broadcast integer arrays that index a numpy array to the same
result. A bare integer drops its axis, as oindex does, so it is
given as a 0-d array and does not contribute a result dimension.
Source code in src/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 src/zarr/testing/strategies.py
rectilinear_chunk_grids ¶
A RectilinearChunkGridMetadata over shape, per dimension a bare-int
step or explicit edges, which may sum beyond the extent.
Source code in src/zarr/testing/strategies.py
rectilinear_chunk_shape_declarations ¶
rectilinear_chunk_shape_declarations(
draw: DrawFn, *, shape: tuple[int, ...]
) -> tuple[
list[RectilinearDimSpecJSON],
tuple[int | tuple[int, ...], ...],
]
The chunk_shapes of a stored rectilinear chunk grid, with its meaning.
Samples the whole declaration space of the spec. Per dimension: a bare int step, or an edge list written in full or run-length encoded (canonically, or with arbitrary grouping). Edge lists may sum beyond the extent.
Returns (declaration, chunk_shapes): the JSON value to store, and the
chunk_shapes that parsing it must produce.
Source code in src/zarr/testing/strategies.py
rectilinear_chunks ¶
A chunks= specification declaring a rectilinear grid over shape, as an
explicit edge list per dimension summing to the extent (any edges, for a zero
extent). A 0-d shape gives [].
To also draw bare-int steps, see _rectilinear_chunks.
Source code in src/zarr/testing/strategies.py
rectilinear_dim_edges ¶
rectilinear_dim_edges(
draw: DrawFn,
*,
extent: int,
max_chunks: int = _RECTILINEAR_MAX_CHUNKS_PER_DIM,
) -> list[int]
Explicit chunk edge lengths summing exactly to extent.
A zero extent has no chunks for edges to cover, and no non-empty list
of positive edges sums to 0; its edges are the chunks the axis grows into
on append or resize, so any non-empty list of positive edges is drawn.
Two modes: "uneven" cuts the extent at random dividers; "uniform" repeats
one size with an optional remainder, optionally shuffled so equal edges
are not all adjacent. At most max_chunks chunks keeps property tests
fast.
Source code in src/zarr/testing/strategies.py
safe_unicode_for_dtype ¶
Generate UTF-8-safe text constrained to max_len of dtype.
Source code in src/zarr/testing/strategies.py
shard_shapes ¶
shard_shapes(
draw: DrawFn,
*,
shape: tuple[int, ...],
chunk_shape: tuple[int, ...],
) -> tuple[int, ...]
Source code in src/zarr/testing/strategies.py
sharded_arrays ¶
sharded_arrays(
draw: DrawFn,
*,
shapes: SearchStrategy[
tuple[int, ...]
] = _sharded_shapes,
nested: bool | None = None,
) -> Any
Generate a zarr v3 array whose chunks are grouped into shards.
arrays shards only a small fraction of its draws (a v3 array with a
regular chunk grid, every axis larger than a chunk that is itself larger
than 1, and then only half the time), so a property test that must
exercise the sharding codec should draw from this strategy directly. Every
draw is sharded: the chunk shape and the shard shape (an integral number of
chunks per axis, possibly a single chunk) are drawn from shapes, and
the codec's subchunk write order and inner codec chain are drawn as in
arrays. shapes must generate shapes with at least one element on
every axis.
nested selects one level of recursive sharding: the drawn chunks are
grouped into inner shards, which are themselves grouped into the shards
stored in the array, so the outer ShardingCodec wraps an inner one with
its own subchunk write order. None (the default) draws it, so half the
examples nest. For a nested array Array.chunks is the inner shard shape
(the outer codec's chunk shape); the innermost chunk shape is the inner
codec's chunk_shape.
Source code in src/zarr/testing/strategies.py
simple_arrays ¶
simple_arrays(
draw: DrawFn,
*,
shapes: SearchStrategy[tuple[int, ...]] = array_shapes,
) -> Any
Source code in src/zarr/testing/strategies.py
structured_dtypes ¶
structured_dtypes(
draw: DrawFn,
*,
allow_extended: bool = False,
max_depth: int = 3,
) -> dtype[void]
A strategy for native NumPy structured dtypes, flat or nested.
With allow_extended=False (the default), generate packed fields without titles or
subarray shapes. With allow_extended=True, also generate field titles, subarray fields,
and align=True layouts, independently. The current native dtype conversion rejects
titles and subarray fields and accepts padding with a warning. These are implementation
behaviors, not restrictions imposed by the V2 format.
Source code in src/zarr/testing/strategies.py
v2_dtypes ¶
v3_dtypes ¶
zdtypes ¶
Generate instances of the built-in registered ZDType classes, including nested Struct.
Struct fields are restricted to fixed-size data types, as required by the V3 struct
extension. This strategy samples bounded lengths and normalized datetime units/scales;
it does not cover every valid instance or arbitrary third-party dtype constructors.