Skip to content

Commit 8d34e56

Browse files
authored
Fully type _operator module
1 parent 72b7368 commit 8d34e56

File tree

1 file changed

+196
-47
lines changed

1 file changed

+196
-47
lines changed

stdlib/_operator.pyi

Lines changed: 196 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
2-
from _typeshed import SupportsGetItem, SupportsMul, SupportsRMul
2+
from _typeshed import (SupportsGetItem, SupportsMul, SupportsRMul, SupportsDunderGE, SupportsDunderGT, SupportsDunderLE,
3+
SupportsDunderLT, SupportsBool, SupportsAdd, SupportsRAdd, SupportsSub, SupportsRSub)
34
from collections.abc import Callable, Container, Iterable, MutableMapping, MutableSequence, Sequence
45
from operator import attrgetter as attrgetter, itemgetter as itemgetter, methodcaller as methodcaller
56
from typing import Any, AnyStr, Protocol, SupportsAbs, SupportsIndex, TypeVar, overload, type_check_only
@@ -13,39 +14,151 @@ _K = TypeVar("_K")
1314
_V = TypeVar("_V")
1415
_P = ParamSpec("_P")
1516

16-
# The following protocols return "Any" instead of bool, since the comparison
17-
# operators can be overloaded to return an arbitrary object. For example,
18-
# the numpy.array comparison dunders return another numpy.array.
17+
_SupportsComparison: TypeAlias = SupportsDunderLE | SupportsDunderGE | SupportsDunderGT | SupportsDunderLT
1918

2019
@type_check_only
21-
class _SupportsDunderLT(Protocol):
22-
def __lt__(self, other: Any, /) -> Any: ...
20+
class _SupportsInversion(Protocol[_T_co]):
21+
def __invert__(self) -> _T_co: ...
2322

2423
@type_check_only
25-
class _SupportsDunderGT(Protocol):
26-
def __gt__(self, other: Any, /) -> Any: ...
24+
class _SupportsNeg(Protocol[_T_co]):
25+
def __neg__(self) -> _T_co: ...
2726

2827
@type_check_only
29-
class _SupportsDunderLE(Protocol):
30-
def __le__(self, other: Any, /) -> Any: ...
28+
class _SupportsPos(Protocol[_T_co]):
29+
def __pos__(self) -> _T_co: ...
3130

3231
@type_check_only
33-
class _SupportsDunderGE(Protocol):
34-
def __ge__(self, other: Any, /) -> Any: ...
32+
class _SupportsAnd(Protocol[_T_contra, _T_co]):
33+
def __and__(self, other: _T_contra) -> _T_co: ...
3534

36-
_SupportsComparison: TypeAlias = _SupportsDunderLE | _SupportsDunderGE | _SupportsDunderGT | _SupportsDunderLT
35+
@type_check_only
36+
class _SupportsRAnd(Protocol[_T_contra, _T_co]):
37+
def __rand__(self, other: _T_contra) -> _T_co: ...
3738

3839
@type_check_only
39-
class _SupportsInversion(Protocol[_T_co]):
40-
def __invert__(self) -> _T_co: ...
40+
class _SupportsLShift(Protocol[_T_contra, _T_co]):
41+
def __lshift__(self, other: _T_contra) -> _T_co: ...
4142

4243
@type_check_only
43-
class _SupportsNeg(Protocol[_T_co]):
44-
def __neg__(self) -> _T_co: ...
44+
class _SupportsRLShift(Protocol[_T_contra, _T_co]):
45+
def __rlshift__(self, other: _T_contra) -> _T_co: ...
4546

4647
@type_check_only
47-
class _SupportsPos(Protocol[_T_co]):
48-
def __pos__(self) -> _T_co: ...
48+
class _SupportsRShift(Protocol[_T_contra, _T_co]):
49+
def __rshift__(self, other: _T_contra) -> _T_co: ...
50+
51+
@type_check_only
52+
class _SupportsRRShift(Protocol[_T_contra, _T_co]):
53+
def __rrshift__(self, other: _T_contra) -> _T_co: ...
54+
55+
@type_check_only
56+
class _SupportsTrueDiv(Protocol[_T_contra, _T_co]):
57+
def __truediv__(self, other: _T_contra) -> _T_co: ...
58+
59+
@type_check_only
60+
class _SupportsRTrueDiv(Protocol[_T_contra, _T_co]):
61+
def __rtruediv__(self, other: _T_contra) -> _T_co: ...
62+
63+
@type_check_only
64+
class _SupportsFloorDiv(Protocol[_T_contra, _T_co]):
65+
def __floordiv__(self, other: _T_contra) -> _T_co: ...
66+
67+
@type_check_only
68+
class _SupportsRFloorDiv(Protocol[_T_contra, _T_co]):
69+
def __rfloordiv__(self, other: _T_contra) -> _T_co: ...
70+
71+
@type_check_only
72+
class _SupportsMod(Protocol[_T_contra, _T_co]):
73+
def __mod__(self, other: _T_contra) -> _T_co: ...
74+
75+
@type_check_only
76+
class _SupportsRMod(Protocol[_T_contra, _T_co]):
77+
def __rmod__(self, other: _T_contra) -> _T_co: ...
78+
79+
@type_check_only
80+
class _SupportsMatMul(Protocol[_T_contra, _T_co]):
81+
def __matmul__(self, other: _T_contra) -> _T_co: ...
82+
83+
@type_check_only
84+
class _SupportsRMatMul(Protocol[_T_contra, _T_co]):
85+
def __rmatmul__(self, other: _T_contra) -> _T_co: ...
86+
87+
@type_check_only
88+
class _SupportsOr(Protocol[_T_contra, _T_co]):
89+
def __or__(self, other: _T_contra) -> _T_co: ...
90+
91+
@type_check_only
92+
class _SupportsROr(Protocol[_T_contra, _T_co]):
93+
def __ror__(self, other: _T_contra) -> _T_co: ...
94+
95+
@type_check_only
96+
class _SupportsPow(Protocol[_T_contra, _T_co]):
97+
def __pow__(self, other: _T_contra) -> _T_co: ...
98+
99+
@type_check_only
100+
class _SupportsRPow(Protocol[_T_contra, _T_co]):
101+
def __rpow__(self, other: _T_contra) -> _T_co: ...
102+
103+
@type_check_only
104+
class _SupportsXOr(Protocol[_T_contra, _T_co]):
105+
def __xor__(self, other: _T_contra) -> _T_co: ...
106+
107+
@type_check_only
108+
class _SupportsRXOr(Protocol[_T_contra, _T_co]):
109+
def __rxor__(self, other: _T_contra) -> _T_co: ...
110+
111+
@type_check_only
112+
class _SupportsIAdd(Protocol[_T_contra, _T_co]):
113+
def __iadd__(self, other: _T_contra) -> _T_co: ...
114+
115+
@type_check_only
116+
class _SupportsIAnd(Protocol[_T_contra, _T_co]):
117+
def __iand__(self, other: _T_contra) -> _T_co: ...
118+
119+
@type_check_only
120+
class _SupportsILShift(Protocol[_T_contra, _T_co]):
121+
def __ilshift__(self, other: _T_contra) -> _T_co: ...
122+
123+
@type_check_only
124+
class _SupportsIMod(Protocol[_T_contra, _T_co]):
125+
def __imod__(self, other: _T_contra) -> _T_co: ...
126+
127+
@type_check_only
128+
class _SupportsIMul(Protocol[_T_contra, _T_co]):
129+
def __imul__(self, other: _T_contra) -> _T_co: ...
130+
131+
@type_check_only
132+
class _SupportsIMatMul(Protocol[_T_contra, _T_co]):
133+
def __imatmul__(self, other: _T_contra) -> _T_co: ...
134+
135+
@type_check_only
136+
class _SupportsIOr(Protocol[_T_contra, _T_co]):
137+
def __ior__(self, other: _T_contra) -> _T_co: ...
138+
139+
@type_check_only
140+
class _SupportsIPow(Protocol[_T_contra, _T_co]):
141+
def __ipow__(self, other: _T_contra) -> _T_co: ...
142+
143+
@type_check_only
144+
class _SupportsIRShift(Protocol[_T_contra, _T_co]):
145+
def __irshift__(self, other: _T_contra) -> _T_co: ...
146+
147+
@type_check_only
148+
class _SupportsISub(Protocol[_T_contra, _T_co]):
149+
def __isub__(self, other: _T_contra) -> _T_co: ...
150+
151+
@type_check_only
152+
class _SupportsITrueDiv(Protocol[_T_contra, _T_co]):
153+
def __itruediv__(self, other: _T_contra) -> _T_co: ...
154+
155+
@type_check_only
156+
class _SupportsIXOr(Protocol[_T_contra, _T_co]):
157+
def __ixor__(self, other: _T_contra) -> _T_co: ...
158+
159+
@type_check_only
160+
class _SupportsIFloorDiv(Protocol[_T_contra, _T_co]):
161+
def __ifloordiv__(self, other: _T_contra) -> _T_co: ...
49162

50163
# All four comparison functions must have the same signature, or we get false-positive errors
51164
def lt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
@@ -54,32 +167,68 @@ def eq(a: object, b: object, /) -> Any: ...
54167
def ne(a: object, b: object, /) -> Any: ...
55168
def ge(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
56169
def gt(a: _SupportsComparison, b: _SupportsComparison, /) -> Any: ...
57-
def not_(a: object, /) -> bool: ...
58-
def truth(a: object, /) -> bool: ...
170+
def not_(a: SupportsBool, /) -> bool: ...
171+
def truth(a: SupportsBool, /) -> bool: ...
59172
def is_(a: object, b: object, /) -> bool: ...
60173
def is_not(a: object, b: object, /) -> bool: ...
61174
def abs(a: SupportsAbs[_T], /) -> _T: ...
62-
def add(a, b, /): ...
63-
def and_(a, b, /): ...
64-
def floordiv(a, b, /): ...
175+
@overload
176+
def add(a: SupportsAdd[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
177+
@overload
178+
def add(a: _T_contra, b: SupportsRAdd[_T_contra, _T_co], /) -> _T_co: ...
179+
@overload
180+
def and_(a: _SupportsAnd[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
181+
@overload
182+
def and_(a: _T_contra, b: _SupportsRAnd[_T_contra, _T_co], /) -> _T_co: ...
183+
@overload
184+
def floordiv(a: _SupportsFloorDiv[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
185+
@overload
186+
def floordiv(a: _T_contra, b: _SupportsRFloorDiv[_T_contra, _T_co], /) -> _T_co: ...
65187
def index(a: SupportsIndex, /) -> int: ...
66188
def inv(a: _SupportsInversion[_T_co], /) -> _T_co: ...
67189
def invert(a: _SupportsInversion[_T_co], /) -> _T_co: ...
68-
def lshift(a, b, /): ...
69-
def mod(a, b, /): ...
190+
@overload
191+
def lshift(a: _SupportsLShift[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
192+
@overload
193+
def lshift(a: _T_contra, b: _SupportsRLShift[_T_contra, _T_co], /) -> _T_co: ...
194+
@overload
195+
def mod(a: _SupportsMod[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
196+
@overload
197+
def mod(a: _T_contra, b: _SupportsRMod[_T_contra, _T_co], /) -> _T_co: ...
70198
@overload
71199
def mul(a: SupportsMul[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
72200
@overload
73201
def mul(a: _T_contra, b: SupportsRMul[_T_contra, _T_co], /) -> _T_co: ...
74-
def matmul(a, b, /): ...
202+
@overload
203+
def matmul(a: _SupportsMatMul[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
204+
@overload
205+
def matmul(a: _T_contra, b: _SupportsRMatMul[_T_contra, _T_co], /) -> _T_co: ...
75206
def neg(a: _SupportsNeg[_T_co], /) -> _T_co: ...
76-
def or_(a, b, /): ...
207+
@overload
208+
def or_(a: _SupportsOr[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
209+
@overload
210+
def or_(a: _T_contra, b: _SupportsROr[_T_contra, _T_co], /) -> _T_co: ...
77211
def pos(a: _SupportsPos[_T_co], /) -> _T_co: ...
78-
def pow(a, b, /): ...
79-
def rshift(a, b, /): ...
80-
def sub(a, b, /): ...
81-
def truediv(a, b, /): ...
82-
def xor(a, b, /): ...
212+
@overload
213+
def pow(a: _SupportsPow[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
214+
@overload
215+
def pow(a: _T_contra, b: _SupportsRPow[_T_contra, _T_co], /) -> _T_co: ...
216+
@overload
217+
def rshift(a: _SupportsRShift[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
218+
@overload
219+
def rshift(a: _T_contra, b: _SupportsRRShift[_T_contra, _T_co], /) -> _T_co: ...
220+
@overload
221+
def sub(a: SupportsSub[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
222+
@overload
223+
def sub(a: _T_contra, b: SupportsRSub[_T_contra, _T_co], /) -> _T_co: ...
224+
@overload
225+
def truediv(a: _SupportsTrueDiv[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
226+
@overload
227+
def truediv(a: _T_contra, b: _SupportsRTrueDiv[_T_contra, _T_co], /) -> _T_co: ...
228+
@overload
229+
def xor(a: _SupportsXOr[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
230+
@overload
231+
def xor(a: _T_contra, b: _SupportsRXOr[_T_contra, _T_co], /) -> _T_co: ...
83232
def concat(a: Sequence[_T], b: Sequence[_T], /) -> Sequence[_T]: ...
84233
def contains(a: Container[object], b: object, /) -> bool: ...
85234
def countOf(a: Iterable[object], b: object, /) -> int: ...
@@ -101,20 +250,20 @@ def setitem(a: MutableSequence[_T], b: slice[int | None], c: Sequence[_T], /) ->
101250
@overload
102251
def setitem(a: MutableMapping[_K, _V], b: _K, c: _V, /) -> None: ...
103252
def length_hint(obj: object, default: int = 0, /) -> int: ...
104-
def iadd(a, b, /): ...
105-
def iand(a, b, /): ...
106-
def iconcat(a, b, /): ...
107-
def ifloordiv(a, b, /): ...
108-
def ilshift(a, b, /): ...
109-
def imod(a, b, /): ...
110-
def imul(a, b, /): ...
111-
def imatmul(a, b, /): ...
112-
def ior(a, b, /): ...
113-
def ipow(a, b, /): ...
114-
def irshift(a, b, /): ...
115-
def isub(a, b, /): ...
116-
def itruediv(a, b, /): ...
117-
def ixor(a, b, /): ...
253+
def iadd(a: _SupportsIAdd[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
254+
def iand(a: _SupportsIAnd[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
255+
def iconcat(a: MutableSequence[_T], b: Sequence[_T], /) -> MutableSequence[_T]: ...
256+
def ifloordiv(a: _SupportsIFloorDiv[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
257+
def ilshift(a: _SupportsILShift[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
258+
def imod(a: _SupportsIMod[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
259+
def imul(a: _SupportsIMul[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
260+
def imatmul(a: _SupportsIMatMul[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
261+
def ior(a: _SupportsIOr[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
262+
def ipow(a: _SupportsIPow[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
263+
def irshift(a: _SupportsIRShift[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
264+
def isub(a: _SupportsISub[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
265+
def itruediv(a: _SupportsITrueDiv[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
266+
def ixor(a: _SupportsIXOr[_T_contra, _T_co], b: _T_contra, /) -> _T_co: ...
118267

119268
if sys.version_info >= (3, 11):
120269
def call(obj: Callable[_P, _R], /, *args: _P.args, **kwargs: _P.kwargs) -> _R: ...

0 commit comments

Comments
 (0)