From d8013bebd2de3d378b0b9179e47c386c8b1b4a74 Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Thu, 3 Dec 2026 16:31:11 +0800 Subject: [PATCH 1/4] fix(workflows): return operand values from or and like Python The workflow expression evaluator returned booleans from `and`/`or`: `{{ inputs.retries or 3 }}` evaluated to `True` where Python gives `3`, and `{{ 'a' and 'b' }}` gave `True` instead of `'b'`, so a default rendered into a step input or a command line came out as `--attempts True`. Follow Python: `or` returns the first truthy operand's value, `and` the first falsy one, and when no operand decides, the last operand's value. Evaluation still stops at the first deciding operand, so guards keep protecting the rest; validation still walks every operand through the parse tree; `if:` conditions keep their truthiness because the steps coerce the result with `bool()`. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- .../workflows/expressions/engine.py | 18 +++++---- tests/test_workflows.py | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/specify_cli/workflows/expressions/engine.py b/src/specify_cli/workflows/expressions/engine.py index ace0685347..25db19a142 100644 --- a/src/specify_cli/workflows/expressions/engine.py +++ b/src/specify_cli/workflows/expressions/engine.py @@ -472,15 +472,19 @@ def _eval(node: Any, namespace_: dict[str, Any]) -> Any: return True if tag in ("and", "or"): - # Left to right, stopping at the first operand that decides the result, - # so a guard protects the rest: a true operand decides `or`, a false one - # `and`. Validation walks the parse tree instead, so it still visits - # every operand. + # Python semantics: the result is an operand's value, not a boolean. + # Left to right, stopping at the first operand that decides the result + # so a guard protects the rest: `or` returns the first truthy operand's + # value, `and` the first falsy one, and when no operand decides the + # last operand's value is the result. Validation walks the parse tree + # instead, so it still visits every operand. decides = tag == "or" + value: Any = None for operand in node[1]: - if bool(_eval(operand, namespace_)) is decides: - return decides - return not decides + value = _eval(operand, namespace_) + if bool(value) is decides: + return value + return value raise ValueError(f"unsupported expression node {tag!r}") diff --git a/tests/test_workflows.py b/tests/test_workflows.py index 9363607118..fcf8577249 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -448,6 +448,43 @@ def test_long_boolean_chains_and_deep_parentheses_evaluate(self): ctx, ) + def test_or_returns_operand_values_like_python(self): + """`or` hands back the first truthy operand's value, not a boolean, so + `{{ inputs.retries or 3 }}` is `3`, exactly as in Python.""" + from specify_cli.workflows.expressions import evaluate_expression + from specify_cli.workflows.base import StepContext + + ctx = StepContext(inputs={"retries": 0, "name": ""}) + assert evaluate_expression("{{ inputs.retries or 3 }}", ctx) == 3 + assert evaluate_expression("{{ inputs.name or 'unnamed' }}", ctx) == "unnamed" + assert evaluate_expression("{{ 0 or 3 }}", ctx) == 3 + assert evaluate_expression("{{ '' or 'd' }}", ctx) == "d" + assert evaluate_expression("{{ [] or 'd' }}", ctx) == "d" + # When no operand decides, the last operand's value is the result. + assert evaluate_expression("{{ '' or 0 }}", ctx) == 0 + + def test_and_returns_operand_values_like_python(self): + """`and` hands back the first falsy operand's value, or the last + operand's value when every operand is truthy, as in Python.""" + from specify_cli.workflows.expressions import evaluate_expression + from specify_cli.workflows.base import StepContext + + ctx = StepContext() + assert evaluate_expression("{{ 'a' and 'b' }}", ctx) == "b" + assert evaluate_expression("{{ 0 and 'b' }}", ctx) == 0 + assert evaluate_expression("{{ 2 and 7 }}", ctx) == 7 + assert evaluate_expression("{{ '' and 0 }}", ctx) == "" + + def test_rendered_command_line_keeps_operand_values(self): + """A default inside a command line renders the value, not `True`.""" + from specify_cli.workflows.expressions import evaluate_expression + from specify_cli.workflows.base import StepContext + + ctx = StepContext() + assert evaluate_expression( + "retry --attempts {{ inputs.retries or 3 }} --tag demo", ctx + ) == "retry --attempts 3 --tag demo" + def test_list_literal_preserves_quoted_commas(self): from specify_cli.workflows.expressions import evaluate_expression from specify_cli.workflows.base import StepContext From 74b466c44c548fbe191cfd500d77474e21412a1b Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Thu, 3 Dec 2026 16:36:34 +0800 Subject: [PATCH 2/4] docs(workflows): describe and/or operand values Keep the expression documentation consistent with the engine change: `and`/`or` return the deciding operand's value as in Python, and a condition coerces that value to a boolean, so `if:` guards are unaffected. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- docs/workflows/expressions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/workflows/expressions.md b/docs/workflows/expressions.md index 3548702e28..49607cd0d4 100644 --- a/docs/workflows/expressions.md +++ b/docs/workflows/expressions.md @@ -94,7 +94,7 @@ Notes on the newer filters: condition: "{{ inputs.items | length }}" # 0 is False, any non-zero count is True ``` -`and` and `or` return `true` or `false` and evaluate their right side only when the left side does not decide the result: `and` skips it when the left side is false, and `or` skips it when the left side is true. A condition can therefore guard a filter that would fail on an empty value. `{{ inputs.payload != '' and (inputs.payload | from_json | length) }}` is false for an empty optional `payload` instead of failing the run on invalid JSON. When the right side is evaluated, its errors still fail the run. +`and` and `or` return the deciding operand's value, as in Python: `a or b` is the first truthy operand, `a and b` the first falsy one, and when no operand decides, the last operand's value. The right side is evaluated only when the left side does not decide the result: `and` skips it when the left side is false, and `or` skips it when the left side is true. A condition coerces that value to a boolean, so an `if:` guard sees the same truthiness as before, and it can therefore guard a filter that would fail on an empty value. `{{ inputs.payload != '' and (inputs.payload | from_json | length) }}` is false for an empty optional `payload` instead of failing the run on invalid JSON. When the right side is evaluated, its errors still fail the run. Operators bind from loosest to tightest as `or`, `and`, `not`, then the comparisons. All comparisons (`==`, `!=`, `>`, `<`, `>=`, `<=`, `in`, `not in`) share one level and chain as in Python and Jinja2: `{{ 0 <= inputs.n <= 10 }}` means `{{ 0 <= inputs.n and inputs.n <= 10 }}`, with the shared operand evaluated once and evaluation stopping at the first false pair — a later operand is evaluated only when every pair before it held, with the same guard behavior as `and`. Explicit parentheses end a chain: `{{ (0 <= inputs.n) <= 10 }}` compares the grouped `true`/`false` result with `10`, exactly as in Python. From cda55a43d0fb0f7dfd50860c880732d84b4a58c8 Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Thu, 3 Dec 2026 17:00:03 +0800 Subject: [PATCH 3/4] docs(workflows): state the string-normalization exception for and/or results `evaluate_condition()` normalizes string results: text that strips and lowercases to `false` is false and `true` is true. Since `and`/`or` now return operand values, an expression whose deciding operand is such a string evaluates differently, so drop the "same truthiness as before" promise and document the `{{ 'false' or 'fallback' }}` before/after, with a test pinning the condition-level behaviour. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- docs/workflows/expressions.md | 2 +- tests/test_workflows.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/workflows/expressions.md b/docs/workflows/expressions.md index 49607cd0d4..61ed609d58 100644 --- a/docs/workflows/expressions.md +++ b/docs/workflows/expressions.md @@ -94,7 +94,7 @@ Notes on the newer filters: condition: "{{ inputs.items | length }}" # 0 is False, any non-zero count is True ``` -`and` and `or` return the deciding operand's value, as in Python: `a or b` is the first truthy operand, `a and b` the first falsy one, and when no operand decides, the last operand's value. The right side is evaluated only when the left side does not decide the result: `and` skips it when the left side is false, and `or` skips it when the left side is true. A condition coerces that value to a boolean, so an `if:` guard sees the same truthiness as before, and it can therefore guard a filter that would fail on an empty value. `{{ inputs.payload != '' and (inputs.payload | from_json | length) }}` is false for an empty optional `payload` instead of failing the run on invalid JSON. When the right side is evaluated, its errors still fail the run. +`and` and `or` return the deciding operand's value, as in Python: `a or b` is the first truthy operand, `a and b` the first falsy one, and when no operand decides, the last operand's value. The right side is evaluated only when the left side does not decide the result: `and` skips it when the left side is false, and `or` skips it when the left side is true. A condition coerces that value the way it coerces any expression result: a string whose text, ignoring case and surrounding whitespace, equals `false` is false and `true` is true, and anything else follows its ordinary truthiness. So an expression whose deciding operand is such a string now evaluates differently: `{{ 'false' or 'fallback' }}` was the boolean `true` and is now the string `'false'`, which a condition reads as false. A condition can still guard a filter that would fail on an empty value. `{{ inputs.payload != '' and (inputs.payload | from_json | length) }}` is false for an empty optional `payload` instead of failing the run on invalid JSON. When the right side is evaluated, its errors still fail the run. Operators bind from loosest to tightest as `or`, `and`, `not`, then the comparisons. All comparisons (`==`, `!=`, `>`, `<`, `>=`, `<=`, `in`, `not in`) share one level and chain as in Python and Jinja2: `{{ 0 <= inputs.n <= 10 }}` means `{{ 0 <= inputs.n and inputs.n <= 10 }}`, with the shared operand evaluated once and evaluation stopping at the first false pair — a later operand is evaluated only when every pair before it held, with the same guard behavior as `and`. Explicit parentheses end a chain: `{{ (0 <= inputs.n) <= 10 }}` compares the grouped `true`/`false` result with `10`, exactly as in Python. diff --git a/tests/test_workflows.py b/tests/test_workflows.py index fcf8577249..e1f04d0c1f 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -485,6 +485,17 @@ def test_rendered_command_line_keeps_operand_values(self): "retry --attempts {{ inputs.retries or 3 }} --tag demo", ctx ) == "retry --attempts 3 --tag demo" + def test_condition_normalizes_and_or_operand_values(self): + """A condition coerces the value `and`/`or` return like any result, + string normalization included, so a deciding `'false'` reads false.""" + from specify_cli.workflows.expressions import evaluate_condition, evaluate_expression + from specify_cli.workflows.base import StepContext + + ctx = StepContext() + assert evaluate_expression("{{ 'false' or 'fallback' }}", ctx) == "false" + assert evaluate_condition("{{ 'false' or 'fallback' }}", ctx) is False + assert evaluate_condition("{{ inputs.missing or 'run' }}", ctx) is True + def test_list_literal_preserves_quoted_commas(self): from specify_cli.workflows.expressions import evaluate_expression from specify_cli.workflows.base import StepContext From 4b0cb24e3269da733c0eafb0b6e4bdd4c48c68e8 Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Thu, 3 Dec 2026 23:10:12 +0800 Subject: [PATCH 4/4] fix(workflows): do not truth-test the final and/or operand Python returns the last `and`/`or` operand without calling `bool()` on it. The evaluator truth-tested every operand it reached, so `{{ true and (inputs.value | custom) }}` failed when the custom filter returned a value whose `__bool__` raises, even though the value renders fine. Test only the deciding operands and return the last operand's value directly; short-circuiting and the validation walk are unchanged. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- .../workflows/expressions/engine.py | 13 +++-- tests/test_workflows.py | 49 +++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/specify_cli/workflows/expressions/engine.py b/src/specify_cli/workflows/expressions/engine.py index 25db19a142..306f509d2e 100644 --- a/src/specify_cli/workflows/expressions/engine.py +++ b/src/specify_cli/workflows/expressions/engine.py @@ -475,16 +475,19 @@ def _eval(node: Any, namespace_: dict[str, Any]) -> Any: # Python semantics: the result is an operand's value, not a boolean. # Left to right, stopping at the first operand that decides the result # so a guard protects the rest: `or` returns the first truthy operand's - # value, `and` the first falsy one, and when no operand decides the - # last operand's value is the result. Validation walks the parse tree + # value, `and` the first falsy one. Deciding operands are truth-tested, + # so the short-circuit keeps protecting the rest; the last operand's + # value is returned without a truth test, as in Python -- a custom + # filter may hand back a value whose ``__bool__`` raises, and the + # expression can still render it. Validation walks the parse tree # instead, so it still visits every operand. decides = tag == "or" - value: Any = None - for operand in node[1]: + operands = node[1] + for operand in operands[:-1]: value = _eval(operand, namespace_) if bool(value) is decides: return value - return value + return _eval(operands[-1], namespace_) raise ValueError(f"unsupported expression node {tag!r}") diff --git a/tests/test_workflows.py b/tests/test_workflows.py index e1f04d0c1f..975ef8f264 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -496,6 +496,55 @@ def test_condition_normalizes_and_or_operand_values(self): assert evaluate_condition("{{ 'false' or 'fallback' }}", ctx) is False assert evaluate_condition("{{ inputs.missing or 'run' }}", ctx) is True + def test_and_final_operand_is_not_truth_tested(self): + """`and` returns the last operand's value without calling `bool()` on + it, as in Python, so a value whose `__bool__` raises still comes out.""" + from specify_cli.workflows.expressions import evaluate_expression + from specify_cli.workflows.base import StepContext + from specify_cli.workflows.expressions.filters import ( + register_custom_filter, + unregister_custom_filter, + ) + + class Boolless(str): + def __bool__(self): + raise TypeError("__bool__ raises on this value") + + register_custom_filter("raises", lambda value: Boolless(value)) + try: + ctx = StepContext(inputs={"value": "rendered"}) + assert evaluate_expression( + "{{ true and (inputs.value | raises) }}", ctx + ) == "rendered" + # A deciding operand is still truth-tested, as in Python. + with pytest.raises(TypeError, match="__bool__ raises"): + evaluate_expression("{{ (inputs.value | raises) and 'x' }}", ctx) + finally: + unregister_custom_filter("raises") + + def test_or_final_operand_is_not_truth_tested(self): + """`or` likewise returns the last operand's value without a truth test + when no earlier operand decides.""" + from specify_cli.workflows.expressions import evaluate_expression + from specify_cli.workflows.base import StepContext + from specify_cli.workflows.expressions.filters import ( + register_custom_filter, + unregister_custom_filter, + ) + + class Boolless(str): + def __bool__(self): + raise TypeError("__bool__ raises on this value") + + register_custom_filter("raises", lambda value: Boolless(value)) + try: + ctx = StepContext(inputs={"value": "rendered"}) + assert evaluate_expression( + "{{ false or (inputs.value | raises) }}", ctx + ) == "rendered" + finally: + unregister_custom_filter("raises") + def test_list_literal_preserves_quoted_commas(self): from specify_cli.workflows.expressions import evaluate_expression from specify_cli.workflows.base import StepContext