Skip to content

Commit 03fe755

Browse files
authored
move _operator classes to operator (#12745)
1 parent 055d043 commit 03fe755

2 files changed

Lines changed: 106 additions & 40 deletions

File tree

stdlib/_operator.pyi

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import sys
22
from _typeshed import SupportsGetItem
33
from collections.abc import Callable, Container, Iterable, MutableMapping, MutableSequence, Sequence
4-
from typing import Any, AnyStr, Generic, Protocol, SupportsAbs, SupportsIndex, TypeVar, final, overload
5-
from typing_extensions import ParamSpec, TypeAlias, TypeIs, TypeVarTuple, Unpack
4+
from operator import attrgetter as attrgetter, itemgetter as itemgetter, methodcaller as methodcaller
5+
from typing import Any, AnyStr, Protocol, SupportsAbs, SupportsIndex, TypeVar, overload
6+
from typing_extensions import ParamSpec, TypeAlias, TypeIs
67

78
_R = TypeVar("_R")
89
_T = TypeVar("_T")
910
_T_co = TypeVar("_T_co", covariant=True)
10-
_T1 = TypeVar("_T1")
11-
_T2 = TypeVar("_T2")
1211
_K = TypeVar("_K")
1312
_V = TypeVar("_V")
1413
_P = ParamSpec("_P")
15-
_Ts = TypeVarTuple("_Ts")
1614

1715
# The following protocols return "Any" instead of bool, since the comparison
1816
# operators can be overloaded to return an arbitrary object. For example,
@@ -92,40 +90,6 @@ def setitem(a: MutableSequence[_T], b: slice, c: Sequence[_T], /) -> None: ...
9290
@overload
9391
def setitem(a: MutableMapping[_K, _V], b: _K, c: _V, /) -> None: ...
9492
def length_hint(obj: object, default: int = 0, /) -> int: ...
95-
@final
96-
class attrgetter(Generic[_T_co]):
97-
@overload
98-
def __new__(cls, attr: str, /) -> attrgetter[Any]: ...
99-
@overload
100-
def __new__(cls, attr: str, attr2: str, /) -> attrgetter[tuple[Any, Any]]: ...
101-
@overload
102-
def __new__(cls, attr: str, attr2: str, attr3: str, /) -> attrgetter[tuple[Any, Any, Any]]: ...
103-
@overload
104-
def __new__(cls, attr: str, attr2: str, attr3: str, attr4: str, /) -> attrgetter[tuple[Any, Any, Any, Any]]: ...
105-
@overload
106-
def __new__(cls, attr: str, /, *attrs: str) -> attrgetter[tuple[Any, ...]]: ...
107-
def __call__(self, obj: Any, /) -> _T_co: ...
108-
109-
@final
110-
class itemgetter(Generic[_T_co]):
111-
@overload
112-
def __new__(cls, item: _T, /) -> itemgetter[_T]: ...
113-
@overload
114-
def __new__(cls, item1: _T1, item2: _T2, /, *items: Unpack[_Ts]) -> itemgetter[tuple[_T1, _T2, Unpack[_Ts]]]: ...
115-
# __key: _KT_contra in SupportsGetItem seems to be causing variance issues, ie:
116-
# TypeVar "_KT_contra@SupportsGetItem" is contravariant
117-
# "tuple[int, int]" is incompatible with protocol "SupportsIndex"
118-
# preventing [_T_co, ...] instead of [Any, ...]
119-
#
120-
# A suspected mypy issue prevents using [..., _T] instead of [..., Any] here.
121-
# https://github.com/python/mypy/issues/14032
122-
def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ...
123-
124-
@final
125-
class methodcaller:
126-
def __init__(self, name: str, /, *args: Any, **kwargs: Any) -> None: ...
127-
def __call__(self, obj: Any) -> Any: ...
128-
12993
def iadd(a: Any, b: Any, /) -> Any: ...
13094
def iand(a: Any, b: Any, /) -> Any: ...
13195
def iconcat(a: Any, b: Any, /) -> Any: ...

stdlib/operator.pyi

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,66 @@
11
import sys
2-
from _operator import *
2+
from _operator import (
3+
abs as abs,
4+
add as add,
5+
and_ as and_,
6+
concat as concat,
7+
contains as contains,
8+
countOf as countOf,
9+
delitem as delitem,
10+
eq as eq,
11+
floordiv as floordiv,
12+
ge as ge,
13+
getitem as getitem,
14+
gt as gt,
15+
iadd as iadd,
16+
iand as iand,
17+
iconcat as iconcat,
18+
ifloordiv as ifloordiv,
19+
ilshift as ilshift,
20+
imatmul as imatmul,
21+
imod as imod,
22+
imul as imul,
23+
index as index,
24+
indexOf as indexOf,
25+
inv as inv,
26+
invert as invert,
27+
ior as ior,
28+
ipow as ipow,
29+
irshift as irshift,
30+
is_ as is_,
31+
is_not as is_not,
32+
isub as isub,
33+
itruediv as itruediv,
34+
ixor as ixor,
35+
le as le,
36+
length_hint as length_hint,
37+
lshift as lshift,
38+
lt as lt,
39+
matmul as matmul,
40+
mod as mod,
41+
mul as mul,
42+
ne as ne,
43+
neg as neg,
44+
not_ as not_,
45+
or_ as or_,
46+
pos as pos,
47+
pow as pow,
48+
rshift as rshift,
49+
setitem as setitem,
50+
sub as sub,
51+
truediv as truediv,
52+
truth as truth,
53+
xor as xor,
54+
)
55+
from _typeshed import SupportsGetItem
56+
from typing import Any, Generic, TypeVar, final, overload
57+
from typing_extensions import TypeVarTuple, Unpack
58+
59+
_T = TypeVar("_T")
60+
_T_co = TypeVar("_T_co", covariant=True)
61+
_T1 = TypeVar("_T1")
62+
_T2 = TypeVar("_T2")
63+
_Ts = TypeVarTuple("_Ts")
364

465
__all__ = [
566
"abs",
@@ -59,9 +120,13 @@ __all__ = [
59120
]
60121

61122
if sys.version_info >= (3, 11):
123+
from _operator import call as call
124+
62125
__all__ += ["call"]
63126

64127
if sys.version_info >= (3, 14):
128+
from _operator import is_none as is_none, is_not_none as is_not_none
129+
65130
__all__ += ["is_none", "is_not_none"]
66131

67132
__lt__ = lt
@@ -111,3 +176,40 @@ __itruediv__ = itruediv
111176
__ixor__ = ixor
112177
if sys.version_info >= (3, 11):
113178
__call__ = call
179+
180+
# At runtime, these classes are implemented in C as part of the _operator module
181+
# However, they consider themselves to live in the operator module, so we'll put
182+
# them here.
183+
@final
184+
class attrgetter(Generic[_T_co]):
185+
@overload
186+
def __new__(cls, attr: str, /) -> attrgetter[Any]: ...
187+
@overload
188+
def __new__(cls, attr: str, attr2: str, /) -> attrgetter[tuple[Any, Any]]: ...
189+
@overload
190+
def __new__(cls, attr: str, attr2: str, attr3: str, /) -> attrgetter[tuple[Any, Any, Any]]: ...
191+
@overload
192+
def __new__(cls, attr: str, attr2: str, attr3: str, attr4: str, /) -> attrgetter[tuple[Any, Any, Any, Any]]: ...
193+
@overload
194+
def __new__(cls, attr: str, /, *attrs: str) -> attrgetter[tuple[Any, ...]]: ...
195+
def __call__(self, obj: Any, /) -> _T_co: ...
196+
197+
@final
198+
class itemgetter(Generic[_T_co]):
199+
@overload
200+
def __new__(cls, item: _T, /) -> itemgetter[_T]: ...
201+
@overload
202+
def __new__(cls, item1: _T1, item2: _T2, /, *items: Unpack[_Ts]) -> itemgetter[tuple[_T1, _T2, Unpack[_Ts]]]: ...
203+
# __key: _KT_contra in SupportsGetItem seems to be causing variance issues, ie:
204+
# TypeVar "_KT_contra@SupportsGetItem" is contravariant
205+
# "tuple[int, int]" is incompatible with protocol "SupportsIndex"
206+
# preventing [_T_co, ...] instead of [Any, ...]
207+
#
208+
# A suspected mypy issue prevents using [..., _T] instead of [..., Any] here.
209+
# https://github.com/python/mypy/issues/14032
210+
def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ...
211+
212+
@final
213+
class methodcaller:
214+
def __init__(self, name: str, /, *args: Any, **kwargs: Any) -> None: ...
215+
def __call__(self, obj: Any) -> Any: ...

0 commit comments

Comments
 (0)