Skip to content

fix(zarr-metadata): make JSONValue's array arm covariant - #4264

Merged
d-v-b merged 3 commits into
zarr-developers:mainfrom
d-v-b:jsonvalue-sequence-arm
Aug 14, 2026
Merged

fix(zarr-metadata): make JSONValue's array arm covariant#4264
d-v-b merged 3 commits into
zarr-developers:mainfrom
d-v-b:jsonvalue-sequence-arm

Conversation

@d-v-b

@d-v-b d-v-b commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Summary

list[T] | tuple[T] is invariant but Sequence[T] is covariant. In the context of our JSONValue type, we want covariance, to support intuitive propagation of the "isintance" operator when narrowing JSONValue. That means we need to change our JSONValue union. This is technically a breaking change and so the next release of zarr-metadata will indicate this. Why is it breaking? because strings inhabit Sequence[str], but not list[JSONValue] | tuple[JSONValue]. Routines handling JSONValue-shaped inputs will need to guard for strings where previously they did not. Low blast area but a teeny blast nonetheless.

cc @kylebarron i doubt this affects zarrista but I will do a check in any case.

Written by claude, with an original PR here: d-v-b#295

Author attestation

  • I am a human, these are my changes, and I have reviewed and understood every change and can explain why each is correct.

TODO

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/user-guide/*.md
  • Changes documented as a new file in changes/
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

`list["JSONValue"] | tuple["JSONValue", ...]` is invariant in the element
type, so a value typed with any narrower element -- a `list[str]` field on
a TypedDict, a `Sequence[float]` -- was not assignable to `JSONValue`, and
a TypedDict carrying such fields was not assignable to
`Mapping[str, JSONValue]`. pyright's diagnostic for the failure suggests
the fix verbatim: "Consider switching from list to Sequence which is
covariant."

The array arm is now `Sequence["JSONValue"]`. The docstring records the
deliberate type-level cost (`Sequence` admits `str`/`bytes`; runtime
narrowing must exclude them regardless of the alias's spelling).

Found while aliasing zarr-cm's JsonValue to this type: the two aliases
are structurally identical except for this arm, and with it changed,
pyright unifies them across the package boundary.

Assisted-by: ClaudeCode:claude-opus-5
@github-actions github-actions Bot added the needs release notes Automatically applied to PRs which haven't added release notes label Aug 14, 2026
@d-v-b

d-v-b commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

SMWG (self-merge when green)

@d-v-b
d-v-b marked this pull request as ready for review August 14, 2026 09:50
@d-v-b
d-v-b merged commit b1d4132 into zarr-developers:main Aug 14, 2026
38 checks passed
d-v-b added a commit to d-v-b/zarr-python that referenced this pull request Aug 14, 2026
Consume the pending news fragments — zarr-developers#4232's constant-naming-grammar removal
note, zarr-developers#4264's note widening `JSONValue`'s array arm to the covariant
`Sequence`, and zarr-developers#4248's sdist allowlist — into CHANGELOG.md via towncrier
for the zarr_metadata-v0.5.0 release.

Minor, not patch: the `JSONValue` widening changes a published type's
meaning for every consumer that annotates against it, and zarr-developers#4232 removes the
old version-last constant spellings outright.

Assisted-by: ClaudeCode:claude-opus-5
d-v-b added a commit that referenced this pull request Aug 14, 2026
* chore(zarr-metadata): note the sdist allowlist, and let misc entries speak

#4248 gave this package's sdist an explicit allowlist and merged without a
news fragment, so a user-visible packaging change was about to miss the
0.5.0 notes. Add one.

Filing it as `misc` exposed that towncrier's built-in `misc` type sets
`showcontent = false`: the entry would render as a bare PR link, which tells
a reader nothing. Restate all five types the `changes/README.md` menu offers
— declaring any type replaces the built-in set — as the defaults verbatim
except for `misc`, which now shows its content. A change worth a release
note is worth a sentence, whatever its category.

Assisted-by: ClaudeCode:claude-opus-5

* chore(zarr-metadata): build 0.5.0 changelog

Consume the pending news fragments — #4232's constant-naming-grammar removal
note, #4264's note widening `JSONValue`'s array arm to the covariant
`Sequence`, and #4248's sdist allowlist — into CHANGELOG.md via towncrier
for the zarr_metadata-v0.5.0 release.

Minor, not patch: the `JSONValue` widening changes a published type's
meaning for every consumer that annotates against it, and #4232 removes the
old version-last constant spellings outright.

Assisted-by: ClaudeCode:claude-opus-5
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant