Skip to content

Commit 43fc65a

Browse files
committed
fix: fixed the await outside coroutine within list comprehensions error
1 parent ada4720 commit 43fc65a

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

mypy/message_registry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
8080
ASYNC_FOR_OUTSIDE_COROUTINE: Final = '"async for" outside async function'
8181
ASYNC_WITH_OUTSIDE_COROUTINE: Final = '"async with" outside async function'
8282

83+
AWAIT_WITH_OUTSIDE_COROUTINE: Final = '"await" outside coroutine ("async def")'
84+
8385
INCOMPATIBLE_TYPES_IN_YIELD: Final = ErrorMessage('Incompatible types in "yield"')
8486
INCOMPATIBLE_TYPES_IN_YIELD_FROM: Final = ErrorMessage('Incompatible types in "yield from"')
8587
INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION: Final = "Incompatible types in string interpolation"

mypy/semanal.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6075,14 +6075,22 @@ def visit_type_application(self, expr: TypeApplication) -> None:
60756075
expr.types[i] = analyzed
60766076

60776077
def visit_list_comprehension(self, expr: ListComprehension) -> None:
6078-
if any(expr.generator.is_async) or has_await_expression(expr):
6078+
if any(expr.generator.is_async):
60796079
if not self.is_func_scope() or not self.function_stack[-1].is_coroutine:
60806080
self.fail(
60816081
message_registry.ASYNC_FOR_OUTSIDE_COROUTINE,
60826082
expr,
60836083
code=codes.SYNTAX,
60846084
serious=True,
60856085
)
6086+
elif has_await_expression(expr):
6087+
if not self.is_func_scope() or not self.function_stack[-1].is_coroutine:
6088+
self.fail(
6089+
message_registry.AWAIT_WITH_OUTSIDE_COROUTINE,
6090+
expr,
6091+
code=codes.SYNTAX,
6092+
serious=True,
6093+
)
60866094

60876095
expr.generator.accept(self)
60886096

@@ -6180,7 +6188,7 @@ def visit_await_expr(self, expr: AwaitExpr) -> None:
61806188
self.fail('"await" outside function', expr, serious=True, code=codes.TOP_LEVEL_AWAIT)
61816189
elif not self.function_stack[-1].is_coroutine and self.scope_stack[-1] != SCOPE_COMPREHENSION:
61826190
self.fail(
6183-
'"await" outside coroutine ("async def")',
6191+
message_registry.AWAIT_WITH_OUTSIDE_COROUTINE,
61846192
expr,
61856193
serious=True,
61866194
code=codes.AWAIT_NOT_ASYNC,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,15 +1013,16 @@ async def foo(x: int) -> int: ...
10131013

10141014
# These are allowed in some cases:
10151015
top_level = await foo(1) # E: "await" outside function [top-level-await]
1016-
crasher = [await foo(x) for x in [1, 2, 3]] # E: "async for" outside async function [syntax] \
1016+
crasher = [await foo(x) for x in [1, 2, 3]] # E: "await" outside coroutine ("async def") [syntax] \
10171017
# E: "await" outside function [top-level-await]
10181018

10191019
def bad() -> None:
10201020
# These are always critical / syntax issues:
1021-
y = [await foo(x) for x in [1, 2, 3]] # E: "async for" outside async function [syntax]
1021+
y = [await foo(x) for x in [1, 2, 3]] # E: "await" outside coroutine ("async def") [syntax]
10221022
async def good() -> None:
10231023
y = [await foo(x) for x in [1, 2, 3]] # OK
10241024

1025+
10251026
[builtins fixtures/async_await.pyi]
10261027
[typing fixtures/typing-async.pyi]
10271028

0 commit comments

Comments
 (0)