From 78a4a93a80d9e0faf0b98e1fc0c1302b28e9ffef Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 20 Feb 2023 15:31:19 +0000 Subject: [PATCH 1/2] Use `ParamSpec` for `classmethod` and `staticmethod` --- stdlib/abc.pyi | 11 ++++++----- stdlib/builtins.pyi | 25 +++++++++++++------------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/stdlib/abc.pyi b/stdlib/abc.pyi index 068dab4752be..2b76b7b0bf19 100644 --- a/stdlib/abc.pyi +++ b/stdlib/abc.pyi @@ -3,11 +3,12 @@ import sys from _typeshed import SupportsWrite from collections.abc import Callable from typing import Any, Generic, TypeVar -from typing_extensions import Literal +from typing_extensions import Concatenate, Literal, ParamSpec _T = TypeVar("_T") _R_co = TypeVar("_R_co", covariant=True) _FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) +_P = ParamSpec("_P") # These definitions have special processing in mypy class ABCMeta(type): @@ -28,13 +29,13 @@ class ABCMeta(type): def abstractmethod(funcobj: _FuncT) -> _FuncT: ... -class abstractclassmethod(classmethod[_R_co], Generic[_R_co]): +class abstractclassmethod(classmethod[_P, _R_co, _T], Generic[_P, _R_co, _T]): __isabstractmethod__: Literal[True] - def __init__(self: abstractclassmethod[_R_co], callable: Callable[..., _R_co]) -> None: ... + def __init__(self, callable: Callable[Concatenate[_T, _P], _R_co]) -> None: ... -class abstractstaticmethod(staticmethod[_R_co], Generic[_R_co]): +class abstractstaticmethod(staticmethod[_P, _R_co], Generic[_P, _R_co]): __isabstractmethod__: Literal[True] - def __init__(self, callable: Callable[..., _R_co]) -> None: ... + def __init__(self, callable: Callable[_P, _R_co]) -> None: ... class abstractproperty(property): __isabstractmethod__: Literal[True] diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 63cf288e2d32..6f73eccfde8c 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -54,7 +54,7 @@ from typing import ( # noqa: Y022 overload, type_check_only, ) -from typing_extensions import Literal, LiteralString, Self, SupportsIndex, TypeAlias, TypeGuard, final +from typing_extensions import Concatenate, Literal, LiteralString, ParamSpec, Self, SupportsIndex, TypeAlias, TypeGuard, final if sys.version_info >= (3, 9): from types import GenericAlias @@ -75,6 +75,7 @@ _SupportsNextT = TypeVar("_SupportsNextT", bound=SupportsNext[Any], covariant=Tr _SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant=True) _AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any]) _AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True) +_P = ParamSpec("_P") class object: __doc__: str | None @@ -111,32 +112,32 @@ class object: def __dir__(self) -> Iterable[str]: ... def __init_subclass__(cls) -> None: ... -class staticmethod(Generic[_R_co]): +class staticmethod(Generic[_P, _R_co]): @property - def __func__(self) -> Callable[..., _R_co]: ... + def __func__(self) -> Callable[_P, _R_co]: ... @property def __isabstractmethod__(self) -> bool: ... - def __init__(self: staticmethod[_R_co], __f: Callable[..., _R_co]) -> None: ... - def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[..., _R_co]: ... + def __init__(self, __f: Callable[_P, _R_co]) -> None: ... + def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[_P, _R_co]: ... if sys.version_info >= (3, 10): __name__: str __qualname__: str @property - def __wrapped__(self) -> Callable[..., _R_co]: ... - def __call__(self, *args: Any, **kwargs: Any) -> _R_co: ... + def __wrapped__(self) -> Callable[_P, _R_co]: ... + def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R_co: ... -class classmethod(Generic[_R_co]): +class classmethod(Generic[_P, _R_co, _T]): @property - def __func__(self) -> Callable[..., _R_co]: ... + def __func__(self) -> Callable[Concatenate[_T, _P], _R_co]: ... @property def __isabstractmethod__(self) -> bool: ... - def __init__(self: classmethod[_R_co], __f: Callable[..., _R_co]) -> None: ... - def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[..., _R_co]: ... + def __init__(self, __f: Callable[Concatenate[_T, _P], _R_co]) -> None: ... + def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[_P, _R_co]: ... if sys.version_info >= (3, 10): __name__: str __qualname__: str @property - def __wrapped__(self) -> Callable[..., _R_co]: ... + def __wrapped__(self) -> Callable[Concatenate[_T, _P], _R_co]: ... class type: @property From 10652ec8dca6c29558ea724f42bdd8ffb7744a23 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 21 Feb 2023 08:57:41 +0000 Subject: [PATCH 2/2] Reorder the type variables --- stdlib/abc.pyi | 6 +++--- stdlib/builtins.pyi | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/abc.pyi b/stdlib/abc.pyi index 2b76b7b0bf19..ec04d8f85d12 100644 --- a/stdlib/abc.pyi +++ b/stdlib/abc.pyi @@ -2,7 +2,7 @@ import _typeshed import sys from _typeshed import SupportsWrite from collections.abc import Callable -from typing import Any, Generic, TypeVar +from typing import Any, TypeVar from typing_extensions import Concatenate, Literal, ParamSpec _T = TypeVar("_T") @@ -29,11 +29,11 @@ class ABCMeta(type): def abstractmethod(funcobj: _FuncT) -> _FuncT: ... -class abstractclassmethod(classmethod[_P, _R_co, _T], Generic[_P, _R_co, _T]): +class abstractclassmethod(classmethod[_T, _P, _R_co]): __isabstractmethod__: Literal[True] def __init__(self, callable: Callable[Concatenate[_T, _P], _R_co]) -> None: ... -class abstractstaticmethod(staticmethod[_P, _R_co], Generic[_P, _R_co]): +class abstractstaticmethod(staticmethod[_P, _R_co]): __isabstractmethod__: Literal[True] def __init__(self, callable: Callable[_P, _R_co]) -> None: ... diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 6f73eccfde8c..fb4802d86b2e 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -126,7 +126,7 @@ class staticmethod(Generic[_P, _R_co]): def __wrapped__(self) -> Callable[_P, _R_co]: ... def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R_co: ... -class classmethod(Generic[_P, _R_co, _T]): +class classmethod(Generic[_T, _P, _R_co]): @property def __func__(self) -> Callable[Concatenate[_T, _P], _R_co]: ... @property