Skip to content

feat(zarr-metadata): compare each codec in its simplest spelling for the chunk it receives - #4640

Merged
d-v-b merged 2 commits into
zarr-developers:mainfrom
d-v-b:feat/zarr-metadata-chunk-canonical
Nov 17, 2026
Merged

d-v-b merged 2 commits into
zarr-developers:mainfrom
d-v-b:feat/zarr-metadata-chunk-canonical

Conversation

@d-v-b

@d-v-b d-v-b commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

This PR handles the non-ambiguity between the various ways semantically identical bytes codices can be configured in the codec pipeline.

🤖 AI text below 🤖

A v3 codec that receives an array can declare its simplest spelling for the chunk it receives, with chunk_canonical on ArrayCodecDefinition. It is a function of the configuration, the nested declarations and the Chunk, for two spellings that mean the same thing for one chunk and not for another, which canonical cannot tell from the configuration alone. codec_for_chunk in zarr_metadata.v3.definition returns the codec of a Stage in that spelling, with each codec of the pipelines it contains simplified for the chunk that one receives. The bytes codec declares one: it drops an endian when each value takes one byte. So two ZarrV3ArrayMetadata models that differ only in the endian of a bytes codec that receives values of one byte (bool, int8, uint8 or r8) are now equal, hash alike and refine each other. A bytes codec that receives values of several bytes, as it does after a cast_value to float32, is compared by its endian as before.

Why

pydantic-zarr drops the no-op endian of a bytes codec itself, so that a spec built by hand equals one read back from zarr-python (3.0 wrote the endian for single-byte data types, 3.1 does not). To do that it re-derived the data type each bytes codec receives, and got it wrong after a cast_value (fixed in zarr-developers/pydantic-zarr#203). zarr-metadata already tracks that chunk for its rules, but a definition's canonical receives the configuration alone, so the equivalence could not be declared where the codec is defined. The v2 models already treat <i1 and |i1 as one dtype, and v3 blosc drops a typesize that noshuffle ignores; this is the same kind of equivalence, with the chunk as an input.

Design notes
  • codec_for_chunk(stage) returns a declaration, not JSON. Each codec of a pipeline the codec contains is simplified first, for the chunk that one receives, and then the codec's own configuration by chunk_canonical. Because the result is an ordinary AcceptedDeclaration, the existing declaration_key, canonical_of and refines apply to it unchanged, and nothing else had to learn about chunks. When nothing is simplified the codec itself is returned.
  • chunk_canonical runs before canonical, which then simplifies the result as it does any configuration.
  • The result of chunk_canonical is checked, as the result of canonical is: the definition must allow it, the chunk rules must find no problem with it, and each nested declaration must stay as it is. A failure raises ValueError, a bug in the definition.
  • A codec the chunk rules find a problem with, a codec that receives bytes, and a codec nothing claims are left as they are. A chunk whose data type is unknown simplifies nothing.
  • to_json still writes each codec as given, and AcceptedDeclaration.__eq__ on a codec alone is unchanged: only the array model, which knows the chunk, compares codecs this way.
  • New internal helpers in definition/_definition.py: respelled builds the declaration another spelling reads as, and item_at reads a location in a configuration.
Tests and checks
  • tests/v3/test_pipelines.py: one parametrized test of the simplest spelling per chunk (single-byte and multi-byte data types, after a cast, inside shards and nested shards, index codecs, unknown data types and unknown chunks), one test that a codec with a problem is left alone, and one test per error case of a faulty chunk_canonical.
  • tests/model/test_array.py: ==, hash and refines of array models, for pairs that are and are not one array.
  • Run locally: the package suite on Python 3.11 and 3.14 (just test-versions), ruff 0.17.3, pyright, mkdocs build --strict, the changelog check and the pre-commit hooks.
Stack bookkeeping

🤖 Generated with Claude Code

…the chunk it receives

A definition's `canonical` reduces two spellings of a configuration to
one when they mean the same thing wherever the declaration is used. It
receives the configuration alone, so it cannot reduce spellings that
mean the same thing only for some chunks. The `endian` of a `bytes`
codec is one: it means nothing when each value takes one byte, and is
required when a value takes several.

`ArrayCodecDefinition` gains `chunk_canonical`, a function of the
configuration, the nested declarations and the chunk, and
`codec_for_chunk` returns the codec of a `Stage` in that spelling, with
each codec of the pipelines it contains simplified for the chunk that
one receives. The result is a declaration, so `declaration_key`,
`canonical_of` and `refines` work on it as on any other. Its
configuration is checked again: the definition must allow it, the chunk
rules must find no problem with it, and each nested declaration must
stay as it is.

`bytes` declares a `chunk_canonical` that drops `endian` for a data type
stored in single bytes, and a v3 array model compares, hashes and
refines by its codecs as `codec_for_chunk` returns them. So an `int8`
array is one array with either `endian` or with none, as zarr-python 3.0
and 3.1 write it, while a `bytes` codec after a `cast_value` to a
multi-byte data type is still compared by its `endian`.

Assisted-by: ClaudeCode:claude-fable-5-2
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 zarr-metadata | 🛠️ Build #35931045 | 📁 Comparing 070840f against latest (95ec04a)

  🔍 Preview build  

6 files changed · ± 6 modified

± Modified

@d-v-b
d-v-b marked this pull request as ready for review November 17, 2026 15:58
@d-v-b
d-v-b merged commit 9831e99 into zarr-developers:main Nov 17, 2026
45 checks passed
d-v-b added a commit to d-v-b/zarr-python that referenced this pull request Nov 17, 2026
…canonical-documents

Main now carries the chunk-aware canonical hook (zarr-developers#4640) and the nested declaration order fix (zarr-developers#4641). The one conflict was in model/_array_v3.py, where this branch adds canonical_array_document_v3 next to codecs_for_chunks; both are kept.

Assisted-by: ClaudeCode:claude-fable-5-2
d-v-b added a commit to d-v-b/zarr-python that referenced this pull request Nov 17, 2026
… document it becomes

A review of zarr-developers#4640 found gaps in how _for_chunk checks the result of chunk_canonical:

- a result that added a nested declaration passed, and the respelled codec then held a declaration its nested mapping did not;
- a result that was not JSON raised an error that did not name the definition;
- an array written as a list was reported as a changed nested declaration;
- a problem inside a nested declaration, which is the document's, was held against the result.

The result is now refined as any JSON is, its declaration locations are compared with the codec's, and problems under a nested location are left out. respelled refuses a location that holds no nested declaration with the TypeError its docstring promises.

Assisted-by: ClaudeCode:claude-fable-5-2
d-v-b added a commit that referenced this pull request Dec 4, 2026
…pelling, and check the result (#4647)

* fix(zarr-metadata): simplify a codec for its chunk in the spelling its canonical gives

chunk_canonical received the configuration as written, and canonical ran afterwards. A codec whose canonical output its chunk_canonical reduces further then had two keys for one meaning: array models that differed only in such spellings were unequal, and the simplest spelling was not a fixed point.

_for_chunk now hands chunk_canonical the configuration as canonical spells it, and checks that chunk_canonical has nothing left to simplify in its own result. A definition that leaves chunk_canonical at its default is skipped without calling either.

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): add the changelog fragment

Assisted-by: ClaudeCode:claude-fable-5-2

* fix(zarr-metadata): check what a codec's chunk_canonical gives as the document it becomes

A review of #4640 found gaps in how _for_chunk checks the result of chunk_canonical:

- a result that added a nested declaration passed, and the respelled codec then held a declaration its nested mapping did not;
- a result that was not JSON raised an error that did not name the definition;
- an array written as a list was reported as a changed nested declaration;
- a problem inside a nested declaration, which is the document's, was held against the result.

The result is now refined as any JSON is, its declaration locations are compared with the codec's, and problems under a nested location are left out. respelled refuses a location that holds no nested declaration with the TypeError its docstring promises.

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): name the changelog fragment after the upstream PR

Assisted-by: ClaudeCode:claude-fable-5-2

* fix(zarr-metadata): keep a codec that holds a refused declaration as written in codec_for_chunk

`codec_for_chunk` filtered out problems located inside a nested
declaration, but `read_configuration` stops before running the rules
when a nested declaration is malformed, so a `chunk_canonical` result
that broke the codec's own rules could pass. A codec that holds a
refused declaration now has no simpler spelling, as with any other
problem, and its `chunk_canonical` is not asked. Correct the docs on
what `codec_for_chunk` returns and on the simplest-spelling check, and
test the early return for a codec with the default `chunk_canonical`.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(zarr-metadata): say that chunk_canonical is not asked about a codec that holds a refused declaration

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(zarr-metadata): drop unreleased behavior from the chunk_canonical fragment

`chunk_canonical` arrived in #4640, which is not released yet, so the
fragment describes the behavior instead of how it changed.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(zarr-metadata): say that a codec holding a refused declaration keeps only its own members

`codec_for_chunk` still simplifies each codec of a pipeline such a
codec contains, so the fragment, the docstring and the docs say that
the codec keeps its own members as written.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
d-v-b added a commit that referenced this pull request Dec 4, 2026
… from one normal form (#4648)

* feat(zarr-metadata): compare each codec in its simplest spelling for the chunk it receives

A definition's `canonical` reduces two spellings of a configuration to
one when they mean the same thing wherever the declaration is used. It
receives the configuration alone, so it cannot reduce spellings that
mean the same thing only for some chunks. The `endian` of a `bytes`
codec is one: it means nothing when each value takes one byte, and is
required when a value takes several.

`ArrayCodecDefinition` gains `chunk_canonical`, a function of the
configuration, the nested declarations and the chunk, and
`codec_for_chunk` returns the codec of a `Stage` in that spelling, with
each codec of the pipelines it contains simplified for the chunk that
one receives. The result is a declaration, so `declaration_key`,
`canonical_of` and `refines` work on it as on any other. Its
configuration is checked again: the definition must allow it, the chunk
rules must find no problem with it, and each nested declaration must
stay as it is.

`bytes` declares a `chunk_canonical` that drops `endian` for a data type
stored in single bytes, and a v3 array model compares, hashes and
refines by its codecs as `codec_for_chunk` returns them. So an `int8`
array is one array with either `endian` or with none, as zarr-python 3.0
and 3.1 write it, while a `bytes` codec after a `cast_value` to a
multi-byte data type is still compared by its `endian`.

Assisted-by: ClaudeCode:claude-fable-5-2

* feat(zarr-metadata): return each model with its document in its simplest equivalent spelling

Two models are equal when their documents mean the same thing, and
`to_json` writes each document as it was given, so equal models can
write different documents. A consumer that needs one spelling for all of
them, to compare documents field by field or to hash them, had to
normalize each member itself.

Each model gains `canonicalize`: the v3 and v2 array, group and
consolidated metadata models. It returns the model read from its
document in its simplest equivalent spelling, written from the parts
that `==` compares: each declaration as `canonical_of` writes it, each
v3 codec as `codec_for_chunk` returns it for the chunk it receives, and
the fill value in the canonical spelling of its data type. Empty
`attributes`, empty v3 `storage_transformers` and a v2
`dimension_separator` of "." are left out, since a document without them
means the same. Everything else is kept as written.

The result equals the model, and equal models in one scope give the
same document apart from the order of the members of an object, which a
`JSONWriter(sort_keys=True)` sorts. A group keeps the documents its
`consolidated_metadata` lists as written, since it does not read them;
the consolidated metadata models canonicalize each node they list.

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): name the changelog fragment after the upstream PR

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): name the changelog fragment after the upstream PR

Assisted-by: ClaudeCode:claude-fable-5-2

* fix(zarr-metadata): simplify a codec for its chunk in the spelling its canonical gives

chunk_canonical received the configuration as written, and canonical ran afterwards. A codec whose canonical output its chunk_canonical reduces further then had two keys for one meaning: array models that differed only in such spellings were unequal, and the simplest spelling was not a fixed point.

_for_chunk now hands chunk_canonical the configuration as canonical spells it, and checks that chunk_canonical has nothing left to simplify in its own result. A definition that leaves chunk_canonical at its default is skipped without calling either.

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): add the changelog fragment

Assisted-by: ClaudeCode:claude-fable-5-2

* test(zarr-metadata): check what canonicalize simplifies, in scopes that are not the default

A review found that the Hypothesis tests passed with a canonicalize that returned the model's own document, that no test noticed a model losing its scope, and that four behaviours had no case: a claimed extra field, a v2 fill value, a chunk grid and a storage transformer that are respelled.

The parametrized cases are now read in scopes that are not the default ones. The generated documents are compared with the same document reordered at every depth and, for arrays, with each declaration in its own simplest spelling. The reordering helper reverses every object, which the fix in #4641 allows.

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): say how canonicalize writes a declaration nothing claims

Its configuration is kept, but its envelope is written in the shortest form: an empty configuration and a must_understand of true are left out.

Assisted-by: ClaudeCode:claude-fable-5-2

* fix(zarr-metadata): check what a codec's chunk_canonical gives as the document it becomes

A review of #4640 found gaps in how _for_chunk checks the result of chunk_canonical:

- a result that added a nested declaration passed, and the respelled codec then held a declaration its nested mapping did not;
- a result that was not JSON raised an error that did not name the definition;
- an array written as a list was reported as a changed nested declaration;
- a problem inside a nested declaration, which is the document's, was held against the result.

The result is now refined as any JSON is, its declaration locations are compared with the codec's, and problems under a nested location are left out. respelled refuses a location that holds no nested declaration with the TypeError its docstring promises.

Assisted-by: ClaudeCode:claude-fable-5-2

* fix(zarr-metadata): compare codecs for the chunk the richer reading knows when judging a refinement

Since #4640 an array model's refines compared each side's codecs as simplified for that side's own chunks. A richer scope can know more of a chunk than a poorer one: it claims the data type, a codec that comes before, or the codec itself. It then dropped a bytes codec's endian that the poorer reading kept, and refined_in(...) no longer refined the model it came from.

codec_refines compares the codec of a stage with another reading of it, simplifying both for the chunks the stage knows. A gain is judged by what the unclaimed codec reads as with the stage's claims, simplified the same way. ZarrV3ArrayMetadata.refines uses it for each codec.

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): add the changelog fragment

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): name the changelog fragment after the upstream PR

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): name the changelog fragment after the upstream PR

Assisted-by: ClaudeCode:claude-fable-5-2

* refactor(zarr-metadata): derive each model's key, canonicalize and refines from one normal form

Each of the six models writes its document in its simplest equivalent
spelling in one function. The key that == and hash compare is that
document as JSON text with the claims the scope made, canonicalize reads
it, and refines reads the other model's document in this model's scope
and compares. The per-model key functions, the per-model refines
traversals and the declaration-level refines are removed.

A model no longer refines one whose document its scope reads with a
problem, such as a configuration key that the definition does not
declare under a name the other scope left unclaimed.

Assisted-by: ClaudeCode:claude-fable-5-2

* test(zarr-metadata): check refinement across scopes and member order for every generated document

Carries the tests of #4648 that do not name
codec_refines, which this branch does not have.

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): add the changelog fragment

Assisted-by: ClaudeCode:claude-fable-5-2

* refactor(zarr-metadata): simplify a codec for its chunk inside the walk that writes its simplest spelling

canonical_declaration writes a declaration in its simplest spelling and
lets a caller that knows more take part: a pipeline gives the spelling of
each codec it placed, and a step that simplifies the codec's own members
for the chunk it receives. codec_for_chunk returns that spelling as JSON,
so nothing builds a second declaration to read again, and respelled is
removed. canonical is called from one function.

Assisted-by: ClaudeCode:claude-fable-5-2

* docs(zarr-metadata): say that canonicalize keeps a nested listing as written

A group compares the documents its consolidated_metadata lists as
written, so ZarrV3ConsolidatedMetadata.canonicalize writes a listed group
as that group's own canonicalize does and keeps the group's own listing
as written. Simplifying it would give a model that does not equal the
original. The docstrings no longer promise every listed document in its
simplest spelling, and no longer suggest that writing a canonical listing
back with replace(consolidated_metadata=...) gives an equal group.

The table test gains a listing that holds a group with its own listing.

Assisted-by: ClaudeCode:claude-opus-5-5

* test(zarr-metadata): check that v2 documents canonicalize to one document exactly when equal

Add a property test that draws v2 array documents with a respelled twin
and asserts that canonicalizing is idempotent, round-trips, and agrees
with `==`. Lift the hypothesis deadline on the canonicalize property
tests. Correct the docs on which declarations drop `must_understand`,
the `canonical_of` docstring, and say in the changelog that a group
keeps its consolidated listing as written.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(zarr-metadata): respell an overflowing float32 fill value in the v2 canonicalize test

The respelled twin now swaps `1e39` and `"Infinity"` for a `<f4` fill
value, and the test checks that the canonical document leaves out a
`dimension_separator` of "." and empty attributes, and keeps the
model's context.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(zarr-metadata): keep a codec that holds a refused declaration as written in codec_for_chunk

`codec_for_chunk` filtered out problems located inside a nested
declaration, but `read_configuration` stops before running the rules
when a nested declaration is malformed, so a `chunk_canonical` result
that broke the codec's own rules could pass. A codec that holds a
refused declaration now has no simpler spelling, as with any other
problem, and its `chunk_canonical` is not asked. Correct the docs on
what `codec_for_chunk` returns and on the simplest-spelling check, and
test the early return for a codec with the default `chunk_canonical`.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(zarr-metadata): type the canonical document before reading its members

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(zarr-metadata): say that chunk_canonical is not asked about a codec that holds a refused declaration

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(zarr-metadata): carry a model's normal form and key across a scope that reads it alike

`refines` reads the other model's document in this model's scope, and
`with_context` reused the reading but rebuilt the normal form and key,
so every `refines` paid for both. A model now caches its claims, and
`with_context` to a scope that reads every claim identically carries
the cached normal form, claims and key to the new model. v2
consolidated metadata reuses its node readings in that case too, as
v3 consolidated metadata does.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(zarr-metadata): correct stale wording and changelog fragments for refines across scopes

Describe `codec_for_chunk` as writing JSON in the fragments that
introduce it, drop the "Before" clause that described unreleased
behavior, correct a comment and a test docstring that still described
refines declaration by declaration, and check that a v2 model does not
refine a document its own scope reads with a problem.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(zarr-metadata): drop unreleased behavior from the chunk_canonical fragment

`chunk_canonical` arrived in #4640, which is not released yet, so the
fragment describes the behavior instead of how it changed.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(zarr-metadata): say that a codec holding a refused declaration keeps only its own members

`codec_for_chunk` still simplifies each codec of a pipeline such a
codec contains, so the fragment, the docstring and the docs say that
the codec keeps its own members as written.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(zarr-metadata): un-nest the canonical call and test that a moved model reads as a fresh one

`_spelled` asks a module-level function instead of a nested one.
`refined_in` uses the model's cached claims. A model moved by
`with_context` into a scope that reads it alike now has a test that it
equals, and hashes as, the same document read in that scope, for v2 and
v3 consolidated metadata. The `REFUSED_GZIP` docstring says a codec
that holds it has no simplest spelling.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(zarr-metadata): check each node moved by with_context against a fresh read

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs release notes Automatically applied to PRs which haven't added release notes zarr-metadata Specific to the zarr-metadata sub-package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant