Skip to content

fix(api): drop unreachable scalar branch in save_array - #412

Open
d-v-b wants to merge 1 commit into
mainfrom
fix/save-array-dead-scalar-branch
Open

d-v-b wants to merge 1 commit into
mainfrom
fix/save-array-dead-scalar-branch

Conversation

@d-v-b

@d-v-b d-v-b commented Oct 21, 2026

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

save_array rejects anything that is not NDArrayLike before it reaches np.isscalar(arr), and numpy scalars never pass that protocol check because they lack __len__ and __setitem__, so the np.array(arr) fallback could never run. This PR deletes the dead branch and adds a test pinning the TypeError path that actually handles scalars.

Why now

numpy 2.5.4 changed the np.isscalar stub from TypeGuard[...] to TypeIs[...]. A TypeIs lets mypy intersect the already-narrowed NDArrayLike with the scalar union, discover the intersection is empty, and report Statement is unreachable. That is the lint failure on the upstream dependabot bump zarr-developers/zarr-python#4549, where mypy fails only because the lock moves numpy from 2.5.2 to 2.5.4. The agent bisected it: mypy 2.4.0 with numpy 2.5.2 passes, mypy 2.3.1 with numpy 2.5.4 fails.

Verification

  • uv run --frozen mypy passes on all 203 files under both numpy 2.5.2 (this branch's lock) and 2.5.4 (the dependabot lock).
  • The 78 save-related tests in tests/test_api.py pass, plus the new test_save_array_rejects_scalar.
  • All pre-commit hooks passed at commit time.

No changelog fragment: internal cleanup with no user-visible behavior change.

Stack bookkeeping

Independent of other PRs. Once this lands on upstream main, the dependabot PR 4549 needs a rebase for its lint job to go green.

🤖 Generated with Claude Code

`save_array` rejects anything that is not `NDArrayLike` before it reaches
`np.isscalar(arr)`. numpy scalars lack `__len__` and `__setitem__`, so they
never pass the protocol check, and the `np.array(arr)` fallback was dead.

numpy 2.5.4 changed the `np.isscalar` stub from `TypeGuard` to `TypeIs`,
which lets mypy intersect the narrowed `NDArrayLike` with the scalar union
and report the branch as unreachable. Delete it and pin the error path.

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant