Skip to content

Commit e784648

Browse files
authored
stubtest: do not error if a stub is async, but runtime is not (#12234)
1 parent feca706 commit e784648

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

mypy/stubtest.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -700,18 +700,6 @@ def _verify_signature(
700700
yield 'runtime does not have **kwargs argument "{}"'.format(stub.varkw.variable.name)
701701

702702

703-
def _verify_coroutine(
704-
stub: nodes.FuncItem, runtime: Any, *, runtime_is_coroutine: bool
705-
) -> Optional[str]:
706-
if stub.is_coroutine:
707-
if not runtime_is_coroutine:
708-
return 'is an "async def" function in the stub, but not at runtime'
709-
else:
710-
if runtime_is_coroutine:
711-
return 'is an "async def" function at runtime, but not in the stub'
712-
return None
713-
714-
715703
@verify.register(nodes.FuncItem)
716704
def verify_funcitem(
717705
stub: nodes.FuncItem, runtime: MaybeMissing[Any], object_path: List[str]
@@ -735,21 +723,20 @@ def verify_funcitem(
735723
stub_sig = Signature.from_funcitem(stub)
736724
runtime_sig = Signature.from_inspect_signature(signature)
737725
runtime_sig_desc = f'{"async " if runtime_is_coroutine else ""}def {signature}'
726+
stub_desc = f'def {stub_sig!r}'
738727
else:
739-
runtime_sig_desc = None
740-
741-
coroutine_mismatch_error = _verify_coroutine(
742-
stub,
743-
runtime,
744-
runtime_is_coroutine=runtime_is_coroutine
745-
)
728+
runtime_sig_desc, stub_desc = None, None
746729

747-
if coroutine_mismatch_error is not None:
730+
# Don't raise an error if the stub is a coroutine, but the runtime isn't.
731+
# That results in false positives.
732+
# See https://github.com/python/typeshed/issues/7344
733+
if runtime_is_coroutine and not stub.is_coroutine:
748734
yield Error(
749735
object_path,
750-
coroutine_mismatch_error,
736+
'is an "async def" function at runtime, but not in the stub',
751737
stub,
752738
runtime,
739+
stub_desc=stub_desc,
753740
runtime_desc=runtime_sig_desc
754741
)
755742

mypy/test/teststubtest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,17 @@ class X:
209209

210210
@collect_cases
211211
def test_coroutines(self) -> Iterator[Case]:
212-
yield Case(
213-
stub="async def foo() -> int: ...",
214-
runtime="def foo(): return 5",
215-
error="foo",
216-
)
217212
yield Case(
218213
stub="def bar() -> int: ...",
219214
runtime="async def bar(): return 5",
220215
error="bar",
221216
)
217+
# Don't error for this one -- we get false positives otherwise
218+
yield Case(
219+
stub="async def foo() -> int: ...",
220+
runtime="def foo(): return 5",
221+
error=None,
222+
)
222223
yield Case(
223224
stub="def baz() -> int: ...",
224225
runtime="def baz(): return 5",

0 commit comments

Comments
 (0)