Skip to content

Disallow assignments to awaited coroutines that do not return #12853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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