Skip to content
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
4 changes: 3 additions & 1 deletion mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def __str__(self) -> str:
TOP_LEVEL_AWAIT: Final = ErrorCode(
"top-level-await", "Warn about top level await experessions", "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 @@ -5434,7 +5434,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 @@ -997,7 +997,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