Closed
Description
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:
Lines 2111 to 2122 in a2012ad
Reproduce the code example:
import numpy as np
def f(x: np.ndarray[tuple[int], np.double]) -> float:
return x.item()
def g(x: np.ndarray[tuple[int], np.str_]) -> str:
return x.item()
Error message:
/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]" for type 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]" for type 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
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