-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: Sum of ndarray subclassess incorrectly typed as np.ndarray #20072
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
It's unfortunate, but without higher-kinded typevars (python/typing#548) I see no way of fixing this issue. The problem is that, when working with typevars, we've no way of decoupling the type from its parameters
Interesting, the appearance of the boolean dtype most definitely looks like a mypy bug. |
Thanks @BvB936! So should I open an issue on the mypy tracker for the bool problem? |
Yes, though I'd recommend narrowing it down to a minimal reproducible example first. |
I think it is the same as #20099. |
@vnmabus it seems to be the case, yes. After some trial and error I've managed to narrow the issue down (see below for a minimal example).
from typing import Any, Generic, overload, TypeVar, TYPE_CHECKING
import numpy as np
_CharType = TypeVar("_CharType", np.str_, np.bytes_)
class CharArray(Generic[_CharType]):
@overload
def strip(self: CharArray[np.str_], chars: str = ...) -> CharArray[np.str_]: ...
@overload
def strip(self: CharArray[np.bytes_], chars: bytes = ...) -> CharArray[np.bytes_]: ...
ar: CharArray[Any]
ar_U: CharArray[np.str_]
ar_S: CharArray[np.bytes_]
if TYPE_CHECKING:
reveal_type(ar.strip()) # E: Revealed type is "__main__.CharArray[numpy.str_]"
reveal_type(ar_U.strip()) # E: Revealed type is "__main__.CharArray[numpy.str_]"
reveal_type(ar_S.strip()) # E: Revealed type is "__main__.CharArray[numpy.bytes_]" |
FYI, I just opened an issue on the mypy tracker (python/mypy#11347). |
This is impossible without higher kinded typing, which isn't supported in Python: |
Describe the issue:
In my code I'm using a slightly customized numpy subclass. When I sum two numpy arrays viewed as this class, the result has a different type. I'd expect the sum to be of the same type as the two addends.
Reproduce the code example:
Error message:
NumPy/Python version information:
Python 3.9.5
Numpy: 1.21.2
Mypy: 0.910
The text was updated successfully, but these errors were encountered: