Skip to content

Commit b8c6c5b

Browse files
committed
Merge branch 'up/4334' into up/4377
2 parents d13c317 + 7a0154e commit b8c6c5b

110 files changed

Lines changed: 11943 additions & 683 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎changes/4356.bugfix.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Coordinate selections (including `vindex` masks) and orthogonal integer-array
2+
selections no longer allocate counts proportional to the total number of chunks.
3+
Previously, selecting a few points on an array with billions of chunks could raise
4+
`MemoryError`. The indexers now store occupied chunk IDs and run-end offsets
5+
(`chunk_run_ends`); unsorted selections can still require sorting. The dense
6+
`chunk_nitems` / `chunk_nitems_cumsum` attributes of `CoordinateIndexer` and
7+
`IntArrayDimIndexer` remain available without deprecation warnings. Each array is
8+
materialized on first access and cached, preserving its identity and in-place
9+
mutations on subsequent reads. These attributes remain dataclass fields, so
10+
`dataclasses.asdict()` materializes them; `repr` omits them to avoid dense allocation
11+
during inspection. Accessing a dense attribute still allocates memory proportional
12+
to the full chunk grid.
13+
14+
The new `zarr.core.common.ceildiv_int` helper uses exact integer arithmetic for
15+
chunk counts and slice calculations. For example, `ceildiv_int(2**62 - 1, 1)`
16+
returns `2**62 - 1`. The existing `ceildiv` helper retains its floating-point behavior.

‎changes/4414.bugfix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
An array `config` with a key that is not a runtime configuration option (for example a misspelled `write_empty_chunk`) now raises a `TypeError` naming the unknown and valid keys. Previously `create_array`, `create`, `open_array` and other functions silently ignored such keys.

‎packages/zarr-metadata/README.md‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Two layers and an optional integration:
1414
specifications, plus types for [`zarr-extensions`](https://github.com/zarr-developers/zarr-extensions/)
1515
and a few widely-used-but-unspecified entities (e.g. consolidated metadata).
1616
- **Document models** (`zarr_metadata.model`): canonical frozen-dataclass
17-
models of whole metadata documents, with structural validators, loc-aware
17+
models of whole metadata documents, with validators, loc-aware
1818
parsers, and store-key (de)serialization. A document produced by `to_json`
1919
shares no mutable state with the model that produced it.
2020
- **Optional Pydantic integration** (`zarr_metadata.pydantic`, requires
@@ -56,11 +56,20 @@ members that the strict model parser rejects.
5656

5757
The model validators enforce the declared document structure and a small set
5858
of context-free consistency rules, including fixed format literals, finite
59-
JSON numbers, non-negative dimensions, non-empty v3 codec pipelines, and one
60-
`dimension_names` entry per array dimension. They do not interpret extension
61-
names or configurations, resolve codec pipelines, or decide whether a data
62-
type, chunk grid, codec, or storage transformer is supported. Those decisions
63-
belong to consumer implementations.
59+
JSON numbers, non-negative dimensions, and one `dimension_names` entry per
60+
array dimension. In a v3 document they also read
61+
each extension point -- the data type, chunk grid, chunk key encoding, each
62+
codec and each storage transformer -- through the definition that claims its
63+
name in a scope, `CORE_AND_EXTENSIONS` unless a `context` is passed: a
64+
configuration its definition refuses is refused, and a key it does not
65+
declare is reported as `unknown_key`. A name nothing in the scope claims is
66+
left unjudged, and whether to support it is the consumer's decision. A v3
67+
fill value is judged against the data type it names, by that data type's
68+
definition, the chunk grid against the shape, by the grid's definition,
69+
and the codecs as a pipeline: in order, each judged by its definition
70+
against the chunk it is handed, a shard's inner and index codecs too.
71+
The validators do no arithmetic on values: whether a fill value survives
72+
a `cast_value` round trip is not judged.
6473

6574
The Pydantic integration's generated JSON Schemas express independently
6675
checkable document structure and field constraints, but they are not a
@@ -75,7 +84,7 @@ versus `shape`. Consumers should run the model parser after schema validation.
7584
At minimum, this library supports what Zarr-Python needs: the complete
7685
Zarr v2 and v3 specs, consolidated metadata, and a subset of the metadata
7786
defined in `zarr-extensions`. We are generally open to contributions that
78-
add types, models, or structural validation for Zarr metadata with a
87+
add types, models, or validation for Zarr metadata with a
7988
published spec.
8089

8190
Runtime array behavior is out of scope: nothing here encodes or decodes
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Attributes are user data, and a non-finite number in them is read,
2+
validated and written back. zarr-python writes attributes with the
3+
defaults of Python's `json` module, so an xarray `_FillValue` or a CF
4+
`missing_value` of NaN is stored as a bare `NaN`, which RFC 8259 lacks;
5+
the model refused such a document and could not read the store at all.
6+
A node's `attributes`, a v2 `.zattrs`, the `.zattrs` entries of a v2
7+
`.zmetadata`, and the attributes of the nodes a v3 group's inline
8+
`consolidated_metadata` holds may now hold `NaN`, `Infinity` and
9+
`-Infinity`, in every document `validate_*`, `parse_*` and `is_*`, and in
10+
`from_key_value` and `to_key_value`. Wherever the spec interprets a value
11+
it spells those numbers as strings, so anywhere else a non-finite number
12+
is still refused, and the store reader now says where one is instead of
13+
failing to decode the document. `is_json`, `validate_json` and
14+
`parse_json` judge by RFC 8259 alone, so a document the node guards
15+
accept can hold a value `is_json` refuses.
16+
17+
`to_key_value` validates the document it writes as `from_key_value`
18+
validates what it reads, and raises `MetadataValidationError` with every
19+
problem instead of writing a document the reader refuses. A model built
20+
by hand is not validated, so the writer is where these are caught:
21+
22+
- a non-finite number outside attributes, which was refused before too;
23+
- a non-string attribute key, which was written as a string;
24+
- a value that is not JSON, which raised `TypeError`;
25+
- anything else the reader refuses, such as `dimension_names` longer than
26+
`shape`, which was written and then refused on read.
27+
28+
Any sequence is written as an array. A number too large for a float, such
29+
as `1e400`, is read as infinity, so in attributes it is now accepted and
30+
written back as `Infinity`.
31+
32+
Through `zarr_metadata.pydantic`, pydantic writes a non-finite attribute
33+
as `null` unless the enclosing model sets `ser_json_inf_nan="constants"`.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
`from_key_value` takes a mapping keyed by `str` or by any narrower key
2+
type, so reading back what `to_key_value` wrote type-checks. `to_key_value`
3+
keys its result by the store-key literals, and since a mapping's key type is
4+
invariant, a `Mapping[str, bytes]` parameter refused it.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`MetadataValidationError` pickles and copies with its message. A copy was
2+
built from the error's message, and its message came out as the
3+
original's characters, one per line.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
**Breaking:** `ValidationProblem` and `MetadataValidationError` check at run
2+
time what their annotations declare, and raise `TypeError` otherwise. A
3+
problem's `loc` is a tuple of strings and integers, so a list, which was
4+
accepted before, is now refused, as is a `bool`; its `message` is a string
5+
and its `kind` one of the kinds; an error holds problems. A caller that is
6+
not type-checked fails where it builds the value, not later, where a
7+
location is read.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
A `bytes` or `bytearray` is not a JSON array. Where a document expects an
2+
array, an empty one passed as an array with no items:
3+
`parse_array_metadata_v3` returned `storage_transformers=b""`, and the v2
4+
readers took `dtype=b""` and `filters=b""`, then failed when the model was
5+
written. The validators and `parse_*` now refuse one at `codecs`,
6+
`storage_transformers`, `dimension_names`, a v2 `dtype` and its field
7+
records, and v2 `filters`.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
A v2 array document's members outside the `.zarray` definition are still
2+
tolerated, and dropped on read, but one that is not a JSON value, or whose
3+
key is not a string, is now a problem, as it is in a v3 document.
4+
`parse_array_metadata_v2` returned such members in a document its type
5+
says holds string keys, which `is_array_metadata_v2` refused.
6+
`ZarrV2ArrayMetadataJSON` declares its other members as JSON values
7+
(`extra_items`), as its pydantic schema already did.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A model read by `from_json` or `from_key_value` shares no mutable state
2+
with the document it was read from, as a document from `to_json` already
3+
shares none with its model. A frozen model held the caller's own
4+
dictionaries, so editing the input after reading it changed the model,
5+
and could make `to_key_value` fail.

0 commit comments

Comments
 (0)