diff --git a/mypy/errorcodes.py b/mypy/errorcodes.py index 50a82be9816d..0833d25ab3c2 100644 --- a/mypy/errorcodes.py +++ b/mypy/errorcodes.py @@ -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" diff --git a/mypy/semanal.py b/mypy/semanal.py index c5a6989f4f61..d5d032b29384 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -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) # diff --git a/test-data/unit/check-async-await.test b/test-data/unit/check-async-await.test index 83a66ef4a815..8f3aff5dfdf4 100644 --- a/test-data/unit/check-async-await.test +++ b/test-data/unit/check-async-await.test @@ -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]