-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Description
Describe the issue:
I have a function that compares an array whose type is revealed as ndarray[tuple[int, ...], dtype[signedinteger[_32Bit | _64Bit]]] to a python int and returns the result, which is expected to be an array of bools (return type hint is npt.NDArray[np.bool]), but mypy and pyright say the return type is Any.
For instance indexer != -1 is revealed as Any by mypy and pyright. Casting using .astype doesn't fix this. Using np.not_equal gets pyright to recognize that the result is an array, and then casting with .astype gives the correct type with pyright.
The example below shows the inferred types for the comparisons with and without casts and using != vs. np.not_equal.
Sorry if this is a duplicate!
Reproduce the code example:
import numpy as np
arr = np.arange(10)
not1 = arr != 1
reveal_type(not1) # revealed type is Any for mypy and pyright
reveal_type(not1.astype(dtype=np.bool)) # revealed type is Any for mypy and pyright
not1 = np.not_equal(arr, 1)
reveal_type(not1) # revealed type is ndarray[tuple[int, ...], dtype[Any]] for pyright, Any for mypy
reveal_type(not1.astype(dtype=np.bool)) # revealed type is ndarray[tuple[int, ...], dtype[numpy.bool[builtins.bool]]] for pyright, Any for mypyError message:
mypy error:
error: Returning Any from function declared to return "ndarray[tuple[int, ...], dtype[numpy.bool[builtins.bool]]]" [no-any-return]Python and NumPy Versions:
python 3.10.14
numpy 2.2.4
Type-checker version and settings:
mypy 1.16.1 (with no-any-return)
pyright 1.1.403
Additional typing packages.
No response