Skip to content

TYP: Fix overlapping overloads issue in "2 in, 1 out" ufuncs - #28168

Merged
charris merged 1 commit into
numpy:mainfrom
jorenham:fix-28162
Jan 16, 2025
Merged

charris merged 1 commit into
numpy:mainfrom
jorenham:fix-28162

Conversation

@jorenham

Copy link
Copy Markdown
Member

Closes #28162


before:

import numpy as np
import numpy.typing as npt

a: npt.NDArray[np.float64] | np.float64
b: npt.NDArray[np.float64] | np.float64
out = np.logical_or(a, b)
reveal_type(out)  # npt.NDArray[Any]  <-- no scalar

after:

import numpy as np
import numpy.typing as npt

a: npt.NDArray[np.float64] | np.float64
b: npt.NDArray[np.float64] | np.float64
out = np.logical_or(a, b)
reveal_type(out)  # npt.NDArray[Any] | Any

The same fix has also been applied to .outer

@jorenham

Copy link
Copy Markdown
Member Author

It's a bug that has gone by unnoticed for 4 years, so I'm not sure whether this should be a backport candidate or not. What do you think @charris?

@github-actions

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/frame.py:11608: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]]", variable has type "ndarray[tuple[int], dtype[Any]]")  [assignment]

@jorenham

Copy link
Copy Markdown
Member Author

@hauntsaninja maybe it's old news, but mypy primer seems to work 🎉

@charris

charris commented Jan 16, 2025

Copy link
Copy Markdown
Member

I'm not sure whether this should be a backport

We will be supporting 2.2.x for another 5-6 months and I would like to make the typing as good as we can. I think there is minimal risk in doing so.

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Jan 16, 2025
@charris
charris merged commit 865dcc1 into numpy:main Jan 16, 2025
@charris

charris commented Jan 16, 2025

Copy link
Copy Markdown
Member

Thanks Joren.

@jorenham
jorenham deleted the fix-28162 branch January 16, 2025 22:38
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 17, 2025
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.

TYP: Return type for logical_or too narrow

2 participants