Skip to content

Commit d75f928

Browse files
fix(workflows): coerce numeric strings for == and != like ordering operators (github#5310)
`<`, `>`, `<=` and `>=` compare two numeric-looking operands as numbers, but `==` and `!=` compared raw values, so captured stdout "10" was both `>= 10` and `<= 10` yet not `== 10`. Route equality through the same both-sides-numeric coercion as `_safe_compare`. Fixes github#5072 Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous) Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 32852b5 commit d75f928

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎src/specify_cli/workflows/expressions/engine.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from . import namespace
5151
from .filters import FILTER_TABLE, _EXPECTED_FILTERS
5252
from .namespace import _resolve_dot_path
53-
from .operators import _safe_compare, _safe_membership
53+
from .operators import _safe_compare, _safe_equals, _safe_membership
5454
from .template import (
5555
_collapse_whitespace,
5656
_find_block_close,
@@ -431,9 +431,9 @@ def _eval(node: Any, namespace_: dict[str, Any]) -> Any:
431431
left = _eval(node[2], namespace_)
432432
right = _eval(node[3], namespace_)
433433
if op == "==":
434-
return left == right
434+
return _safe_equals(left, right, negate=False)
435435
if op == "!=":
436-
return left != right
436+
return _safe_equals(left, right, negate=True)
437437
if op == "in":
438438
return _safe_membership(left, right, negate=False)
439439
if op == "not in":

‎src/specify_cli/workflows/expressions/operators.py‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ def _coerce_number(value: Any) -> Any:
1919
return value
2020

2121

22+
def _coerce_numeric_pair(left: Any, right: Any) -> tuple[Any, Any]:
23+
"""Return both operands as numbers if both look numeric, else unchanged.
24+
25+
Coercing only when *both* convert keeps a numeric string paired with a
26+
plain string as two strings, rather than an int-vs-str mismatch.
27+
"""
28+
cl, cr = _coerce_number(left), _coerce_number(right)
29+
if isinstance(cl, (int, float)) and isinstance(cr, (int, float)):
30+
return cl, cr
31+
return left, right
32+
33+
34+
def _safe_equals(left: Any, right: Any, *, negate: bool) -> bool:
35+
"""Evaluate ``left == right`` (or ``!=``) with the same numeric coercion
36+
as the ordering operators.
37+
38+
Without it, a numeric string such as captured stdout ``"10"`` was both
39+
``>= 10`` and ``<= 10`` but not ``== 10``.
40+
"""
41+
left, right = _coerce_numeric_pair(left, right)
42+
equal = left == right
43+
return not equal if negate else equal
44+
45+
2246
def _safe_membership(left: Any, right: Any, *, negate: bool) -> bool:
2347
"""Safely evaluate ``left in right`` (or ``not in``) without crashing.
2448
@@ -51,12 +75,7 @@ def _safe_compare(left: Any, right: Any, op: str) -> bool:
5175
coercion. A genuinely incomparable pair (e.g. number vs non-numeric string)
5276
raises ``TypeError`` and yields ``False``.
5377
"""
54-
cl, cr = _coerce_number(left), _coerce_number(right)
55-
# Only use the coerced numbers when both converted; otherwise a numeric
56-
# string paired with a plain string would become an int-vs-str mismatch
57-
# (always False) rather than a lexicographic string comparison.
58-
if isinstance(cl, (int, float)) and isinstance(cr, (int, float)):
59-
left, right = cl, cr
78+
left, right = _coerce_numeric_pair(left, right)
6079
try:
6180
if op == ">":
6281
return left > right # type: ignore[operator]

‎tests/test_workflows.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,31 @@ def test_comparison_greater_equal_and_less_equal(self):
16301630
assert evaluate_expression("{{ inputs.n >= 'abc' }}", ctx) is False
16311631
assert evaluate_expression("{{ inputs.n <= 'abc' }}", ctx) is False
16321632

1633+
def test_equality_coerces_numeric_strings_like_ordering(self):
1634+
"""``==``/``!=`` agree with ``>=``/``<=`` for numeric strings.
1635+
1636+
Captured stdout ``"10"`` was both ``>= 10`` and ``<= 10`` but not
1637+
``== 10``, because only the ordering operators coerced numeric strings.
1638+
"""
1639+
from specify_cli.workflows.expressions import evaluate_condition
1640+
from specify_cli.workflows.base import StepContext
1641+
1642+
ctx = StepContext(
1643+
steps={"count": {"output": {"stdout": "10"}}},
1644+
inputs={"s10": "10", "f10": "10.0", "name": "10x"},
1645+
)
1646+
assert evaluate_condition("{{ steps.count.output.stdout >= 10 }}", ctx) is True
1647+
assert evaluate_condition("{{ steps.count.output.stdout <= 10 }}", ctx) is True
1648+
assert evaluate_condition("{{ steps.count.output.stdout == 10 }}", ctx) is True
1649+
assert evaluate_condition("{{ steps.count.output.stdout != 10 }}", ctx) is False
1650+
assert evaluate_condition("{{ steps.count.output.stdout == 11 }}", ctx) is False
1651+
assert evaluate_condition("{{ steps.count.output.stdout != 11 }}", ctx) is True
1652+
# Two numeric strings compare as numbers, as they do for ordering.
1653+
assert evaluate_condition("{{ inputs.s10 == inputs.f10 }}", ctx) is True
1654+
# A non-numeric string is still compared as-is.
1655+
assert evaluate_condition("{{ inputs.name == 10 }}", ctx) is False
1656+
assert evaluate_condition("{{ inputs.name == '10x' }}", ctx) is True
1657+
16331658
def test_boolean_literals_are_case_insensitive(self):
16341659
"""``{{ True }}``/``{{ TRUE }}`` resolve as booleans, as the ``.lower()``
16351660
branch intends; only lowercase was pinned before."""

0 commit comments

Comments
 (0)