Skip to content

Commit 8f45c69

Browse files
committed
[operator] Replace or explain Any
1 parent 8bf7900 commit 8f45c69

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

stdlib/operator.pyi

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ from _operator import (
5353
xor as xor,
5454
)
5555
from _typeshed import SupportsGetItem
56-
from typing import Any, Generic, TypeVar, final, overload
56+
from typing import Any, Generic, ParamSpec, TypeVar, final, overload
5757
from typing_extensions import Self, TypeVarTuple, Unpack
5858

5959
_T = TypeVar("_T")
6060
_T_co = TypeVar("_T_co", covariant=True)
6161
_T1 = TypeVar("_T1")
6262
_T2 = TypeVar("_T2")
6363
_Ts = TypeVarTuple("_Ts")
64+
_P = ParamSpec("_P", default=...)
65+
_R = TypeVar("_R", default=Any)
6466

6567
__all__ = [
6668
"abs",
@@ -182,6 +184,8 @@ if sys.version_info >= (3, 11):
182184
# them here.
183185
@final
184186
class attrgetter(Generic[_T_co]):
187+
# We can't determine the type of the attribute(s) being accessed statucally,
188+
# so we have to use Any for the return type.
185189
@overload
186190
def __new__(cls, attr: str, /) -> attrgetter[Any]: ...
187191
@overload
@@ -192,6 +196,8 @@ class attrgetter(Generic[_T_co]):
192196
def __new__(cls, attr: str, attr2: str, attr3: str, attr4: str, /) -> attrgetter[tuple[Any, Any, Any, Any]]: ...
193197
@overload
194198
def __new__(cls, attr: str, /, *attrs: str) -> attrgetter[tuple[Any, ...]]: ...
199+
# obj needs to have the named attribute(s) with the correct type.
200+
# Unfortunately, we can't check that statically, so we need to use Any.
195201
def __call__(self, obj: Any, /) -> _T_co: ...
196202

197203
@final
@@ -212,6 +218,8 @@ class itemgetter(Generic[_T_co]):
212218
def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ...
213219

214220
@final
215-
class methodcaller:
216-
def __new__(cls, name: str, /, *args: Any, **kwargs: Any) -> Self: ...
217-
def __call__(self, obj: Any) -> Any: ...
221+
class methodcaller(Generic[_P, _R]):
222+
def __new__(cls, name: str, /, *args: _P.args, **kwargs: _P.kwargs) -> Self: ...
223+
# obj needs to have the named method with the correct signature.
224+
# Unfortunately, we can't check that statically, so we need to use Any.
225+
def __call__(self, obj: Any) -> _R: ...

0 commit comments

Comments
 (0)