-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f03d008
Improve abc module and builtin function decorators
srittau f40490b
Remove unused imports
srittau 16b9d8a
Add constructors to abstract{class,static}method
srittau 409aaa1
Use ParamSpec for classmethod
srittau df2f05f
Merge branch 'master' into abcmeta
srittau 02ab1b6
Fixes
srittau 704dc43
Fix ParamSpec
srittau 8d73ca4
Fix abstractclassmethod
srittau e0f6ee1
Fix typo
srittau ddfd634
Consistent type var names
srittau 06a0ce0
@{static,class}method takes pos-only arguments
srittau aa5c5df
Annotate self
srittau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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") | ||
|
@@ -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] | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.