Skip to content

Commit

Permalink
'await' in non-async function is a blocking error (#15384)
Browse files Browse the repository at this point in the history
Fixes, #15339
  • Loading branch information
gregorysantosa authored Aug 12, 2023
1 parent 9dbb123 commit 3631528
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def __hash__(self) -> int:
TOP_LEVEL_AWAIT: Final = ErrorCode(
"top-level-await", "Warn about top level await expressions", "General"
)

AWAIT_NOT_ASYNC: Final = ErrorCode(
"await-not-async", 'Warn about "await" outside coroutine ("async def")', "General"
)
# These error codes aren't enabled by default.
NO_UNTYPED_DEF: Final[ErrorCode] = ErrorCode(
"no-untyped-def", "Check that every function has an annotation", "General"
Expand Down
7 changes: 6 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5455,7 +5455,12 @@ def visit_await_expr(self, expr: AwaitExpr) -> None:
# support top level awaits.
self.fail('"await" outside function', expr, serious=True, code=codes.TOP_LEVEL_AWAIT)
elif not self.function_stack[-1].is_coroutine:
self.fail('"await" outside coroutine ("async def")', expr, serious=True, blocker=True)
self.fail(
'"await" outside coroutine ("async def")',
expr,
serious=True,
code=codes.AWAIT_NOT_ASYNC,
)
expr.expr.accept(self)

#
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-async-await.test
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ crasher = [await foo(x) for x in [1, 2, 3]] # E: "await" outside function [top

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

0 comments on commit 3631528

Please sign in to comment.