Skip to content

fix(zarr-metadata): simplify a codec for its chunk in its canonical spelling, and check the result - #4647

Merged
d-v-b merged 9 commits into
zarr-developers:mainfrom
d-v-b:fix/zarr-metadata-chunk-canonical-order
Dec 4, 2026
Merged

d-v-b merged 9 commits into
zarr-developers:mainfrom
d-v-b:fix/zarr-metadata-chunk-canonical-order

Conversation

@d-v-b

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

Copy link
Copy Markdown
Contributor

🤖 AI text below 🤖

A v3 codec's chunk_canonical receives the configuration as the codec's canonical spells it, so two spellings that canonical reduces to one are simplified for a chunk in the same way. Its result is checked more closely: one that it would simplify again, or that holds a declaration the codec did not hold, raises ValueError, and one that is not JSON raises TypeError naming the definition. A result may write an array as a list. chunk_canonical is not asked about a codec that holds a refused declaration at any depth, which keeps its own members as written.

Why

Subagent reviews of #4640 and #4642 found these in _for_chunk, the function that asks a codec's chunk_canonical, which merged with #4640.

  • Order. chunk_canonical was called on 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. The reproduction is a codec whose canonical lowercases mode and whose chunk_canonical drops a mode of "auto": written with "AUTO", its simplest spelling was {"mode": "auto"}, which is not a fixed point, and two arrays that differed only in the case of mode were unequal.
  • A result that adds a nested declaration passed the checks, and the respelled codec then held a declaration that its nested mapping did not.
  • A result that is not JSON raised an error that did not name the definition.
  • An array written as a list was reported as a changed nested declaration.
  • Cost. The early return for a codec with the default chunk_canonical could never be taken, so every array codec was copied and serialised at every nesting level each time a model was compared or hashed. The first hash of 80 nested shards with nothing to simplify took 23.5 ms on main, against 0.56 ms before feat(zarr-metadata): compare each codec in its simplest spelling for the chunk it receives #4640, and takes 0.66 ms here.

No built-in definition is affected by the first four: bytes is the only one with a chunk_canonical, and it has no canonical.

Design notes
  • _for_chunk asks canonical first and hands its result to chunk_canonical. When chunk_canonical changes nothing in that spelling, the codec stays as written, as before.
  • The result is asked about once more. If chunk_canonical changes its own result, the result was not the simplest spelling, and ValueError names the definition.
  • The result is refined as any JSON is, so a list is read as an array, and what is not JSON raises TypeError naming the definition.
  • The locations of the declarations the result holds are compared with the codec's nested, through a new declaration_locations(definition, configuration).
  • A codec that holds a refused declaration, at any depth, is not simplified for its chunk: chunk_canonical is not asked, and the codec keeps its own members as written. Each codec of a pipeline it contains is still simplified for the chunk that one receives. An earlier version of this PR asked chunk_canonical anyway and left out every problem at or under a nested location; but read_configuration stops before the codec's rules when a nested declaration is malformed, so a result that broke the codec's own rules passed.
  • A definition that leaves chunk_canonical at its default returns before anything is copied.
  • respelled refuses a location that is not the location of a nested declaration with the TypeError its docstring promised. Before, it raised IndexError or KeyError, and accepted a negative index and the empty location.
  • docs/models.md says that, for a codec with no problems, canonical_of writes the codec codec_for_chunk returns in the spelling an array model compares.

Not changed: when a leaf codec is simplified inside many nested shards, each level is rebuilt, so that case is still quadratic in nesting depth. It takes 22 ms at 80 levels, and the reader allows at most 84.

Tests and checks
  • tests/v3/test_pipelines.py: four cases of the parametrized simplest-spelling test with a codec acme.pack that has both functions; one success test of a result that writes a list; one test that a codec holding a refused declaration, directly or inside a shard it holds, keeps its own members and its chunk_canonical is not asked; one test that a codec with the default chunk_canonical is returned without asking its canonical; error tests for a result with something left to simplify, one that adds a nested declaration, and one that is not JSON; the problem test now also uses a chunk_canonical that would simplify a codec its chunk rules refuse.
  • tests/v3/test_definitions.py: direct tests of respelled (three declarations, among them a name that carries its configuration) and of item_at, and one error test of respelled over five bad locations.
  • Each new test fails without the source change it covers, except the problem test and the default-chunk_canonical test, which guard behaviour that already held.
  • Run locally: the package suite on Python 3.11 and 3.14 (just test-versions), ruff 0.17.3, pyright, mkdocs build --strict and the pre-commit hooks.
Review record

Review on 2026-12-04. A fresh subagent reviewed the open zarr-metadata PRs, and the agent confirmed each finding by tracing it before acting on it. Each fix was then reviewed by fresh subagents given the diff; rounds 1 and 2 found nothing serious.

Finding Outcome
Moderate: the problem filter let a chunk_canonical result that breaks the codec's own rules pass when a nested declaration is malformed Fixed in 435521d: a codec holding a refused declaration is not simplified; test with the refused codec held directly and inside a shard
docs/models.md said codec_for_chunk returns the compared spelling, though it returns the codec as written when nothing changes Fixed in 435521d and 701eeee
The ArrayCodecDefinition docstring did not say that the simplest-spelling check asks about the result as canonical spells it Fixed in 435521d
No test of the early return for a codec with the default chunk_canonical Added in 435521d
Round 1: the extension-author contract did not say chunk_canonical is never asked about a codec holding a refused declaration; a test name and a summary line claimed too much Fixed in 5389210
Round 1: _holds_refused repeats the walk in _simplest Not changed here: #4648 removes _holds_refused, and canonical_declaration does that walk once
The fragment described unreleased behavior ("Before, …", "no longer copied"); chunk_canonical arrived in #4640, which is not released Fixed in c5f0cd6
Round 2 (moderate, wording): the fragment said such a codec keeps its configuration as written, but each codec of a pipeline it contains is still simplified Fixed in 701eeee

Bookkeeping

🤖 Generated with Claude Code

…s 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
Assisted-by: ClaudeCode:claude-fable-5-2
… 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
@read-the-docs-community

read-the-docs-community Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

📖 2 files modified


Preview · build overview · zarr-metadata · built Dec. 4, 2026, 6:47 p.m. UTC

d-v-b added a commit to d-v-b/zarr-python that referenced this pull request Nov 18, 2026
… from one normal form

Merges refactor/zarr-metadata-one-normal-form, which holds the branches of
zarr-developers#4642 and zarr-developers#4647, and takes its tree: refines
reads the other model's document in this model's scope, so codec_refines
and read_as_claimed are no longer needed and are removed. The changelog
fragment is rewritten for the new behaviour.

Assisted-by: ClaudeCode:claude-fable-5-2
d-v-b and others added 4 commits December 4, 2026 18:39
…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>
…dec that holds a refused declaration

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

`chunk_canonical` arrived in zarr-developers#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>
…eeps 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>
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.06%. Comparing base (12b4721) to head (7cf911c).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4647   +/-   ##
=======================================
  Coverage   95.06%   95.06%           
=======================================
  Files          96       96           
  Lines       14126    14126           
=======================================
  Hits        13429    13429           
  Misses        697      697           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@d-v-b
d-v-b marked this pull request as ready for review December 4, 2026 21:34
@d-v-b
d-v-b merged commit fb60d41 into zarr-developers:main Dec 4, 2026
35 of 38 checks passed
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