-
-
Notifications
You must be signed in to change notification settings - Fork 11k
TYP: Inconsistent behavior when assigning arrays of a scalar type to arrays using the scalar's alias #29151
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
Comments
From a cursory look, it seems the problem also exists in numpy 2.2. |
The difference between import numpy as np
import numpy.typing as npt
def pos_naive(x: np.float64) -> np.float64:
return +x
def pos_workaround(x: np.double) -> np.float64:
return +x # probably needs a cast or type: ignore
def pos_evil_dont_use_this_pls[T: npt.NBitBase](x: floating[T]) -> floating[T]:
return +x
y = pos_evil_dont_use_this_pls(np.float64(42))
reveal_type(y) # floating[_64Bit]
# ^-- yikes
pos_naive(y) # STOP! You've violated the law! Pay the court a fine or serve your sentence.
pos_workaround(y) # Everything's in order. So So in a perfect word, But to be honest, I'm a bit worried that the cure is worse than the disease, since its leaflet probably isn't obvious to someone that isn't a full-time stub developer. So what do you think we should do here? |
Personally, just from my very limited and perhaps naive perspective, I would prefer eventual consistency across all these aliases, i.e. having
I take it you mean that this kind of workaround is not officially encouraged. So I don't see much reason for a wider However, I also understand that this could be considered quite an impactful change, and that it will probably break some people's existing code (https://xkcd.com/1172/). My opinion here is very much informed by preferentially using very explicit scalar types and doing so on small projects that are easily adapted to changes. I recognize not everyone has that luxury. All that is to say, I would prefer one of two options (or both, one after the other):
|
I'll create a quick PR for this, so we can see what mypy_primer has to see about it. |
Oh and for what it's worth, in numtype they're already bidirectionally equivalent mutual aliases of each other in both ways (and vice-versa). |
Describe the issue:
There exists an inconsistency in numpy 2.3 when assigning an array typed to have a scalar type such as
np.double
to an array variable typed to have its matching alias such asnp.float64
as dtype: For all types exceptnp.float64
andnp.complex128
, the assignment passes type checking with mypy, but fails for the other two. (The example below is limited to the floating point numbers, but similar things happen for complex floating numbers.)This also happens when replacing the scalar type, e.g. when replacing
np.double
withnp.floating[np._typing._nbit_base._64Bit]
, again with only assignment tofloat64
failing and all others passing.From reading some of the recent issues, it was my impression that the assignment of
np.floating
to any more concrete dtype should be forbidden, but this seems to only be the case for the aforementioned two types. In any case, I would expect consistent behavior across all three examples. Am I missing a crucial detail here? Or is this an artifact of the changes tonp.float64
andnp.complex128
introduced in recent versions?Reproduce the code example:
Error message:
Python and NumPy Versions:
2.3.0
3.12.11 | packaged by conda-forge | (main, Jun 4 2025, 14:29:09) [MSC v.1943 64 bit (AMD64)]
Type-checker version and settings:
mypy 1.16.0
Command used:
mypy --strict shape2.py
Additional typing packages.
mypy_extensions 1.1.0, typing_extensions 4.14.0, typing-inspection 0.4.1, annotated_types 0.7.0
No stub packages.
The text was updated successfully, but these errors were encountered: