Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TYP: simplify redundant unions of builtin scalar types #28819

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 1 commit into from
Apr 25, 2025

Conversation

jorenham
Copy link
Member

This comment has been minimized.

@charris
Copy link
Member

charris commented Apr 24, 2025

Needs rebase. I almost prefer the originals, because now I have to remember the builtin type hierarchy :)

@jorenham
Copy link
Member Author

Needs rebase. I almost prefer the originals, because now I have to remember the builtin type hierarchy :)

Yea I agree that those "type promotions" are certainly annoying, and actually plain wrong (e.g. int doesn't subclass float at runtime). So it's not strange to want to be explicit about it, and write int | float instead of float. I've actually tried doing this for a while, but quickly realized that it wasn't a good idea.

One issue is that the typing spec explicitly says that we should float instead of float | int (https://typing.python.org/en/latest/reference/best_practices.html#types). The same goes for ruff's PYI041 rule, which is enabled by default.

But besides formalities, there are actual practical problems that it can lead to. The main issue is that both mypy and pright don't always treat them as equivalent, which can lead to incorrect inference. It also can negatively affect the runtime of type-checkers in a very bad way. The incorrect type inference can be seen in scipy/scipy-stubs#492, and in data-apis/array-api-compat#291 (comment) I also explain my thoughts on the performance stuff in a bit more detail.

@jorenham jorenham force-pushed the typing/simplify-redundant-unions branch from 93698af to fb55d0b Compare April 24, 2025 21:10
Copy link

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

vision (https://github.com/pytorch/vision)
- torchvision/utils.py:270: error: Unused "type: ignore" comment  [unused-ignore]

@jorenham
Copy link
Member Author

jorenham commented Apr 24, 2025

the circleci devdocs build failure seems unrelated

@charris charris merged commit da04224 into numpy:main Apr 25, 2025
73 of 75 checks passed
@charris
Copy link
Member

charris commented Apr 25, 2025

Thanks Joren.

@jorenham jorenham deleted the typing/simplify-redundant-unions branch April 25, 2025 16:27
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