diff options
| -rw-r--r-- | numpy/__init__.pyi | 25 | ||||
| -rw-r--r-- | numpy/typing/_callable.py | 9 | ||||
| -rw-r--r-- | numpy/typing/tests/data/pass/comparisons.py | 247 | ||||
| -rw-r--r-- | numpy/typing/tests/data/reveal/comparisons.py | 247 |
4 files changed, 524 insertions, 4 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 2366e9b75..ad37979ed 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -47,6 +47,7 @@ from numpy.typing._callable import ( _FloatDivMod, _ComplexOp, _NumberOp, + _ComparisonOp, ) from typing import ( @@ -1013,12 +1014,8 @@ class _ArrayOrScalarCommon: def __repr__(self) -> str: ... def __copy__(self: _ArraySelf) -> _ArraySelf: ... def __deepcopy__(self: _ArraySelf, __memo: Optional[dict] = ...) -> _ArraySelf: ... - def __lt__(self, other): ... - def __le__(self, other): ... def __eq__(self, other): ... def __ne__(self, other): ... - def __gt__(self, other): ... - def __ge__(self, other): ... def astype( self: _ArraySelf, dtype: DTypeLike, @@ -1579,6 +1576,10 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container): def __iter__(self) -> Any: ... def __contains__(self, key) -> bool: ... def __index__(self) -> int: ... + def __lt__(self, other: ArrayLike) -> Union[ndarray, bool_]: ... + def __le__(self, other: ArrayLike) -> Union[ndarray, bool_]: ... + def __gt__(self, other: ArrayLike) -> Union[ndarray, bool_]: ... + def __ge__(self, other: ArrayLike) -> Union[ndarray, bool_]: ... def __matmul__(self, other: ArrayLike) -> Any: ... # NOTE: `ndarray` does not implement `__imatmul__` def __rmatmul__(self, other: ArrayLike) -> Any: ... @@ -1689,6 +1690,10 @@ class number(generic, Generic[_NBit_co]): # type: ignore __rpow__: _NumberOp __truediv__: _NumberOp __rtruediv__: _NumberOp + __lt__: _ComparisonOp[_NumberLike] + __le__: _ComparisonOp[_NumberLike] + __gt__: _ComparisonOp[_NumberLike] + __ge__: _ComparisonOp[_NumberLike] class bool_(generic): def __init__(self, __value: object = ...) -> None: ... @@ -1727,6 +1732,10 @@ class bool_(generic): __rmod__: _BoolMod __divmod__: _BoolDivMod __rdivmod__: _BoolDivMod + __lt__: _ComparisonOp[_NumberLike] + __le__: _ComparisonOp[_NumberLike] + __gt__: _ComparisonOp[_NumberLike] + __ge__: _ComparisonOp[_NumberLike] class object_(generic): def __init__(self, __value: object = ...) -> None: ... @@ -1755,6 +1764,10 @@ class datetime64(generic): @overload def __sub__(self, other: Union[timedelta64, _IntLike, _BoolLike]) -> datetime64: ... def __rsub__(self, other: datetime64) -> timedelta64: ... + __lt__: _ComparisonOp[datetime64] + __le__: _ComparisonOp[datetime64] + __gt__: _ComparisonOp[datetime64] + __ge__: _ComparisonOp[datetime64] # Support for `__index__` was added in python 3.8 (bpo-20092) if sys.version_info >= (3, 8): @@ -1845,6 +1858,10 @@ class timedelta64(generic): def __rmod__(self, other: timedelta64) -> timedelta64: ... def __divmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ... def __rdivmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ... + __lt__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]] + __le__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]] + __gt__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]] + __ge__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]] class unsignedinteger(integer[_NBit_co]): # NOTE: `uint64 + signedinteger -> float64` diff --git a/numpy/typing/_callable.py b/numpy/typing/_callable.py index 91b7a4ec2..c703df28a 100644 --- a/numpy/typing/_callable.py +++ b/numpy/typing/_callable.py @@ -20,6 +20,7 @@ from typing import ( ) from numpy import ( + ndarray, generic, bool_, timedelta64, @@ -41,6 +42,7 @@ from ._scalars import ( _NumberLike, ) from . import NBitBase +from ._array_like import ArrayLike if sys.version_info >= (3, 8): from typing import Protocol @@ -312,6 +314,12 @@ if TYPE_CHECKING or HAVE_PROTOCOL: class _NumberOp(Protocol): def __call__(self, __other: _NumberLike) -> number: ... + class _ComparisonOp(Protocol[_T]): + @overload + def __call__(self, __other: _T) -> bool_: ... + @overload + def __call__(self, __other: ArrayLike) -> Union[ndarray, bool_]: ... + else: _BoolOp = Any _BoolBitOp = Any @@ -334,3 +342,4 @@ else: _FloatDivMod = Any _ComplexOp = Any _NumberOp = Any + _ComparisonOp = Any diff --git a/numpy/typing/tests/data/pass/comparisons.py b/numpy/typing/tests/data/pass/comparisons.py new file mode 100644 index 000000000..b298117a6 --- /dev/null +++ b/numpy/typing/tests/data/pass/comparisons.py @@ -0,0 +1,247 @@ +import numpy as np + +c16 = np.complex128() +f8 = np.float64() +i8 = np.int64() +u8 = np.uint64() + +c8 = np.complex64() +f4 = np.float32() +i4 = np.int32() +u4 = np.uint32() + +dt = np.datetime64(0, "D") +td = np.timedelta64(0, "D") + +b_ = np.bool_() + +b = bool() +c = complex() +f = float() +i = int() + +AR = np.array([0], dtype=np.int64) +AR.setflags(write=False) + +SEQ = (0, 1, 2, 3, 4) + +# Time structures + +dt > dt + +td > td +td > i +td > i4 +td > i8 +td > AR +td > SEQ + +# boolean + +b_ > b +b_ > b_ +b_ > i +b_ > i8 +b_ > i4 +b_ > u8 +b_ > u4 +b_ > f +b_ > f8 +b_ > f4 +b_ > c +b_ > c16 +b_ > c8 +b_ > AR +b_ > SEQ + +# Complex + +c16 > c16 +c16 > f8 +c16 > i8 +c16 > c8 +c16 > f4 +c16 > i4 +c16 > b_ +c16 > b +c16 > c +c16 > f +c16 > i +c16 > AR +c16 > SEQ + +c16 > c16 +f8 > c16 +i8 > c16 +c8 > c16 +f4 > c16 +i4 > c16 +b_ > c16 +b > c16 +c > c16 +f > c16 +i > c16 +AR > c16 +SEQ > c16 + +c8 > c16 +c8 > f8 +c8 > i8 +c8 > c8 +c8 > f4 +c8 > i4 +c8 > b_ +c8 > b +c8 > c +c8 > f +c8 > i +c8 > AR +c8 > SEQ + +c16 > c8 +f8 > c8 +i8 > c8 +c8 > c8 +f4 > c8 +i4 > c8 +b_ > c8 +b > c8 +c > c8 +f > c8 +i > c8 +AR > c8 +SEQ > c8 + +# Float + +f8 > f8 +f8 > i8 +f8 > f4 +f8 > i4 +f8 > b_ +f8 > b +f8 > c +f8 > f +f8 > i +f8 > AR +f8 > SEQ + +f8 > f8 +i8 > f8 +f4 > f8 +i4 > f8 +b_ > f8 +b > f8 +c > f8 +f > f8 +i > f8 +AR > f8 +SEQ > f8 + +f4 > f8 +f4 > i8 +f4 > f4 +f4 > i4 +f4 > b_ +f4 > b +f4 > c +f4 > f +f4 > i +f4 > AR +f4 > SEQ + +f8 > f4 +i8 > f4 +f4 > f4 +i4 > f4 +b_ > f4 +b > f4 +c > f4 +f > f4 +i > f4 +AR > f4 +SEQ > f4 + +# Int + +i8 > i8 +i8 > u8 +i8 > i4 +i8 > u4 +i8 > b_ +i8 > b +i8 > c +i8 > f +i8 > i +i8 > AR +i8 > SEQ + +u8 > u8 +u8 > i4 +u8 > u4 +u8 > b_ +u8 > b +u8 > c +u8 > f +u8 > i +u8 > AR +u8 > SEQ + +i8 > i8 +u8 > i8 +i4 > i8 +u4 > i8 +b_ > i8 +b > i8 +c > i8 +f > i8 +i > i8 +AR > i8 +SEQ > i8 + +u8 > u8 +i4 > u8 +u4 > u8 +b_ > u8 +b > u8 +c > u8 +f > u8 +i > u8 +AR > u8 +SEQ > u8 + +i4 > i8 +i4 > i4 +i4 > i +i4 > b_ +i4 > b +i4 > AR +i4 > SEQ + +u4 > i8 +u4 > i4 +u4 > u8 +u4 > u4 +u4 > i +u4 > b_ +u4 > b +u4 > AR +u4 > SEQ + +i8 > i4 +i4 > i4 +i > i4 +b_ > i4 +b > i4 +AR > i4 +SEQ > i4 + +i8 > u4 +i4 > u4 +u8 > u4 +u4 > u4 +b_ > u4 +b > u4 +i > u4 +AR > u4 +SEQ > u4 diff --git a/numpy/typing/tests/data/reveal/comparisons.py b/numpy/typing/tests/data/reveal/comparisons.py new file mode 100644 index 000000000..82d1fa6de --- /dev/null +++ b/numpy/typing/tests/data/reveal/comparisons.py @@ -0,0 +1,247 @@ +import numpy as np + +c16 = np.complex128() +f8 = np.float64() +i8 = np.int64() +u8 = np.uint64() + +c8 = np.complex64() +f4 = np.float32() +i4 = np.int32() +u4 = np.uint32() + +dt = np.datetime64(0, "D") +td = np.timedelta64(0, "D") + +b_ = np.bool_() + +b = bool() +c = complex() +f = float() +i = int() + +AR = np.array([0], dtype=np.int64) +AR.setflags(write=False) + +SEQ = (0, 1, 2, 3, 4) + +# Time structures + +reveal_type(dt > dt) # E: numpy.bool_ + +reveal_type(td > td) # E: numpy.bool_ +reveal_type(td > i) # E: numpy.bool_ +reveal_type(td > i4) # E: numpy.bool_ +reveal_type(td > i8) # E: numpy.bool_ +reveal_type(td > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(td > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +# boolean + +reveal_type(b_ > b) # E: numpy.bool_ +reveal_type(b_ > b_) # E: numpy.bool_ +reveal_type(b_ > i) # E: numpy.bool_ +reveal_type(b_ > i8) # E: numpy.bool_ +reveal_type(b_ > i4) # E: numpy.bool_ +reveal_type(b_ > u8) # E: numpy.bool_ +reveal_type(b_ > u4) # E: numpy.bool_ +reveal_type(b_ > f) # E: numpy.bool_ +reveal_type(b_ > f8) # E: numpy.bool_ +reveal_type(b_ > f4) # E: numpy.bool_ +reveal_type(b_ > c) # E: numpy.bool_ +reveal_type(b_ > c16) # E: numpy.bool_ +reveal_type(b_ > c8) # E: numpy.bool_ +reveal_type(b_ > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(b_ > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +# Complex + +reveal_type(c16 > c16) # E: numpy.bool_ +reveal_type(c16 > f8) # E: numpy.bool_ +reveal_type(c16 > i8) # E: numpy.bool_ +reveal_type(c16 > c8) # E: numpy.bool_ +reveal_type(c16 > f4) # E: numpy.bool_ +reveal_type(c16 > i4) # E: numpy.bool_ +reveal_type(c16 > b_) # E: numpy.bool_ +reveal_type(c16 > b) # E: numpy.bool_ +reveal_type(c16 > c) # E: numpy.bool_ +reveal_type(c16 > f) # E: numpy.bool_ +reveal_type(c16 > i) # E: numpy.bool_ +reveal_type(c16 > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(c16 > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(c16 > c16) # E: numpy.bool_ +reveal_type(f8 > c16) # E: numpy.bool_ +reveal_type(i8 > c16) # E: numpy.bool_ +reveal_type(c8 > c16) # E: numpy.bool_ +reveal_type(f4 > c16) # E: numpy.bool_ +reveal_type(i4 > c16) # E: numpy.bool_ +reveal_type(b_ > c16) # E: numpy.bool_ +reveal_type(b > c16) # E: numpy.bool_ +reveal_type(c > c16) # E: numpy.bool_ +reveal_type(f > c16) # E: numpy.bool_ +reveal_type(i > c16) # E: numpy.bool_ +reveal_type(AR > c16) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(SEQ > c16) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(c8 > c16) # E: numpy.bool_ +reveal_type(c8 > f8) # E: numpy.bool_ +reveal_type(c8 > i8) # E: numpy.bool_ +reveal_type(c8 > c8) # E: numpy.bool_ +reveal_type(c8 > f4) # E: numpy.bool_ +reveal_type(c8 > i4) # E: numpy.bool_ +reveal_type(c8 > b_) # E: numpy.bool_ +reveal_type(c8 > b) # E: numpy.bool_ +reveal_type(c8 > c) # E: numpy.bool_ +reveal_type(c8 > f) # E: numpy.bool_ +reveal_type(c8 > i) # E: numpy.bool_ +reveal_type(c8 > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(c8 > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(c16 > c8) # E: numpy.bool_ +reveal_type(f8 > c8) # E: numpy.bool_ +reveal_type(i8 > c8) # E: numpy.bool_ +reveal_type(c8 > c8) # E: numpy.bool_ +reveal_type(f4 > c8) # E: numpy.bool_ +reveal_type(i4 > c8) # E: numpy.bool_ +reveal_type(b_ > c8) # E: numpy.bool_ +reveal_type(b > c8) # E: numpy.bool_ +reveal_type(c > c8) # E: numpy.bool_ +reveal_type(f > c8) # E: numpy.bool_ +reveal_type(i > c8) # E: numpy.bool_ +reveal_type(AR > c8) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(SEQ > c8) # E: Union[numpy.ndarray, numpy.bool_] + +# Float + +reveal_type(f8 > f8) # E: numpy.bool_ +reveal_type(f8 > i8) # E: numpy.bool_ +reveal_type(f8 > f4) # E: numpy.bool_ +reveal_type(f8 > i4) # E: numpy.bool_ +reveal_type(f8 > b_) # E: numpy.bool_ +reveal_type(f8 > b) # E: numpy.bool_ +reveal_type(f8 > c) # E: numpy.bool_ +reveal_type(f8 > f) # E: numpy.bool_ +reveal_type(f8 > i) # E: numpy.bool_ +reveal_type(f8 > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(f8 > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(f8 > f8) # E: numpy.bool_ +reveal_type(i8 > f8) # E: numpy.bool_ +reveal_type(f4 > f8) # E: numpy.bool_ +reveal_type(i4 > f8) # E: numpy.bool_ +reveal_type(b_ > f8) # E: numpy.bool_ +reveal_type(b > f8) # E: numpy.bool_ +reveal_type(c > f8) # E: numpy.bool_ +reveal_type(f > f8) # E: numpy.bool_ +reveal_type(i > f8) # E: numpy.bool_ +reveal_type(AR > f8) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(SEQ > f8) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(f4 > f8) # E: numpy.bool_ +reveal_type(f4 > i8) # E: numpy.bool_ +reveal_type(f4 > f4) # E: numpy.bool_ +reveal_type(f4 > i4) # E: numpy.bool_ +reveal_type(f4 > b_) # E: numpy.bool_ +reveal_type(f4 > b) # E: numpy.bool_ +reveal_type(f4 > c) # E: numpy.bool_ +reveal_type(f4 > f) # E: numpy.bool_ +reveal_type(f4 > i) # E: numpy.bool_ +reveal_type(f4 > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(f4 > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(f8 > f4) # E: numpy.bool_ +reveal_type(i8 > f4) # E: numpy.bool_ +reveal_type(f4 > f4) # E: numpy.bool_ +reveal_type(i4 > f4) # E: numpy.bool_ +reveal_type(b_ > f4) # E: numpy.bool_ +reveal_type(b > f4) # E: numpy.bool_ +reveal_type(c > f4) # E: numpy.bool_ +reveal_type(f > f4) # E: numpy.bool_ +reveal_type(i > f4) # E: numpy.bool_ +reveal_type(AR > f4) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(SEQ > f4) # E: Union[numpy.ndarray, numpy.bool_] + +# Int + +reveal_type(i8 > i8) # E: numpy.bool_ +reveal_type(i8 > u8) # E: numpy.bool_ +reveal_type(i8 > i4) # E: numpy.bool_ +reveal_type(i8 > u4) # E: numpy.bool_ +reveal_type(i8 > b_) # E: numpy.bool_ +reveal_type(i8 > b) # E: numpy.bool_ +reveal_type(i8 > c) # E: numpy.bool_ +reveal_type(i8 > f) # E: numpy.bool_ +reveal_type(i8 > i) # E: numpy.bool_ +reveal_type(i8 > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(i8 > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(u8 > u8) # E: numpy.bool_ +reveal_type(u8 > i4) # E: numpy.bool_ +reveal_type(u8 > u4) # E: numpy.bool_ +reveal_type(u8 > b_) # E: numpy.bool_ +reveal_type(u8 > b) # E: numpy.bool_ +reveal_type(u8 > c) # E: numpy.bool_ +reveal_type(u8 > f) # E: numpy.bool_ +reveal_type(u8 > i) # E: numpy.bool_ +reveal_type(u8 > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(u8 > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(i8 > i8) # E: numpy.bool_ +reveal_type(u8 > i8) # E: numpy.bool_ +reveal_type(i4 > i8) # E: numpy.bool_ +reveal_type(u4 > i8) # E: numpy.bool_ +reveal_type(b_ > i8) # E: numpy.bool_ +reveal_type(b > i8) # E: numpy.bool_ +reveal_type(c > i8) # E: numpy.bool_ +reveal_type(f > i8) # E: numpy.bool_ +reveal_type(i > i8) # E: numpy.bool_ +reveal_type(AR > i8) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(SEQ > i8) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(u8 > u8) # E: numpy.bool_ +reveal_type(i4 > u8) # E: numpy.bool_ +reveal_type(u4 > u8) # E: numpy.bool_ +reveal_type(b_ > u8) # E: numpy.bool_ +reveal_type(b > u8) # E: numpy.bool_ +reveal_type(c > u8) # E: numpy.bool_ +reveal_type(f > u8) # E: numpy.bool_ +reveal_type(i > u8) # E: numpy.bool_ +reveal_type(AR > u8) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(SEQ > u8) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(i4 > i8) # E: numpy.bool_ +reveal_type(i4 > i4) # E: numpy.bool_ +reveal_type(i4 > i) # E: numpy.bool_ +reveal_type(i4 > b_) # E: numpy.bool_ +reveal_type(i4 > b) # E: numpy.bool_ +reveal_type(i4 > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(i4 > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(u4 > i8) # E: numpy.bool_ +reveal_type(u4 > i4) # E: numpy.bool_ +reveal_type(u4 > u8) # E: numpy.bool_ +reveal_type(u4 > u4) # E: numpy.bool_ +reveal_type(u4 > i) # E: numpy.bool_ +reveal_type(u4 > b_) # E: numpy.bool_ +reveal_type(u4 > b) # E: numpy.bool_ +reveal_type(u4 > AR) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(u4 > SEQ) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(i8 > i4) # E: numpy.bool_ +reveal_type(i4 > i4) # E: numpy.bool_ +reveal_type(i > i4) # E: numpy.bool_ +reveal_type(b_ > i4) # E: numpy.bool_ +reveal_type(b > i4) # E: numpy.bool_ +reveal_type(AR > i4) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(SEQ > i4) # E: Union[numpy.ndarray, numpy.bool_] + +reveal_type(i8 > u4) # E: numpy.bool_ +reveal_type(i4 > u4) # E: numpy.bool_ +reveal_type(u8 > u4) # E: numpy.bool_ +reveal_type(u4 > u4) # E: numpy.bool_ +reveal_type(b_ > u4) # E: numpy.bool_ +reveal_type(b > u4) # E: numpy.bool_ +reveal_type(i > u4) # E: numpy.bool_ +reveal_type(AR > u4) # E: Union[numpy.ndarray, numpy.bool_] +reveal_type(SEQ > u4) # E: Union[numpy.ndarray, numpy.bool_] |
