Skip to content

fix(chunked): annotate ndarray with generic args; add py3.10 mypy gate#697

Merged
henryiii merged 1 commit into
mainfrom
fix/ndarray-type-args
Jun 11, 2026
Merged

fix(chunked): annotate ndarray with generic args; add py3.10 mypy gate#697
henryiii merged 1 commit into
mainfrom
fix/ndarray-type-args

Conversation

@henryiii

Copy link
Copy Markdown
Member

🤖 AI text below 🤖

Problem

boost-histogram's downstream hist CI job (example: scikit-hep/boost-histogram#1145) was failing on its Python 3.10 leg with:

src/hist/chunked.py:60: error: Missing type arguments for generic type "ndarray"  [type-arg]
... (9 total)

np.ndarray is generic. NumPy ≥2.3 added PEP 696 defaults to ndarray's type parameters, so its stubs silently accept a bare np.ndarray. NumPy ≤2.2 has no defaults, so strict mypy (disallow_any_generics) flags it.

The downstream job type-checks on Python 3.10, where the newest installable NumPy is 2.2.x — hence the failure. It passed on the 3.14 leg, and never showed up here, because hist's only mypy run is the pre-commit hook pinned to numpy~=2.4.0 (which has the defaults).

The accumulators.pyi changes in that PR are unrelated — they don't affect hist.

Fix

  • src/hist/chunked.py: replace the nine bare np.ndarray annotations with np.typing.NDArray[Any] (the convention already used in plot.py). Correct under every supported NumPy version, including the numpy>=1.21.3 floor. The runtime isinstance(value, np.ndarray) check is untouched.

Preventing regressions

There is no single NumPy pin that both lacks the defaults and ships Python 3.14 wheels, so the pre-commit hook can't catch this. Instead:

  • noxfile.py: a new nox -s mypy session pinned to Python 3.10, where NumPy permanently resolves to <2.3 (no defaults), mirroring the downstream environment.
  • .github/workflows/ci.yml: a Type check job running it, added to the pass gate.

Verified the new session catches the bug (9 errors with the fix reverted) and passes with it. Used normal resolution rather than --resolution=lowest-direct to avoid unrelated noise from boost-histogram 1.6.1 (no MultiCell, non-generic Histogram).

prek -a and the chunked tests (61 passed) are green.

Bare `np.ndarray` annotations in chunked.py are accepted by NumPy >=2.3
stubs (which added PEP 696 defaults to ndarray's type parameters) but
rejected as `[type-arg]` under strict mypy with NumPy <2.3. This broke
boost-histogram's downstream "hist" job, which type-checks on Python 3.10
where NumPy resolves to <2.3.

Replace the nine bare `np.ndarray` annotations with `np.typing.NDArray[Any]`
(the convention already used in plot.py), correct under every supported
NumPy version.

Add a Python-3.10-pinned `mypy` nox session and CI job so this class of
"works on new NumPy, breaks on old" type error is caught locally and in CI,
mirroring the downstream check.

Assisted-by: ClaudeCode:claude-opus-4.8
@henryiii henryiii merged commit b74cbd7 into main Jun 11, 2026
12 checks passed
@henryiii henryiii deleted the fix/ndarray-type-args branch June 11, 2026 16:53
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