Skip to content

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

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

yxshee wants to merge 4 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), two expression-evaluator syntax gaps from #5073 remained on main:

  • Integer-mantissa exponent literals (src/specify_cli/workflows/expressions/engine.py): NUMBER previously attached the optional exponent suffix only to decimal numbers (1.5e3), rejecting integer-mantissa exponents such as 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 rejecting malformed forms (1e, 1e+, 1e3.5, 1_e3) as ValueError. String comparison semantics in operators.py are unchanged: integer-mantissa exponent strings without a decimal point (such as "1e3") remain strings during comparisons ("1e3" == 1000 and "1e3" == 1e3 remain False), while decimal-mantissa exponent strings containing . (such as "1.0e3" or "1.5e3") keep existing float() coercion in operators._coerce_number.
  • Chained list indexing and first-segment path guard (src/specify_cli/workflows/expressions/namespace.py, src/specify_cli/workflows/expressions/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. Valid chained access (including negative indices such as matrix[-1][-2]) resolves to the target element, while invalid chained access raises IndexError (out-of-range index) or TypeError (non-list target) instead of silently returning None. Applying the first-segment leading-digit/hyphen and reserved-word guards to the indexed branch also 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).
  • Documentation (docs/workflows/expressions.md): Documented exponent literals (distinguishing comparison semantics for integer-mantissa vs. decimal-mantissa exponent strings), chained/negative list indexing, and their expected errors (ValueError, IndexError, TypeError).

Testing

Verification on 2048e217 (macOS arm64, Python 3.14.6):

  • PATH="$PWD/.venv/bin:$PATH" LC_ALL=en_US.UTF-8 COLUMNS=1000 .venv/bin/python -m pytest -n auto tests/test_workflows.py tests/unit/test_condition_expression_block.py tests/specify_cli/workflows tests/workflows -q: 3,776 passed, 29 skipped
  • uvx ruff@0.15.0 check src tests: passed
  • .venv/bin/python .github/scripts/check_file_helper_migration.py --base $(git merge-base upstream/main HEAD) --head HEAD: passed
  • npx markdownlint-cli2 docs/workflows/expressions.md: 0 issues
  • git diff --check $(git merge-base upstream/main HEAD)..HEAD: passed

AI Disclosure

Implemented and updated on behalf of @yxshee using Antigravity (model: Gemini 4 Neon (XHigh), autonomous mode; commit 774012ff used Gemini 4 Neon (High), autonomous mode). AI assistance covered code, documentation, test authoring, verification execution, and PR description drafting.

…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.

…nd document expression syntax

Addresses maintainer review feedback on github#5523:
- Revert implicit exponent-string coercion in operators.py so "1e3" stays a string during comparisons while exponent numeric literals like 1e3 evaluate as float.
- Raise IndexError on out-of-range chained list indices and TypeError on non-list chained indexing targets instead of silently returning None.
- Document exponent literals and chained indexing (including expected errors) in docs/workflows/expressions.md and expand positive and expected-error tests.

Assisted-by: Antigravity (model: Gemini 4 Neon (XHigh), autonomous)
Copilot AI balanced review requested due to automatic review settings November 25, 2026 21:17
@yxshee

yxshee commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the review feedback in 23bd09c8:

  • Numeric strings: Reverted all changes to src/specify_cli/workflows/expressions/operators.py, removing implicit exponent-string coercion during comparisons. Exponent literals (1e3, -2e+3, 5e-2) continue to evaluate as float, while exponent-form strings such as "1e3" remain strings ("1e3" == 1000 and "1e3" == 1e3 evaluate to False, and string-to-string ordering stays lexicographic). Existing main comparison semantics for incomparable types are left unchanged.
  • Chained indexing: Updated _resolve_dot_path in src/specify_cli/workflows/expressions/namespace.py so chained indexing (matrix[0][1], negative indices like matrix[-1][-2], item[0][1]) raises IndexError for out-of-range indices and TypeError when indexing a non-list value instead of silently returning None, while preserving existing single-index behavior on main.
  • Documentation and tests: Documented exponent literals, chained indexing, and their expected errors in docs/workflows/expressions.md, and expanded tests/test_workflows.py and tests/unit/test_condition_expression_block.py with positive and expected-error coverage (IndexError, TypeError, ValueError, workflow IfThenStep branching, and condition remediation).

Also updated the PR description to match the final diff.

Verification on 23bd09c8 (macOS arm64, Python 3.14.6):

  • PATH="$PWD/.venv/bin:$PATH" LC_ALL=en_US.UTF-8 COLUMNS=1000 .venv/bin/python -m pytest -n auto tests/test_workflows.py tests/unit/test_condition_expression_block.py tests/specify_cli/workflows tests/workflows -q: 3,776 passed, 29 skipped
  • uvx ruff@0.15.0 check src tests: passed
  • .venv/bin/python .github/scripts/check_file_helper_migration.py --base $(git merge-base upstream/main HEAD) --head HEAD: passed
  • npx markdownlint-cli2 docs/workflows/expressions.md: 0 issues
  • .venv/bin/specify --help and git diff --check: passed

AI disclosure: Posted on behalf of @yxshee by Antigravity (model: Gemini 4 Neon (XHigh), autonomous mode). The agent authored the code, documentation, and test updates in 23bd09c8, ran the verification commands 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.

🟡 Changes recommended

The documentation incorrectly claims exponent strings are not coerced, although decimal-mantissa forms still undergo float() conversion, misleading workflow authors about comparison semantics.

1 open issue

Review effort: Balanced

Comment thread docs/workflows/expressions.md Resolved
Addresses Copilot review feedback on github#5523 (discussion_r4701012470):
- Distinguish integer-mantissa exponent strings without a decimal point
  (such as "1e3", which are not coerced during comparisons) from
  decimal-mantissa exponent strings containing "." (such as "1.0e3" or
  "1.5e3", which retain existing float() coercion in operators._coerce_number).
- Add clarifying assertions in test_workflows.py covering both forms
  while leaving operators.py unchanged relative to main.

Assisted-by: Antigravity (model: Gemini 4 Neon (XHigh), autonomous)
Copilot AI balanced review requested due to automatic review settings November 25, 2026 21:37
@yxshee

yxshee commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the Copilot documentation accuracy comment (discussion_r4701012470) in 2048e217:

  • Clarified docs/workflows/expressions.md to distinguish integer-mantissa exponent strings without a decimal point (such as "1e3", which remain strings during comparisons) from decimal-mantissa exponent strings containing . (such as "1.0e3" or "1.5e3", which keep existing float coercion in operators._coerce_number when compared with a number or numeric string).
  • Added clarifying assertions for "1.0e3" and "1.5e3" alongside "1e3" in tests/test_workflows.py while keeping src/specify_cli/workflows/expressions/operators.py unchanged relative to main.

Verification on 2048e217 (macOS arm64, Python 3.14.6):

  • PATH="$PWD/.venv/bin:$PATH" LC_ALL=en_US.UTF-8 COLUMNS=1000 .venv/bin/python -m pytest -n auto tests/test_workflows.py tests/unit/test_condition_expression_block.py tests/specify_cli/workflows tests/workflows -q: 3,776 passed, 29 skipped
  • uvx ruff@0.15.0 check src tests: passed
  • .venv/bin/python .github/scripts/check_file_helper_migration.py --base $(git merge-base upstream/main HEAD) --head HEAD: passed
  • npx markdownlint-cli2 docs/workflows/expressions.md: 0 issues
  • git diff --check: passed

AI disclosure: Posted on behalf of @yxshee by Antigravity (model: Gemini 4 Neon (XHigh), autonomous mode). The agent authored the documentation and test clarification in 2048e217, ran the verification commands listed above, updated the PR description to match, 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

The change couples expression tokenization, namespace resolution, and condition handling with runtime exceptions that affect workflow branching, requiring human review of chained-index failure semantics and compatibility.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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