Skip to content

TYP: Add shape typing to return values of np.nonzero and ndarray.nonzero, simplify MaskedArray.nonzero return type #29303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 1, 2025

Conversation

MarcoGorelli
Copy link
Member

follow-up from #28845 (review)

This comment has been minimized.

@MarcoGorelli
Copy link
Member Author

Looks like the mypy-primer difference is due to python/mypy#17282 (comment)

I think it's debatable what should be done here then, but I think at least ndarray.nonzero and MaskedArray.nonzero should be aligned

Given the (noisy?) "Variadic tuple unpacking requires a star target" error, I'm inclined to suggest that just tuple[_1d_array[intp], ...] should be fine in both cases, it's also simpler to read / maintain - @jorenham any thoughts on that?

@jorenham jorenham self-requested a review July 1, 2025 13:33
@jorenham
Copy link
Member

jorenham commented Jul 1, 2025

Given the (noisy?) "Variadic tuple unpacking requires a star target" error, I'm inclined to suggest that just tuple[_1d_array[intp], ...] should be fine in both cases, it's also simpler to read / maintain

It's pretty annoying that mypy doesn't support this. Given the primer diffs, and the fact that there are several other related mypy issues (python/mypy#19110, python/mypy#19109, python/mypy#19106, python/mypy#19105), I agree that we should fall back to using tuple[{}, ...] here. For future reference it could help to reference python/mypy#17282 (and perhaps this PR) there.

But either way, the shape-typing is already a good improvement, so this is still a win :)

@MarcoGorelli MarcoGorelli changed the title TYP: Ensure that return type of nonzero has at least one element TYP: Add shape typing to return values of np.nonzero and ndarray.nonzero, simplify MaskedArray.nonzero return type Jul 1, 2025
Copy link

github-actions bot commented Jul 1, 2025

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/internals/managers.py:1527: error: "BlockPlacement" has no attribute "increment_above"  [attr-defined]

@MarcoGorelli MarcoGorelli marked this pull request as ready for review July 1, 2025 16:55
@jorenham jorenham merged commit 3727d5b into numpy:main Jul 1, 2025
78 checks passed
@jorenham
Copy link
Member

jorenham commented Jul 1, 2025

Thanks @MarcoGorelli

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