Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #25 from iElectric/patch-1

don't issue Unused Variables warning when using locals() in current scope
  • Loading branch information...
commit 1e36a5331100a1ddfb8899fd20359844aca4492c 2 parents 20f1c29 + 947e988
@kevinw authored
Showing with 32 additions and 0 deletions.
  1. +3 −0  NEWS.txt
  2. +6 −0 pyflakes/checker.py
  3. +23 −0 pyflakes/test/test_other.py
View
3  NEWS.txt
@@ -1,3 +1,6 @@
+0.4.1 (xxx):
+ - Don't issue Unused Variable warning when using locals() in current scope
+
0.4.0 (2009-11-25):
- Fix reporting for certain SyntaxErrors which lack line number
information.
View
6 pyflakes/checker.py
@@ -131,6 +131,7 @@ def names(self):
class Scope(dict):
importStarred = False # set to True when import * is found
+ usesLocals = False
def __repr__(self):
@@ -428,6 +429,10 @@ def NAME(self, node):
"""
Handle occurrence of Name (which can be a load/store/delete access.)
"""
+ if node.id == 'locals' and isinstance(node.parent, _ast.Call):
+ # we are doing locals() call in current scope
+ self.scope.usesLocals = True
+
# Locate the name in locals / function / globals scopes.
if isinstance(node.ctx, (_ast.Load, _ast.AugLoad)):
# try local scope
@@ -569,6 +574,7 @@ def checkUnusedAssignments():
"""
for name, binding in self.scope.iteritems():
if (not binding.used and not name in self.scope.globals
+ and not self.scope.usesLocals
and isinstance(binding, Assignment)):
self.report(messages.UnusedVariable,
binding.source, name)
View
23 pyflakes/test/test_other.py
@@ -202,6 +202,29 @@ def a():
''', m.UnusedVariable)
+ def test_unusedVariable_asLocals(self):
+ """
+ Using locals() it is perfectly valid to have unused variables
+ """
+ self.flakes('''
+ def a():
+ b = 1
+ return locals()
+ ''')
+
+ def test_unusedVariable_noLocals(self):
+ """
+ Using locals() in wrong scope should not matter
+ """
+ self.flakes('''
+ def a():
+ locals()
+ def a():
+ b = 1
+ return
+ ''', m.UnusedVariable)
+
+
def test_assignToGlobal(self):
"""
Assigning to a global and then not using that global is perfectly
Please sign in to comment.
Something went wrong with that request. Please try again.