Skip to content

Commit

Permalink
Add checker for local variables inside functions
Browse files Browse the repository at this point in the history
Do not signal globals as local variables
  • Loading branch information
stephan-hof committed Aug 12, 2012
1 parent 14add34 commit 31a9435
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pep8.py
Expand Up @@ -1251,6 +1251,32 @@ def visit_importfrom(self, node, parents):
self.error_at_node(node, self.W803)


class VariablesInFunctionsASTCheck(BaseAstCheck):
"""
Local variables in functions should be in lowercase
"""
LOWER_CASE = re.compile('[a-z][a-z0-9_]*$').match
E805 = "E805 Variables in functions should be lowercase"

def visit_assign(self, node, parents):
parent_func = self.get_parent_function(parents)
if parent_func is None:
return

for name in self._get_target_names(node):
if name in parent_func.global_names:
return
if not self.LOWER_CASE(name):
self.error_at_node(node, self.E805)

def _get_target_names(self, node):
targets = set()
for target in node.targets:
if isinstance(target, ast.Name):
targets.add(target.id)
return targets


##############################################################################
# Helper functions
##############################################################################
Expand Down
34 changes: 34 additions & 0 deletions testsuite/E85.py
@@ -0,0 +1,34 @@
#: Okay
def test():
good = 1
#: Okay
def test():
def test2():
good = 1
#: Okay
GOOD = 1
#: Okay
class Test(object):
GOOD = 1
#: E805
def test():
Bad = 1
#: E805
def test():
VERY = 2
#: E805
def test():
def test2():
class Foo(object):
def test3(self):
Bad = 3
#: Okay
def good():
global Bad
Bad = 1
#: E805
def bad():
global Bad

def foo():
Bad = 1

0 comments on commit 31a9435

Please sign in to comment.