Skip to content

TYP: Optional numpy.number type parameters #27736

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 2 commits into from
Nov 14, 2024

Conversation

jorenham
Copy link
Member

The type parameters of np.number and its abstract subtypes now default to Any.
This means that typecheckers will now infer np.floating as np.floating[Any], instead of rejecting it (when configured in strict mode).

This affects


The reason for choosing Any as the default, instead of setting it to the upper bound (numpy.typing.NBitBase), is because these type parameters (TypeVars) are invariant.
If they weren't invariant, then x: int16 = int8(3) would be a valid statement, which we probably don't want. So the invariance means that x: number[NBitBase] = int8(3) is rejected, but x: number[Any] = int8(3) is accepted.

I usually tend to avoid using Any, but because those _NBit type-parameters aren't actually annotating anything, the Any won't affect anything "outside" of the number types, so it's no problem to use it here.

@jorenham jorenham added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Nov 11, 2024
@jorenham jorenham removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Nov 12, 2024
@charris charris merged commit 8c46459 into numpy:main Nov 14, 2024
69 checks passed
@charris
Copy link
Member

charris commented Nov 14, 2024

Thanks Joren.

@jorenham jorenham deleted the typing/scalar-type-parameter-defaults branch November 14, 2024 17:59
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