diff options
author | ryneeverett <ryneeverett@gmail.com> | 2015-08-13 22:56:51 -0400 |
---|---|---|
committer | ryneeverett <ryneeverett@gmail.com> | 2015-08-13 22:56:51 -0400 |
commit | b8a5841d9f4c47bd6fecf1473612587b3df0e301 (patch) | |
tree | 58761d4ef11eb36772fd8260501bcb5b7c85edfc | |
parent | 39fe2a5a19111f24f4b4af0aa431773340a7e1fb (diff) | |
download | pyflakes-b8a5841d9f4c47bd6fecf1473612587b3df0e301.tar.gz |
Don't mark shadowing variable as 'used'.
Shadowing a used variable should still potentially raise an
UnusedVariable warning. Alter tests' expected results to agree with
this premise.
-rw-r--r-- | pyflakes/checker.py | 7 | ||||
-rw-r--r-- | pyflakes/test/test_other.py | 13 | ||||
-rw-r--r-- | pyflakes/test/test_undefined_names.py | 5 |
3 files changed, 19 insertions, 6 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py index fcdce9c..088a7d4 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -96,10 +96,11 @@ class Binding(object): line number that this binding was last used """ - def __init__(self, name, source): + def __init__(self, name, source, isglobal=False): self.name = name self.source = source self.used = False + self.isglobal = isglobal def __str__(self): return self.name @@ -526,7 +527,7 @@ class Checker(object): binding = ExportBinding(name, node.parent, self.scope) else: binding = Assignment(name, node) - if name in self.scope: + if name in self.scope and self.scope[name].isglobal: binding.used = self.scope[name].used self.addBinding(node, binding) @@ -687,7 +688,7 @@ class Checker(object): # One 'global' statement can bind multiple (comma-delimited) names. for node_name in node.names: - node_value = Assignment(node_name, node) + node_value = Assignment(node_name, node, isglobal=True) # Remove UndefinedName messages already reported for this name. self.messages = [ diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py index ac116c8..d3de9c1 100644 --- a/pyflakes/test/test_other.py +++ b/pyflakes/test/test_other.py @@ -523,6 +523,17 @@ class TestUnusedAssignment(TestCase): return ''', m.UnusedVariable) + def test_unusedReassignedVariable(self): + """ + Shadowing a used variable can still raise an UnusedVariable warning. + """ + self.flakes(''' + def a(): + b = 1 + b.foo() + b = 2 + ''', m.UnusedVariable) + def test_assignToGlobal(self): """ Assigning to a global and then not using that global is perfectly @@ -604,7 +615,7 @@ class TestUnusedAssignment(TestCase): if i > 2: return x x = i * 2 - ''') + ''', m.UnusedVariable) def test_tupleUnpacking(self): """ diff --git a/pyflakes/test/test_undefined_names.py b/pyflakes/test/test_undefined_names.py index faaaf8c..7285423 100644 --- a/pyflakes/test/test_undefined_names.py +++ b/pyflakes/test/test_undefined_names.py @@ -193,7 +193,7 @@ class Test(TestCase): while o is not True: del o o = False - ''') + ''', m.UnusedVariable) def test_delWhileNested(self): """ @@ -209,7 +209,7 @@ class Test(TestCase): with context(): del o o = False - ''') + ''', m.UnusedVariable) def test_globalFromNestedScope(self): """Global names are available from nested scopes.""" @@ -315,6 +315,7 @@ class Test(TestCase): d += 4 e[any] = 5 ''', + m.UnusedVariable, # a m.UndefinedName, # b m.UndefinedName, # c m.UndefinedName, m.UnusedVariable, # d |