Skip to content

Commit e9bc2fb

Browse files
committed
fix(zarr-metadata): sample document shapes, listings and data types evenly
Shapes capped axes at 2**16 and rank at 5, with a quarter of documents rank 0 and few holding a zero-length axis. Draw each rank about as often, up to 8, and axes that are zero, short, or past 32 bits. Draw a document's data type first: drawn after the shape, the first in the list was a fifth of all documents. A listed group now sometimes holds the empty listing the reference implementation writes, a fresh one for each group. New tests bound these shares. Assisted-by: ClaudeCode:claude-opus-5-5
1 parent 9ca1a8c commit e9bc2fb

3 files changed

Lines changed: 70 additions & 6 deletions

File tree

‎packages/zarr-metadata/src/zarr_metadata/testing/_document.py‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
scope_of,
2727
strategy_for,
2828
)
29-
from zarr_metadata.testing._strategy import CHARACTERS, MAX_RANK, NOTHING_KNOWN
29+
from zarr_metadata.testing._strategy import CHARACTERS, NOTHING_KNOWN
3030
from zarr_metadata.v3._definition import (
3131
ArrayExtraFieldDefinition,
3232
Chunk,
@@ -82,8 +82,16 @@
8282

8383
_ATTRIBUTES: Final = st.dictionaries(_TEXT, _JSON, max_size=_MAX_MEMBERS)
8484

85-
SHAPES: Final = st.lists(st.integers(0, 2**16), max_size=MAX_RANK).map(tuple)
86-
"""The shape of an array: no axes, or some, each of length 0 or more."""
85+
_MAX_SHAPE_RANK: Final = 8
86+
"""The most axes an array is drawn with."""
87+
88+
_AXIS_LENGTHS: Final = st.one_of(st.just(0), st.integers(1, 64), st.integers(65, 2**63 - 1))
89+
"""The length of an axis: none, short, or long enough to overflow a 32-bit size."""
90+
91+
SHAPES: Final = st.integers(0, _MAX_SHAPE_RANK).flatmap(
92+
lambda rank: st.tuples(*([_AXIS_LENGTHS] * rank))
93+
)
94+
"""The shape of an array: each number of axes about as likely as any other, each axis of any length."""
8795

8896

8997
def _spelled(
@@ -159,10 +167,12 @@ def array_document(
159167
strategies: Strategies,
160168
) -> ZarrV3ArrayMetadataJSON:
161169
"""An array document, each of whose members fits the others; a `ValueError` when its definitions refuse it."""
162-
shape = draw(shapes)
163-
chunk_grid, lengths = draw(_chunk_grid(shape, spellings, strategies))
170+
# The data type is drawn first: Hypothesis draws the first of many
171+
# options far more often when a draw of variable length comes before it.
164172
data_type = draw(data_type_declarations(data_type_names(strategies), strategies))
165173
read_data_type = read(data_type, DataTypeDefinition, strategies)
174+
shape = draw(shapes)
175+
chunk_grid, lengths = draw(_chunk_grid(shape, spellings, strategies))
166176
pipeline = pipelines_for(Chunk(lengths, read_data_type), strategies)
167177
if pipeline is None:
168178
reject() # no array -> bytes codec the strategies hold takes this data type
@@ -255,6 +265,16 @@ def consolidated_group_document(
255265
metadata[path] = draw(
256266
group_document() if is_group else array_document(shapes, spellings, strategies)
257267
)
268+
if is_group and draw(st.booleans()):
269+
# The empty listing the reference implementation writes on each group it lists.
270+
metadata[path] = {
271+
**metadata[path],
272+
ZARR_V3_CONSOLIDATED_METADATA_KEY: {
273+
"kind": "inline",
274+
"must_understand": False,
275+
"metadata": {},
276+
},
277+
}
258278
listing: ZarrV3ConsolidatedMetadataJSON = {
259279
"kind": "inline",
260280
"must_understand": False,

‎packages/zarr-metadata/src/zarr_metadata/testing/strategies.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def consolidated_group_documents(
246246
247247
It lists every node by its path, in one flat listing, and the parent
248248
of each node is a group it also lists. A listed group has no listing
249-
of its own. Each array is drawn as `array_documents` draws it, and
249+
of its own, or an empty one, as the reference implementation writes. Each array is drawn as `array_documents` draws it, and
250250
raises as it raises.
251251
"""
252252
strategies = briefly(strategies)

‎packages/zarr-metadata/tests/testing/test_documents.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from hypothesis import find, given, settings
1616
from hypothesis import strategies as st
1717

18+
from tests.testing._shares import share_of
1819
from zarr_metadata.definition import AcceptedDeclaration, Definition, resolve
1920
from zarr_metadata.definition._definition import canonicalize
2021
from zarr_metadata.model import (
@@ -241,6 +242,13 @@ def _listed(document: Mapping[str, Any]) -> Mapping[str, Any]:
241242
"an array": lambda d: any(node["node_type"] == "array" for node in _listed(d).values()),
242243
"a group": lambda d: any(node["node_type"] == "group" for node in _listed(d).values()),
243244
"a node below a listed group": lambda d: any("/" in path for path in _listed(d)),
245+
"a listed group with an empty listing of its own": lambda d: any(
246+
"consolidated_metadata" in node for node in _listed(d).values()
247+
),
248+
"a listed group with no listing of its own": lambda d: any(
249+
node["node_type"] == "group" and "consolidated_metadata" not in node
250+
for node in _listed(d).values()
251+
),
244252
}
245253

246254

@@ -329,3 +337,39 @@ def test_a_fill_value_its_data_type_refuses_is_a_value_error() -> None:
329337
def test_a_data_type_no_codec_takes_is_not_drawn(document: Mapping[str, Any]) -> None:
330338
"""Without `vlen-utf8`, nothing encodes a `string`, so no document holds one."""
331339
assert document["data_type"] == "int8"
340+
341+
342+
_DOCUMENT_SHARES: dict[str, tuple[Callable[[Mapping[str, Any]], bool], float, float]] = {
343+
"no axes": (lambda d: d["shape"] == (), 0.03, 0.25),
344+
"more axes than five": (lambda d: len(d["shape"]) > 5, 0.1, 1),
345+
"a zero-length axis": (lambda d: 0 in d["shape"], 0.1, 1),
346+
"an axis past 32 bits": (lambda d: any(n >= 2**32 for n in d["shape"]), 0.1, 1),
347+
"the first data type": (lambda d: _name(d["data_type"]) == "bool", 0, 0.12),
348+
}
349+
"""What a share of array documents holds, as a low and a high bound: what a test running only a few hundred examples meets."""
350+
351+
352+
@pytest.mark.parametrize("target", _DOCUMENT_SHARES)
353+
def test_array_documents_share(target: str) -> None:
354+
counts, low, high = _DOCUMENT_SHARES[target]
355+
assert low <= share_of(array_documents(), counts) <= high
356+
357+
358+
def _empty_listings(document: Mapping[str, Any]) -> list[Any]:
359+
return [
360+
node["consolidated_metadata"]
361+
for node in _listed(document).values()
362+
if "consolidated_metadata" in node
363+
]
364+
365+
366+
def test_listed_groups_do_not_share_an_empty_listing() -> None:
367+
"""A test that changes one listing changes no other."""
368+
document: Mapping[str, Any] = find(
369+
consolidated_group_documents(),
370+
lambda drawn: len(_empty_listings(drawn)) >= 2,
371+
settings=_REACH,
372+
)
373+
first, second, *_ = _empty_listings(document)
374+
assert first is not second
375+
assert first["metadata"] is not second["metadata"]

0 commit comments

Comments
 (0)