8000 ENH: Add annotations for 9 `ndarray`/`generic` magic methods by BvB93 · Pull Request #17613 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add annotations for 9 ndarray/generic magic methods #17613

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

Merged
merged 9 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension 8000

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 67 additions & 20 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ from numpy.typing._callable import (
_BoolBitOp,
_BoolSub,
_BoolTrueDiv,
_BoolMod,
_BoolDivMod,
_TD64Div,
_IntTrueDiv,
_UnsignedIntOp,
_UnsignedIntBitOp,
_UnsignedIntMod,
_UnsignedIntDivMod,
_SignedIntOp,
_SignedIntBitOp,
_SignedIntMod,
_SignedIntDivMod,
_FloatOp,
_FloatMod,
_FloatDivMod,
_ComplexOp,
_NumberOp,
)
Expand All @@ -55,8 +63,6 @@ from typing import (
overload,
Sequence,
Sized,
SupportsAbs,
SupportsBytes,
SupportsComplex,
SupportsFloat,
SupportsInt,
Expand Down Expand Up @@ -978,9 +984,7 @@ _ArrayLikeIntOrBool = Union[

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

class _ArrayOrScalarCommon(
SupportsInt, SupportsFloat, SupportsComplex, SupportsBytes, SupportsAbs[Any]
):
class _ArrayOrScalarCommon:
@property
def T(self: _ArraySelf) -> _ArraySelf: ...
@property
Expand All @@ -1004,9 +1008,6 @@ class _ArrayOrScalarCommon(
@property
def strides(self) -> _Shape: ...
def __array__(self, __dtype: DtypeLike = ...) -> ndarray: ...
def __int__(self) -> int: ...
def __float__(self) -> float: ...
def __complex__(self) -> complex: ...
def __bool__(self) -> bool: ...
def __bytes__(self) -> bytes: ...
def __str__(self) -> str: ...
Expand All @@ -1019,13 +1020,6 @@ class _ArrayOrScalarCommon(
def __ne__(self, other): ...
def __gt__(self, other): ...
def __ge__(self, other): ...
def __mod__(self, other): ...
def __rmod__(self, other): ...
def __divmod__(self, other): ...
def __rdivmod__(self, other): ...
def __neg__(self: _ArraySelf) -> _ArraySelf: ...
def __pos__(self: _ArraySelf) -> _ArraySelf: ...
def __abs__(self: _ArraySelf) -> _ArraySelf: ...
def astype(
self: _ArraySelf,
dtype: DtypeLike,
Expand Down Expand Up @@ -1572,14 +1566,28 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
# Many of these special methods are irrelevant currently, since protocols
# aren't supported yet. That said, I'm adding them for completeness.
# https://docs.python.org/3/reference/datamodel.html
def __int__(self) -> int: ...
def __float__(self) -> float: ...
def __complex__(self) -> complex: ...
def __len__(self) -> int: ...
def __setitem__(self, key, value): ...
def __iter__(self) -> Any: ...
def __contains__(self, key) -> bool: ...
def __index__(self) -> int: ...
def __matmul__(self, other): ...
def __imatmul__(self, other): ...
def __rmatmul__(self, other): ...
def __matmul__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
# NOTE: `ndarray` does not implement `__imatmul__`
def __rmatmul__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
def __neg__(self: _ArraySelf) -> Union[_ArraySelf, generic]: ...
def __pos__(self: _ArraySelf) -> Union[_ArraySelf, generic]: ...
def __abs__(self: _ArraySelf) -> Union[_ArraySelf, generic]: ...
def __mod__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
def __rmod__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
def __divmod__(
self, other: ArrayLike
) -> Union[Tuple[ndarray, ndarray], Tuple[generic, generic]]: ...
def __rdivmod__(
self, other: ArrayLike
) -> Union[Tuple[ndarray, ndarray], Tuple[generic, generic]]: ...
def __add__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
def __radd__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
def __sub__(self, other: ArrayLike) -> Union[ndarray, generic]: ...
Expand Down Expand Up @@ -1610,7 +1618,7 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
def __itruediv__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
def __ifloordiv__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
def __ipow__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
def __imod__(self, other): ...
def __imod__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
def __ilshift__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
def __irshift__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
def __iand__(self: _ArraySelf, other: ArrayLike) -> _ArraySelf: ...
Expand Down Expand Up @@ -1639,6 +1647,12 @@ class number(generic, Generic[_NBit_co]): # type: ignore
def real(self: _ArraySelf) -> _ArraySelf: ...
@property
def imag(self: _ArraySelf) -> _ArraySelf: ...
def __int__(self) -> int: ...
def __float__(self) -> float: ...
def __complex__(self) -> complex: ...
def __neg__(self: _ArraySelf) -> _ArraySelf: ...
def __pos__(self: _ArraySelf) -> _ArraySelf: ...
def __abs__(self: _ArraySelf) -> _ArraySelf: ...
# Ensure that objects annotated as `number` support arithmetic operations
__add__: _NumberOp
__radd__: _NumberOp
Expand All @@ -1659,6 +1673,10 @@ class bool_(generic):
def real(self: _ArraySelf) -> _ArraySelf: ...
@property
def imag(self: _ArraySelf) -> _ArraySelf: ...
def __int__(self) -> int: ...
def __float__(self) -> float: ...
def __complex__(self) -> complex: ...
def __abs__(self: _ArraySelf) -> _ArraySelf: ...
__add__: _BoolOp[bool_]
__radd__: _BoolOp[bool_]
__sub__: _BoolSub
Expand All @@ -1682,6 +1700,10 @@ class bool_(generic):
__rxor__: _BoolBitOp[bool_]
__or__: _BoolBitOp[bool_]
__ror__: _BoolBitOp[bool_]
__mod__: _BoolMod
__rmod__: _BoolMod
__divmod__: _BoolDivMod
__rdivmod__: _BoolDivMod

class object_(generic):
def __init__(self, __value: object = ...) -> None: ...
Expand Down Expand Up @@ -1727,6 +1749,8 @@ class integer(number[_NBit_co]): # type: ignore
def __index__(self) -> int: ...
__truediv__: _IntTrueDiv[_NBit_co]
__rtruediv__: _IntTrueDiv[_NBit_co]
def __mod__(self, value: Union[_IntLike, integer]) -> integer: ...
def __rmod__(self, value: Union[_IntLike, integer]) -> integer: ...
def __invert__(self: _IntType) -> _IntType: ...
# Ensure that objects annotated as `integer` support bit-wise operations
def __lshift__(self, other: Union[_IntLike, _BoolLike]) -> integer: ...
Expand Down Expand Up @@ -1762,6 +1786,10 @@ class signedinteger(integer[_NBit_co]):
__rxor__: _SignedIntBitOp[_NBit_co]
__or__: _SignedIntBitOp[_NBit_co]
__ror__: _SignedIntBitOp[_NBit_co]
__mod__: _SignedIntMod[_NBit_co]
__rmod__: _SignedIntMod[_NBit_co]
__divmod__: _SignedIntDivMod[_NBit_co]
__rdivmod__: _SignedIntDivMod[_NBit_co]

int8 = signedinteger[_8Bit]
int16 = signedinteger[_16Bit]
Expand All @@ -1774,6 +1802,12 @@ class timedelta64(generic):
__value: Union[None, int, _CharLike, dt.timedelta, timedelta64] = ...,
__format: Union[_CharLike, Tuple[_CharLike, _IntLike]] = ...,
) -> None: ...
def __int__(self) -> int: ...
def __float__(self) -> float: ...
def __complex__(self) -> complex: ...
def __neg__(self: _ArraySelf) -> _ArraySelf: ...
def __pos__(self: _ArraySelf) -> _ArraySelf: ...
def __abs__(self: _ArraySelf) -> _ArraySelf: ...
def __add__(self, other: Union[timedelta64, _IntLike, _BoolLike]) -> timedelta64: ...
def __radd__(self, other: Union[timedelta64, _IntLike, _BoolLike]) -> timedelta64: ...
def __sub__(self, other: Union[timedelta64, _IntLike, _BoolLike]) -> timedelta64: ...
Expand All @@ -1785,6 +1819,9 @@ class timedelta64(generic):
def __rtruediv__(self, other: timedelta64) -> float64: ...
def __rfloordiv__(self, other: timedelta64) -> int64: ...
def __mod__(self, other: timedelta64) -> timedelta64: ...
def __rmod__(self, other: timedelta64) -> timedelta64: ...
def __divmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ...
def __rdivmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ...

class unsignedinteger(integer[_NBit_co]):
# NOTE: `uint64 + signedinteger -> float64`
Expand All @@ -1809,6 +1846,10 @@ class unsignedinteger(integer[_NBit_co]):
__rxor__: _UnsignedIntBitOp[_NBit_co]
__or__: _UnsignedIntBitOp[_NBit_co]
__ror__: _UnsignedIntBitOp[_NBit_co]
__mod__: _UnsignedIntMod[_NBit_co]
__rmod__: _UnsignedIntMod[_NBit_co]
__divmod__: _UnsignedIntDivMod[_NBit_co]
__rdivmod__: _UnsignedIntDivMod[_NBit_co]

uint8 = unsignedinteger[_8Bit]
uint16 = unsignedinteger[_16Bit]
Expand All @@ -1834,6 +1875,10 @@ class floating(inexact[_NBit_co]):
__rfloordiv__: _FloatOp[_NBit_co]
__pow__: _FloatOp[_NBit_co]
__rpow__: _FloatOp[_NBit_co]
__mod__: _FloatMod[_NBit_co]
__rmod__: _FloatMod[_NBit_co]
__divmod__: _FloatDivMod[_NBit_co]
__rdivmod__: _FloatDivMod[_NBit_co]

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

class character(flexible): ... # type: ignore
class character(flexible): # type: ignore
def __int__(self) -> int: ...
def __float__(self) -> float: ...

# NOTE: Most `np.bytes_` / `np.str_` methods return their
# builtin `bytes` / `str` counterpart
Expand Down
Loading
0