Skip to content

TYP: nonzero and single-arg where shape-typing#31143

Merged
charris merged 5 commits into
numpy:mainfrom
jorenham:typing/nonzero
Apr 4, 2026
Merged

TYP: nonzero and single-arg where shape-typing#31143
charris merged 5 commits into
numpy:mainfrom
jorenham:typing/nonzero

Conversation

@jorenham

@jorenham jorenham commented Apr 4, 2026

Copy link
Copy Markdown
Member

nonzero, ndarray.nonzero, and where (single arg) return a tuple of 1d intp arrays, where the number of tuple elements is the number of input dimensions. This adds shape-aware overloads to these functions for the first three ndim cases.

AI Disclosure

I am not a robot ☑️

@jorenham jorenham added this to the 2.5.0 Release milestone Apr 4, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@jorenham

jorenham commented Apr 4, 2026

Copy link
Copy Markdown
Member Author

The new mypy primer error for pandas is correct; np.where raises if you pass it a bare integer, so passing int | ndarray[tuple[Any, ...], dtype[Any]] is unsafe, and should indeed be reported as an error.

@charris

charris commented Apr 4, 2026

Copy link
Copy Markdown
Member

Needs rebase.

@github-actions

github-actions Bot commented Apr 4, 2026

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/indexes/interval.py:982: error: Argument 1 to "where" has incompatible type "int | ndarray[tuple[Any, ...], dtype[Any]]"; expected "ndarray[tuple[Never, Never, Never, Never], dtype[Any]]"  [arg-type]

@charris charris merged commit f053104 into numpy:main Apr 4, 2026
14 checks passed
@charris

charris commented Apr 4, 2026

Copy link
Copy Markdown
Member

Thanks Joren.

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.

2 participants