From f03d008f89134d942b848fb628289a03eba0e65d Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 23 Jun 2021 13:03:40 +0200 Subject: [PATCH 01/11] Improve abc module and builtin function decorators * Make classmethod and staticmethod generic over the function. * abstractclassmethod and abstractstaticmethod are classes, not functions. * Remove mypy-specific comments. --- stdlib/abc.pyi | 14 ++++++-------- stdlib/builtins.pyi | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/stdlib/abc.pyi b/stdlib/abc.pyi index 5dd21763a9ce..d84797f1f8e1 100644 --- a/stdlib/abc.pyi +++ b/stdlib/abc.pyi @@ -1,19 +1,17 @@ -from typing import Any, Callable, Type, TypeVar +from typing import Any, Callable, ClassVar, Generic, Type, TypeVar +from typing_extensions import Literal _T = TypeVar("_T") _FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) -# These definitions have special processing in mypy -class ABCMeta(type): - def register(cls: ABCMeta, subclass: Type[_T]) -> Type[_T]: ... - def abstractmethod(funcobj: _FuncT) -> _FuncT: ... +class abstractclassmethod(classmethod[_FuncT], Generic[_FuncT]): ... +class abstractstaticmethod(staticmethod[_FuncT], Generic[_FuncT]): ... class abstractproperty(property): ... -# These two are deprecated and not supported by mypy -def abstractstaticmethod(callable: _FuncT) -> _FuncT: ... -def abstractclassmethod(callable: _FuncT) -> _FuncT: ... +class ABCMeta(type): + def register(cls: ABCMeta, subclass: Type[_T]) -> Type[_T]: ... class ABC(metaclass=ABCMeta): ... diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index de7194ed75c2..e77866910b28 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -16,6 +16,7 @@ from _typeshed import ( SupportsWrite, ) from ast import AST, mod +from collections.abc import Callable from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper from types import CodeType, TracebackType from typing import ( @@ -26,7 +27,6 @@ from typing import ( AsyncIterator, BinaryIO, ByteString, - Callable, Dict, FrozenSet, Generic, @@ -80,6 +80,7 @@ _T4 = TypeVar("_T4") _T5 = TypeVar("_T5") _TT = TypeVar("_TT", bound="type") _TBE = TypeVar("_TBE", bound="BaseException") +_FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) class object: __doc__: Optional[str] @@ -109,19 +110,19 @@ class object: def __dir__(self) -> Iterable[str]: ... def __init_subclass__(cls) -> None: ... -class staticmethod(object): # Special, only valid as a decorator. - __func__: Callable[..., Any] +class staticmethod(Generic[_FuncT]): # Special, only valid as a decorator. + __func__: _FuncT __isabstractmethod__: bool - def __init__(self, f: Callable[..., Any]) -> None: ... + def __init__(self, f: _FuncT) -> None: ... def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... - def __get__(self, obj: _T, type: Optional[Type[_T]] = ...) -> Callable[..., Any]: ... + def __get__(self, obj: _T, type: Type[_T] | None = ...) -> _FuncT: ... -class classmethod(object): # Special, only valid as a decorator. - __func__: Callable[..., Any] +class classmethod(Generic[_FuncT]): # Special, only valid as a decorator. + __func__: _FuncT __isabstractmethod__: bool - def __init__(self, f: Callable[..., Any]) -> None: ... + def __init__(self, f: _FuncT) -> None: ... def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... - def __get__(self, obj: _T, type: Optional[Type[_T]] = ...) -> Callable[..., Any]: ... + def __get__(self, obj: _T, type: Type[_T] | None = ...) -> _FuncT: ... class type(object): __base__: type From f40490b65042bf429daee43a09acc02680795a92 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 23 Jun 2021 13:18:25 +0200 Subject: [PATCH 02/11] Remove unused imports --- stdlib/abc.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/abc.pyi b/stdlib/abc.pyi index d84797f1f8e1..c676ce66c5e3 100644 --- a/stdlib/abc.pyi +++ b/stdlib/abc.pyi @@ -1,5 +1,4 @@ -from typing import Any, Callable, ClassVar, Generic, Type, TypeVar -from typing_extensions import Literal +from typing import Any, Callable, Generic, Type, TypeVar _T = TypeVar("_T") _FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) From 16b9d8a10eb28b2f066a217b2f073e36580af26d Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 23 Jun 2021 13:26:35 +0200 Subject: [PATCH 03/11] Add constructors to abstract{class,static}method --- stdlib/abc.pyi | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/stdlib/abc.pyi b/stdlib/abc.pyi index c676ce66c5e3..3d42ea5f4eff 100644 --- a/stdlib/abc.pyi +++ b/stdlib/abc.pyi @@ -5,8 +5,12 @@ _FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) def abstractmethod(funcobj: _FuncT) -> _FuncT: ... -class abstractclassmethod(classmethod[_FuncT], Generic[_FuncT]): ... -class abstractstaticmethod(staticmethod[_FuncT], Generic[_FuncT]): ... +class abstractclassmethod(classmethod[_FuncT], Generic[_FuncT]): + def __init__(self, callable: _FuncT) -> None: ... + +class abstractstaticmethod(staticmethod[_FuncT], Generic[_FuncT]): + def __init__(self, callable: _FuncT) -> None: ... + class abstractproperty(property): ... class ABCMeta(type): From 409aaa1983a384ddc0b8ac1c57c24058ddc6bb6d Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 28 Jun 2021 13:21:07 +0200 Subject: [PATCH 04/11] Use ParamSpec for classmethod --- stdlib/builtins.pyi | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index e77866910b28..f204d44a2c7c 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -59,7 +59,7 @@ from typing import ( ValuesView, overload, ) -from typing_extensions import Literal, SupportsIndex, final +from typing_extensions import Literal, ParamSpec, SupportsIndex, final if sys.version_info >= (3, 9): from types import GenericAlias @@ -81,6 +81,8 @@ _T5 = TypeVar("_T5") _TT = TypeVar("_TT", bound="type") _TBE = TypeVar("_TBE", bound="BaseException") _FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) +_ClsT = Type("_ClsT", bound=Type[Any]) +_P = ParamSpec("_P") class object: __doc__: Optional[str] @@ -117,12 +119,12 @@ class staticmethod(Generic[_FuncT]): # Special, only valid as a decorator. def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... def __get__(self, obj: _T, type: Type[_T] | None = ...) -> _FuncT: ... -class classmethod(Generic[_FuncT]): # Special, only valid as a decorator. - __func__: _FuncT +class classmethod(Generic[_ClsT, _P, _T_co]): # Special, only valid as a decorator. + __func__: Callable[[_ClsT, _P], _T_co] __isabstractmethod__: bool - def __init__(self, f: _FuncT) -> None: ... + def __init__(self, f: Callable[[_ClsT, _P], _T_co]) -> None: ... def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... - def __get__(self, obj: _T, type: Type[_T] | None = ...) -> _FuncT: ... + def __get__(self, obj: _T | None, type: Type[_T] | None = ...) -> Callable[[_P], _T_co]: ... class type(object): __base__: type From 02ab1b635bef99dc3f82dc79eac5656b3424d610 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 28 Jun 2021 13:28:05 +0200 Subject: [PATCH 05/11] Fixes --- stdlib/abc.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/abc.pyi b/stdlib/abc.pyi index ed5f8211d732..6301ce6d4528 100644 --- a/stdlib/abc.pyi +++ b/stdlib/abc.pyi @@ -1,5 +1,5 @@ from _typeshed import SupportsWrite -from typing import Any, Callable, Tuple, Type, TypeVar +from typing import Any, Callable, Generic, Tuple, Type, TypeVar _T = TypeVar("_T") _FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) @@ -22,7 +22,6 @@ class abstractstaticmethod(staticmethod[_FuncT], Generic[_FuncT]): def __init__(self, callable: _FuncT) -> None: ... class abstractproperty(property): ... - class ABC(metaclass=ABCMeta): ... def get_cache_token() -> object: ... From 704dc43922049ccd5d0ac0018ba40c7a71192647 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 28 Jun 2021 13:36:06 +0200 Subject: [PATCH 06/11] Fix ParamSpec --- stdlib/builtins.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index f8e52416a381..8af8297daa19 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -59,7 +59,7 @@ from typing import ( ValuesView, overload, ) -from typing_extensions import Literal, ParamSpec, SupportsIndex, final +from typing_extensions import Concatenate, Literal, ParamSpec, SupportsIndex, final if sys.version_info >= (3, 9): from types import GenericAlias @@ -120,11 +120,11 @@ class staticmethod(Generic[_FuncT]): # Special, only valid as a decorator. def __get__(self, obj: _T, type: Type[_T] | None = ...) -> _FuncT: ... class classmethod(Generic[_ClsT, _P, _T_co]): # Special, only valid as a decorator. - __func__: Callable[[_ClsT, _P], _T_co] + __func__: Callable[Concatenate[_ClsT, _P], _T_co] __isabstractmethod__: bool - def __init__(self, f: Callable[[_ClsT, _P], _T_co]) -> None: ... + def __init__(self, f: Callable[Concatenate[_ClsT, _P], _T_co]) -> None: ... def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... - def __get__(self, obj: _T | None, type: Type[_T] | None = ...) -> Callable[[_P], _T_co]: ... + def __get__(self, obj: _T | None, type: Type[_T] | None = ...) -> Callable[_P, _T_co]: ... class type(object): __base__: type From 8d73ca40a33645e61bc2ea3be5fc456493a4e584 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 28 Jun 2021 13:42:37 +0200 Subject: [PATCH 07/11] Fix abstractclassmethod --- stdlib/abc.pyi | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/stdlib/abc.pyi b/stdlib/abc.pyi index 6301ce6d4528..7e2fb359734e 100644 --- a/stdlib/abc.pyi +++ b/stdlib/abc.pyi @@ -1,7 +1,11 @@ from _typeshed import SupportsWrite from typing import Any, Callable, Generic, Tuple, Type, TypeVar +from typing_extensions import Concatenate, ParamSpec _T = TypeVar("_T") +_ClsT = Type("_ClsT", bound=Type[Any]) +_P = ParamSpec("_P") +_R_co = TypeVar("_R_co", covariant=True) _FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) # These definitions have special processing in mypy @@ -15,8 +19,8 @@ class ABCMeta(type): def abstractmethod(funcobj: _FuncT) -> _FuncT: ... -class abstractclassmethod(classmethod[_FuncT], Generic[_FuncT]): - def __init__(self, callable: _FuncT) -> None: ... +class abstractclassmethod(classmethod[_ClsT, _P, _R_co], Generic[_ClsT, _P, _R_co]): + def __init__(self, callable: Callable[Concatenate[_ClsT, _P], _R_co]) -> None: ... class abstractstaticmethod(staticmethod[_FuncT], Generic[_FuncT]): def __init__(self, callable: _FuncT) -> None: ... From e0f6ee1c124cf03d941eed4f92705aee005801c8 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 28 Jun 2021 13:43:38 +0200 Subject: [PATCH 08/11] Fix typo --- stdlib/abc.pyi | 2 +- stdlib/builtins.pyi | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/abc.pyi b/stdlib/abc.pyi index 7e2fb359734e..d2e35601b47e 100644 --- a/stdlib/abc.pyi +++ b/stdlib/abc.pyi @@ -3,7 +3,7 @@ from typing import Any, Callable, Generic, Tuple, Type, TypeVar from typing_extensions import Concatenate, ParamSpec _T = TypeVar("_T") -_ClsT = Type("_ClsT", bound=Type[Any]) +_ClsT = TypeVar("_ClsT", bound=Type[Any]) _P = ParamSpec("_P") _R_co = TypeVar("_R_co", covariant=True) _FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 8af8297daa19..00fef2b04882 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -81,7 +81,7 @@ _T5 = TypeVar("_T5") _TT = TypeVar("_TT", bound="type") _TBE = TypeVar("_TBE", bound="BaseException") _FuncT = TypeVar("_FuncT", bound=Callable[..., Any]) -_ClsT = Type("_ClsT", bound=Type[Any]) +_ClsT = TypeVar("_ClsT", bound=Type[Any]) _P = ParamSpec("_P") class object: From ddfd63431fbfb2184d849bb08c2e371c17f2a767 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 28 Jun 2021 17:02:45 +0200 Subject: [PATCH 09/11] Consistent type var names --- stdlib/builtins.pyi | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 00fef2b04882..2d2f2038685c 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -70,6 +70,7 @@ class _SupportsTrunc(Protocol): _T = TypeVar("_T") _T_co = TypeVar("_T_co", covariant=True) _T_contra = TypeVar("_T_contra", contravariant=True) +_R_co = TypeVar("_R_co", covariant=True) _KT = TypeVar("_KT") _VT = TypeVar("_VT") _S = TypeVar("_S") @@ -119,12 +120,12 @@ class staticmethod(Generic[_FuncT]): # Special, only valid as a decorator. def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... def __get__(self, obj: _T, type: Type[_T] | None = ...) -> _FuncT: ... -class classmethod(Generic[_ClsT, _P, _T_co]): # Special, only valid as a decorator. - __func__: Callable[Concatenate[_ClsT, _P], _T_co] +class classmethod(Generic[_ClsT, _P, _R_co]): # Special, only valid as a decorator. + __func__: Callable[Concatenate[_ClsT, _P], _R_co] __isabstractmethod__: bool - def __init__(self, f: Callable[Concatenate[_ClsT, _P], _T_co]) -> None: ... + def __init__(self, f: Callable[Concatenate[_ClsT, _P], _R_co]) -> None: ... def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... - def __get__(self, obj: _T | None, type: Type[_T] | None = ...) -> Callable[_P, _T_co]: ... + def __get__(self, obj: _T | None, type: Type[_T] | None = ...) -> Callable[_P, _R_co]: ... class type(object): __base__: type From 06a0ce0003ae359ccc723f252c5a56fd8516bbea Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 28 Jun 2021 17:04:21 +0200 Subject: [PATCH 10/11] @{static,class}method takes pos-only arguments --- stdlib/builtins.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 2d2f2038685c..f938f309d851 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -116,14 +116,14 @@ class object: class staticmethod(Generic[_FuncT]): # Special, only valid as a decorator. __func__: _FuncT __isabstractmethod__: bool - def __init__(self, f: _FuncT) -> None: ... + def __init__(self, __f: _FuncT) -> None: ... def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... def __get__(self, obj: _T, type: Type[_T] | None = ...) -> _FuncT: ... class classmethod(Generic[_ClsT, _P, _R_co]): # Special, only valid as a decorator. __func__: Callable[Concatenate[_ClsT, _P], _R_co] __isabstractmethod__: bool - def __init__(self, f: Callable[Concatenate[_ClsT, _P], _R_co]) -> None: ... + def __init__(self, __f: Callable[Concatenate[_ClsT, _P], _R_co]) -> None: ... def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... def __get__(self, obj: _T | None, type: Type[_T] | None = ...) -> Callable[_P, _R_co]: ... From aa5c5df7d21567d5adc7cc6ef0f30a36f172f6f7 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Mon, 28 Jun 2021 17:06:17 +0200 Subject: [PATCH 11/11] Annotate self --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index f938f309d851..11a5dce4d81d 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -123,7 +123,7 @@ class staticmethod(Generic[_FuncT]): # Special, only valid as a decorator. class classmethod(Generic[_ClsT, _P, _R_co]): # Special, only valid as a decorator. __func__: Callable[Concatenate[_ClsT, _P], _R_co] __isabstractmethod__: bool - def __init__(self, __f: Callable[Concatenate[_ClsT, _P], _R_co]) -> None: ... + def __init__(self: classmethod[_ClsT, _P, _R_co], __f: Callable[Concatenate[_ClsT, _P], _R_co]) -> None: ... def __new__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T: ... def __get__(self, obj: _T | None, type: Type[_T] | None = ...) -> Callable[_P, _R_co]: ...