π« Toolkit to help you get started with SDD or any other process!
fix(workflows): resolve negative list indices in expressions (#4416)
Ngo Quoc Vieta40bbcb691a7
diff --git a/src/specify_cli/workflows/expressions.py b/src/specify_cli/workflows/expressions.pyindex 5fb036ebf..3fc770fa0 100644--- a/src/specify_cli/workflows/expressions.py+++ b/src/specify_cli/workflows/expressions.py@@ -139,7 +139,7 @@ _EXPR_PATTERN = re.compile(r"\{\{(.+?)\}\}")# against it, and the condition gate below reuses it rather than describing the# same shape a second time, so widening what indexing accepts cannot leave the# evaluator and the gate disagreeing.-_INDEXED_SEGMENT = re.compile(r"^([\w-]+)\[(\d+)\]$")+_INDEXED_SEGMENT = re.compile(r"^([\w-]+)\[(-?\d+)\]$")_PLAIN_SEGMENT = re.compile(r"^[\w-]+$")@@ -147,12 +147,13 @@ _PLAIN_SEGMENT = re.compile(r"^[\w-]+$")def _resolve_dot_path(obj: Any, path: str) -> Any:"""Resolve a dotted path like ``steps.specify.output.file`` against *obj*.- Supports dict key access and list indexing (e.g., ``task_list[0]``).+ Supports dict key access and list indexing, including the negative form+ Python and Jinja2 both accept (e.g., ``task_list[0]``, ``task_list[-1]``)."""parts = path.split(".")current = objfor part in parts:- # Handle list indexing: name[0]+ # Handle list indexing: name[0], name[-1]idx_match = _INDEXED_SEGMENT.match(part)if idx_match:key, idx = idx_match.group(1), int(idx_match.group(2))@@ -160,7 +161,7 @@ def _resolve_dot_path(obj: Any, path: str) -> Any:current = current.get(key)else:return None- if isinstance(current, list) and 0 <= idx < len(current):+ if isinstance(current, list) and -len(current) <= idx < len(current):current = current[idx]else:return Nonediff --git a/tests/test_workflows.py b/tests/test_workflows.pyindex d5869887f..6918e2ce9 100644--- a/tests/test_workflows.py+++ b/tests/test_workflows.py@@ -1013,6 +1013,25 @@ class TestExpressions:result = evaluate_expression("{{ steps.tasks.output.task_list[0].file }}", ctx)assert result == "a.md"+ def test_negative_list_indexing(self):+ """``list[-1]`` resolves from the end, as Python and Jinja2 both do.++ Without it the index silently fell through to a dict lookup for the+ literal key ``"task_list[-1]"`` and produced ``None``, so a template+ reaching for the last element rendered empty with no error.+ """+ from specify_cli.workflows.expressions import evaluate_expression+ from specify_cli.workflows.base import StepContext++ ctx = StepContext(+ steps={"tasks": {"output": {"task_list": [{"file": "a.md"}, {"file": "b.md"}]}}}+ )+ assert evaluate_expression("{{ steps.tasks.output.task_list[-1].file }}", ctx) == "b.md"+ assert evaluate_expression("{{ steps.tasks.output.task_list[-2].file }}", ctx) == "a.md"+ # Out of range in either direction stays None rather than raising.+ assert evaluate_expression("{{ steps.tasks.output.task_list[-3] }}", ctx) is None+ assert evaluate_expression("{{ steps.tasks.output.task_list[2] }}", ctx) is None+def test_context_run_id_resolves(self):"""``{{ context.run_id }}`` resolves to ``StepContext.run_id``.diff --git a/tests/unit/test_condition_expression_block.py b/tests/unit/test_condition_expression_block.pyindex 528798d5e..1f7b43ed0 100644--- a/tests/unit/test_condition_expression_block.py+++ b/tests/unit/test_condition_expression_block.py@@ -946,16 +946,17 @@ def test_a_literal_never_reaches_the_resolver():def test_gate_reads_the_shared_indexed_segment_definition(monkeypatch):"""Widening `_INDEXED_SEGMENT` alone must reach the gate.- `steps.β¦β.task_list[-1]` is rejected today because `_INDEXED_SEGMENT` β the- one place `_resolve_dot_path` says what an index looks like β accepts digits- only. Widening it there and nowhere else must be enough; if the gate keeps- its own copy of the shape (as `_PATH_SEGMENT` used to), this fails.+ `steps.β¦β.task_list[+1]` is rejected today because `_INDEXED_SEGMENT` β the+ one place `_resolve_dot_path` says what an index looks like β accepts an+ optional minus sign and digits only. Widening it there and nowhere else must+ be enough; if the gate keeps its own copy of the shape (as `_PATH_SEGMENT`+ used to), this fails."""- path = "steps.tasks.output.task_list[-1].file"+ path = "steps.tasks.output.task_list[+1].file"assert expressions._unresolvable_term(path) is not Nonemonkeypatch.setattr(- expressions, "_INDEXED_SEGMENT", re.compile(r"^([\w-]+)\[(-?\d+)\]$")+ expressions, "_INDEXED_SEGMENT", re.compile(r"^([\w-]+)\[([+-]?\d+)\]$"))assert expressions._unresolvable_term(path) is None