Skip to content

Commit be55326

Browse files
hamdanalsrittauAlexWaygood
authored
Replace overload hack in operator.itemgetter with PEP 646 Unpack (#11136)
Co-authored-by: Sebastian Rittau <srittau@rittau.biz> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 6ae413f commit be55326

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

stdlib/_operator.pyi

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import sys
22
from _typeshed import SupportsGetItem
33
from collections.abc import Callable, Container, Iterable, MutableMapping, MutableSequence, Sequence
44
from typing import Any, AnyStr, Generic, Protocol, SupportsAbs, TypeVar, overload
5-
from typing_extensions import ParamSpec, SupportsIndex, TypeAlias, final
5+
from typing_extensions import ParamSpec, SupportsIndex, TypeAlias, TypeVarTuple, Unpack, final
66

77
_R = TypeVar("_R")
88
_T = TypeVar("_T")
99
_T_co = TypeVar("_T_co", covariant=True)
10+
_T1 = TypeVar("_T1")
11+
_T2 = TypeVar("_T2")
1012
_K = TypeVar("_K")
1113
_V = TypeVar("_V")
1214
_P = ParamSpec("_P")
15+
_Ts = TypeVarTuple("_Ts")
1316

1417
# The following protocols return "Any" instead of bool, since the comparison
1518
# operators can be overloaded to return an arbitrary object. For example,
@@ -105,22 +108,10 @@ class attrgetter(Generic[_T_co]):
105108

106109
@final
107110
class itemgetter(Generic[_T_co]):
108-
# mypy lacks support for PEP 646 https://github.com/python/mypy/issues/12280
109-
# So we have to define all of these overloads to simulate unpacking the arguments
110111
@overload
111-
def __new__(cls, item: _T_co) -> itemgetter[_T_co]: ...
112+
def __new__(cls, __item: _T) -> itemgetter[_T]: ...
112113
@overload
113-
def __new__(cls, item: _T_co, __item2: _T_co) -> itemgetter[tuple[_T_co, _T_co]]: ...
114-
@overload
115-
def __new__(cls, item: _T_co, __item2: _T_co, __item3: _T_co) -> itemgetter[tuple[_T_co, _T_co, _T_co]]: ...
116-
@overload
117-
def __new__(
118-
cls, item: _T_co, __item2: _T_co, __item3: _T_co, __item4: _T_co
119-
) -> itemgetter[tuple[_T_co, _T_co, _T_co, _T_co]]: ...
120-
@overload
121-
def __new__(
122-
cls, item: _T_co, __item2: _T_co, __item3: _T_co, __item4: _T_co, *items: _T_co
123-
) -> itemgetter[tuple[_T_co, ...]]: ...
114+
def __new__(cls, __item1: _T1, __item2: _T2, *items: Unpack[_Ts]) -> itemgetter[tuple[_T1, _T2, Unpack[_Ts]]]: ...
124115
# __key: _KT_contra in SupportsGetItem seems to be causing variance issues, ie:
125116
# TypeVar "_KT_contra@SupportsGetItem" is contravariant
126117
# "tuple[int, int]" is incompatible with protocol "SupportsIndex"

0 commit comments

Comments
 (0)