Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ast match of None value params #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion nbparameterise/code_drivers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def check_fillable_node(node, path):
return
elif isinstance(node, ast.NameConstant) and (node.value in (True, False)):
return
elif isinstance(node, ast.NameConstant) and (node.value is None):
return
elif isinstance(node, ast.List):
for n in node.elts:
check_fillable_node(n, path)
Expand All @@ -39,7 +41,7 @@ def check_fillable_node(node, path):
check_fillable_node(n, path)
return

raise astcheck.ASTMismatch(path, node, 'number, string, boolean, list or dict')
raise astcheck.ASTMismatch(path, node, 'none, number, string, boolean, list or dict')

definition_pattern = ast.Assign(targets=[ast.Name()], value=check_fillable_node)

Expand All @@ -54,6 +56,8 @@ def type_and_value(node, comments={}):
return list, [type_and_value(n)[1] for n in node.elts], comment
elif isinstance(node, ast.NameConstant) and (node.value in (True, False)):
return bool, node.value, comment
elif isinstance(node, ast.NameConstant) and (node.value is None):
return type(None), node.value, comment
elif isinstance(node, ast.Dict):
return dict, {type_and_value(node.keys[i])[1]: type_and_value(node.values[i])[1] for i in range(len(node.keys))}, comment
elif isinstance(node, ast.UnaryOp):
Expand Down
1 change: 1 addition & 0 deletions tests/sample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"d = False # comment:bool\n",
"e = [0, 1.0, True, \"text\", [0, 1]]\n",
"f = {0: 0, \"item\": True, \"dict\": {0: \"text\"}} # comment:dict\n",
"g = None\n",
"print(\"This should be ignored\")"
]
},
Expand Down
7 changes: 5 additions & 2 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_extract(self):
Parameter('d', bool, False),
Parameter('e', list, [0, 1.0, True, "text", [0, 1]]),
Parameter('f', dict, {0: 0, "item": True, "dict": {0: "text"}}),
Parameter('g', type(None), None),
]
assert self.params[4].comment == '# comment:bool'
assert self.params[6].comment == '# comment:dict'
Expand Down Expand Up @@ -50,16 +51,18 @@ def test_rebuild(self):
def test_new_values(self):
params = code.parameter_values(self.params,
a = "New text",
c = 12.0
c = 12.0,
g = "Not none",
)

assert [p.name for p in params] == ['a', 'b', 'b2', 'c', 'd', 'e', 'f']
assert [p.name for p in params] == ['a', 'b', 'b2', 'c', 'd', 'e', 'f', 'g']

assert params[0].value == 'New text'
assert params[1].value == 12
assert params[3].value == 12.0
assert isinstance(params[3].value, float)
assert params[4].value == False
assert params[7].value == "Not none"


def test_parameter_repr():
Expand Down