Skip to content

Commit d9d45ba

Browse files
authored
Add examples for await-outside-async and fix activation of message (#6016)
1 parent 0bbc01c commit d9d45ba

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Release date: TBA
2626

2727
Closes #1555
2828

29+
* Fix bug where specifically enabling just ``await-outside-async`` was not possible.
2930

3031
..
3132
Insert your changelog randomly, it will reduce merge conflicts
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import asyncio
2+
3+
4+
def main():
5+
await asyncio.sleep(1) # [await-outside-async]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import asyncio
2+
3+
4+
async def main():
5+
await asyncio.sleep(1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `PEP 492 <https://peps.python.org/pep-0492/#await-expression>`_

pylint/checkers/typecheck.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,20 @@ def visit_for(self, node: nodes.For) -> None:
19531953

19541954
self.add_message("dict-iter-missing-items", node=node)
19551955

1956+
@check_messages("await-outside-async")
1957+
def visit_await(self, node: nodes.Await) -> None:
1958+
self._check_await_outside_coroutine(node)
1959+
1960+
def _check_await_outside_coroutine(self, node: nodes.Await) -> None:
1961+
node_scope = node.scope()
1962+
while not isinstance(node_scope, nodes.Module):
1963+
if isinstance(node_scope, nodes.AsyncFunctionDef):
1964+
return
1965+
if isinstance(node_scope, nodes.FunctionDef):
1966+
break
1967+
node_scope = node_scope.parent.scope()
1968+
self.add_message("await-outside-async", node=node)
1969+
19561970

19571971
class IterableChecker(BaseChecker):
19581972
"""Checks for non-iterables used in an iterable context.
@@ -2064,20 +2078,6 @@ def visit_generatorexp(self, node: nodes.GeneratorExp) -> None:
20642078
for gen in node.generators:
20652079
self._check_iterable(gen.iter, check_async=gen.is_async)
20662080

2067-
@check_messages("await-outside-async")
2068-
def visit_await(self, node: nodes.Await) -> None:
2069-
self._check_await_outside_coroutine(node)
2070-
2071-
def _check_await_outside_coroutine(self, node: nodes.Await) -> None:
2072-
node_scope = node.scope()
2073-
while not isinstance(node_scope, nodes.Module):
2074-
if isinstance(node_scope, nodes.AsyncFunctionDef):
2075-
return
2076-
if isinstance(node_scope, nodes.FunctionDef):
2077-
break
2078-
node_scope = node_scope.parent.scope()
2079-
self.add_message("await-outside-async", node=node)
2080-
20812081

20822082
def register(linter: "PyLinter") -> None:
20832083
linter.register_checker(TypeChecker(linter))

0 commit comments

Comments
 (0)