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

Stubtest: give more helpful errors if a function is missing from the stub #16517

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ def __init__(
self.stub_object = stub_object
self.runtime_object = runtime_object
self.stub_desc = stub_desc or str(getattr(stub_object, "type", stub_object))
self.runtime_desc = runtime_desc or _truncate(repr(runtime_object), 100)

if runtime_desc is None:
runtime_sig = safe_inspect_signature(runtime_object)
if runtime_sig is None:
self.runtime_desc = _truncate(repr(runtime_object), 100)
else:
runtime_is_async = inspect.iscoroutinefunction(runtime_object)
description = describe_runtime_callable(runtime_sig, is_async=runtime_is_async)
self.runtime_desc = _truncate(description, 100)
else:
self.runtime_desc = runtime_desc

def is_missing_stub(self) -> bool:
"""Whether or not the error is for something missing from the stub."""
Expand Down Expand Up @@ -1000,7 +1010,7 @@ def verify_funcitem(
if signature:
stub_sig = Signature.from_funcitem(stub)
runtime_sig = Signature.from_inspect_signature(signature)
runtime_sig_desc = f'{"async " if runtime_is_coroutine else ""}def {signature}'
runtime_sig_desc = describe_runtime_callable(signature, is_async=runtime_is_coroutine)
stub_desc = str(stub_sig)
else:
runtime_sig_desc, stub_desc = None, None
Expand Down Expand Up @@ -1482,6 +1492,10 @@ def safe_inspect_signature(runtime: Any) -> inspect.Signature | None:
return None


def describe_runtime_callable(signature: inspect.Signature, *, is_async: bool) -> str:
return f'{"async " if is_async else ""}def {signature}'


def is_subtype_helper(left: mypy.types.Type, right: mypy.types.Type) -> bool:
"""Checks whether ``left`` is a subtype of ``right``."""
left = mypy.types.get_proper_type(left)
Expand Down