Skip to content

refactor(zarr-metadata): type v3 node documents by their TypedDict before reading them in a scope - #493

Draft
d-v-b wants to merge 27 commits into
mainfrom
claude/audit-array-members-v3-480ed5
Draft

d-v-b wants to merge 27 commits into
mainfrom
claude/audit-array-members-v3-480ed5

Conversation

@d-v-b

@d-v-b d-v-b commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

Reading a v3 array or group document now checks each member against the document's TypedDict before any declaration is read, so a problem in one member no longer hides another, and a problem in shape or dimension_names is located at the element, such as ('shape', 1). Nesting is counted from the root of the input, also in a consolidated listing. A non-finite number (NaN, Infinity, -Infinity) is now JSON everywhere: whether text may hold one is left to the routines that read and write it. A data type may still refuse a non-finite fill value that it writes by name, as the float types do in both formats, and it declares that spelling with the new fill_value_named. named_fill_value calls it, and the repair functions use it with a new context argument, so an extension data type takes part. A value that is not JSON no longer stops a declaration's own checks. typed_json.check returns each array as a tuple. Some problem messages, and their order, have changed.

typed_json.UserJSONValue is removed: use JSONValue, which may now hold a non-finite number. ExtraFieldDefinition's finite argument is removed: drop it, since every extension field may now hold one.

Design

Reading a v3 node document is now a type refinement followed by semantic checks, instead of hand-written member checks (members_past_the_levels, check_literal(doc, "zarr_format", 3), the _MEMBERS_V3 tables).

  1. Refinement lives in the annotation checker. The parsers _typed_json compiles from a TypedDict refine raw input as they check it:

    • scalar leaves reject values that are not JSON (bytes, a non-string key, an int too long to write as text);
    • containers accept any array or object and check depth from the root via loc;
    • a JSONValue leaf refines with refine_json;
    • a key a closed TypedDict does not declare is reported and its value is still refined.

    typed_json.check no longer pre-refines the whole value, and neither does a definition's read_configuration: the finite flag and the whole-configuration refine pass are gone.

  2. A non-finite number is JSON. Whether JSON text may hold NaN, Infinity or -Infinity is left to the routines that read and write it (Python's json module does both). The rules about a value's meaning stay: a value spelled as a float's fill value (the fill value, a complex component, a struct field, a codec scalar such as scale_offset's scale) written as a non-finite number is refused because the spec says to write it by its name. refine_user_data, UserJSONValue and the purity gate on nested declarations (_pure) are removed. The integer digit limit stays, because past it rule messages and json_text-based equality would raise.

  3. The data type owns the spelling. No code outside a data type knows how it writes a non-finite number. WithFillValue gains fill_value_named, and named_fill_value(data_type, value) calls it for a data type a scope read. The v3 float types and the v2 float and complex dtypes build their refusal and their hook from one function per format; complex composes its component's, and struct its fields'. The non_finite_fill_value repair resolves the array's data type in the context it is given (new keyword on the three repair functions, passed on by the repaired readers), runs the hook only for a fill value the data type's rules refuse, and writes only the non-finite numbers the hook gives a string for. This replaces a generic helper and a hard-coded list of core type names.

  4. typed_document(doc, shape, at) (definition/_definition.py) types the document with the same compiled checker (_checker) that reads configurations. The declaration leaf returns a _NestedDeclaration holding its refined JSON, what was written, and its refine problems. A declaration that is not JSON is therefore refused by its kind without hiding the member's other declarations. Undeclared string keys are passed on as extension fields to the claiming definition.

  5. read_array_v3 / read_group_v3 then read each nested declaration in the scope (read_found), the extension fields, and the cross-member checks (fill value against data type, grid against shape, the codec pipeline, dimension_names length).

  6. Configurations share the unwritten-configuration rule. A declaration that writes no configuration has the empty one, which is not in the input and is not depth-checked (_given, shared by _read and _read_carried).

  7. One envelope rule. read_consolidated_metadata_v3 checks the envelope with typed_json.check against ZarrV3ConsolidatedMetadataEnvelopeJSON, the same TypedDict the group's extension field uses. The entries of metadata are left out because each is read as a document.

  8. Envelope check derived. The declaration envelope check is derived from str | ZarrV3NamedConfigJSON (definition/_envelope.py).

An earlier version of this PR added a parallel set of member and leaf parsers (FoundDeclaration, _MemberParsers, _Refining, compile-time guards). They duplicated the checker and are deleted. src is +427/−351 against 4b2dede.

Behavior changes
  • Locations: problems in shape / dimension_names point at the element.
  • Refine before type: a member that has the wrong type and also holds a non-JSON value (such as bytes) reports the non-JSON value only.
  • Non-finite numbers:
    • A number may be NaN, Infinity or -Infinity anywhere, including in a configuration, a nested declaration, and an undeclared key. Previously only attributes and unclaimed extension fields could hold one.
    • A value spelled as a float's fill value (including a complex component, a struct field, and scale_offset / cast_value scalars) written as a non-finite number is still refused, in v2 and v3.
    • The repair now also writes a struct's float fields by name, accepts v3 object-form data types ({"name": "float64"}) and any v2 float typestr the reader accepts (such as <f08), and works for an extension data type that declares fill_value_named. A complex fill value with one component of the wrong type ([NaN, null]) is no longer partly repaired; the strict read refuses it either way. A fill value whose data type nothing in scope reads is not interpreted, so it may hold one.
    • ExtraFieldDefinition(finite=...), refine_user_data and typed_json.UserJSONValue are removed.
    • typed_json.check returns arrays as tuples.
  • Exhaustive declarations: a value that is not JSON no longer stops a declaration's own checks. Its other problems (a wrong envelope member, a non-string key, a nested declaration too deep) are reported beside it, and the declaration is still refused. The group reader and the standalone consolidated reader now report the same problems for an extension field's envelope.
  • Consolidated listings: read_consolidated_metadata_v3 reports the same envelope problems as the group reader, in the same order. Missing-key messages name the key, and depth counts from the group.
  • Messages and order:
    • codecs: [] reports the generic MinLen message.
    • Problem order: type layer, then extension fields, then declarations, then cross-member checks.
  • JSON Schema: output gains additionalProperties (any JSON value) on the node documents and their attributes.
  • Unchanged: validity verdicts, models, to_json output and declaration placement (106,398 differential runs against 4b2dede), apart from the non-finite change above and one exception. A v2 structured dtype nested 127 levels deep, whose innermost record has a subarray shape, is now refused as nested too deep (see the review record).

Performance against 4b2dede (read_array_metadata_v3, CPython 3.11 arm64):

  • small array: 47 → 64 µs;
  • array with 5k attributes: 7.3 → 7.4 ms;
  • 500-array consolidated listing: 25.4 → 33.8 ms.

The non-finite change did not move these numbers (measured again at a3aa5e7).

Adversarial review record

Fresh reviewer agents were given the diff and files, not the author's conclusions. Each finding was reproduced before it was fixed, and each fix has a test that fails without it.

First design (5f94a6a–9553a2cc8):

  • Round 1: 2 serious findings, fixed in 3ddee67.
    • A non-JSON declaration hid the other declarations' problems.
    • Claimed extension fields were refined by the wrong layer.
  • Rounds 2–4: no serious findings. Latent paths became compile-time errors in 8d76087, and a test was tightened in 9553a2c.

Rework onto the checker (e2eccca–aec16c1d5):

  • Round 1 (e2eccca): 3 serious and 1 moderate, all fixed in 074377c.
    • finite=False semantics left undecided; now decided by annotation, with docstrings updated.
    • A nested declaration that is not JSON was read as null.
    • check accepted a non-JSON value under an undeclared key.
    • The synthesized empty configuration was depth-checked.
  • Round 2 (074377c): 1 serious, fixed in da32aeb via _given. A carried data type such as r16 at the last level was refused. Test gaps were filled in the same commit.
  • Round 3 (da32aeb): no serious findings.
    • Moderate: the standalone consolidated reader did not refine unexpected envelope members. Fixed in b88c121.
    • Minor: a docstring overstated unknown-key tolerance. Fixed in b88c121.
  • Round 4 (b88c121): 1 serious, fixed in fcc5795. The hand-written envelope check counted depth one level short and ordered problems differently. The envelope check was replaced by the group's TypedDict checker rather than patched again.
  • Rounds 5 and 6 (fcc5795, two independent reviewers): no serious findings. One missing test was added in aec16c1.

The gap noted after round 6 (the group reader stopping at the first non-JSON problem in an extension field) is closed by removing finite and the whole-value refine passes:

Checker as the only refiner (1aa86a8–789f2e380):

  • Round 1 (1aa86a8, two reviewers): 2 serious and 1 moderate, all fixed in 4fb9eed.
    • Depth was counted from the declaration rather than the root for an envelope member.
    • A nested declaration holding NaN let the configuration holding it be accepted, so it wrote a non-finite number.
    • Moderate: read_configuration mixed None and UNSET for "not JSON", and some docs were stale.
  • Round 2 (4fb9eed): 3 serious.
    • Fixed in 789f2e3: a rule saw a nested declaration that is not JSON as null, so a rule reading a nested codec's name raised TypeError. Rules now run only when every nested declaration is JSON.
    • Rejected, predates the PR: a configuration holding a refused nested declaration is refused, so cross-declaration checks (codec pipeline, struct fill value) do not run on it. Base 4b2dede behaves the same. See follow-ups.
    • Accepted, not fixed here: a v2 struct's configuration is the synthetic {"fields": records}, so the checker counts one level more than the document. A struct nested 127 levels deep with a subarray shape at the bottom (which NumPy can build) is refused. Base accepted it. A correct fix checks the records at their document location, which belongs with porting the v2 readers onto the chain (see follow-ups); threading a depth offset through every checker parser would be a larger change than the bug.
  • Round 3 (789f2e3): no serious or moderate findings. Minor: the NaN case of the new test passed whether or not the rules ran (shown with a mutant of _named). The test was split in 5542559, and the mutant now fails it. Also noted, by design: a nested declaration of no JSON type suppresses the holder's rules, so a problem they would find in a sibling is not reported, as for a nested declaration with no name.
  • Round 4 (4fb9eed..5542559): no serious findings. Moderate: no test showed that a nested declaration whose configuration is not an object keeps its holder's rules from running. Minor: the gate's comment and _held's docstring did not say the rules see only nested declarations that are JSON. Both fixed in 56561b5.

Non-finite numbers are JSON (ddacc86, 56561b5), after the author decided that what counts as valid JSON is left to the JSON IO routines:

  • Round 1 (two reviewers, spec and crash lenses): no serious findings.
    • Fixed in 5d23015: docs/validation-rules.md still said check rejects a non-finite number everywhere; the repair TypedDicts called a bare NaN "not JSON"; the kept rule's message said "fill value" although scale_offset and cast_value spell scalars the same way; the rule was untested for float16, float64, both complex types and a struct field.
    • Fixed in c7f34fa: the pydantic warning, the JSON writer and to_key_value docstrings still said non-finite numbers are kept to attributes; the definition docs now warn that an unbounded float may be non-finite; the removals moved to their own changelog fragment.
    • Declined: drawing non-finite numbers from the public hypothesis strategies (a change to public strategies, better done on its own); a deprecated UserJSONValue alias (the package is pre-1.0); spelling a non-finite fill value by name in create_default (not a regression).
    • Kept, after a question to the author: the integer digit limit. Without it, ints past sys.get_int_max_str_digits crash f-string rule messages and json_text-based == and hash.
  • Round 2 (56561b5..c7f34fa, fix diff only): no serious findings, so the review stopped after two clean rounds. All fixed in a3aa5e7:
    • Moderate: the docs named only the fill value and complex components as refused, while the rule also covers a struct's float field and codec scalars spelled as a fill value (scale_offset's scale and offset, cast_value's fill value and scalar_map). The rule is now stated once, in non_finite_fill_value's docstring, and the other texts follow it.
    • Moderate: the definition docs implied a bounded float cannot be non-finite; a one-sided bound admits an infinity.
    • Moderate: the removal fragment named private functions (refine_user_data, refine_json); it now lists the public removals with their migrations.
    • Minor: stale wording in the attribute docstrings ("may also be"), the testing strategy, the repair docstring, and line length in the typed_json docstring.
    • Not acted on: the public hypothesis strategies never draw a non-finite number, so property tests do not exercise the policy (same as the round 1 decline).

The data type owns the spelling (c27d46c), after the author pointed out that a generic non_finite_name cannot be judged outside a specific data type:

  • Round 1 (two reviewers; one ran 5,264 repair cases against the parent with no core-type regression): 1 serious, fixed in d4d05fc. The repair found names only in a scalar or a list, so an object-shaped extension fill value was never repaired and struct had no hook. The repair now walks lists and objects and struct composes its fields. Also fixed there: the repair wrote changes it did not record (now it writes only the non-finite numbers), the hook's contract was wider than the repair (narrowed to non-finite numbers), the rule and hook could drift (one function per format), and stale docs (validation-rules, extensions, DataTypeDefinition). Declined: sharing the naming function between v2 and v3, since each spec spells the names itself.
  • Round 2 (c27d46c~1..d4d05fc): no serious findings. Moderate, fixed in d11b6ce: the repair ran an extension's hook on every listed array, so a raising hook broke the repaired read of a document the strict read accepts. Minor, fixed there too: docs/repair.md named the wrong default v3 scope and did not mention struct fields.
  • Round 3 (d11b6ce only): no serious findings, so the loop stopped after rounds 2 and 3. A 3,000-example fuzz found no repair the pre-walk dropped. Moderate, fixed anyway in 08d0cf7 because the fix is simpler than the code it replaces: a data type whose rules accept a bare NaN still had its hook run on a document the strict read accepts. The repair now runs the hook only when the data type's rules refuse the fill value, which makes "the repaired read never fails where the strict read accepts" hold by construction and removes the pre-walk (and its cost on large fill values).
  • Round 4 (08d0cf7 only, reviewing the change made after the loop stopped): no serious findings; 8,550 repair inputs gave output identical to the parent's for every core type. Fixed in e9f017a: the struct hook named a field whose own type accepts a bare NaN (it now skips such a field); a rules error raised during a repair did not say which listed entry it was reading. Corrected in the docstrings and the 08d0cf7 message: the repair compares with reading the listed documents (read_consolidated_metadata_v3), not with the group read, which never reads them; and the rules check costs about 0.3 ms more per listed array with a large fill value, not less. The review stopped here: rounds 2 to 4 found nothing serious, and each later round found narrower extension-only edges.

Process note: the late rounds kept finding the same kind of gap, a promise about the repair worded more broadly than the code. Stating each such invariant as a test before the change would have caught them in round 1.

Duplication audit of the type-checking path

After this PR, these routines still repeat work that the annotation checker does, or build the problems it builds:

  1. A declaration is walked several times. _refined refines the whole declaration. When it is not JSON, _not_json refines it again to explain why, and _unread refines the configuration once more. Then the checker walks the configuration. With one refiner and no purity gate this is now a cost, not a semantic risk: every pass uses refine_json. The structural fix is to refine once, at the document location, with the checker's result as the declaration's JSON. That also removes the v2 struct depth exception, which comes from refining in configuration space (definition/_definition.py:316-341; v3/_definition.py:592-620 repeats the trio for extension fields).
  2. The v2 readers are hand-checked. model/_array_v2.py:213-334 refines each member with its own refine_json call. model/_group_v2.py:99-103,740-741 uses missing_keys, unexpected_keys, check_literal and validate_attributes. These are the member checks this PR removed for v3. Porting them onto typed_document deletes most of model/_shared.py:40-300.
  3. The v2 envelope is hand-written (v2/_definition.py:221-233,316), while v3's is derived from str | ZarrV3NamedConfigJSON.
  4. Problem messages are built by hand outside the checker. "missing required key" and "unexpected key" appear in model/_shared.py:44,58, model/_group_v3.py:892, model/_group_v2.py:731 and v2/_definition.py:316. (The non-string key messages in model/_shared.py now use the shared non_string_key problem.)
  5. The node-type dispatch (model/_group_v3.py:875-897, _node_type) checks zarr_format and node_type by hand before typing. A union of the array and group TypedDicts would make it a checker call.
  6. The consolidated listing re-walks an entry for depth (model/_group_v3.py:1030, refine_json(document, at)), although the checker already counts depth from the root.

Process suggestion: a test that fails when a ValidationProblem with kind missing_key or unknown_key, or a non-string key message, is built outside _typed_json and _json, with an allowlist for semantic rules (struct fields, blosc typesize). New readers would then reach for the checker instead of re-deriving its messages.

Testing and follow-ups
  • Checks: the zarr-metadata suite passes on 3.11 and 3.14. pyright and ruff are clean.
  • Follow-ups:
    • Move the v2 readers onto typed_document. This also fixes the v2 struct depth exception above.
    • Run cross-declaration checks on a refused declaration by carrying its configuration on RefusedDeclaration.
    • The duplication audit below.

🤖 Generated with Claude Code

d-v-b and others added 27 commits December 4, 2026 00:17
…fore reading them in a scope

The v3 array and group readers now read a document in two steps. First
`typed_document` checks each member against its annotation in the node's
TypedDict, refining it once; then the semantic layer resolves each
declaration in the scope and checks members against each other (fill
value against data type, grid against shape, the codec pipeline,
dimension names against shape). The hand-written checks that restated the
annotations (`check_literal`, `dimension_lengths`, `attributes_of`, the
inline `dimension_names` checks, `members_past_the_levels`) are gone from
the v3 readers.

`attributes` and unclaimed extra fields are annotated `UserJSONValue`, so
the annotation decides where a non-finite number may be. Problems are now
located at the bad entry (`("shape", 1)` rather than `("shape",)`), and
reported in document order.

Assisted-by: ClaudeCode:claude-opus-5-5
…m its TypedDict

`envelope_problems` now parses a declaration as `str | ZarrV3NamedConfigJSON`
and adds only what the annotation cannot say: the extension-name syntax,
and `must_understand: false` where a reader must understand the
declaration. Configuration values are passed on unread, so the envelope
check still walks only the envelope.

Assisted-by: ClaudeCode:claude-opus-5-5
… keep claimed extension fields

The type layer from 5f94a6a refined a member whole before typing it,
so a member holding a declaration that was not JSON lost its other
problems, and a claimed extension field was refined by the type layer
instead of by the definition that claims it. Now:

- a member that holds a declaration is parsed as written, and each
  declaration is refined where it sits (`_FindingDeclaration`), so a
  declaration that is not JSON is refused by its kind without hiding
  problems elsewhere in the member;
- other members are refined once, by `refine_json`, or by
  `refine_user_data` when annotated `UserJSONValue`, then typed;
- a member the TypedDict does not declare is passed on as written to
  `read_extra_fields_v3`, which refines it by the claiming definition;
- only members that can hold a declaration are walked again to collect
  and swap out declarations, so large attributes are not copied.

Problem order is: the type layer (missing keys, non-string keys,
member types), then extension fields, then declarations, then the
cross-member checks (fill value, grid lengths, pipeline, dimension
names).

Removes the now unused `holds`, `_GROUP_METADATA_REQUIRED_KEYS_V3` and
the hand-written v3 member constants.

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

A member that holds a declaration is parsed as written, and only its
declarations, JSON values and user data are refined where they sit; the
member itself is made a tuple when it is another kind of array. Any
other leaf (a scalar, a nested array) would have read a value that was
never refined, so compiling such a member now raises `TypeError`. So
does a declaration of a kind that refines its declarations its own way,
which removes the unreachable `resolve` branch of `read_found`, and an
open TypedDict whose other keys may hold anything but user data, since
`typed_document` passes those on as written.

`_collect` and `_putting_back` now take the declaration type, so the
walk over a checker's `_NestedDeclaration`s and over `typed_document`'s
`FoundDeclaration`s is one walk.

Adds tests/model/test_typed_document.py: one test that every problem
in a document is reported (a non-JSON codec beside a bad one, NaN and
nesting past the levels in separate codecs, a claimed extension field),
which fails before 3ddee67, and one test for each compile-time error.

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

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

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…and type documents with it

The checker in `_typed_json` assumed refined JSON, so reading a v3 node
document needed a parallel member-by-member refine around it (the
`_MemberLeaf` probe compile, `_Refining`, `_FindingDeclaration`,
`FoundDeclaration`, `_Member`, `_MemberParsers`, and compile-time guards
for the leaves that machinery could not refine). That duplicated the
checker and `_NestedDeclaration`.

Each parser now refines what it reads: a scalar or `Literal` reads a
`StrEnum` or `IntEnum` member as its JSON, an array may be any sequence
and a mapping any mapping, a number must be finite except under
`UserJSONValue`, and a container must sit within `JSON_DEPTH` levels of
the root, which `loc` locates it below. A value that is not JSON is
reported where it sits; a container where a scalar is expected is
reported once as the wrong type, as v2 documents already were.

`typed_document` is now the document's TypedDict checker (`_checker`)
plus the existing `_NestedDeclaration` walk, restricted to the members
that can hold a declaration (`_declaring`) and typed cleanly. The
declaration leaf refines each declaration where it sits and carries the
declaration as written and every reason it is not JSON, which
`read_found` hands to `resolve_refined`. `typed_json.check` no longer
refines the whole value first, so it honors `UserJSONValue`.

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

Review of e2eccca found the configuration path half migrated to the
refining checker:

- A nested declaration that is not JSON (a NaN in a codec held by a
  `finite=False` configuration) was read as null and reported as "got
  null" at the declaration. `_envelope` now reports why it is not JSON,
  and the configuration's nested-declaration loop refuses it through
  `resolve_refined`, as a document's declarations are; `_moved` locates
  the declaration and its problems together.
- `typed_json.check` returned a value when a closed TypedDict's
  undeclared key held a value that is not JSON. `object_of` now refines
  that value as user data, so it is reported but may hold any number.
- A declaration that writes no configuration at the last level a reader
  walks was refused, because the empty configuration made up for it was
  depth-checked. It is no longer checked: it is not in the input.

A number that is not finite now stands only where the annotation is
`UserJSONValue`, also in a `finite=False` configuration, whose `float`
members accepted one before. The `finite` docstrings say so.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…th readers and refine undeclared values strictly

A declaration that writes no configuration has the empty one, which is
not in the input; `_read` and `_read_carried` now share `_given`, so
neither counts it as a level (a carried data type such as `r16` at the
last level was refused). A value under a key a TypedDict does not
declare is refined by `refine_json`: no annotation says it may hold a
number that is not finite, so `finite=False` no longer lets one through
there. The unused `_checked` is removed.

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

`read_consolidated_metadata_v3` reported a member the envelope does not
declare but did not refine its value, while the group reader, which
types the same envelope with the checker, does. A value that is not JSON,
or a number that is not finite, under such a member is now reported by
both. The package docstring says an unknown key's value must still be
JSON, and a test pins NaN under an undeclared key of a `finite=False`
extension field.

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

`read_consolidated_metadata_v3` checked the envelope by hand, so it
drifted from the group's extension field, which checks the same member
by `ZarrV3ConsolidatedMetadataEnvelopeJSON`: it counted depth under an
unexpected member one level short, ordered problems differently, and
reported `kind: Infinity` as a wrong type. It now runs that checker,
with the entries of `metadata` left out because each is read as a
document, so both readers report the same problems in the same order.

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

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

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…of what a v3 declaration may hold

`ExtraFieldDefinition.finite` is removed: a configuration says where a
number that is not finite may stand by typing that member `UserJSONValue`.

A v3 declaration is no longer refined strictly before it is read, a pass
that stopped at the first value that was not JSON and so hid every other
problem. Its kind refines it as user data only to build the JSON a
reading holds (`UNSET` when it is not JSON). Then its envelope check,
its definition's configuration check, or the kind's check of an unread
configuration (`_unread`) each decide by annotation, on the declaration
as written when it is not JSON. The same holds for nested declarations
and for `read_configuration`. The envelope check passes the configuration
on once it is an object, so its keys are checked once. An extension
field's configuration keeps keys that are not strings, so its checker
reports them. v2 kinds, whose envelopes are checked by hand, still refine
strictly first.

A refused declaration now holds what was written when it is JSON with
any number, and a NaN in a nested codec refuses that codec, not the one
that holds it.

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

The envelope check counted depth from the declaration, so a container
nested too deep under an envelope member it does not declare left the
declaration's JSON unset with only an unknown key to explain it, and
`read_configuration` put `None` where such a codec was. The envelope
check now counts from the root, and a declaration whose JSON is unset is
always reported with why it is not JSON (`_explained`, `_not_json`).

A declaration holding a nested one that is not JSON, or whose number is
not finite, is refused, so an accepted declaration always writes JSON.
Its rules still run when every nested declaration has a name, read as
written, so their problems are reported. A key that is not a string is
reported with one message everywhere.

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

A nested declaration that was not JSON was shown to the rules of the
declaration holding it as null, so a rule that reads a nested codec's name
failed. A nested declaration now counts as named only when it is JSON, as
the rules contract says, and the holder's rules do not run otherwise.

The two hand-written non-string key messages in model/_shared.py use the
shared `non_string_key` problem.

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

The test for a nested declaration that is not JSON had a NaN case that
passed whether or not the holder's rules ran. Split it: one test shows the
rules skip a nested declaration of no JSON type, and one shows they still
run, and report the codec beside it, when the nested declaration holds a
non-finite number.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mber to the JSON routines

A number that is not finite is now JSON wherever the package reads one, as
Python's `json` module reads and writes it. `refine_user_data`, the
`UserJSONValue` alias and the `_user_json` checker leaf are removed, so a
declaration, a configuration, attributes and an unclaimed member are all
refined the same way.

Where the spec gives a non-finite number a meaning, its own rule stays: a
float fill value, or a complex component, that is a non-finite number is
refused and must be written by its name. The rule now lives with the
float fill value rules in both formats (`non_finite_fill_value`), and the
`non_finite_fill_value` repair shares its naming (`non_finite_name`).

An integer with more digits than the interpreter converts to text is still
refused: without that guard, rule messages and `json_text`-based equality
would raise on it, and `json.loads` never builds one.

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

`_pure` refused a declaration whose nested declaration held a non-finite
number. A non-finite number is JSON now, and a nested declaration that is
not JSON already leaves the declaration holding it without JSON, so it is
refused through `json is UNSET`.

Also from review round 4 of the rules gate: pin that a nested declaration
whose configuration is not an object keeps its holder's rules from running,
and say in the gate's comment and `_held`'s docstring that the rules see
only nested declarations that are JSON.

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

Review round 1 of the non-finite change found stale text and test gaps:

- docs/validation-rules.md still said `check` rejects a non-finite
  number everywhere. It now says a number may be non-finite anywhere
  except as a float fill value, and that a fill value of a data type
  nothing reads is not interpreted. The changelog fragment says so too.
- The repair TypedDicts' docstrings no longer call a bare `NaN` "not
  JSON", or give `check` as the reason the fill value is not a member.
- The message no longer says "fill value", since scale_offset and
  cast_value spell their scalars the same way.
- The kept rule is pinned for float16, float64, both complex types and a
  struct float field, and an unread data type's fill value is pinned as
  free to hold any number.

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

Address NaN review round 1 (crash lens): the pydantic warning, the
writer and to_key_value docs no longer say non-finite numbers are kept
to attributes; the definition docs warn that an unbounded float may be
non-finite; the removals move to their own changelog fragment.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… refused as a non-finite number

Address NaN review round 2 (no serious findings):

- docs/validation-rules.md, the changelog and `non_finite_fill_value`'s
  docstring named only the fill value and complex components, while the
  rule also covers a struct's float field and codec scalars spelled as a
  fill value (`scale_offset`, `cast_value`). The docstring now states the
  rule once; the other texts follow it.
- The definition docs said an unbounded `float` may be non-finite, which
  implied a bounded one is safe; a one-sided bound admits an infinity.
- The removal fragment named the private `refine_user_data` and
  `refine_json`; it now gives the public removals and their migrations.
- Minor wording in the attribute docstrings, the testing strategy, the
  repair docstring and the `typed_json` module docstring.

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

`non_finite_name` and `non_finite_fill_value` decided, in the layer that
knows no data type, how a non-finite number is written. The repair then
chose which arrays to apply that to from a list of core type names and a
prefix test on the name.

Each float type now owns its names: the v3 float types and the v2 float
and complex dtypes refuse a non-finite number with their own spec's
spelling, and declare it with the new `fill_value_named` hook on
`WithFillValue` (complex composes its component's). `named_fill_value`
calls the hook for a data type a scope read. The `non_finite_fill_value`
repair resolves the array's data type in the scope it is given, a new
`context` argument on the repair functions that the repaired readers
pass on, and writes what the type names. An extension data type that
declares the hook is repaired too; one nothing in the scope reads is
left for the strict read.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… names it, and nothing else

Review round 1 of c27d46c:

- The repair found names only in a scalar or a list, so an extension
  data type whose fill value is an object was never repaired, and
  `struct` declared no `fill_value_named`. The repair now walks lists and
  objects, and `struct` writes each field as that field's type does.
- The repair wrote the hook's whole result while recording only some of
  its changes. It now writes only the non-finite numbers the hook gives
  a string for, and its message names the bare token it replaced.
- The hook's contract is narrowed to non-finite numbers, which is all
  the repair writes.
- Within each format, the float rule is built from the same function as
  the hook, so the two cannot drift apart.
- docs/validation-rules.md no longer lists the float fill value rule
  among the rules that need no context; the `DataTypeDefinition`
  docstring and docs/extensions.md list the hook; the repair docstrings
  say they run the data type's code.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…value holding a non-finite number

Review round 2: the repair ran an extension's `fill_value_named` on every
listed array, so a hook that raised made the repaired reader fail on a
document the strict reader accepts, which never runs the hook. The repair
now first walks the fill value, with its own stack and to the reader's
depth, and asks the data type only when it finds a non-finite number.

docs/repair.md gives each format's default scope and says the v3 repair
also writes a struct's float fields.

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

Review round 3: the pre-walk skipped the hook only for a fill value with
no non-finite number, so a data type whose rules accept a bare `NaN`
still had its `fill_value_named` run on a document whose read accepts
it. The repair now checks the fill value against the data type's rules
first, as reading the document does, and runs the hook only when they
report a problem. The pre-walk is gone; the rules check costs more than
it did on a large fill value (about 0.3 ms per listed array with a
1,024-element fill value), and nothing on a scalar one.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s it, and locate rule errors in a repair

Review round 4 (no serious findings):

- The struct hook named every non-finite field once any field was
  refused, including a field whose own type accepts a bare `NaN`. It now
  skips a field its type accepts.
- An error raised by a data type's rules during a repair now names the
  listed entry it was reading.
- The repair docstrings compared the repair with the group's strict
  read, which never reads the documents its consolidated metadata lists.
  They now compare it with reading those documents, which runs the same
  data type code.

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant