Skip to content

Commit ada6797

Browse files
committed
Merge pull request #27 from ryneeverett/used-global-import
Don't report UnusedImport when binding global name
2 parents c2086d6 + ad8e83d commit ada6797

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

pyflakes/checker.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ def addBinding(self, node, value):
448448
elif isinstance(existing, Importation) and value.redefines(existing):
449449
existing.redefined.append(node)
450450

451+
if value.name in self.scope:
452+
# then assume the rebound name is used as a global or within a loop
453+
value.used = self.scope[value.name].used
454+
451455
self.scope[value.name] = value
452456

453457
def getNodeHandler(self, node_class):
@@ -526,9 +530,6 @@ def handleNodeStore(self, node):
526530
binding = ExportBinding(name, node.parent, self.scope)
527531
else:
528532
binding = Assignment(name, node)
529-
if name in self.scope:
530-
# then assume the rebound name is used as a global or within a loop
531-
binding.used = self.scope[name].used
532533
self.addBinding(node, binding)
533534

534535
def handleNodeDelete(self, node):

pyflakes/test/test_imports.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,16 @@ def f(): global foo
525525
def g(): foo.is_used()
526526
''')
527527

528+
def test_assignedToGlobal(self):
529+
"""
530+
Binding an import to a declared global should not cause it to be
531+
reported as unused.
532+
"""
533+
self.flakes('''
534+
def f(): global foo; import foo
535+
def g(): foo.is_used()
536+
''')
537+
528538
@skipIf(version_info >= (3,), 'deprecated syntax')
529539
def test_usedInBackquote(self):
530540
self.flakes('import fu; `fu`')

0 commit comments

Comments
 (0)