Skip to content

Commit 12653ca

Browse files
committed
Merge pull request #26 from ryneeverett/unused-reassigned-variable
Add tests to document hard to test cases It would be nice if shadowing an unused variable was an error, but this is quite difficult to implement in a way which does not also raise false positives in the context of loops.
2 parents 1c5bbdf + ec8dbbb commit 12653ca

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

pyflakes/checker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ def handleNodeStore(self, node):
527527
else:
528528
binding = Assignment(name, node)
529529
if name in self.scope:
530+
# then assume the rebound name is used as a global or within a loop
530531
binding.used = self.scope[name].used
531532
self.addBinding(node, binding)
532533

pyflakes/test/test_other.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,30 @@ def a():
523523
return
524524
''', m.UnusedVariable)
525525

526+
@skip("todo: Difficult because it does't apply in the context of a loop")
527+
def test_unusedReassignedVariable(self):
528+
"""
529+
Shadowing a used variable can still raise an UnusedVariable warning.
530+
"""
531+
self.flakes('''
532+
def a():
533+
b = 1
534+
b.foo()
535+
b = 2
536+
''', m.UnusedVariable)
537+
538+
def test_variableUsedInLoop(self):
539+
"""
540+
Shadowing a used variable cannot raise an UnusedVariable warning in the
541+
context of a loop.
542+
"""
543+
self.flakes('''
544+
def a():
545+
b = True
546+
while b:
547+
b = False
548+
''')
549+
526550
def test_assignToGlobal(self):
527551
"""
528552
Assigning to a global and then not using that global is perfectly

0 commit comments

Comments
 (0)