Skip to content
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

improve typing in inspect #6020

Merged
merged 2 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions stdlib/inspect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,24 @@ if sys.version_info >= (3, 10):
else:
def signature(obj: Callable[..., Any], *, follow_wrapped: bool = ...) -> Signature: ...

class _void: ...
class _empty: ...

class Signature:
def __init__(self, parameters: Sequence[Parameter] | None = ..., *, return_annotation: Any = ...) -> None: ...
# TODO: can we be more specific here?
empty: object
def __init__(
self, parameters: Sequence[Parameter] | None = ..., *, return_annotation: Any = ..., __validate_parameters__: bool = ...
) -> None: ...
empty: ClassVar[Type[_empty]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you try just writing empty = _empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, I did and Python 3.10 complains

inspect.Signature.empty variable differs from runtime type Literal[<class 'inspect._empty'>]

and 3.8 and 3.9

inspect.Signature.empty variable differs from runtime type builtins.type

Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume stubtest complains, but the other tests run? In that case, I'd suggest to just silence the stubtest warnings. But maybe @hauntsaninja has a different opinion?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like it should be fine? Not sure what type checkers think of type aliases within class scope, possible something like that is what's throwing stubtest off

@property
def parameters(self) -> types.MappingProxyType[str, Parameter]: ...
# TODO: can we be more specific here?
@property
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: Self, *, parameters: Sequence[Parameter] | None = ..., return_annotation: Any = ...) -> Self: ...
def replace(
self: Self, *, parameters: Sequence[Parameter] | Type[_void] | None = ..., return_annotation: Any = ...
) -> Self: ...
if sys.version_info >= (3, 10):
@classmethod
def from_callable(
Expand Down Expand Up @@ -166,7 +172,7 @@ class _ParameterKind(enum.IntEnum):

class Parameter:
def __init__(self, name: str, kind: _ParameterKind, *, default: Any = ..., annotation: Any = ...) -> None: ...
empty: Any
empty: ClassVar[Type[_empty]]
name: str
default: Any
annotation: Any
Expand All @@ -178,7 +184,12 @@ class Parameter:
KEYWORD_ONLY: ClassVar[Literal[_ParameterKind.KEYWORD_ONLY]]
VAR_KEYWORD: ClassVar[Literal[_ParameterKind.VAR_KEYWORD]]
def replace(
self: Self, *, name: str | None = ..., kind: _ParameterKind | None = ..., default: Any = ..., annotation: Any = ...
self: Self,
*,
name: str | Type[_void] = ...,
kind: _ParameterKind | Type[_void] = ...,
default: Any = ...,
annotation: Any = ...,
) -> Self: ...

class BoundArguments:
Expand Down
2 changes: 0 additions & 2 deletions tests/stubtest_allowlists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ inspect.Parameter.POSITIONAL_ONLY
inspect.Parameter.POSITIONAL_OR_KEYWORD
inspect.Parameter.VAR_KEYWORD
inspect.Parameter.VAR_POSITIONAL
inspect.Parameter.replace
inspect.Signature.replace
io.BufferedRandom.truncate
io.BufferedReader.seek
io.BufferedReader.truncate
Expand Down