Skip to content

Commit b8a5841

Browse files
committed
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.
1 parent 39fe2a5 commit b8a5841

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

pyflakes/checker.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ class Binding(object):
9696
line number that this binding was last used
9797
"""
9898

99-
def __init__(self, name, source):
99+
def __init__(self, name, source, isglobal=False):
100100
self.name = name
101101
self.source = source
102102
self.used = False
103+
self.isglobal = isglobal
103104

104105
def __str__(self):
105106
return self.name
@@ -526,7 +527,7 @@ def handleNodeStore(self, node):
526527
binding = ExportBinding(name, node.parent, self.scope)
527528
else:
528529
binding = Assignment(name, node)
529-
if name in self.scope:
530+
if name in self.scope and self.scope[name].isglobal:
530531
binding.used = self.scope[name].used
531532
self.addBinding(node, binding)
532533

@@ -687,7 +688,7 @@ def GLOBAL(self, node):
687688

688689
# One 'global' statement can bind multiple (comma-delimited) names.
689690
for node_name in node.names:
690-
node_value = Assignment(node_name, node)
691+
node_value = Assignment(node_name, node, isglobal=True)
691692

692693
# Remove UndefinedName messages already reported for this name.
693694
self.messages = [

pyflakes/test/test_other.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,17 @@ def a():
523523
return
524524
''', m.UnusedVariable)
525525

526+
def test_unusedReassignedVariable(self):
527+
"""
528+
Shadowing a used variable can still raise an UnusedVariable warning.
529+
"""
530+
self.flakes('''
531+
def a():
532+
b = 1
533+
b.foo()
534+
b = 2
535+
''', m.UnusedVariable)
536+
526537
def test_assignToGlobal(self):
527538
"""
528539
Assigning to a global and then not using that global is perfectly
@@ -604,7 +615,7 @@ def f():
604615
if i > 2:
605616
return x
606617
x = i * 2
607-
''')
618+
''', m.UnusedVariable)
608619

609620
def test_tupleUnpacking(self):
610621
"""

pyflakes/test/test_undefined_names.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def _worker():
193193
while o is not True:
194194
del o
195195
o = False
196-
''')
196+
''', m.UnusedVariable)
197197

198198
def test_delWhileNested(self):
199199
"""
@@ -209,7 +209,7 @@ def _worker():
209209
with context():
210210
del o
211211
o = False
212-
''')
212+
''', m.UnusedVariable)
213213

214214
def test_globalFromNestedScope(self):
215215
"""Global names are available from nested scopes."""
@@ -315,6 +315,7 @@ def f(seq):
315315
d += 4
316316
e[any] = 5
317317
''',
318+
m.UnusedVariable, # a
318319
m.UndefinedName, # b
319320
m.UndefinedName, # c
320321
m.UndefinedName, m.UnusedVariable, # d

0 commit comments

Comments
 (0)