summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Frost <indigo@bitglue.com>2015-03-20 11:51:05 -0400
committerPhil Frost <indigo@bitglue.com>2015-03-20 11:51:05 -0400
commit03e7a01268674e9e33cb47fdd434e7006c35b23c (patch)
tree961771637c45dd315065078ac8dc281cd6c2ad89
parenteb71c715ef3de565c4c36c01785ac4710873d5af (diff)
parent1fc8eacfbc5093581769bf9ae605ee356f377db3 (diff)
downloadpyflakes-03e7a01268674e9e33cb47fdd434e7006c35b23c.tar.gz
Merge pull request #14 from myint/comprehension
Properly support list comprehensions in Python 3
-rw-r--r--pyflakes/checker.py4
-rw-r--r--pyflakes/test/test_imports.py2
-rw-r--r--pyflakes/test/test_other.py4
-rw-r--r--pyflakes/test/test_undefined_names.py9
4 files changed, 18 insertions, 1 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 0ff1bb1..df2f907 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -672,7 +672,7 @@ class Checker(object):
EQ = NOTEQ = LT = LTE = GT = GTE = IS = ISNOT = IN = NOTIN = ignore
# additional node types
- LISTCOMP = COMPREHENSION = KEYWORD = handleChildren
+ COMPREHENSION = KEYWORD = handleChildren
def GLOBAL(self, node):
"""
@@ -688,6 +688,8 @@ class Checker(object):
self.handleChildren(node)
self.popScope()
+ LISTCOMP = handleChildren if PY2 else GENERATOREXP
+
DICTCOMP = SETCOMP = GENERATOREXP
def NAME(self, node):
diff --git a/pyflakes/test/test_imports.py b/pyflakes/test/test_imports.py
index 56149b5..389ebbf 100644
--- a/pyflakes/test/test_imports.py
+++ b/pyflakes/test/test_imports.py
@@ -473,6 +473,8 @@ class Test(TestCase):
self.flakes('import fu; [fu for _ in range(1)]')
self.flakes('import fu; [1 for _ in range(1) if fu]')
+ @skipIf(version_info >= (3,),
+ 'in Python 3 list comprehensions execute in a separate scope')
def test_redefinedByListComp(self):
self.flakes('import fu; [1 for fu in range(1)]',
m.RedefinedInListComp)
diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py
index 966c723..6e5950e 100644
--- a/pyflakes/test/test_other.py
+++ b/pyflakes/test/test_other.py
@@ -21,6 +21,8 @@ class Test(TestCase):
f()
''', m.UndefinedLocal, m.UnusedVariable)
+ @skipIf(version_info >= (3,),
+ 'in Python 3 list comprehensions execute in a separate scope')
def test_redefinedInListComp(self):
"""
Test that shadowing a variable in a list comprehension raises
@@ -220,6 +222,8 @@ class Test(TestCase):
[a for a in '12']
''')
+ @skipIf(version_info >= (3,),
+ 'in Python 3 list comprehensions execute in a separate scope')
def test_redefinedElseInListComp(self):
"""
Test that shadowing a variable in a list comprehension in
diff --git a/pyflakes/test/test_undefined_names.py b/pyflakes/test/test_undefined_names.py
index bbded24..67c3f1f 100644
--- a/pyflakes/test/test_undefined_names.py
+++ b/pyflakes/test/test_undefined_names.py
@@ -13,6 +13,15 @@ class Test(TestCase):
def test_definedInListComp(self):
self.flakes('[a for a in range(10) if a]')
+ @skipIf(version_info < (3,),
+ 'in Python 2 list comprehensions execute in the same scope')
+ def test_undefinedInListComp(self):
+ self.flakes('''
+ [a for a in range(10)]
+ a
+ ''',
+ m.UndefinedName)
+
def test_functionsNeedGlobalScope(self):
self.flakes('''
class a: