Skip to content

Commit 4b80666

Browse files
jorenhamcharris
authored andcommitted
TYP: fix np.bool method declarations
1 parent f2a6b75 commit 4b80666

4 files changed

Lines changed: 192 additions & 105 deletions

File tree

numpy/__init__.pyi

Lines changed: 188 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@ from numpy._typing import (
134134
)
135135

136136
from numpy._typing._callable import (
137-
_BoolOp,
138-
_BoolBitOp,
139-
_BoolSub,
140-
_BoolTrueDiv,
141-
_BoolMod,
142-
_BoolDivMod,
143137
_IntTrueDiv,
144138
_UnsignedIntOp,
145139
_UnsignedIntBitOp,
@@ -803,6 +797,7 @@ _1NShapeT = TypeVar("_1NShapeT", bound=tuple[L[1], *tuple[L[1], ...]]) # (1,) |
803797
_ScalarT = TypeVar("_ScalarT", bound=generic)
804798
_ScalarT_co = TypeVar("_ScalarT_co", bound=generic, default=Any, covariant=True)
805799
_NumberT = TypeVar("_NumberT", bound=number)
800+
_InexactT = TypeVar("_InexactT", bound=inexact)
806801
_RealNumberT = TypeVar("_RealNumberT", bound=floating | integer)
807802
_FloatingT_co = TypeVar("_FloatingT_co", bound=floating, default=floating, covariant=True)
808803
_IntegerT = TypeVar("_IntegerT", bound=integer)
@@ -3824,12 +3819,14 @@ class bool(generic[_BoolItemT_co], Generic[_BoolItemT_co]):
38243819
def __init__(self: np.bool[builtins.bool], value: object, /) -> None: ...
38253820

38263821
def __bool__(self, /) -> _BoolItemT_co: ...
3822+
38273823
@overload
38283824
def __int__(self: np.bool[L[False]], /) -> L[0]: ...
38293825
@overload
38303826
def __int__(self: np.bool[L[True]], /) -> L[1]: ...
38313827
@overload
38323828
def __int__(self, /) -> L[0, 1]: ...
3829+
38333830
def __abs__(self) -> Self: ...
38343831

38353832
@overload
@@ -3839,23 +3836,191 @@ class bool(generic[_BoolItemT_co], Generic[_BoolItemT_co]):
38393836
@overload
38403837
def __invert__(self, /) -> np.bool: ...
38413838

3842-
__add__: _BoolOp[np.bool]
3843-
__radd__: _BoolOp[np.bool]
3844-
__sub__: _BoolSub
3845-
__rsub__: _BoolSub
3846-
__mul__: _BoolOp[np.bool]
3847-
__rmul__: _BoolOp[np.bool]
3848-
__truediv__: _BoolTrueDiv
3849-
__rtruediv__: _BoolTrueDiv
3850-
__floordiv__: _BoolOp[int8]
3851-
__rfloordiv__: _BoolOp[int8]
3852-
__pow__: _BoolOp[int8]
3853-
__rpow__: _BoolOp[int8]
3854-
3855-
__lshift__: _BoolBitOp[int8]
3856-
__rlshift__: _BoolBitOp[int8]
3857-
__rshift__: _BoolBitOp[int8]
3858-
__rrshift__: _BoolBitOp[int8]
3839+
@overload
3840+
def __add__(self, other: _NumberT, /) -> _NumberT: ...
3841+
@overload
3842+
def __add__(self, other: builtins.bool | bool_, /) -> bool_: ...
3843+
@overload
3844+
def __add__(self, other: int, /) -> int_: ...
3845+
@overload
3846+
def __add__(self, other: float, /) -> float64: ...
3847+
@overload
3848+
def __add__(self, other: complex, /) -> complex128: ...
3849+
3850+
@overload
3851+
def __radd__(self, other: _NumberT, /) -> _NumberT: ...
3852+
@overload
3853+
def __radd__(self, other: builtins.bool, /) -> bool_: ...
3854+
@overload
3855+
def __radd__(self, other: int, /) -> int_: ...
3856+
@overload
3857+
def __radd__(self, other: float, /) -> float64: ...
3858+
@overload
3859+
def __radd__(self, other: complex, /) -> complex128: ...
3860+
3861+
@overload
3862+
def __sub__(self, other: _NumberT, /) -> _NumberT: ...
3863+
@overload
3864+
def __sub__(self, other: int, /) -> int_: ...
3865+
@overload
3866+
def __sub__(self, other: float, /) -> float64: ...
3867+
@overload
3868+
def __sub__(self, other: complex, /) -> complex128: ...
3869+
3870+
@overload
3871+
def __rsub__(self, other: _NumberT, /) -> _NumberT: ...
3872+
@overload
3873+
def __rsub__(self, other: int, /) -> int_: ...
3874+
@overload
3875+
def __rsub__(self, other: float, /) -> float64: ...
3876+
@overload
3877+
def __rsub__(self, other: complex, /) -> complex128: ...
3878+
3879+
@overload
3880+
def __mul__(self, other: _NumberT, /) -> _NumberT: ...
3881+
@overload
3882+
def __mul__(self, other: builtins.bool | bool_, /) -> bool_: ...
3883+
@overload
3884+
def __mul__(self, other: int, /) -> int_: ...
3885+
@overload
3886+
def __mul__(self, other: float, /) -> float64: ...
3887+
@overload
3888+
def __mul__(self, other: complex, /) -> complex128: ...
3889+
3890+
@overload
3891+
def __rmul__(self, other: _NumberT, /) -> _NumberT: ...
3892+
@overload
3893+
def __rmul__(self, other: builtins.bool, /) -> bool_: ...
3894+
@overload
3895+
def __rmul__(self, other: int, /) -> int_: ...
3896+
@overload
3897+
def __rmul__(self, other: float, /) -> float64: ...
3898+
@overload
3899+
def __rmul__(self, other: complex, /) -> complex128: ...
3900+
3901+
@overload
3902+
def __pow__(self, other: _NumberT, mod: None = None, /) -> _NumberT: ...
3903+
@overload
3904+
def __pow__(self, other: builtins.bool | bool_, mod: None = None, /) -> int8: ...
3905+
@overload
3906+
def __pow__(self, other: int, mod: None = None, /) -> int_: ...
3907+
@overload
3908+
def __pow__(self, other: float, mod: None = None, /) -> float64: ...
3909+
@overload
3910+
def __pow__(self, other: complex, mod: None = None, /) -> complex128: ...
3911+
3912+
@overload
3913+
def __rpow__(self, other: _NumberT, mod: None = None, /) -> _NumberT: ...
3914+
@overload
3915+
def __rpow__(self, other: builtins.bool, mod: None = None, /) -> int8: ...
3916+
@overload
3917+
def __rpow__(self, other: int, mod: None = None, /) -> int_: ...
3918+
@overload
3919+
def __rpow__(self, other: float, mod: None = None, /) -> float64: ...
3920+
@overload
3921+
def __rpow__(self, other: complex, mod: None = None, /) -> complex128: ...
3922+
3923+
@overload
3924+
def __truediv__(self, other: _InexactT, /) -> _InexactT: ...
3925+
@overload
3926+
def __truediv__(self, other: float | integer | bool_, /) -> float64: ...
3927+
@overload
3928+
def __truediv__(self, other: complex, /) -> complex128: ...
3929+
3930+
@overload
3931+
def __rtruediv__(self, other: _InexactT, /) -> _InexactT: ...
3932+
@overload
3933+
def __rtruediv__(self, other: float | integer, /) -> float64: ...
3934+
@overload
3935+
def __rtruediv__(self, other: complex, /) -> complex128: ...
3936+
3937+
@overload
3938+
def __floordiv__(self, other: _RealNumberT, /) -> _RealNumberT: ...
3939+
@overload
3940+
def __floordiv__(self, other: builtins.bool | bool_, /) -> int8: ...
3941+
@overload
3942+
def __floordiv__(self, other: int, /) -> int_: ...
3943+
@overload
3944+
def __floordiv__(self, other: float, /) -> float64: ...
3945+
3946+
@overload
3947+
def __rfloordiv__(self, other: _RealNumberT, /) -> _RealNumberT: ...
3948+
@overload
3949+
def __rfloordiv__(self, other: builtins.bool, /) -> int8: ...
3950+
@overload
3951+
def __rfloordiv__(self, other: int, /) -> int_: ...
3952+
@overload
3953+
def __rfloordiv__(self, other: float, /) -> float64: ...
3954+
3955+
# keep in sync with __floordiv__
3956+
@overload
3957+
def __mod__(self, other: _RealNumberT, /) -> _RealNumberT: ...
3958+
@overload
3959+
def __mod__(self, other: builtins.bool | bool_, /) -> int8: ...
3960+
@overload
3961+
def __mod__(self, other: int, /) -> int_: ...
3962+
@overload
3963+
def __mod__(self, other: float, /) -> float64: ...
3964+
3965+
# keep in sync with __rfloordiv__
3966+
@overload
3967+
def __rmod__(self, other: _RealNumberT, /) -> _RealNumberT: ...
3968+
@overload
3969+
def __rmod__(self, other: builtins.bool, /) -> int8: ...
3970+
@overload
3971+
def __rmod__(self, other: int, /) -> int_: ...
3972+
@overload
3973+
def __rmod__(self, other: float, /) -> float64: ...
3974+
3975+
# keep in sync with __mod__
3976+
@overload
3977+
def __divmod__(self, other: _RealNumberT, /) -> _2Tuple[_RealNumberT]: ...
3978+
@overload
3979+
def __divmod__(self, other: builtins.bool | bool_, /) -> _2Tuple[int8]: ...
3980+
@overload
3981+
def __divmod__(self, other: int, /) -> _2Tuple[int_]: ...
3982+
@overload
3983+
def __divmod__(self, other: float, /) -> _2Tuple[float64]: ...
3984+
3985+
# keep in sync with __rmod__
3986+
@overload
3987+
def __rdivmod__(self, other: _RealNumberT, /) -> _2Tuple[_RealNumberT]: ...
3988+
@overload
3989+
def __rdivmod__(self, other: builtins.bool, /) -> _2Tuple[int8]: ...
3990+
@overload
3991+
def __rdivmod__(self, other: int, /) -> _2Tuple[int_]: ...
3992+
@overload
3993+
def __rdivmod__(self, other: float, /) -> _2Tuple[float64]: ...
3994+
3995+
@overload
3996+
def __lshift__(self, other: _IntegerT, /) -> _IntegerT: ...
3997+
@overload
3998+
def __lshift__(self, other: builtins.bool | bool_, /) -> int8: ...
3999+
@overload
4000+
def __lshift__(self, other: int, /) -> int_: ...
4001+
4002+
@overload
4003+
def __rlshift__(self, other: _IntegerT, /) -> _IntegerT: ...
4004+
@overload
4005+
def __rlshift__(self, other: builtins.bool, /) -> int8: ...
4006+
@overload
4007+
def __rlshift__(self, other: int, /) -> int_: ...
4008+
4009+
# keep in sync with __lshift__
4010+
@overload
4011+
def __rshift__(self, other: _IntegerT, /) -> _IntegerT: ...
4012+
@overload
4013+
def __rshift__(self, other: builtins.bool | bool_, /) -> int8: ...
4014+
@overload
4015+
def __rshift__(self, other: int, /) -> int_: ...
4016+
4017+
# keep in sync with __rlshift__
4018+
@overload
4019+
def __rrshift__(self, other: _IntegerT, /) -> _IntegerT: ...
4020+
@overload
4021+
def __rrshift__(self, other: builtins.bool, /) -> int8: ...
4022+
@overload
4023+
def __rrshift__(self, other: int, /) -> int_: ...
38594024

38604025
@overload
38614026
def __and__(self: np.bool[L[False]], other: builtins.bool | np.bool, /) -> np.bool[L[False]]: ...
@@ -3899,11 +4064,6 @@ class bool(generic[_BoolItemT_co], Generic[_BoolItemT_co]):
38994064
def __or__(self, other: int, /) -> np.bool | intp: ...
39004065
__ror__ = __or__
39014066

3902-
__mod__: _BoolMod
3903-
__rmod__: _BoolMod
3904-
__divmod__: _BoolDivMod
3905-
__rdivmod__: _BoolDivMod
3906-
39074067
__lt__: _ComparisonOpLT[_NumberLike_co, _ArrayLikeNumber_co]
39084068
__le__: _ComparisonOpLE[_NumberLike_co, _ArrayLikeNumber_co]
39094069
__gt__: _ComparisonOpGT[_NumberLike_co, _ArrayLikeNumber_co]

numpy/_typing/_callable.pyi

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -44,93 +44,20 @@ from ._scalars import (
4444
_NumberLike_co,
4545
)
4646

47-
_T1 = TypeVar("_T1")
48-
_T2 = TypeVar("_T2")
47+
_T = TypeVar("_T")
4948
_T1_contra = TypeVar("_T1_contra", contravariant=True)
5049
_T2_contra = TypeVar("_T2_contra", contravariant=True)
5150

52-
_2Tuple: TypeAlias = tuple[_T1, _T1]
51+
_2Tuple: TypeAlias = tuple[_T, _T]
5352

5453
_NBit1 = TypeVar("_NBit1", bound=NBitBase)
5554
_NBit2 = TypeVar("_NBit2", bound=NBitBase)
5655

5756
_IntType = TypeVar("_IntType", bound=integer)
5857
_FloatType = TypeVar("_FloatType", bound=floating)
5958
_NumberType = TypeVar("_NumberType", bound=number)
60-
_NumberType_co = TypeVar("_NumberType_co", covariant=True, bound=number)
6159
_GenericType_co = TypeVar("_GenericType_co", covariant=True, bound=generic)
6260

63-
@type_check_only
64-
class _BoolOp(Protocol[_GenericType_co]):
65-
@overload
66-
def __call__(self, other: _BoolLike_co, /) -> _GenericType_co: ...
67-
@overload # platform dependent
68-
def __call__(self, other: int, /) -> int_: ...
69-
@overload
70-
def __call__(self, other: float, /) -> float64: ...
71-
@overload
72-
def __call__(self, other: complex, /) -> complex128: ...
73-
@overload
74-
def __call__(self, other: _NumberType, /) -> _NumberType: ...
75-
76-
@type_check_only
77-
class _BoolBitOp(Protocol[_GenericType_co]):
78-
@overload
79-
def __call__(self, other: _BoolLike_co, /) -> _GenericType_co: ...
80-
@overload # platform dependent
81-
def __call__(self, other: int, /) -> int_: ...
82-
@overload
83-
def __call__(self, other: _IntType, /) -> _IntType: ...
84-
85-
@type_check_only
86-
class _BoolSub(Protocol):
87-
# Note that `other: bool` is absent here
88-
@overload
89-
def __call__(self, other: bool, /) -> NoReturn: ...
90-
@overload # platform dependent
91-
def __call__(self, other: int, /) -> int_: ...
92-
@overload
93-
def __call__(self, other: float, /) -> float64: ...
94-
@overload
95-
def __call__(self, other: complex, /) -> complex128: ...
96-
@overload
97-
def __call__(self, other: _NumberType, /) -> _NumberType: ...
98-
99-
@type_check_only
100-
class _BoolTrueDiv(Protocol):
101-
@overload
102-
def __call__(self, other: float | _IntLike_co, /) -> float64: ...
103-
@overload
104-
def __call__(self, other: complex, /) -> complex128: ...
105-
@overload
106-
def __call__(self, other: _NumberType, /) -> _NumberType: ...
107-
108-
@type_check_only
109-
class _BoolMod(Protocol):
110-
@overload
111-
def __call__(self, other: _BoolLike_co, /) -> int8: ...
112-
@overload # platform dependent
113-
def __call__(self, other: int, /) -> int_: ...
114-
@overload
115-
def __call__(self, other: float, /) -> float64: ...
116-
@overload
117-
def __call__(self, other: _IntType, /) -> _IntType: ...
118-
@overload
119-
def __call__(self, other: _FloatType, /) -> _FloatType: ...
120-
121-
@type_check_only
122-
class _BoolDivMod(Protocol):
123-
@overload
124-
def __call__(self, other: _BoolLike_co, /) -> _2Tuple[int8]: ...
125-
@overload # platform dependent
126-
def __call__(self, other: int, /) -> _2Tuple[int_]: ...
127-
@overload
128-
def __call__(self, other: float, /) -> _2Tuple[np.float64]: ...
129-
@overload
130-
def __call__(self, other: _IntType, /) -> _2Tuple[_IntType]: ...
131-
@overload
132-
def __call__(self, other: _FloatType, /) -> _2Tuple[_FloatType]: ...
133-
13461
@type_check_only
13562
class _IntTrueDiv(Protocol[_NBit1]):
13663
@overload

numpy/typing/tests/data/fail/arithmetic.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ AR_f **= AR_LIKE_c # type: ignore[arg-type]
114114

115115
# Scalars
116116

117-
b_ - b_ # type: ignore[call-overload]
117+
b_ - b_ # type: ignore[operator]
118118

119119
dt + dt # type: ignore[operator]
120120
td - dt # type: ignore[operator]

numpy/typing/tests/data/fail/bitwise_ops.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ i = int()
88

99
f8 = np.float64()
1010

11-
b_ >> f8 # type: ignore[call-overload]
11+
b_ >> f8 # type: ignore[operator]
1212
i8 << f8 # type: ignore[call-overload]
1313
i | f8 # type: ignore[operator]
1414
i8 ^ f8 # type: ignore[call-overload]

0 commit comments

Comments
 (0)