Repository navigation
Python: fix(python): fix operator precedence and associativity in WorkflowState fallback evaluator - #10329
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Arithmetic failures are treated as successful operands, producing incorrect comparison results, while early returns leave supported operator chains partially parsed and trigger false syntax errors.
6 open issues
Propagate runtime failures without treating echoes as operands · New Reject unterminated string literals · New Reject unterminated block comments · New Preserve leading-decimal and exponent numeric literals · New Use normalized operands in string concatenation · New Continue parsing after division by zero · New
What changed in this PR
This PR replaces WorkflowState’s string-splitting fallback evaluator with a tokenizer and parser to address #10162 when PowerFx is unavailable.
Changes:
- Adds operator precedence, left-associative arithmetic, parentheses, and quote-aware parsing.
- Defines the supported expression subset and raises
ValueErrorfor unsupported syntax. - Adds regression tests for expression evaluation and malformed-input rejection.
| File | Description |
|---|---|
python/packages/declarative/tests/test_workflow_state.py |
Adds precedence and rejection tests; updates unsupported-operator expectations. |
python/packages/declarative/agent_framework_declarative/_workflows/_state.py |
Introduces tokenization and parsing, replaces string splitting, and documents fallback behavior. |
Review effort: Balanced
Nick (surlymochan)
left a comment
There was a problem hiding this comment.
Reviewed the diff (non-binding review, no approve). I focused on the parser, reading the new _SimpleFormulaEvaluator against the removed _eval_simple.
Verified: the precedence-climbing structure matches the documented order (Or/|| < And < comparisons < + - < * / < unary), operators are recognised only outside string literals and comments, and trailing input is rejected via the EOF check. The old evaluator had the same gaps (no &&, case-sensitive And/Or splits), so this rewrite doesn't regress those.
Two questions, non-blocking:
- Division by zero inside a chained division:
_parse_multiplicativesetsleft = Noneandcontinues on a zero divisor, but the loop then re-enters and computesfloat(None or 0) / next— so10 / 0 / 2evaluates to0.0rather than the documentedNonefor division by zero. A single10 / 0returnsNoneas documented. Is the chained case intended to stayNonetoo? And/Ormatching is case-sensitive (_match_op("||", "Or")), whiletrue/falseare matched case-insensitively. A lowercaseand/orfalls through to_resolve_identifierand is returned as the literal string instead of raising. If formulas in the wild use lowercase operators, that would silently mis-evaluate rather than fail loudly — is that the intended boundary?


Motivation & Context
When PowerFx is not available,
WorkflowState.eval()falls back to_eval_simple(), which evaluates formulas by splitting the string on operators. That approach getsAnd/Orprecedence wrong, treats-and/as right-associative, and matches operators inside string literals, causing formulas to silently produce wrong values (repro cases in #10162). This change replaces the string-splitting logic with a small parser so the fallback agrees with PowerFx on the documented expression subset.Description & Review Guide
_eval_simple()with a tokenizer and precedence-climbing parser (Or < And < comparisons < + - < * / < unary !/Not()/negation), left-associative at every level, with(...)grouping while skipping string literals and comments._UnsupportedExpressionError(aValueError) for expressions outside the documented subset instead of returning incorrect results.Noneas 0 in arithmetic,+string concatenation fallback, division by zero returningNone, and echoing the formula on runtime errors).&now assert the raise.Related Issue
Fixes #10162
AI Assistance
AI assistance details: Used AI to help analyze the issue, the Copilot plan, and draft the PR description. The code changes and tests were written, reviewed, and verified locally.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.