Skip to content

Improve abc module and builtin function decorators (with ParamSpec) #5682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
17 changes: 11 additions & 6 deletions stdlib/abc.pyi
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
from _typeshed import SupportsWrite
from typing import Any, Callable, Dict, Tuple, Type, TypeVar
from typing import Any, Callable, Generic, Tuple, Type, TypeVar
from typing_extensions import Concatenate, ParamSpec

_T = TypeVar("_T")
_ClsT = TypeVar("_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
class ABCMeta(type):
__abstractmethods__: frozenset[str]
def __init__(self, name: str, bases: Tuple[type, ...], namespace: Dict[str, Any]) -> None: ...
def __init__(self, name: str, bases: Tuple[type, ...], namespace: dict[str, Any]) -> None: ...
def __instancecheck__(cls: ABCMeta, instance: Any) -> Any: ...
def __subclasscheck__(cls: ABCMeta, subclass: Any) -> Any: ...
def _dump_registry(cls: ABCMeta, file: SupportsWrite[str] | None = ...) -> None: ...
def register(cls: ABCMeta, subclass: Type[_T]) -> Type[_T]: ...

def abstractmethod(funcobj: _FuncT) -> _FuncT: ...

class abstractproperty(property): ...
class abstractclassmethod(classmethod[_ClsT, _P, _R_co], Generic[_ClsT, _P, _R_co]):
def __init__(self, callable: Callable[Concatenate[_ClsT, _P], _R_co]) -> None: ...

# These two are deprecated and not supported by mypy
def abstractstaticmethod(callable: _FuncT) -> _FuncT: ...
def abstractclassmethod(callable: _FuncT) -> _FuncT: ...
class abstractstaticmethod(staticmethod[_FuncT], Generic[_FuncT]):
def __init__(self, callable: _FuncT) -> None: ...

class abstractproperty(property): ...
class ABC(metaclass=ABCMeta): ...

def get_cache_token() -> object: ...
24 changes: 14 additions & 10 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -26,7 +27,6 @@ from typing import (
AsyncIterator,
BinaryIO,
ByteString,
Callable,
Dict,
FrozenSet,
Generic,
Expand Down Expand Up @@ -59,7 +59,7 @@ from typing import (
ValuesView,
overload,
)
from typing_extensions import Literal, SupportsIndex, final
from typing_extensions import Concatenate, Literal, ParamSpec, SupportsIndex, final

if sys.version_info >= (3, 9):
from types import GenericAlias
Expand All @@ -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")
Expand All @@ -80,6 +81,9 @@ _T4 = TypeVar("_T4")
_T5 = TypeVar("_T5")
_TT = TypeVar("_TT", bound="type")
_TBE = TypeVar("_TBE", bound="BaseException")
_FuncT = TypeVar("_FuncT", bound=Callable[..., Any])
_ClsT = TypeVar("_ClsT", bound=Type[Any])
_P = ParamSpec("_P")

class object:
__doc__: Optional[str]
Expand Down Expand Up @@ -109,19 +113,19 @@ class object:
def __dir__(self) -> Iterable[str]: ...
def __init_subclass__(cls) -> None: ...

class staticmethod(object): # Special, only valid as a decorator.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: These comments should be deleted. They seem misleading and mypy-specific to me.

__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[_ClsT, _P, _R_co]): # Special, only valid as a decorator.
__func__: Callable[Concatenate[_ClsT, _P], _R_co]
__isabstractmethod__: bool
def __init__(self, f: Callable[..., Any]) -> 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, type: Optional[Type[_T]] = ...) -> Callable[..., Any]: ...
def __get__(self, obj: _T | None, type: Type[_T] | None = ...) -> Callable[_P, _R_co]: ...

class type(object):
__base__: type
Expand Down