You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[...]/issue_27965.pyi:3: note: Revealed type is "numpy.ndarray[tuple[builtins.int], numpy.dtype[numpy.float64]]"
[...]/issue_27965.pyi:4: note: Revealed type is "numpy.ndarray[builtins.tuple[builtins.int, ...], numpy.dtype[numpy.floating[Any]]]"
relevant pyright output:
[...]/issue_27965.pyi:3:13 - information: Type of "x := np.zeros(1)" is "ndarray[tuple[int], dtype[float64]]"
[...]/issue_27965.pyi:4:13 - information: Type of "x / 1" is "ndarray[tuple[int, ...], dtype[floating[Any]]]"
The np.zeros(1) is inferred by both mypy and pyright as a ndarray with 1-d shape and a dtype of float64. This is correct in the most accurate way that's (reasonably) possible.
But np.zeros(1) / 1 isn't as precisely typed, and is inferred as ndarray with ?-d shape and a dtype of floating.
Now the problem emerges when we do x = x / 1, which means we're assigning ndarray[tuple[int, ...], floating] to ndarray[(int), float64]. But this is invalid, because
tuple[int, ...] isn't assignable to tuple[int]
floating isn't assignable to float64 ()
This is because we can't assign something less specific to something more specific, just like you can't assign int to a bool.
But if we flip the LHS and RHS, then it actually is allowed. So in the same way that bool is assignable to int, tuple[int] is assignable to tuple[int, ...] and float64 is assignable to floating.
We can use this to create a very clean workaround to this issue:
Note that this workaround is only required for mypy users: Pyright infers the type of x as ndarray[tuple[int, ...], dtype[floating]], because it looks at more than one line of code at a time, and isn't "greedy" (algorithmically speaking) like mypy is.
Describe the issue:
Arithmetic operators like division do not take shape typing into account, resulting in false-positive type hinting issues.
Originally reported here: #27957 (comment)
Some debugging work done by @jorenham at here: #27957 (comment)
Opening a separate bug report as requested
Reproduce the code example:
Error message:
Python and NumPy Versions:
2.2.0
3.13.0 (main, Nov 21 2024, 10:46:46) [Clang 16.0.0 (clang-1600.0.26.4)]
Type-checker version and settings:
mypy 1.11.2 (compiled: no)
mypy --strict
Additional typing packages.
No response
The text was updated successfully, but these errors were encountered: