diff --git a/nbparameterise/code_drivers/python.py b/nbparameterise/code_drivers/python.py index 6101d62..0171a21 100644 --- a/nbparameterise/code_drivers/python.py +++ b/nbparameterise/code_drivers/python.py @@ -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) @@ -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) @@ -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): diff --git a/tests/sample.ipynb b/tests/sample.ipynb index 48a7e6e..5c3028d 100644 --- a/tests/sample.ipynb +++ b/tests/sample.ipynb @@ -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\")" ] }, diff --git a/tests/test_basic.py b/tests/test_basic.py index 4840661..8ba3424 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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' @@ -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():