fix(chunked): annotate ndarray with generic args; add py3.10 mypy gate#697
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Problem
boost-histogram's downstream
histCI job (example: scikit-hep/boost-histogram#1145) was failing on its Python 3.10 leg with:np.ndarrayis generic. NumPy ≥2.3 added PEP 696 defaults tondarray's type parameters, so its stubs silently accept a barenp.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.pyichanges in that PR are unrelated — they don't affect hist.Fix
src/hist/chunked.py: replace the nine barenp.ndarrayannotations withnp.typing.NDArray[Any](the convention already used inplot.py). Correct under every supported NumPy version, including thenumpy>=1.21.3floor. The runtimeisinstance(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 newnox -s mypysession pinned to Python 3.10, where NumPy permanently resolves to <2.3 (no defaults), mirroring the downstream environment..github/workflows/ci.yml: aType checkjob running it, added to thepassgate.Verified the new session catches the bug (9 errors with the fix reverted) and passes with it. Used normal resolution rather than
--resolution=lowest-directto avoid unrelated noise from boost-histogram 1.6.1 (noMultiCell, non-genericHistogram).prek -aand the chunked tests (61 passed) are green.