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
In version 2.2.0, I have been completely unable to type the return value of a call to ndarray.item() which does not fail type checking. I have tried both MyPy and Pyright.
Am I doing something wrong here? Or is it an issue with the type checker(s)? It seems they don't even see all the overloads:
/dev/np_typing.py
/dev/np_typing.py:4:33 - error: Type "double" cannot be assigned to type variable "_DType_co@ndarray"
Type "double" is not assignable to upper bound "dtype[Any]"fortype variable "_DType_co@ndarray""floating[_NBitDouble]" is not assignable to "dtype[Any]" (reportInvalidTypeArguments)
/dev/np_typing.py:5:14 - error: Cannot access attribute "item"for class "ndarray[tuple[int], double]"
Could not bind method "item" because "ndarray[tuple[int], double]" is not assignable to parameter "self""ndarray[tuple[int], double]" is not assignable to "ndarray[Any, StringDType]"
Type parameter "_DType_co@ndarray" is covariant, but "double" is not a subtype of "StringDType""floating[_NBitDouble]" is not assignable to "StringDType"
Could not bind method "item" because "ndarray[tuple[int], double]" is not assignable to parameter "self""ndarray[tuple[int], double]" is not assignable to "ndarray[Any, StringDType]"
Type parameter "_DType_co@ndarray" is covariant, but "double" is not a subtype of "StringDType""floating[_NBitDouble]" is not assignable to "StringDType" (reportAttributeAccessIssue)
/dev/np_typing.py:8:33 - error: Type "str_" cannot be assigned to type variable "_DType_co@ndarray"
Type "str_" is not assignable to upper bound "dtype[Any]"fortype variable "_DType_co@ndarray""str_" is not assignable to "dtype[Any]" (reportInvalidTypeArguments)
/dev/np_typing.py:9:14 - error: Cannot access attribute "item"for class "ndarray[tuple[int], str_]"
Could not bind method "item" because "ndarray[tuple[int], str_]" is not assignable to parameter "self""ndarray[tuple[int], str_]" is not assignable to "ndarray[Any, StringDType]"
Type parameter "_DType_co@ndarray" is covariant, but "str_" is not a subtype of "StringDType""str_" is not assignable to "StringDType"
Could not bind method "item" because "ndarray[tuple[int], str_]" is not assignable to parameter "self""ndarray[tuple[int], str_]" is not assignable to "ndarray[Any, StringDType]"
Type parameter "_DType_co@ndarray" is covariant, but "str_" is not a subtype of "StringDType""str_" is not assignable to "StringDType" (reportAttributeAccessIssue)
4 errors, 0 warnings, 0 informations
Oh and quick tip, np.float64 is almost always the same as np.double, but np.float64 has better annotations, for instance it actually has float as a supertype, whereas np.double doesn't.
Awesome thanks! I guess that I just tried to replace NDArray[np.str_] with ndarray[Shape, np.str_], and I didn't notice that NDArray also added the dtype for me. Thanks a lot for your help!
Describe the issue:
In version 2.2.0, I have been completely unable to type the return value of a call to
ndarray.item()
which does not fail type checking. I have tried both MyPy and Pyright.Am I doing something wrong here? Or is it an issue with the type checker(s)? It seems they don't even see all the overloads:
numpy/numpy/__init__.pyi
Lines 2111 to 2122 in a2012ad
Reproduce the code example:
Error message:
Python and NumPy Versions:
2.2.0
3.11.10 (main, Sep 7 2024, 01:03:31) [GCC 11.4.0]
Type-checker version and settings:
pyright 1.1.390 with default settings
Additional typing packages.
No response
The text was updated successfully, but these errors were encountered: