summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2018-10-30 13:44:06 -0700
committerIan Stapleton Cordasco <graffatcolmingov@gmail.com>2018-10-30 15:44:06 -0500
commita3eed737212ef8ac6a6ac740da529791651603b2 (patch)
treedbc2b383d700bea2858e438bc5198ba1562ce7bb
parent45fc732466056fe35c85936ff25491df7905c597 (diff)
downloadpyflakes-a3eed737212ef8ac6a6ac740da529791651603b2.tar.gz
Consider AsyncFor the same as For in variable contexts (#380)
-rw-r--r--pyflakes/checker.py10
-rw-r--r--pyflakes/test/test_other.py8
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
@@ -1789,6 +1789,14 @@ class TestAsyncStatements(TestCase):
''')
@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('''
async def read_data(db):