Skip to content

Commit

Permalink
Disallow assignments to awaited coroutines that do not return (#12853)
Browse files Browse the repository at this point in the history
Resolves #12837

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja authored May 25, 2022
1 parent 405efd2 commit 87e73fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 8 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3987,6 +3987,8 @@ def accept(self,
typ = self.visit_yield_from_expr(node, allow_none_return=True)
elif allow_none_return and isinstance(node, ConditionalExpr):
typ = self.visit_conditional_expr(node, allow_none_return=True)
elif allow_none_return and isinstance(node, AwaitExpr):
typ = self.visit_await_expr(node, allow_none_return=True)
else:
typ = node.accept(self)
except Exception as err:
Expand Down Expand Up @@ -4099,15 +4101,18 @@ def visit_yield_expr(self, e: YieldExpr) -> Type:
'actual type', 'expected type')
return self.chk.get_generator_receive_type(return_type, False)

def visit_await_expr(self, e: AwaitExpr) -> Type:
def visit_await_expr(self, e: AwaitExpr, allow_none_return: bool = False) -> Type:
expected_type = self.type_context[-1]
if expected_type is not None:
expected_type = self.chk.named_generic_type('typing.Awaitable', [expected_type])
actual_type = get_proper_type(self.accept(e.expr, expected_type))
if isinstance(actual_type, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=actual_type)
return self.check_awaitable_expr(actual_type, e,
message_registry.INCOMPATIBLE_TYPES_IN_AWAIT)
ret = self.check_awaitable_expr(actual_type, e,
message_registry.INCOMPATIBLE_TYPES_IN_AWAIT)
if not allow_none_return and isinstance(get_proper_type(ret), NoneType):
self.chk.msg.does_not_return_value(None, e)
return ret

def check_awaitable_expr(self, t: Type, ctx: Context, msg: Union[str, ErrorMessage]) -> Type:
"""Check the argument to `await` and extract the type of value.
Expand Down
5 changes: 2 additions & 3 deletions test-data/unit/pythoneval-asyncio.test
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ Outside 42

-- Errors

[case testErrorAssigningCoroutineThatDontReturn-xfail]
# https://github.com/python/mypy/issues/12837
[case testErrorAssigningCoroutineThatDontReturn]
from typing import Any
import asyncio
from asyncio import Future
Expand All @@ -262,7 +261,7 @@ try:
finally:
loop.close()
[out]
_program.py:13: error: Function does not return a value
_program.py:11: error: Function does not return a value

[case testErrorReturnIsNotTheSameType]
from typing import Any
Expand Down

0 comments on commit 87e73fe

Please sign in to comment.