Skip to content

Commit 6cbdab8

Browse files
authored
Stubtest: more helpful errors if a function is missing from stub (#16517)
1 parent 058f8fd commit 6cbdab8

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

mypy/stubtest.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,17 @@ def __init__(
102102
self.stub_object = stub_object
103103
self.runtime_object = runtime_object
104104
self.stub_desc = stub_desc or str(getattr(stub_object, "type", stub_object))
105-
self.runtime_desc = runtime_desc or _truncate(repr(runtime_object), 100)
105+
106+
if runtime_desc is None:
107+
runtime_sig = safe_inspect_signature(runtime_object)
108+
if runtime_sig is None:
109+
self.runtime_desc = _truncate(repr(runtime_object), 100)
110+
else:
111+
runtime_is_async = inspect.iscoroutinefunction(runtime_object)
112+
description = describe_runtime_callable(runtime_sig, is_async=runtime_is_async)
113+
self.runtime_desc = _truncate(description, 100)
114+
else:
115+
self.runtime_desc = runtime_desc
106116

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

14841494

1495+
def describe_runtime_callable(signature: inspect.Signature, *, is_async: bool) -> str:
1496+
return f'{"async " if is_async else ""}def {signature}'
1497+
1498+
14851499
def is_subtype_helper(left: mypy.types.Type, right: mypy.types.Type) -> bool:
14861500
"""Checks whether ``left`` is a subtype of ``right``."""
14871501
left = mypy.types.get_proper_type(left)

0 commit comments

Comments
 (0)