E580 Merge pull request #17613 from BvB93/cleanup · numpy/numpy@b10ee24 · GitHub
[go: up one dir, main page]

Skip to content

Commit b10ee24

Browse files
authored
Merge pull request #17613 from BvB93/cleanup
ENH: Add annotations for 9 `ndarray`/`generic` magic methods
2 parents c5b2676 + 939985d commit b10ee24

File tree

10 files changed

+609
-43
lines changed

10 files changed

+609
-43
lines changed

numpy/__init__.pyi

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,21 @@ from numpy.typing._callable import (
2828
_BoolBitOp,
2929
_BoolSub,
3030
_BoolTrueDiv,
31+
_BoolMod,
32+
_BoolDivMod,
3133
_TD64Div,
3234
_IntTrueDiv,
3335
_UnsignedIntOp,
3436
_UnsignedIntBitOp,
37+
_UnsignedIntMod,
38+
_UnsignedIntDivMod,
3539
_SignedIntOp,
3640
_SignedIntBitOp,
41+
_SignedIntMod,
42+
_SignedIntDivMod,
3743
_FloatOp,
44+
_FloatMod,
45+
_FloatDivMod,
3846
_ComplexOp,
3947
_NumberOp,
4048
)
@@ -55,8 +63,6 @@ from typing import (
5563
overload,
5664
Sequence,
5765
Sized,
58-
SupportsAbs,
59-
SupportsBytes,
6066
SupportsComplex,
6167
SupportsFloat,
6268
SupportsInt,
@@ -978,9 +984,7 @@ _ArrayLikeIntOrBool = Union[
978984

979985
_ArraySelf = TypeVar("_ArraySelf", bound=_ArrayOrScalarCommon)
980986

981-
class _ArrayOrScalarCommon(
982-
SupportsInt, SupportsFloat, SupportsComplex, SupportsBytes, SupportsAbs[Any]
983-
):
987+
class _ArrayOrScalarCommon:
984988
@property
985989
def T(self: _ArraySelf) -> _ArraySelf: ...
986990
@property
@@ -1004,9 +1008,6 @@ class _ArrayOrScalarCommon(
10041008
@property
10051009
def strides(self) -> _Shape: ...
10061010
def __array__(self, __dtype: DtypeLike = ...) -> ndarray: ...
1007-
def __int__(self) -> int: ...
1008-
def __float__(self) -> float: ...
1009-
def __complex__(self) -> complex: ...
10101011
def __bool__(self) -> bool: ...
10111012
def __bytes__(self) -> bytes: ...
10121013
def __str__(self) -> str: ...
@@ -1019,13 +1020,6 @@ class _ArrayOrScalarCommon(
10191020
def __ne__(self, other): ...
10201021
def __gt__(self, other): ...
10211022
def __ge__(self, other): ...
1022-
def __mod__(self, other): ...
1023-
def __rmod__(self, other): ...
1024-
def __divmod__(self, other): ...
1025-
def __rdivmod__(self, other): ...
1026-
def __neg__(self: _ArraySelf) -> _ArraySelf: ...
1027-
def __pos__(self: _ArraySelf) -> _ArraySelf: ...
1028-
def __abs__(self: _ArraySelf) -> _ArraySelf: ...
10291023
def astype(
10301024
self: _ArraySelf,
10311025
dtype: DtypeLike,
@@ -1572,14 +1566,28 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
15721566
# Many of these special methods are irrelevant currently, since protocols
15731567
# aren't supported yet. That said, I'm adding them for completeness.
15741568
# https://docs.python.org/3/reference/datamodel.html
1569+
def __int__(self) -> int: ...
1570+
def __float__(self) -> float: ...
1571+
def __complex__(self) -> complex: ...
15751572
def __len__(self) -> int: ...
15761573
def __setitem__(self, key, value): ...
15771574
def __iter__(self) -> Any: ...
15781575
def __contains__(self, key) -> bool: ...
15791576
def __index__(self) -> int: ...
1580-
def __matmul__(self, other): ...
1581-
def __imatmul__(self, other): ...
1582-
def __rmatmul__(self, other): ...
1577+
def __matmul__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
1578+
# NOTE: `ndarray` does not implement `__imatmul__`
1579+
def __rmatmul__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
1580+
def __neg__(self: _ArraySelf) -> Union[_ArraySelf, generic]: ...
1581+
def __pos__(self: _ArraySelf) -> Union[_ArraySelf, generic]: ...
1582+
def __abs__(self: _ArraySelf) -> Union[_ArraySelf, generic]: ...
1583+
def __mod__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
1584+
def __rmod__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
1585+
def __divmod__(
1586+
self, other: ArrayLike
1587+
) -> Union[Tuple[ndarray, ndarray], Tuple[generic, generic]]: ...
1588+
def __rdivmod__(
1589+
self, other: ArrayLike
1590+
) -> Union[Tuple[ndarray, ndarray], Tuple[generic, generic]]: ...
15831591
def __add__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
15841592
def __radd__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
15851593
def __sub__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
@@ -1610,7 +1618,7 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
16101618
def __itruediv__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
16111619
def __ifloordiv__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
16121620
def __ipow__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
1613-
def __imod__(self, other): ...
1621+
def __imod__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
16141622
def __ilshift__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
16151623
def __irshift__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
16161624
def __iand__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
@@ -1639,6 +1647,12 @@ class number(generic, Generic[_NBit_co]): # type: ignore
16391647
def real(self: _ArraySelf) -> _ArraySelf: ...
16401648
@property
16411649
def imag(self: _ArraySelf) -> _ArraySelf: ...
1650+
def __int__(self) -> int: ...
1651+
def __float__(self) -> float: ...
1652+
def __complex__(self) -> complex: ...
1653+
def __neg__(self: _ArraySelf) -> _ArraySelf: ...
1654+
def __pos__(self: _ArraySelf) -> _ArraySelf: ...
1655+
def __abs__(self: _ArraySelf) -> _ArraySelf: ...
16421656
# Ensure that objects annotated as `number` support arithmetic operations
16431657
__add__: _NumberOp
16441658
__radd__: _NumberOp
@@ -1659,6 +1673,10 @@ class bool_(generic):
16591673
def real(self: _ArraySelf) -> _ArraySelf: ...
16601674
@property
16611675
def imag(self: _ArraySelf) -> _ArraySelf: ...
1676+
def __int__(self) -> int: ...
1677+
def __float__(self) -> float: ...
1678+
def __complex__(self) -> complex: ...
1679+
def __abs__(self: _ArraySelf) -> _ArraySelf: ...
16621680
__add__: _BoolOp[bool_]
16631681
__radd__: _BoolOp[bool_]
16641682
__sub__: _BoolSub
@@ -1682,6 +1700,10 @@ class bool_(generic):
16821700
__rxor__: _BoolBitOp[bool_]
16831701
__or__: _BoolBitOp[bool_]
16841702
__ror__: _BoolBitOp[bool_]
1703+
__mod__: _BoolMod
1704+
__rmod__: _BoolMod
1705+
__divmod__: _BoolDivMod
1706+
__rdivmod__: _BoolDivMod
16851707

16861708
class object_(generic):
16871709
def __init__(self, __value: object = ...) -> None: ...
@@ -1727,6 +1749,8 @@ class integer(number[_NBit_co]): # type: ignore
17271749
def __index__(self) -> int: ...
17281750
__truediv__: _IntTrueDiv[_NBit_co]
17291751
__rtruediv__: _IntTrueDiv[_NBit_co]
1752+
def __mod__(self, value: Union[_IntLike, integer]) -> integer: ...
1753+
def __rmod__(self, value: Union[_IntLike, integer]) -> integer: ...
17301754
def __invert__(self: _IntType) -> _IntType: ...
17311755
# Ensure that objects annotated as `integer` support bit-wise operations
17321756
def __lshift__(self, other: Union[_IntLike, _BoolLike]) -> integer: ...
@@ -1762,6 +1786,10 @@ class signedinteger(integer[_NBit_co]):
17621786
__rxor__: _SignedIntBitOp[_NBit_co]
17631787
__or__: _SignedIntBitOp[_NBit_co]
17641788
__ror__: _SignedIntBitOp[_NBit_co]
1789+
__mod__: _SignedIntMod[_NBit_co]
1790+
__rmod__: _SignedIntMod[_NBit_co]
1791+
__divmod__: _SignedIntDivMod[_NBit_co]
1792+
__rdivmod__: _SignedIntDivMod[_NBit_co]
17651793

17661794
int8 = signedinteger[_8Bit]
17671795
int16 = signedinteger[_16Bit]
@@ -1774,6 +1802,12 @@ class timedelta64(generic):
17741802
__value: Union[None, int, _CharLike, dt.timedelta, timedelta64] = ...,
17751803
__format: Union[_CharLike, Tuple[_CharLike, _IntLike]] = ...,
17761804
) -> None: ...
1805+
def __int__(self) -> int: ...
1806+
def __float__(self) -> float: ...
1807+
def __complex__(self) -> complex: ...
1808+
def __neg__(self: _ArraySelf) -> _ArraySelf: ...
1809+
def __pos__(self: _ArraySelf) -> _ArraySelf: ...
1810+
def __abs__(self: _ArraySelf) -> _ArraySelf: ...
17771811
def __add__(self, other: Union[timedelta64, _IntLike, _BoolLike]) -> timedelta64: ...
17781812
def __radd__(self, other: Union[timedelta64, _IntLike, _BoolLike]) -> timedelta64: ...
17791813
def __sub__(self, other: Union[timedelta64, _IntLike, _BoolLike]) -> timedelta64: ...
@@ -1785,6 +1819,9 @@ class timedelta64(generic):
17851819
def __rtruediv__(self, other: timedelta64) -> float64: ...
17861820
def __rfloordiv__(self, other: timedelta64) -> int64: ...
17871821
def __mod__(self, other: timedelta64) -> timedelta64: ...
1822+
def __rmod__(self, other: timedelta64) -> timedelta64: ...
1823+
def __divmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ...
1824+
def __rdivmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ...
17881825

17891826
class unsignedinteger(integer[_NBit_co]):
17901827
# NOTE: `uint64 + signedinteger -> float64`
@@ -1809,6 +1846,10 @@ class unsignedinteger(integer[_NBit_co]):
18091846
__rxor__: _UnsignedIntBitOp[_NBit_co]
18101847
__or__: _UnsignedIntBitOp[_NBit_co]
18111848
__ror__: _UnsignedIntBitOp[_NBit_co]
1849+
__mod__: _UnsignedIntMod[_NBit_co]
1850+
__rmod__: _UnsignedIntMod[_NBit_co]
1851+
__divmod__: _UnsignedIntDivMod[_NBit_co]
1852+
__rdivmod__: _UnsignedIntDivMod[_NBit_co]
18121853

18131854
uint8 = unsignedinteger[_8Bit]
18141855
uint16 = unsignedinteger[_16Bit]
@@ -1834,6 +1875,10 @@ class floating(inexact[_NBit_co]):
18341875
__rfloordiv__: _FloatOp[_NBit_co]
18351876
__pow__: _FloatOp[_NBit_co]
18361877
__rpow__: _FloatOp[_NBit_co]
1878+
__mod__: _FloatMod[_NBit_co]
1879+
__rmod__: _FloatMod[_NBit_co]
1880+
__divmod__: _FloatDivMod[_NBit_co]
1881+
__rdivmod__: _FloatDivMod[_NBit_co]
18371882

18381883
float16 = floating[_16Bit]
18391884
float32 = floating[_32Bit]
@@ -1878,7 +1923,9 @@ class void(flexible):
18781923
self, val: ArrayLike, dtype: DtypeLike, offset: int = ...
18791924
) -> None: ...
18801925

1881-
class character(flexible): ... # type: ignore
1926+
class character(flexible): # type: ignore
1927+
def __int__(self) -> int: ...
1928+
def __float__(self) -> float: ...
18821929

18831930
# NOTE: Most `np.bytes_` / `np.str_` methods return their
18841931
# builtin `bytes` / `str` counterpart

0 commit comments

Comments
 (0)
0