Skip to content

Commit

Permalink
Add examples for await-outside-async and fix activation of message (
Browse files Browse the repository at this point in the history
  • Loading branch information
DudeNr33 authored Mar 28, 2022
1 parent 0bbc01c commit d9d45ba
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Release date: TBA

Closes #1555

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

..
Insert your changelog randomly, it will reduce merge conflicts
Expand Down
5 changes: 5 additions & 0 deletions doc/data/messages/a/await-outside-async/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import asyncio


def main():
await asyncio.sleep(1) # [await-outside-async]
5 changes: 5 additions & 0 deletions doc/data/messages/a/await-outside-async/good.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import asyncio


async def main():
await asyncio.sleep(1)
1 change: 1 addition & 0 deletions doc/data/messages/a/await-outside-async/related.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `PEP 492 <https://peps.python.org/pep-0492/#await-expression>`_
28 changes: 14 additions & 14 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,20 @@ def visit_for(self, node: nodes.For) -> None:

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

@check_messages("await-outside-async")
def visit_await(self, node: nodes.Await) -> None:
self._check_await_outside_coroutine(node)

def _check_await_outside_coroutine(self, node: nodes.Await) -> None:
node_scope = node.scope()
while not isinstance(node_scope, nodes.Module):
if isinstance(node_scope, nodes.AsyncFunctionDef):
return
if isinstance(node_scope, nodes.FunctionDef):
break
node_scope = node_scope.parent.scope()
self.add_message("await-outside-async", node=node)


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

@check_messages("await-outside-async")
def visit_await(self, node: nodes.Await) -> None:
self._check_await_outside_coroutine(node)

def _check_await_outside_coroutine(self, node: nodes.Await) -> None:
node_scope = node.scope()
while not isinstance(node_scope, nodes.Module):
if isinstance(node_scope, nodes.AsyncFunctionDef):
return
if isinstance(node_scope, nodes.FunctionDef):
break
node_scope = node_scope.parent.scope()
self.add_message("await-outside-async", node=node)


def register(linter: "PyLinter") -> None:
linter.register_checker(TypeChecker(linter))
Expand Down

0 comments on commit d9d45ba

Please sign in to comment.