Skip to content

Commit 2d5ec1d

Browse files
fix(workflows): address review feedback on github#3468
github#3447 was fixed independently by github#3448 (merged first), which added the same _safe_membership helper this branch introduced. Per Copilot review: - Revert the redundant _safe_contains rename in expressions.py so the file matches main; the working membership guard already lives there. - Drop the duplicate test_in_operator_non_iterable_right_operand test and fold its only new coverage (not in against float/bool/None right operands, which the base test only checked for the int case) into the existing test_membership_against_non_iterable_is_false_not_error. Also merges latest upstream/main into the branch. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent bd17df9 commit 2d5ec1d

2 files changed

Lines changed: 21 additions & 41 deletions

File tree

‎src/specify_cli/workflows/expressions.py‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,9 @@ def _evaluate_simple_expression(expr: str, namespace: dict[str, Any]) -> Any:
464464
if op == "<=":
465465
return _safe_compare(left, right, "<=")
466466
if op == " in ":
467-
return _safe_contains(left, right)
467+
return _safe_membership(left, right, negate=False)
468468
if op == " not in ":
469-
return not _safe_contains(left, right)
469+
return _safe_membership(left, right, negate=True)
470470

471471
# Numeric literal
472472
try:
@@ -511,21 +511,24 @@ def _coerce_number(value: Any) -> Any:
511511
return value
512512

513513

514-
def _safe_contains(left: Any, right: Any) -> bool:
515-
"""Return ``left in right`` safely.
514+
def _safe_membership(left: Any, right: Any, *, negate: bool) -> bool:
515+
"""Safely evaluate ``left in right`` (or ``not in``) without crashing.
516516
517-
Returns ``False`` when *right* is ``None`` or when the membership test raises
518-
``TypeError`` (e.g. non-iterable *right*, unhashable *left*, or incompatible
519-
operand types), instead of propagating the exception and crashing the workflow.
520-
521-
``not in`` is derived by negating this result.
517+
``left in right`` raises ``TypeError`` whenever the operands don't support
518+
membership testing — most commonly a non-iterable right operand (``None``,
519+
an int, a bool), but also cases like an unhashable ``left`` against a set.
520+
In every such case the membership relation is undefined, so treat it as
521+
``False`` (``not in`` as ``True``) rather than leaking the error out of the
522+
evaluator and crashing the whole workflow. Mirrors the graceful
523+
``TypeError`` handling in ``_safe_compare`` for the ordering operators, and
524+
generalizes the previous ``right is not None`` guard to any operand pair
525+
that can't be membership-tested.
522526
"""
523-
if right is None:
524-
return False
525527
try:
526-
return left in right
528+
contained = left in right
527529
except TypeError:
528-
return False
530+
contained = False
531+
return not contained if negate else contained
529532

530533

531534
def _safe_compare(left: Any, right: Any, op: str) -> bool:

‎tests/test_workflows.py‎

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -439,29 +439,6 @@ def test_operator_splitting_is_quote_aware(self):
439439
assert evaluate_expression("{{ inputs.a == 9 or inputs.b == 2 }}", plain) is True
440440
assert evaluate_expression("{{ inputs.missing | default('a and b') }}", plain) == "a and b"
441441

442-
def test_in_operator_non_iterable_right_operand(self):
443-
"""`in`/`not in` against a non-iterable right operand must not crash.
444-
445-
`left in right` raises TypeError when right is an int/bool/float, which
446-
used to leak a raw traceback and crash the run. It should evaluate like
447-
the `right is None` branch beside it: nothing is contained in a
448-
non-container, so `in` -> False and `not in` -> True (issue #3447).
449-
"""
450-
from specify_cli.workflows.expressions import evaluate_condition
451-
from specify_cli.workflows.base import StepContext
452-
453-
ctx = StepContext(inputs={"tag": "x", "count": 5, "flag": True, "ratio": 1.5})
454-
# in -> False, not in -> True for every non-iterable right operand.
455-
for right in ("count", "flag", "ratio"):
456-
assert evaluate_condition(f"{{{{ inputs.tag in inputs.{right} }}}}", ctx) is False
457-
assert evaluate_condition(f"{{{{ inputs.tag not in inputs.{right} }}}}", ctx) is True
458-
# A missing (None) right operand keeps the same behavior.
459-
assert evaluate_condition("{{ inputs.tag in inputs.missing }}", ctx) is False
460-
assert evaluate_condition("{{ inputs.tag not in inputs.missing }}", ctx) is True
461-
# Genuine containment against iterables still works (no regression).
462-
assert evaluate_condition("{{ 'cat' in 'cat and dog' }}", StepContext()) is True
463-
assert evaluate_condition("{{ 'zzz' not in 'cat and dog' }}", StepContext()) is True
464-
465442
def test_pipe_detection_is_quote_aware(self):
466443
from specify_cli.workflows.expressions import evaluate_expression
467444
from specify_cli.workflows.base import StepContext
@@ -497,11 +474,11 @@ def test_membership_against_non_iterable_is_false_not_error(self):
497474
# previous `right is not None` guard and mirrors _safe_compare, which
498475
# already swallows TypeError for the ordering operators.
499476
ctx = StepContext(inputs={"tag": "x", "count": 5, "ratio": 1.5, "flag": True})
500-
assert evaluate_expression("{{ inputs.tag in inputs.count }}", ctx) is False
501-
assert evaluate_expression("{{ inputs.tag not in inputs.count }}", ctx) is True
502-
assert evaluate_expression("{{ 'a' in inputs.ratio }}", ctx) is False
503-
assert evaluate_expression("{{ 'a' in inputs.flag }}", ctx) is False
504-
assert evaluate_expression("{{ inputs.tag in inputs.missing }}", ctx) is False
477+
# `in` -> False and `not in` -> True for every non-iterable right
478+
# operand (int, float, bool, None), so neither operator can drift.
479+
for right in ("count", "ratio", "flag", "missing"):
480+
assert evaluate_expression(f"{{{{ inputs.tag in inputs.{right} }}}}", ctx) is False
481+
assert evaluate_expression(f"{{{{ inputs.tag not in inputs.{right} }}}}", ctx) is True
505482
# A condition that would otherwise crash the run now evaluates cleanly.
506483
assert evaluate_condition("{{ inputs.tag in inputs.count }}", ctx) is False
507484

0 commit comments

Comments
 (0)