Skip to content

Support exponent numbers and chained indexing in workflow expressions - #5523

Open
yxshee wants to merge 2 commits into
github:mainfrom
yxshee:fix/5073-workflow-expression-syntax
Open

yxshee wants to merge 2 commits into
github:mainfrom
yxshee:fix/5073-workflow-expression-syntax

Conversation

@yxshee

@yxshee yxshee commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Description

Closes #5073.

After the Lark grammar landed in #5171 (resolving escaped-quote syntax rejection and unspaced word operators), three expression-evaluator gaps from #5073 remained on main:

  • Integer-mantissa exponent literals (engine.py): NUMBER only attached the optional exponent suffix to decimal numbers (1.5e3), rejecting integer-mantissa exponents like 1e3, 1E3, -2e+3, and 5e-2, and _BuildAst.number only called float() when "." was present. Moving the optional (?:[eE][+-]?\d(?:_?\d)*)? suffix outside the decimal/integer alternation and converting e/E literals via float() makes {{ 1e3 }} evaluate to 1000.0 while keeping malformed forms (1e, 1e+, 1e3.5, 1_e3) rejected as ValueError.
  • Exponent-form numeric string coercion (operators.py): _coerce_number only called float() when "." appeared in the string, so exponent-form strings like "1e3" fell through as plain strings and silently caused {{ inputs.s > 100 }} and {{ inputs.s == 1000 }} to return False — disagreeing with WorkflowEngine._coerce_input("x", "1e3", {"type": "number"}) == 1000. _coerce_number now tries int() first (preserving arbitrary-precision integers) and falls back to finite float() only for digit-bearing strings, so "1e3" coerces to 1000.0 while non-finite strings ("inf", "-inf", "Infinity", "nan", "1e400") remain plain strings.
  • Chained list indexing and first-segment path guard (namespace.py, engine.py): _INDEXED_SEGMENT previously accepted only a single [index], rejecting {{ steps.plan.output.matrix[0][1] }} and {{ item[0][1] }}. Widening _INDEXED_SEGMENT to ^([\w-]+)((?:\[-?\d+\])+)$ and iterating each bracketed index in _resolve_dot_path propagates automatically to both the Lark PATH terminal and condition diagnostics. Applying the first-segment leading-digit/hyphen and reserved-word guards to the indexed branch as well prevents 1[0] and and[0] from silently lexing as paths returning None (raising ValueError instead), and lets not[0] negate the list literal [0] (False).

Testing

Reproduction and regression evidence on macOS arm64, Python 3.14.6:

  • Before (upstream/main): Running the issue [Bug]: Unsupported expression syntax evaluates to None silently instead of raising #5073 reproduction script gave {{ 1e3 }} -> ValueError, {{ inputs.n < 1e3 }} -> ValueError, {{ inputs.s > 100 }} (with s = "1e3") -> False, {{ steps.plan.output.matrix[0][1] }} -> ValueError; {{ 1[0] }}, {{ and[0] }}, and {{ not[0] }} silently evaluated to None. The new regression tests in tests/test_workflows.py and tests/unit/test_condition_expression_block.py failed on upstream/main.

  • After (feb936f): The [Bug]: Unsupported expression syntax evaluates to None silently instead of raising #5073 reproduction script evaluates {{ 1e3 }} -> 1000.0, {{ inputs.n < 1e3 }} -> True, {{ inputs.s > 100 }} -> True, {{ steps.plan.output.matrix[0][1] }} -> 'b', unspaced word operators -> True, and escaped quotes ('it\'s') -> ValueError.

  • .venv/bin/specify --help: pass

  • PATH="$PWD/.venv/bin:$PATH" LC_ALL=en_US.UTF-8 COLUMNS=1000 .venv/bin/python -m pytest -n auto tests/unit/test_condition_expression_block.py tests/specify_cli/workflows tests/workflows tests/test_workflows.py -q: 3,770 passed, 29 skipped.

  • .venv/bin/python .github/scripts/check_file_helper_migration.py --base upstream/main --head HEAD: pass

  • uvx ruff@0.15.0 check src tests and git diff --check upstream/main HEAD: pass

  • Tested locally with .venv/bin/specify --help (Windows PowerShell: .\.venv\Scripts\specify.exe --help)

  • Ran relevant existing tests with .venv/bin/python -m pytest tests -q (Windows PowerShell: .\.venv\Scripts\python.exe -m pytest tests -q); use affected test paths for focused checks and LC_ALL=en_US.UTF-8 on macOS

  • Tested with a sample project (if applicable)

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

AI disclosure: Authored on behalf of @yxshee using Antigravity (model: Gemini 4 Neon (XHigh), autonomous mode). The agent investigated #5073 against the Lark expression evaluator on main, wrote the fixes and regression tests in feb936f, ran the verification commands listed above, and drafted this PR description. The commit carries a matching Assisted-by: trailer.

…eading-index paths

Closes github#5073.

After the Lark grammar landed in github#5171, three expression-evaluator gaps
from github#5073 remained on main:

- Integer-mantissa exponent literals (such as 1e3, 1E3, -2e+3, 5e-2) were
  rejected by the NUMBER terminal and _BuildAst.number, even though
  decimal exponents like 1.5e3 were accepted.
- _coerce_number only tried float() when a string contained ".", so
  exponent-form numeric strings like "1e3" silently failed to coerce in
  ordering and equality comparisons (evaluating "1e3" > 100 to False
  while WorkflowEngine._coerce_input coerces "1e3" to 1000). Coercing via
  int() first and finite float() for digit-bearing strings aligns
  expression comparisons with workflow input coercion while keeping
  non-finite strings ("inf", "nan", overflow) as plain strings.
- _INDEXED_SEGMENT matched only one [index] suffix, rejecting chained
  list indexing such as steps.plan.output.matrix[0][1] and item[0][1].
  Widening _INDEXED_SEGMENT and walking each bracketed index in
  _resolve_dot_path propagates automatically to the Lark PATH terminal
  and condition diagnostics.
- The first-segment PATH regex did not apply its leading-digit/hyphen and
  reserved-word guards to the indexed branch, allowing 1[0], and[0], and
  not[0] to silently resolve as missing paths (None) instead of raising a
  syntax error (or, for not[0], negating the list literal [0]).

Assisted-by: Antigravity (model: Gemini 4 Neon (XHigh), autonomous)
@yxshee
yxshee requested a review from mnriem as a code owner November 23, 2026 01:22
Copilot AI adaptive review requested due to automatic review settings November 23, 2026 01:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The numeric fallback can silently round integer strings exceeding Python’s digit limit, producing incorrect comparisons; it needs restriction to decimal or exponent spellings and a conversion-limit regression test.

1 open issue
What changed in this PR

This PR addresses the remaining expression-evaluator gaps from #5073 after the Lark parser migration, extending numeric and indexing support in workflow expressions.

Changes:

  • Accepts integer-mantissa exponent literals and finite exponent-form numeric strings in comparisons.
  • Supports chained list indexing and rejects invalid first-segment paths.
  • Adds regression coverage for valid expressions, malformed syntax, and unresolved indices.
File Description
tests/​unit/​test_condition_expression_block.py Updates diagnostic expectations for exponents and chained indexing.
tests/​test_workflows.py Adds numeric coercion, indexing, and invalid-syntax regression cases.
src/​specify_cli/​workflows/​expressions/​operators.py Adds finite floating-point fallback for numeric strings.
src/​specify_cli/​workflows/​expressions/​namespace.py Resolves multiple list indices within a path segment.
src/​specify_cli/​workflows/​expressions/​engine.py Extends exponent parsing and guards indexed root paths.

Review effort: Balanced (adaptive)

When an integer string exceeds Python's configured int-to-str digit
limit, int() raises ValueError. Restrict the float() fallback to
strings containing '.', 'e', or 'E' so overlong integer strings remain
unchanged instead of silently rounding through float() during
comparisons.

Add a conversion-limit regression test covering _coerce_number and
evaluate_condition.

Assisted-by: Antigravity (model: Gemini 4 Neon (High), autonomous)
Copilot AI adaptive review requested due to automatic review settings November 23, 2026 02:16
@yxshee

yxshee commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the Copilot review finding on feb936f (discussion_r4661013609) in 774012f:

  • Restricted the float() fallback in _coerce_number (src/specify_cli/workflows/expressions/operators.py) to strings containing ., e, or E (in addition to a digit), so integer strings rejected by Python's int_max_str_digits limit remain unchanged strings instead of rounding through float().
  • Added conversion-limit regression test test_overlong_integer_string_does_not_fallback_to_float in tests/test_workflows.py covering _coerce_number and evaluate_condition.

Verification on 774012f (macOS arm64, Python 3.14.6):

  • test_overlong_integer_string_does_not_fallback_to_float failed on feb936f (rounded to 9007199254740992.0) and passes on 774012f.
  • LC_ALL=en_US.UTF-8 .venv/bin/python -m pytest tests/test_workflows.py tests/unit/test_condition_expression_block.py -q: 1,331 passed
  • LC_ALL=en_US.UTF-8 .venv/bin/python -m pytest tests/specify_cli/workflows tests/workflows -q: 2,440 passed, 29 skipped
  • uvx ruff@0.15.0 check src tests, .venv/bin/python .github/scripts/check_file_helper_migration.py --base $(git merge-base upstream/main HEAD) --head HEAD, and git diff --check: pass.

AI disclosure: Posted on behalf of @yxshee by Antigravity (model: Gemini 4 Neon (High), autonomous mode). The agent authored the fix in 774012f, wrote and ran the regression test and lint/guard checks listed above, and drafted this comment.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Interacting changes to tokenization, numeric coercion, namespace indexing, and diagnostics affect workflow branching and interpolation, requiring broader review than limited-scope automated approval.

No open issues 🎉

1 resolved since last review

Review effort: Balanced (adaptive)

@mnriem mnriem added the triage-must-have Verdict: high-value, important work for Spec Kit — do first label Nov 23, 2026
@mnriem
mnriem requested a balanced review from Copilot November 23, 2026 16:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Cross-cutting changes to tokenization, path resolution, diagnostics, and numeric coercion affect workflow branch and loop decisions, exceeding the limited contract-preserving review scope.

No open issues 🎉

Review effort: Balanced

@mnriem

mnriem commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

The exponent-literal support looks good. Before merging, please keep the newly supported behavior aligned with Python rather than extending existing differences:

  • Numeric strings: Please do not add implicit exponent-string conversion during comparisons. In Python, "1e3" == 1000 is false, and "1e3" > 100 raises TypeError. Supporting the numeric literal 1e3 does not require changing string comparisons.
  • Chained indexing: Valid access such as matrix[0][1], including negative indices, should work. Invalid access should raise the appropriate error rather than silently return None, which can hide mistakes and select the wrong workflow branch.
  • Documentation and tests: Please document the new literal and indexing syntax, and cover both successful evaluation and expected errors.

This concerns behavior introduced by this PR; unrelated existing differences can be addressed separately.

AI disclosure: Drafted for @mnriem using GitHub Copilot (GPT-6.1 Sol), interactively, based on automated code review and testing. The analysis and wording are AI-generated.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

triage-must-have Verdict: high-value, important work for Spec Kit — do first

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Unsupported expression syntax evaluates to None silently instead of raising

3 participants