From 65b8a3bb5f56fad2ef51c6270293550426cf21d7 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 27 Jan 2022 14:13:26 +0000 Subject: [PATCH 1/3] Improve `inspect` stubs --- stdlib/inspect.pyi | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/stdlib/inspect.pyi b/stdlib/inspect.pyi index 26ea5bacab2c..be4d51fda76a 100644 --- a/stdlib/inspect.pyi +++ b/stdlib/inspect.pyi @@ -23,8 +23,14 @@ from types import ( if sys.version_info >= (3, 7): from types import ClassMethodDescriptorType, WrapperDescriptorType, MemberDescriptorType, MethodDescriptorType -from typing import Any, ClassVar, Coroutine, NamedTuple, Protocol, TypeVar, Union -from typing_extensions import Literal, TypeGuard +from typing import Any, ClassVar, Coroutine, Generic, NamedTuple, Protocol, TypeVar, Union +from typing_extensions import Literal, ParamSpec, TypeGuard + +_P = ParamSpec("_P") +_R = TypeVar("_R") +_T = TypeVar("_T") +_T_cont = TypeVar("_T_cont", contravariant=True) +_V_cont = TypeVar("_V_cont", contravariant=True) # # Types and members @@ -80,9 +86,6 @@ if sys.version_info >= (3, 8): else: def isasyncgenfunction(object: object) -> bool: ... -_T_cont = TypeVar("_T_cont", contravariant=True) -_V_cont = TypeVar("_V_cont", contravariant=True) - class _SupportsSet(Protocol[_T_cont, _V_cont]): def __set__(self, __instance: _T_cont, __value: _V_cont) -> None: ... @@ -145,49 +148,49 @@ def indentsize(line: str) -> int: ... # if sys.version_info >= (3, 10): def signature( - obj: Callable[..., Any], + obj: Callable[..., _R], *, follow_wrapped: bool = ..., globals: Mapping[str, Any] | None = ..., locals: Mapping[str, Any] | None = ..., eval_str: bool = ..., - ) -> Signature: ... + ) -> Signature[_R]: ... else: - def signature(obj: Callable[..., Any], *, follow_wrapped: bool = ...) -> Signature: ... + def signature(obj: Callable[..., _R], *, follow_wrapped: bool = ...) -> Signature[_R]: ... class _void: ... class _empty: ... -class Signature: +# TODO make this class generic on a ParamSpec as well as the return-type +class Signature(Generic[_R]): def __init__( - self, parameters: Sequence[Parameter] | None = ..., *, return_annotation: Any = ..., __validate_parameters__: bool = ... + self, parameters: Sequence[Parameter] | None = ..., *, return_annotation: _R = ..., __validate_parameters__: bool = ... ) -> None: ... empty = _empty @property def parameters(self) -> types.MappingProxyType[str, Parameter]: ... - # TODO: can we be more specific here? @property - def return_annotation(self) -> Any: ... + def return_annotation(self) -> _R: ... def bind(self, *args: Any, **kwargs: Any) -> BoundArguments: ... def bind_partial(self, *args: Any, **kwargs: Any) -> BoundArguments: ... def replace( - self: Self, *, parameters: Sequence[Parameter] | type[_void] | None = ..., return_annotation: Any = ... - ) -> Self: ... + self, *, parameters: Sequence[Parameter] | type[_void] | None = ..., return_annotation: _T = ... + ) -> Signature[_T]: ... if sys.version_info >= (3, 10): @classmethod def from_callable( cls, - obj: Callable[..., Any], + obj: Callable[..., _R], *, follow_wrapped: bool = ..., globals: Mapping[str, Any] | None = ..., locals: Mapping[str, Any] | None = ..., eval_str: bool = ..., - ) -> Signature: ... + ) -> Signature[_R]: ... else: @classmethod - def from_callable(cls, obj: Callable[..., Any], *, follow_wrapped: bool = ...) -> Signature: ... + def from_callable(cls, obj: Callable[..., _R], *, follow_wrapped: bool = ...) -> Signature[_R]: ... if sys.version_info >= (3, 10): def get_annotations( @@ -240,8 +243,8 @@ class BoundArguments: arguments: OrderedDict[str, Any] args: tuple[Any, ...] kwargs: dict[str, Any] - signature: Signature - def __init__(self, signature: Signature, arguments: OrderedDict[str, Any]) -> None: ... + signature: Signature[Any] + def __init__(self, signature: Signature[Any], arguments: OrderedDict[str, Any]) -> None: ... def apply_defaults(self) -> None: ... # @@ -318,7 +321,7 @@ def formatargvalues( formatvalue: Callable[[Any], str] | None = ..., ) -> str: ... def getmro(cls: type) -> tuple[type, ...]: ... -def getcallargs(__func: Callable[..., Any], *args: Any, **kwds: Any) -> dict[str, Any]: ... +def getcallargs(__func: Callable[_P, Any], *args: _P.args, **kwds: _P.kwargs) -> dict[str, Any]: ... class ClosureVars(NamedTuple): nonlocals: Mapping[str, Any] From b7ecaeaec1573b692d8f38b016fb1eeaef8e3fff Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 27 Jan 2022 15:32:15 +0000 Subject: [PATCH 2/3] Address review --- stdlib/inspect.pyi | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/stdlib/inspect.pyi b/stdlib/inspect.pyi index be4d51fda76a..b600e342fdc6 100644 --- a/stdlib/inspect.pyi +++ b/stdlib/inspect.pyi @@ -23,12 +23,10 @@ from types import ( if sys.version_info >= (3, 7): from types import ClassMethodDescriptorType, WrapperDescriptorType, MemberDescriptorType, MethodDescriptorType -from typing import Any, ClassVar, Coroutine, Generic, NamedTuple, Protocol, TypeVar, Union +from typing import Any, ClassVar, Coroutine, NamedTuple, Protocol, TypeVar, Union from typing_extensions import Literal, ParamSpec, TypeGuard _P = ParamSpec("_P") -_R = TypeVar("_R") -_T = TypeVar("_T") _T_cont = TypeVar("_T_cont", contravariant=True) _V_cont = TypeVar("_V_cont", contravariant=True) @@ -148,49 +146,48 @@ def indentsize(line: str) -> int: ... # if sys.version_info >= (3, 10): def signature( - obj: Callable[..., _R], + obj: Callable[..., Any], *, follow_wrapped: bool = ..., globals: Mapping[str, Any] | None = ..., locals: Mapping[str, Any] | None = ..., eval_str: bool = ..., - ) -> Signature[_R]: ... + ) -> Signature: ... else: - def signature(obj: Callable[..., _R], *, follow_wrapped: bool = ...) -> Signature[_R]: ... + def signature(obj: Callable[..., Any], *, follow_wrapped: bool = ...) -> Signature: ... class _void: ... class _empty: ... -# TODO make this class generic on a ParamSpec as well as the return-type -class Signature(Generic[_R]): +class Signature: def __init__( - self, parameters: Sequence[Parameter] | None = ..., *, return_annotation: _R = ..., __validate_parameters__: bool = ... + self, parameters: Sequence[Parameter] | None = ..., *, return_annotation: Any = ..., __validate_parameters__: bool = ... ) -> None: ... empty = _empty @property def parameters(self) -> types.MappingProxyType[str, Parameter]: ... @property - def return_annotation(self) -> _R: ... + def return_annotation(self) -> Any: ... def bind(self, *args: Any, **kwargs: Any) -> BoundArguments: ... def bind_partial(self, *args: Any, **kwargs: Any) -> BoundArguments: ... def replace( - self, *, parameters: Sequence[Parameter] | type[_void] | None = ..., return_annotation: _T = ... - ) -> Signature[_T]: ... + self: Self, *, parameters: Sequence[Parameter] | type[_void] | None = ..., return_annotation: _T = ... + ) -> Self: ... if sys.version_info >= (3, 10): @classmethod def from_callable( - cls, - obj: Callable[..., _R], + cls: type[Self], + obj: Callable[..., Any], *, follow_wrapped: bool = ..., globals: Mapping[str, Any] | None = ..., locals: Mapping[str, Any] | None = ..., eval_str: bool = ..., - ) -> Signature[_R]: ... + ) -> Self: ... else: @classmethod - def from_callable(cls, obj: Callable[..., _R], *, follow_wrapped: bool = ...) -> Signature[_R]: ... + def from_callable(cls: type[Self], obj: Callable[..., Any], *, follow_wrapped: bool = ...) -> Self: ... if sys.version_info >= (3, 10): def get_annotations( @@ -243,8 +240,8 @@ class BoundArguments: arguments: OrderedDict[str, Any] args: tuple[Any, ...] kwargs: dict[str, Any] - signature: Signature[Any] - def __init__(self, signature: Signature[Any], arguments: OrderedDict[str, Any]) -> None: ... + signature: Signature + def __init__(self, signature: Signature, arguments: OrderedDict[str, Any]) -> None: ... def apply_defaults(self) -> None: ... # From aeb4b577a0363128edf830eb140161fddc6130e7 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 27 Jan 2022 15:33:06 +0000 Subject: [PATCH 3/3] Update inspect.pyi --- stdlib/inspect.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/inspect.pyi b/stdlib/inspect.pyi index b600e342fdc6..f270ad574562 100644 --- a/stdlib/inspect.pyi +++ b/stdlib/inspect.pyi @@ -172,7 +172,7 @@ class Signature: def bind(self, *args: Any, **kwargs: Any) -> BoundArguments: ... def bind_partial(self, *args: Any, **kwargs: Any) -> BoundArguments: ... def replace( - self: Self, *, parameters: Sequence[Parameter] | type[_void] | None = ..., return_annotation: _T = ... + self: Self, *, parameters: Sequence[Parameter] | type[_void] | None = ..., return_annotation: Any = ... ) -> Self: ... if sys.version_info >= (3, 10): @classmethod