Skip to content

fix(zarr-metadata)!: guards narrow only when they say yes - #4429

Merged
d-v-b merged 1 commit into
zarr-developers:mainfrom
d-v-b:fix/zarr-metadata-guards-typeguard
Sep 26, 2026
Merged

d-v-b merged 1 commit into
zarr-developers:mainfrom
d-v-b:fix/zarr-metadata-guards-typeguard

Conversation

@d-v-b

@d-v-b d-v-b commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

🤖 AI text below 🤖

Breaking for type checkers. TypeIs[T] promises True exactly for the values that are T, and type checkers narrow T away when it returns False. Every is_* guard in zarr_metadata.model is stricter than its type: is_json(math.nan) is False for a float, and the document guards are False for well-typed documents that break a structural rule. So this type-checks under pyright strict, and fails at run time:

def payload(v: JSONValue | bytes) -> bytes:
    if is_json(v):
        return b""
    return v  # a float, when v is NaN

The guards are now TypeGuards, which narrow only on True. zarr-python itself does not import zarr_metadata yet, so only outside consumers are affected.

This is one of eight independent PRs from a review of the casts in zarr_metadata.model, each based on main and each passing just check alone, with a test that fails without its fix where there is behaviour to test:

#4425, #4426, #4428 and #4430 have merged, as have #4420, #4421 and #4422, and each of the four left is rebased onto them, so its tests are type-checked as #4421 requires. Of those four, only #4423 and #4429 touch the same lines, the TypeGuard import, so whichever of them merges second needs a one-line rebase. All eight together, merged with #4421's type-checked tests, pass just check.

🤖 Generated with Claude Code

@read-the-docs-community

read-the-docs-community Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 zarr-metadata | 🛠️ Build #34777863 | 📁 Comparing e48c105 against latest (d21a5b4)

  🔍 Preview build  

1 file changed
± api/model/index.html

@github-actions github-actions Bot added needs release notes Automatically applied to PRs which haven't added release notes zarr-metadata Specific to the zarr-metadata sub-package labels Sep 26, 2026
@d-v-b
d-v-b force-pushed the fix/zarr-metadata-guards-typeguard branch 2 times, most recently from b4dfa2d to e48c105 Compare September 26, 2026 18:41
@d-v-b
d-v-b marked this pull request as ready for review September 26, 2026 18:46
@codecov

codecov Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.37%. Comparing base (5a27243) to head (93b192d).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #4429       +/-   ##
===========================================
+ Coverage   52.54%   94.37%   +41.83%     
===========================================
  Files          93       93               
  Lines       13174    13174               
===========================================
+ Hits         6922    12433     +5511     
+ Misses       6252      741     -5511     

see 73 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

`TypeIs[T]` promises True exactly for the values that are `T`, and type
checkers narrow `T` away when it returns False. Every `is_*` guard is
stricter than its type: `is_json(math.nan)` is False for a `float`, and
the document guards are False for well-typed documents that break a
structural rule. A consumer narrowing on the False branch type-checked
and then failed at run time. The guards are now `TypeGuard`s, which
narrow only on True.

BREAKING CHANGE: code that relied on a False result narrowing the type
now sees the type it passed in.

Assisted-by: ClaudeCode:claude-opus-5-5
@d-v-b
d-v-b force-pushed the fix/zarr-metadata-guards-typeguard branch from e48c105 to 93b192d Compare September 26, 2026 18:55
@d-v-b
d-v-b merged commit 8422d2d into zarr-developers:main Sep 26, 2026
19 of 20 checks passed
@d-v-b
d-v-b deleted the fix/zarr-metadata-guards-typeguard branch September 27, 2026 12:06
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