From a3eed737212ef8ac6a6ac740da529791651603b2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 30 Oct 2018 13:44:06 -0700 Subject: Consider AsyncFor the same as For in variable contexts (#380) --- pyflakes/checker.py | 10 ++++++---- pyflakes/test/test_other.py | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pyflakes/checker.py b/pyflakes/checker.py index c2f8d2e..2060ce4 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -56,8 +56,10 @@ else: return [n.body + n.orelse] + [[hdl] for hdl in n.handlers] if PY34: + FOR_TYPES = (ast.For,) LOOP_TYPES = (ast.While, ast.For) else: + FOR_TYPES = (ast.For, ast.AsyncFor) LOOP_TYPES = (ast.While, ast.For, ast.AsyncFor) @@ -637,7 +639,7 @@ class Checker(object): messg = messages.UnusedImport self.report(messg, value.source, str(value)) for node in value.redefined: - if isinstance(self.getParent(node), ast.For): + if isinstance(self.getParent(node), FOR_TYPES): messg = messages.ImportShadowedByLoopVar elif used: continue @@ -717,14 +719,14 @@ class Checker(object): not self.differentForks(node, existing.source)): parent_stmt = self.getParent(value.source) - if isinstance(existing, Importation) and isinstance(parent_stmt, ast.For): + if isinstance(existing, Importation) and isinstance(parent_stmt, FOR_TYPES): self.report(messages.ImportShadowedByLoopVar, node, value.name, existing.source) elif scope is self.scope: if (isinstance(parent_stmt, ast.comprehension) and not isinstance(self.getParent(existing.source), - (ast.For, ast.comprehension))): + (FOR_TYPES, ast.comprehension))): self.report(messages.RedefinedInListComp, node, value.name, existing.source) elif not existing.used and value.redefines(existing): @@ -834,7 +836,7 @@ class Checker(object): break parent_stmt = self.getParent(node) - if isinstance(parent_stmt, (ast.For, ast.comprehension)) or ( + if isinstance(parent_stmt, (FOR_TYPES, ast.comprehension)) or ( parent_stmt != node.parent and not self.isLiteralTupleUnpacking(parent_stmt)): binding = Binding(name, node) diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py index 2d6bdac..e5da024 100644 --- a/pyflakes/test/test_other.py +++ b/pyflakes/test/test_other.py @@ -1788,6 +1788,14 @@ class TestAsyncStatements(TestCase): return output ''') + @skipIf(version_info < (3, 5), 'new in Python 3.5') + def test_asyncForUnderscoreLoopVar(self): + self.flakes(''' + async def coro(it): + async for _ in it: + pass + ''') + @skipIf(version_info < (3, 5), 'new in Python 3.5') def test_loopControlInAsyncFor(self): self.flakes(''' -- cgit v1.2.1