fix(params): recognise scad.var(name=..., default=...) keyword form

The editor only read the default from the second positional argument, so
keyword declarations were silently ignored.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NiJingzhe
2026-10-20 21:51:31 +08:00
parent ac080ac635
commit 332d95cd80
2 changed files with 20 additions and 3 deletions
+15 -3
View File
@@ -193,9 +193,9 @@ def _read_value(node: ast.expr) -> _Value | None:
if (
isinstance(node, ast.Call)
and _callee(node.func) == "var"
and len(node.args) >= 2
and _var_default_node(node) is not None
):
default = _literal(node.args[1])
default = _literal(_var_default_node(node))
if default is None or isinstance(default, bool):
return None
unit = comment = None
@@ -212,6 +212,17 @@ def _read_value(node: ast.expr) -> _Value | None:
return None
def _var_default_node(call: ast.Call) -> ast.expr | None:
"""The default argument of ``var(name, default, ...)``, positional or ``default=``."""
if len(call.args) >= 2:
return call.args[1]
for keyword in call.keywords:
if keyword.arg == "default":
return keyword.value
return None
def _callee(node: ast.expr) -> str | None:
if isinstance(node, ast.Name):
return node.id
@@ -595,7 +606,8 @@ def commit_value(code: str, name: str, value: float | int | bool) -> str | None:
node = statement.value
if declared.declared == "var":
assert isinstance(node, ast.Call)
node = node.args[1]
node = _var_default_node(node)
assert node is not None
text = _format_value(declared.default, value)
if text is None or node.end_lineno != node.lineno:
return None
+5
View File
@@ -112,6 +112,11 @@ class CommitValueTest(unittest.TestCase):
assert out is not None
self.assertIn('OD = scad.var("od", 120.0, comment="é", unit="mm") # 外径', out)
def test_var_with_keyword_name_and_default_is_recognised(self) -> None:
code = 'J = scad.var(name="j1", default=0.0, comment="joint")\n'
out = commit_value(code, "J", 15)
self.assertEqual(out, 'J = scad.var(name="j1", default=15.0, comment="joint")\n')
def test_refuses_what_does_not_fit(self) -> None:
self.assertIsNone(commit_value(self.CODE, "MISSING", 1))
self.assertIsNone(commit_value(self.CODE, "F", 1.0))