diff --git a/src/specify_cli/workflows/expressions/engine.py b/src/specify_cli/workflows/expressions/engine.py index 0fc93ac7c2..edd876ddfa 100644 --- a/src/specify_cli/workflows/expressions/engine.py +++ b/src/specify_cli/workflows/expressions/engine.py @@ -50,7 +50,7 @@ from . import namespace from .filters import FILTER_TABLE, _EXPECTED_FILTERS from .namespace import _resolve_dot_path -from .operators import _safe_compare, _safe_membership +from .operators import _safe_compare, _safe_equals, _safe_membership from .template import ( _collapse_whitespace, _find_block_close, @@ -431,9 +431,9 @@ def _eval(node: Any, namespace_: dict[str, Any]) -> Any: left = _eval(node[2], namespace_) right = _eval(node[3], namespace_) if op == "==": - return left == right + return _safe_equals(left, right, negate=False) if op == "!=": - return left != right + return _safe_equals(left, right, negate=True) if op == "in": return _safe_membership(left, right, negate=False) if op == "not in": diff --git a/src/specify_cli/workflows/expressions/operators.py b/src/specify_cli/workflows/expressions/operators.py index bdf26128b6..3cd2a7585d 100644 --- a/src/specify_cli/workflows/expressions/operators.py +++ b/src/specify_cli/workflows/expressions/operators.py @@ -19,6 +19,30 @@ def _coerce_number(value: Any) -> Any: return value +def _coerce_numeric_pair(left: Any, right: Any) -> tuple[Any, Any]: + """Return both operands as numbers if both look numeric, else unchanged. + + Coercing only when *both* convert keeps a numeric string paired with a + plain string as two strings, rather than an int-vs-str mismatch. + """ + cl, cr = _coerce_number(left), _coerce_number(right) + if isinstance(cl, (int, float)) and isinstance(cr, (int, float)): + return cl, cr + return left, right + + +def _safe_equals(left: Any, right: Any, *, negate: bool) -> bool: + """Evaluate ``left == right`` (or ``!=``) with the same numeric coercion + as the ordering operators. + + Without it, a numeric string such as captured stdout ``"10"`` was both + ``>= 10`` and ``<= 10`` but not ``== 10``. + """ + left, right = _coerce_numeric_pair(left, right) + equal = left == right + return not equal if negate else equal + + def _safe_membership(left: Any, right: Any, *, negate: bool) -> bool: """Safely evaluate ``left in right`` (or ``not in``) without crashing. @@ -51,12 +75,7 @@ def _safe_compare(left: Any, right: Any, op: str) -> bool: coercion. A genuinely incomparable pair (e.g. number vs non-numeric string) raises ``TypeError`` and yields ``False``. """ - cl, cr = _coerce_number(left), _coerce_number(right) - # Only use the coerced numbers when both converted; otherwise a numeric - # string paired with a plain string would become an int-vs-str mismatch - # (always False) rather than a lexicographic string comparison. - if isinstance(cl, (int, float)) and isinstance(cr, (int, float)): - left, right = cl, cr + left, right = _coerce_numeric_pair(left, right) try: if op == ">": return left > right # type: ignore[operator] diff --git a/tests/test_workflows.py b/tests/test_workflows.py index 0d8b7d9bea..04fd5903cb 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -1630,6 +1630,31 @@ def test_comparison_greater_equal_and_less_equal(self): assert evaluate_expression("{{ inputs.n >= 'abc' }}", ctx) is False assert evaluate_expression("{{ inputs.n <= 'abc' }}", ctx) is False + def test_equality_coerces_numeric_strings_like_ordering(self): + """``==``/``!=`` agree with ``>=``/``<=`` for numeric strings. + + Captured stdout ``"10"`` was both ``>= 10`` and ``<= 10`` but not + ``== 10``, because only the ordering operators coerced numeric strings. + """ + from specify_cli.workflows.expressions import evaluate_condition + from specify_cli.workflows.base import StepContext + + ctx = StepContext( + steps={"count": {"output": {"stdout": "10"}}}, + inputs={"s10": "10", "f10": "10.0", "name": "10x"}, + ) + assert evaluate_condition("{{ steps.count.output.stdout >= 10 }}", ctx) is True + assert evaluate_condition("{{ steps.count.output.stdout <= 10 }}", ctx) is True + assert evaluate_condition("{{ steps.count.output.stdout == 10 }}", ctx) is True + assert evaluate_condition("{{ steps.count.output.stdout != 10 }}", ctx) is False + assert evaluate_condition("{{ steps.count.output.stdout == 11 }}", ctx) is False + assert evaluate_condition("{{ steps.count.output.stdout != 11 }}", ctx) is True + # Two numeric strings compare as numbers, as they do for ordering. + assert evaluate_condition("{{ inputs.s10 == inputs.f10 }}", ctx) is True + # A non-numeric string is still compared as-is. + assert evaluate_condition("{{ inputs.name == 10 }}", ctx) is False + assert evaluate_condition("{{ inputs.name == '10x' }}", ctx) is True + def test_boolean_literals_are_case_insensitive(self): """``{{ True }}``/``{{ TRUE }}`` resolve as booleans, as the ``.lower()`` branch intends; only lowercase was pinned before."""