Skip to content

Commit

Permalink
Merge pull request #8 from stitchfix/bugfix-py3-parse-function
Browse files Browse the repository at this point in the history
Fix ast parsing for functions in python 3
  • Loading branch information
kzielnicki authored Oct 21, 2017
2 parents b6e1ec6 + d0cef35 commit 76e246e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions nodebook/nodebookcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def visit_FunctionDef(self, node):
self.locals.add(node.name)
self.generic_visit(node)

def visit_arg(self, node):
self.locals.add(node.arg)

def visit_AugAssign(self, node):
target = node.target
while (type(target) is ast.Subscript):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='nodebook',
version='0.2.0',
version='0.2.1',
author='Kevin Zielnicki',
author_email='[email protected]',
license='Stitch Fix 2017',
Expand Down
10 changes: 10 additions & 0 deletions tests/test_nodebookcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ def test_multiline(self, rf):
assert rf.locals == {'pd', 'y'}
assert rf.imports == {'pandas'}

def test_function(self, rf):
code_tree = ast.parse(
"def add(x,y):\n"
" return x+y\n"
)
rf.visit(code_tree)
assert rf.inputs == set()
assert rf.locals == {'add', 'x', 'y'}
assert rf.imports == set()


class TestNodebook(object):
@pytest.fixture()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_pickledict.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def test_df(self, mydict):
df = pd.DataFrame({'a': [0, 1, 2], 'b': ['foo', 'bar', 'baz']})
mydict['test_df'] = df
assert mydict['test_df'].equals(df)

def test_func(self, mydict):
def add(a, b):
return a + b
mydict['test_func'] = add
assert mydict['test_func'](3,5) == 8

def test_immutability(self, mydict):
l = [1, 2, 3]
Expand Down

0 comments on commit 76e246e

Please sign in to comment.