From fe4f78e593c5a5830498a2ea87a1a111acc5c419 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 18 Nov 2023 15:40:40 +0000 Subject: [PATCH 1/2] Stubtest: give more helpful errors if a function is missing from the stub --- mypy/stubtest.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 46468e8e18e0..20635bdf96d7 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -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.""" @@ -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(runtime_sig, is_async=runtime_is_coroutine) stub_desc = str(stub_sig) else: runtime_sig_desc, stub_desc = None, None @@ -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) From a8bdf941ff363a547fe1e1d3bcb9f99d5cf69b42 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 18 Nov 2023 15:52:46 +0000 Subject: [PATCH 2/2] fix --- mypy/stubtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 20635bdf96d7..98ab8a043aaf 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -1010,7 +1010,7 @@ def verify_funcitem( if signature: stub_sig = Signature.from_funcitem(stub) runtime_sig = Signature.from_inspect_signature(signature) - runtime_sig_desc = describe_runtime_callable(runtime_sig, is_async=runtime_is_coroutine) + 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