diff options
author | Steven Myint <git@stevenmyint.com> | 2015-03-15 05:35:47 -0700 |
---|---|---|
committer | Steven Myint <git@stevenmyint.com> | 2015-03-20 08:05:47 -0700 |
commit | 1fc8eacfbc5093581769bf9ae605ee356f377db3 (patch) | |
tree | 9fa831a77f0b2d6b2cbaf56292efba8c7f3e5b12 | |
parent | 0b1ce4c316d53c9842f2df4ab08856f4f6dc0d83 (diff) | |
download | pyflakes-1fc8eacfbc5093581769bf9ae605ee356f377db3.tar.gz |
Properly support list comprehensions in Python 3
List comprehensions have their own scope in Python 3.
https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries
-rw-r--r-- | pyflakes/checker.py | 4 | ||||
-rw-r--r-- | pyflakes/test/test_imports.py | 2 | ||||
-rw-r--r-- | pyflakes/test/test_other.py | 4 | ||||
-rw-r--r-- | pyflakes/test/test_undefined_names.py | 9 |
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: |