Skip to content

Commit

Permalink
added check for function arg is list
Browse files Browse the repository at this point in the history
  • Loading branch information
omaraboumrad committed Oct 22, 2015
1 parent fe44e09 commit aa49ebe
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ EQUALS TRUE/FALSE

a == True

FUNCTION ARG IS LIST

def foo(x, y=[]):
pass

## FAQ

How do I make the script return a none-0 code when any result found?
Expand Down
2 changes: 1 addition & 1 deletion mastool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Mastool Module
"""
VERSION = '0.0.8'
VERSION = '0.0.9'
1 change: 0 additions & 1 deletion mastool/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def importfrom_names(names):
return [n.name for n in names]



def labeled(text):
"""
decorator to give practices labels
Expand Down
35 changes: 35 additions & 0 deletions mastool/practices.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,38 @@ def find_equals_true_or_false(tree):
found.append(node.lineno)

return found


@h.labeled('DefaultArgIsList')
def find_default_arg_is_list(tree):
"""
>>> code = '''def foo(x, y):
... pass
... '''
>>> tree = ast.parse(code)
>>> assert find_default_arg_is_list(tree) == []
>>> code = '''def foo(x, y=[]):
... pass
... '''
>>> tree = ast.parse(code)
>>> assert find_default_arg_is_list(tree) == [1]
>>> code = '''def foo(x, y=[1,2,3]):
... pass
... '''
>>> tree = ast.parse(code)
>>> assert find_default_arg_is_list(tree) == [1]
"""
found = []

for node in ast.walk(tree):
checks = (
isinstance(node, ast.FunctionDef)
and any([n for n in node.args.defaults if isinstance(n, ast.List)])
)

if checks:
found.append(node.lineno)

return found

0 comments on commit aa49ebe

Please sign in to comment.