11import sys
2- from collections .abc import Callable , Container , Iterable , Mapping , MutableMapping , MutableSequence , Sequence
2+ from _typeshed import SupportsGetItem
3+ from collections .abc import Callable , Container , Iterable , MutableMapping , MutableSequence , Sequence
34from typing import Any , AnyStr , Generic , Protocol , SupportsAbs , TypeVar , overload
45from typing_extensions import ParamSpec , SupportsIndex , TypeAlias , final
56
@@ -77,11 +78,9 @@ def delitem(__a: MutableSequence[Any], __b: slice) -> None: ...
7778@overload
7879def delitem (__a : MutableMapping [_K , Any ], __b : _K ) -> None : ...
7980@overload
80- def getitem (__a : Sequence [_T ], __b : SupportsIndex ) -> _T : ...
81- @overload
8281def getitem (__a : Sequence [_T ], __b : slice ) -> Sequence [_T ]: ...
8382@overload
84- def getitem (__a : Mapping [_K , _V ], __b : _K ) -> _V : ...
83+ def getitem (__a : SupportsGetItem [_K , _V ], __b : _K ) -> _V : ...
8584def indexOf (__a : Iterable [_T ], __b : _T ) -> int : ...
8685@overload
8786def setitem (__a : MutableSequence [_T ], __b : SupportsIndex , __c : _T ) -> None : ...
@@ -106,17 +105,30 @@ class attrgetter(Generic[_T_co]):
106105
107106@final
108107class 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
109110 @overload
110- def __new__ (cls , item : Any ) -> itemgetter [Any ]: ...
111+ def __new__ (cls , item : _T_co ) -> itemgetter [_T_co ]: ...
111112 @overload
112- def __new__ (cls , item : Any , __item2 : Any ) -> itemgetter [tuple [Any , Any ]]: ...
113+ def __new__ (cls , item : _T_co , __item2 : _T_co ) -> itemgetter [tuple [_T_co , _T_co ]]: ...
113114 @overload
114- def __new__ (cls , item : Any , __item2 : Any , __item3 : Any ) -> itemgetter [tuple [Any , Any , Any ]]: ...
115+ def __new__ (cls , item : _T_co , __item2 : _T_co , __item3 : _T_co ) -> itemgetter [tuple [_T_co , _T_co , _T_co ]]: ...
115116 @overload
116- def __new__ (cls , item : Any , __item2 : Any , __item3 : Any , __item4 : Any ) -> itemgetter [tuple [Any , Any , Any , Any ]]: ...
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 ]]: ...
117120 @overload
118- def __new__ (cls , item : Any , * items : Any ) -> itemgetter [tuple [Any , ...]]: ...
119- def __call__ (self , obj : Any ) -> _T_co : ...
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 , ...]]: ...
124+ # __key: _KT_contra in SupportsGetItem seems to be causing variance issues, ie:
125+ # TypeVar "_KT_contra@SupportsGetItem" is contravariant
126+ # "tuple[int, int]" is incompatible with protocol "SupportsIndex"
127+ # preventing [_T_co, ...] instead of [Any, ...]
128+ #
129+ # A suspected mypy issue prevents using [..., _T] instead of [..., Any] here.
130+ # https://github.com/python/mypy/issues/14032
131+ def __call__ (self , obj : SupportsGetItem [Any , Any ]) -> Any : ...
120132
121133@final
122134class methodcaller :
0 commit comments