Repository navigation
fix: resolve state keys before builtins in Condition.safe_expr - #962
Merged
elijahbenizzy merged 1 commit intoOct 17, 2026
Merged
Conversation
Condition.safe_expr looked up its allowlisted builtins before state, so a state key named max, sum or any evaluated to the builtin function and gave a different answer than Condition.expr. Names are now read from state first and fall back to the builtin, and every name that is not the target of a call is reported in reads. Closes apache#960
elijahbenizzy
approved these changes
Oct 17, 2026
elijahbenizzy
left a comment
Contributor
There was a problem hiding this comment.
LGTM, this was a silent wrong-result. One docstring line to add in a follow-up (I'll fold it into #947 since that PR already rewrites the paragraph): bare names resolve state-first, calls always bind the allowlisted builtin even if state has that key -- intentionally different from expr(). Elijah
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #960
Condition.safe_exprresolved a name to an allowlisted builtin before looking at state, so a state key namedmax,sumoranyevaluated to the builtin function.Condition.exprresolves state first, so the same expression gave a different answer:count < maxraisedTypeError, andsum == 0silently returned False and sent a transition down the default edge.Changes
_SafeExprInterpreter._eval_Namenow looks up the state names first and falls back to_SAFE_EXPR_BUILTINS, matchingexpr().readsno longer drops every name found inbuiltins.__dict__. It skips the function name of a call (the validator only admits calls to allowlisted builtins, and the interpreter always resolves those to the builtin) and records every other name, so a state key namedmaxorsumis reported as read and covered by the missing-key check.max(a, b)still calls the builtin even when state has amaxkey.How I tested this
Added
test_safe_expr_state_key_shadows_builtinintests/core/test_action.py. With state{"count": 3, "max": 5, "sum": 0, "any": False}it checks thatcount < max,sum == 0andanygive the same result undersafe_expras underexprand thatreadscontains the shadowed names, and thatmax(count, sum) == 3still calls the builtin withreads == ["count", "sum"]. The test fails on main withTypeError: '<' not supported between instances of 'int' and 'builtin_function_or_method'and passes with the change.pytest tests/core/test_action.pypasses (155 tests).pytest tests/coregives the same result with and without the change apart from the new test; the only failures are the fourtest_visualize_dot_outputcases, which need the Graphvizdotbinary that my environment does not have.Notes
Bare uses of an allowlisted builtin name that is not in state (for example
any == 1) are now listed inreads, so they fail the missing-key check instead of comparing against the builtin function. I could not find a meaningful expression that relied on the old behaviour.Checklist