Skip to content

Commit 583e431

Browse files
committed
TYP: Simplified & transparent unary arithmetic ndarray operators
1 parent 70fde29 commit 583e431

File tree

1 file changed

+13
-35
lines changed

1 file changed

+13
-35
lines changed

numpy/__init__.pyi

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,11 +1760,15 @@ _DType = TypeVar("_DType", bound=dtype[Any])
17601760
_DType_co = TypeVar("_DType_co", covariant=True, bound=dtype[Any])
17611761
_FlexDType = TypeVar("_FlexDType", bound=dtype[flexible])
17621762

1763+
_IntegralArrayT = TypeVar("_IntegralArrayT", bound=NDArray[integer[Any] | np.bool | object_])
1764+
_RealArrayT = TypeVar("_RealArrayT", bound=NDArray[floating[Any] | integer[Any] | timedelta64 | np.bool | object_])
1765+
_NumericArrayT = TypeVar("_NumericArrayT", bound=NDArray[number[Any] | timedelta64 | object_])
1766+
17631767
_Shape1D: TypeAlias = tuple[int]
17641768
_Shape2D: TypeAlias = tuple[int, int]
17651769

1770+
_ShapeType = TypeVar("_ShapeType", bound=_Shape)
17661771
_ShapeType_co = TypeVar("_ShapeType_co", covariant=True, bound=_Shape)
1767-
_ShapeType2 = TypeVar("_ShapeType2", bound=_Shape)
17681772
_Shape2DType_co = TypeVar("_Shape2DType_co", covariant=True, bound=_Shape2D)
17691773
_NumberType = TypeVar("_NumberType", bound=number[Any])
17701774

@@ -1881,11 +1885,11 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
18811885

18821886
def __array_wrap__(
18831887
self,
1884-
array: ndarray[_ShapeType2, _DType],
1888+
array: ndarray[_ShapeType, _DType],
18851889
context: None | tuple[ufunc, tuple[Any, ...], int] = ...,
18861890
return_scalar: builtins.bool = ...,
18871891
/,
1888-
) -> ndarray[_ShapeType2, _DType]: ...
1892+
) -> ndarray[_ShapeType, _DType]: ...
18891893

18901894
@overload
18911895
def __getitem__(self, key: (
@@ -2311,40 +2315,14 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
23112315

23122316
# Unary ops
23132317
@overload
2314-
def __abs__(self: NDArray[_UnknownType]) -> NDArray[Any]: ...
2315-
@overload
2316-
def __abs__(self: NDArray[np.bool]) -> NDArray[np.bool]: ...
2317-
@overload
2318-
def __abs__(self: NDArray[complexfloating[_NBit1, _NBit1]]) -> NDArray[floating[_NBit1]]: ...
2319-
@overload
2320-
def __abs__(self: NDArray[_NumberType]) -> NDArray[_NumberType]: ...
2321-
@overload
2322-
def __abs__(self: NDArray[timedelta64]) -> NDArray[timedelta64]: ...
2323-
@overload
2324-
def __abs__(self: NDArray[object_]) -> Any: ...
2325-
2326-
@overload
2327-
def __invert__(self: NDArray[_UnknownType]) -> NDArray[Any]: ...
2328-
@overload
2329-
def __invert__(self: NDArray[np.bool]) -> NDArray[np.bool]: ...
2330-
@overload
2331-
def __invert__(self: NDArray[_IntType]) -> NDArray[_IntType]: ...
2332-
@overload
2333-
def __invert__(self: NDArray[object_]) -> Any: ...
2334-
2335-
@overload
2336-
def __pos__(self: NDArray[_NumberType]) -> NDArray[_NumberType]: ...
2337-
@overload
2338-
def __pos__(self: NDArray[timedelta64]) -> NDArray[timedelta64]: ...
2339-
@overload
2340-
def __pos__(self: NDArray[object_]) -> Any: ...
2341-
2342-
@overload
2343-
def __neg__(self: NDArray[_NumberType]) -> NDArray[_NumberType]: ...
2318+
def __abs__(self: _RealArrayT, /) -> _RealArrayT: ...
23442319
@overload
2345-
def __neg__(self: NDArray[timedelta64]) -> NDArray[timedelta64]: ...
2320+
def __abs__(self: ndarray[_ShapeType, dtype[complex128]], /) -> ndarray[_ShapeType, dtype[float64]]: ...
23462321
@overload
2347-
def __neg__(self: NDArray[object_]) -> Any: ...
2322+
def __abs__(self: ndarray[_ShapeType, dtype[complexfloating[_NBit1]]], /) -> ndarray[_ShapeType, dtype[floating[_NBit1]]]: ...
2323+
def __invert__(self: _IntegralArrayT, /) -> _IntegralArrayT: ... # noqa: PYI019
2324+
def __neg__(self: _NumericArrayT, /) -> _NumericArrayT: ... # noqa: PYI019
2325+
def __pos__(self: _NumericArrayT, /) -> _NumericArrayT: ... # noqa: PYI019
23482326

23492327
# Binary ops
23502328
@overload

0 commit comments

Comments
 (0)