Skip to content

Commit 67b69c5

Browse files
committed
[types] Make MethodType generic
1 parent f76037a commit 67b69c5

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

stdlib/types.pyi

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from collections.abc import (
1717
ValuesView,
1818
)
1919
from importlib.machinery import ModuleSpec
20-
from typing import Any, ClassVar, Literal, ParamSpec, TypeVar, final, overload
20+
from typing import Any, ClassVar, Generic, Literal, ParamSpec, TypeVar, final, overload
2121
from typing_extensions import Self, TypeAliasType, TypeVarTuple, deprecated, disjoint_base
2222

2323
if sys.version_info >= (3, 14):
@@ -70,8 +70,13 @@ if sys.version_info >= (3, 15):
7070

7171
_T1 = TypeVar("_T1")
7272
_T2 = TypeVar("_T2")
73+
_P = ParamSpec("_P")
74+
_P_default = ParamSpec("_P_default", default=...)
75+
_R = TypeVar("_R")
76+
_R_default = TypeVar("_R_default", default=Any)
7377
_KT_co = TypeVar("_KT_co", covariant=True)
7478
_VT_co = TypeVar("_VT_co", covariant=True)
79+
_Fn = TypeVar("_Fn", bound=Callable[..., object])
7580

7681
# Make sure this class definition stays roughly in line with `builtins.function`
7782
@final
@@ -461,7 +466,7 @@ class CoroutineType(Coroutine[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co]):
461466
def __class_getitem__(cls, item: Any, /) -> Any: ...
462467

463468
@final
464-
class MethodType:
469+
class MethodType(Generic[_P_default, _R_default]):
465470
@property
466471
def __closure__(self) -> tuple[CellType, ...] | None: ... # inherited from the added function
467472
@property
@@ -476,8 +481,8 @@ class MethodType:
476481
def __name__(self) -> str: ... # inherited from the added function
477482
@property
478483
def __qualname__(self) -> str: ... # inherited from the added function
479-
def __new__(cls, func: Callable[..., Any], instance: object, /) -> Self: ...
480-
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
484+
def __new__(cls, func: Callable[_P_default, _R_default], instance: object, /) -> Self: ...
485+
def __call__(self, *args: _P_default.args, **kwargs: _P_default.kwargs) -> _R_default: ...
481486

482487
if sys.version_info >= (3, 13):
483488
def __get__(self, instance: object, owner: type | None = None, /) -> Self: ...
@@ -674,10 +679,6 @@ class DynamicClassAttribute(property):
674679
def setter(self, fset: Callable[[Any, Any], object]) -> DynamicClassAttribute: ...
675680
def deleter(self, fdel: Callable[[Any], object]) -> DynamicClassAttribute: ...
676681

677-
_Fn = TypeVar("_Fn", bound=Callable[..., object])
678-
_R = TypeVar("_R")
679-
_P = ParamSpec("_P")
680-
681682
# it's not really an Awaitable, but can be used in an await expression. Real type: Generator & Awaitable
682683
@overload
683684
def coroutine(func: Callable[_P, Generator[Any, Any, _R]]) -> Callable[_P, Awaitable[_R]]: ...

0 commit comments

Comments
 (0)