Skip to content

Commit 2dc53ca

Browse files
authored
Improve abc module and builtin function decorators (#5703)
1 parent 584336a commit 2dc53ca

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

requirements-tests.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mypy==0.931
2-
pytype==2022.01.05; platform_system != "Windows" and python_version < "3.10"
2+
pytype==2022.01.07; platform_system != "Windows" and python_version < "3.10"
33
# must match .pre-commit-config.yaml
44
black==22.1.0
55
flake8==4.0.1

stdlib/abc.pyi

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import sys
22
from _typeshed import SupportsWrite
3-
from typing import Any, Callable, TypeVar
3+
from collections.abc import Callable
4+
from typing import Any, Generic, TypeVar
5+
from typing_extensions import Literal
46

57
_T = TypeVar("_T")
8+
_R_co = TypeVar("_R_co", covariant=True)
69
_FuncT = TypeVar("_FuncT", bound=Callable[..., Any])
710

811
# These definitions have special processing in mypy
@@ -16,12 +19,15 @@ class ABCMeta(type):
1619

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

19-
class abstractproperty(property): ...
22+
class abstractclassmethod(classmethod[_R_co], Generic[_R_co]):
23+
__isabstractmethod__: Literal[True]
24+
def __init__(self: abstractclassmethod[_R_co], callable: Callable[..., _R_co]) -> None: ...
2025

21-
# These two are deprecated and not supported by mypy
22-
def abstractstaticmethod(callable: _FuncT) -> _FuncT: ...
23-
def abstractclassmethod(callable: _FuncT) -> _FuncT: ...
26+
class abstractstaticmethod(staticmethod[_R_co], Generic[_R_co]):
27+
__isabstractmethod__: Literal[True]
28+
def __init__(self, callable: Callable[..., _R_co]) -> None: ...
2429

30+
class abstractproperty(property): ...
2531
class ABC(metaclass=ABCMeta): ...
2632

2733
def get_cache_token() -> object: ...

stdlib/builtins.pyi

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ from _typeshed import (
2222
SupportsTrunc,
2323
SupportsWrite,
2424
)
25+
from collections.abc import Callable
2526
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
2627
from types import CodeType, TracebackType, _Cell
2728
from typing import (
@@ -30,7 +31,6 @@ from typing import (
3031
Any,
3132
BinaryIO,
3233
ByteString,
33-
Callable,
3434
Generic,
3535
Iterable,
3636
Iterator,
@@ -61,6 +61,7 @@ if sys.version_info >= (3, 9):
6161
_T = TypeVar("_T")
6262
_T_co = TypeVar("_T_co", covariant=True)
6363
_T_contra = TypeVar("_T_contra", contravariant=True)
64+
_R_co = TypeVar("_R_co", covariant=True)
6465
_KT = TypeVar("_KT")
6566
_VT = TypeVar("_VT")
6667
_S = TypeVar("_S")
@@ -69,7 +70,6 @@ _T2 = TypeVar("_T2")
6970
_T3 = TypeVar("_T3")
7071
_T4 = TypeVar("_T4")
7172
_T5 = TypeVar("_T5")
72-
_R = TypeVar("_R") # Return-type TypeVar
7373
_SupportsNextT = TypeVar("_SupportsNextT", bound=SupportsNext[Any], covariant=True)
7474
_SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant=True)
7575

@@ -112,26 +112,26 @@ class object:
112112
def __dir__(self) -> Iterable[str]: ...
113113
def __init_subclass__(cls) -> None: ...
114114

115-
class staticmethod(Generic[_R]):
116-
__func__: Callable[..., _R]
115+
class staticmethod(Generic[_R_co]):
116+
__func__: Callable[..., _R_co]
117117
__isabstractmethod__: bool
118-
def __init__(self: staticmethod[_R], __f: Callable[..., _R]) -> None: ...
119-
def __get__(self, __obj: _T, __type: type[_T] | None = ...) -> Callable[..., _R]: ...
118+
def __init__(self: staticmethod[_R_co], __f: Callable[..., _R_co]) -> None: ...
119+
def __get__(self, __obj: _T, __type: type[_T] | None = ...) -> Callable[..., _R_co]: ...
120120
if sys.version_info >= (3, 10):
121121
__name__: str
122122
__qualname__: str
123-
__wrapped__: Callable[..., _R]
124-
def __call__(self, *args: Any, **kwargs: Any) -> _R: ...
123+
__wrapped__: Callable[..., _R_co]
124+
def __call__(self, *args: Any, **kwargs: Any) -> _R_co: ...
125125

126-
class classmethod(Generic[_R]):
127-
__func__: Callable[..., _R]
126+
class classmethod(Generic[_R_co]):
127+
__func__: Callable[..., _R_co]
128128
__isabstractmethod__: bool
129-
def __init__(self: classmethod[_R], __f: Callable[..., _R]) -> None: ...
130-
def __get__(self, __obj: _T, __type: type[_T] | None = ...) -> Callable[..., _R]: ...
129+
def __init__(self: classmethod[_R_co], __f: Callable[..., _R_co]) -> None: ...
130+
def __get__(self, __obj: _T, __type: type[_T] | None = ...) -> Callable[..., _R_co]: ...
131131
if sys.version_info >= (3, 10):
132132
__name__: str
133133
__qualname__: str
134-
__wrapped__: Callable[..., _R]
134+
__wrapped__: Callable[..., _R_co]
135135

136136
class type:
137137
__base__: type

tests/stubtest_allowlists/py3_common.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ _csv.Dialect.__init__ # C __init__ signature is inaccurate
1919
_socket.*
2020
_threading_local.local.__new__
2121
_weakref.ref.__call__
22-
abc.abstractclassmethod # Deprecated, unsupported by mypy, hard to fix. #6552
23-
abc.abstractstaticmethod # Deprecated, unsupported by mypy, hard to fix. #6552
2422
abc.ABCMeta.__new__ # pytype wants the parameter named cls and not mcls
2523
_weakref.CallableProxyType.__getattr__ # Should have all attributes of proxy
2624
_weakref.ProxyType.__getattr__ # Should have all attributes of proxy

0 commit comments

Comments
 (0)