-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
TYP: Improved ndarray
augmented assignment operators
#29862
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
base: main
Are you sure you want to change the base?
Conversation
@MarcoGorelli do you think we could also apply this trick to get rid of the other arithmetic dunder overrides in |
@overload | ||
def __iadd__(self: NDArray[integer], other: _ArrayLikeInt_co, /) -> ndarray[_ShapeT_co, _DTypeT_co]: ... | ||
def __iadd__(self: _ComplexFloatingArrayT, other: _ArrayLikeComplex_co, /) -> _ComplexFloatingArrayT: ... | ||
@overload | ||
def __iadd__(self: NDArray[floating], other: _ArrayLikeFloat_co, /) -> ndarray[_ShapeT_co, _DTypeT_co]: ... | ||
def __iadd__(self: _InexactArrayT, other: _ArrayLikeFloat_co, /) -> _InexactArrayT: ... | ||
@overload | ||
def __iadd__(self: NDArray[complexfloating], other: _ArrayLikeComplex_co, /) -> ndarray[_ShapeT_co, _DTypeT_co]: ... | ||
def __iadd__(self: _NumberArrayT, other: _ArrayLikeInt_co, /) -> _NumberArrayT: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to flip the overload order here to appease mypy. It started complaining about non-existent overlap once I put the _*ArrayT
typars in, demonstrating that the error is a false-positive.
Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code: colour (https://github.com/colour-science/colour)
- colour/colorimetry/tristimulus_values.py:458: error: Argument 1 to "__imul__" of "ndarray" has incompatible type "object"; expected "_SupportsArray[dtype[numpy.bool[builtins.bool] | integer[Any] | floating[Any]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool] | integer[Any] | floating[Any]]]] | float | _NestedSequence[float]" [arg-type]
+ colour/colorimetry/tristimulus_values.py:458: error: No overload variant of "__imul__" of "ndarray" matches argument type "object" [call-overload]
+ colour/colorimetry/tristimulus_values.py:458: note: Possible overload variants:
+ colour/colorimetry/tristimulus_values.py:458: note: def __imul__(self, _SupportsArray[dtype[numpy.bool[builtins.bool] | integer[Any] | floating[Any]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool] | integer[Any] | floating[Any]]]] | float | _NestedSequence[float], /) -> ndarray[tuple[Any, ...], dtype[floating[Any]]]
+ colour/colorimetry/tristimulus_values.py:458: note: def __imul__(self, _SupportsArray[dtype[numpy.bool[builtins.bool] | integer[Any]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool] | integer[Any]]]] | int | _NestedSequence[int], /) -> ndarray[tuple[Any, ...], dtype[floating[Any]]]
|
@MarcoGorelli Ping. |
By using type-restricted method-bound type parameters for
self
in thendarray.__i{op}__
"augmented assignment" operator methods, they'll now covariantly specialize, and side-step the need for higher-kinded types (sorry about the jargon). I.e., subtypes ofndarray
no longer need to override the__i{op}__
methods.This allows us to get rid of a bunch of duplication in e.g.
np.ma.MaskedArray
, and users with their ownndaray
subclasses are now also able to do the same.This also addresses the issue described in python/mypy#19171 more rigourously than #29092, since the
self
-bound type-parameters are now slightly wider. For example thendarray.__add__
overload that accepts "integer-like array-likes" (_ArrayLikeInt_co
) now applies to allnp.number
arrays, instead of just thenp.integer
arrays. For floating-like array-likes thenp.floating
restriction is now similarly widened tonp.inexact
. I believe this resolves the remaining class ofissues described in python/mypy#19171 (comment) by @david-zwicker.