Skip to content

Commit 3631528

Browse files
'await' in non-async function is a blocking error (#15384)
Fixes, #15339
1 parent 9dbb123 commit 3631528

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

mypy/errorcodes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ def __hash__(self) -> int:
152152
TOP_LEVEL_AWAIT: Final = ErrorCode(
153153
"top-level-await", "Warn about top level await expressions", "General"
154154
)
155-
155+
AWAIT_NOT_ASYNC: Final = ErrorCode(
156+
"await-not-async", 'Warn about "await" outside coroutine ("async def")', "General"
157+
)
156158
# These error codes aren't enabled by default.
157159
NO_UNTYPED_DEF: Final[ErrorCode] = ErrorCode(
158160
"no-untyped-def", "Check that every function has an annotation", "General"

mypy/semanal.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5455,7 +5455,12 @@ def visit_await_expr(self, expr: AwaitExpr) -> None:
54555455
# support top level awaits.
54565456
self.fail('"await" outside function', expr, serious=True, code=codes.TOP_LEVEL_AWAIT)
54575457
elif not self.function_stack[-1].is_coroutine:
5458-
self.fail('"await" outside coroutine ("async def")', expr, serious=True, blocker=True)
5458+
self.fail(
5459+
'"await" outside coroutine ("async def")',
5460+
expr,
5461+
serious=True,
5462+
code=codes.AWAIT_NOT_ASYNC,
5463+
)
54595464
expr.expr.accept(self)
54605465

54615466
#

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ crasher = [await foo(x) for x in [1, 2, 3]] # E: "await" outside function [top
974974

975975
def bad() -> None:
976976
# These are always critical / syntax issues:
977-
y = [await foo(x) for x in [1, 2, 3]] # E: "await" outside coroutine ("async def")
977+
y = [await foo(x) for x in [1, 2, 3]] # E: "await" outside coroutine ("async def") [await-not-async]
978978
async def good() -> None:
979979
y = [await foo(x) for x in [1, 2, 3]] # OK
980980
[builtins fixtures/async_await.pyi]

0 commit comments

Comments
 (0)