Skip to content

Commit ce9ad31

Browse files
committed
Update comments for itemgetter.__call__
1 parent c2cb0b6 commit ce9ad31

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

stdlib/operator.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,21 @@ class attrgetter(Generic[_T_co]):
195195
def __call__(self, obj: Any, /) -> _T_co: ...
196196

197197
@final
198-
class itemgetter(Generic[_T_co]):
198+
class itemgetter(Generic[_T]):
199199
@overload
200200
def __new__(cls, item: _T, /) -> itemgetter[_T]: ...
201201
@overload
202202
def __new__(cls, item1: _T1, item2: _T2, /, *items: Unpack[_Ts]) -> itemgetter[tuple[_T1, _T2, Unpack[_Ts]]]: ...
203203
# __key: _KT_contra in SupportsGetItem seems to be causing variance issues, ie:
204204
# TypeVar "_KT_contra@SupportsGetItem" is contravariant
205205
# "tuple[int, int]" is incompatible with protocol "SupportsIndex"
206-
# preventing [_T_co, _T] instead of [Any, _T]
207-
def __call__(self, obj: SupportsGetItem[Any, _T]) -> _T: ...
206+
# preventing [_T_co, ...] instead of [Any, ...]
207+
#
208+
# If we can't infer a literal key from __new__ (ie: `itemgetter[Literal[0]]` for `itemgetter(0)`),
209+
# then we can't annotate __call__'s return type or it'll break on tuples
210+
#
211+
# These issues are best demonstrated by the `itertools.check_itertools_recipes.unique_justseen` test.
212+
def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ...
208213

209214
@final
210215
class methodcaller:

0 commit comments

Comments
 (0)