Skip to content

Commit 1a553a4

Browse files
ilevkivskyimsullivan
authored andcommitted
Fix crash when checking async generator return type (#5123)
1 parent da4041f commit 1a553a4

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

mypy/checker.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,9 +889,13 @@ def is_unannotated_any(t: Type) -> bool:
889889
ret_type = fdef.type.ret_type
890890
if is_unannotated_any(ret_type):
891891
self.fail(messages.RETURN_TYPE_EXPECTED, fdef)
892-
elif (fdef.is_coroutine and isinstance(ret_type, Instance) and
893-
is_unannotated_any(self.get_coroutine_return_type(ret_type))):
894-
self.fail(messages.RETURN_TYPE_EXPECTED, fdef)
892+
elif fdef.is_generator:
893+
if is_unannotated_any(self.get_generator_return_type(ret_type,
894+
fdef.is_coroutine)):
895+
self.fail(messages.RETURN_TYPE_EXPECTED, fdef)
896+
elif fdef.is_coroutine and isinstance(ret_type, Instance):
897+
if is_unannotated_any(self.get_coroutine_return_type(ret_type)):
898+
self.fail(messages.RETURN_TYPE_EXPECTED, fdef)
895899
if any(is_unannotated_any(t) for t in fdef.type.arg_types):
896900
self.fail(messages.ARGUMENT_TYPE_EXPECTED, fdef)
897901

test-data/unit/check-async-await.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,3 +669,33 @@ async def decorated_host_coroutine() -> None:
669669
[builtins fixtures/async_await.pyi]
670670
[typing fixtures/typing-full.pyi]
671671
[out]
672+
673+
[case testAsyncGenDisallowUntyped]
674+
# flags: --disallow-untyped-defs
675+
# These should not crash
676+
from typing import AsyncGenerator, Any
677+
678+
async def f() -> AsyncGenerator[int, None]:
679+
yield 0
680+
681+
async def g() -> AsyncGenerator[Any, None]:
682+
yield 0
683+
[builtins fixtures/async_await.pyi]
684+
[typing fixtures/typing-full.pyi]
685+
[out]
686+
687+
[case testAsyncGenDisallowUntypedTriggers]
688+
# flags: --disallow-untyped-defs
689+
from typing import AsyncGenerator, Any
690+
691+
async def f() -> AsyncGenerator[Any, Any]:
692+
yield None
693+
694+
async def h() -> Any:
695+
yield 0
696+
697+
async def g(): # E: Function is missing a type annotation
698+
yield 0
699+
[builtins fixtures/async_await.pyi]
700+
[typing fixtures/typing-full.pyi]
701+
[out]

0 commit comments

Comments
 (0)