BUG: LocalPythonExecutor evaluates subscript index before container expression
Problem
LocalPythonExecutor evaluates subscript expressions in a different order from CPython.
For an expression like:
get_container()[get_index()]CPython evaluates get_container() first and get_index() second.
However, the current evaluate_subscript() implementation evaluates subscript.slice before subscript.value, so the order is reversed.
This can silently change program behavior when either expression has side effects.
Steps to reproduce
- Run the following code with the current
mainbranch of smolagents. - Compare the evaluation order produced by native CPython and
LocalPythonExecutor.
from smolagents.local_python_executor import evaluate_python_code
code = "get_container()[get_index()]"
# Native CPython
order = []
def get_container():
order.append("container")
return [42]
def get_index():
order.append("index")
return 0
result = eval(
code,
{
"get_container": get_container,
"get_index": get_index,
},
)
print("CPython result:", result)
print("CPython order :", order)
# smolagents LocalPythonExecutor
order = []
def get_container():
order.append("container")
return [42]
def get_index():
order.append("index")
return 0
result, _ = evaluate_python_code(
code,
state={
"get_container": get_container,
"get_index": get_index,
},
timeout_seconds=None,
)
print("smolagents result:", result)
print("smolagents order :", order)Actual behavior and error logs
No exception is raised, but the observable evaluation order differs from CPython:
CPython result: 42
CPython order : ['container', 'index']
smolagents result: 42
smolagents order : ['index', 'container']The final value happens to be the same in this minimal example, but the side-effect order is reversed.
Expected behavior
LocalPythonExecutor should follow normal Python evaluation order:
container expression
-> index expression
-> __getitem__So for:
get_container()[get_index()]get_container() should run before get_index().
Environment:
- OS: macOS
- Python version: 3.12.14
- Package version:
Name: smolagents
Version: 1.27.0.dev0
Location: /Users/zyj/Developer/smolagents/.venv/lib/python3.12/site-packages
Editable project location: /Users/zyj/Developer/smolagentsCurrent checkout commit: 227ef5e
Additional context (optional)
I searched existing issues and PRs for evaluate_subscript, subscript evaluation order, and related side-effect issues.
PR #2537 addresses evaluation-order semantics for augmented-assignment targets, but I did not find an existing issue or PR covering the evaluation order of ordinary subscript expressions.
The relevant implementation currently evaluates the index before the container expression:
index = evaluate_ast(subscript.slice, ...)
value = evaluate_ast(subscript.value, ...)
return value[index]I would be happy to add a focused regression test and fix this if the issue is accepted.
Disclosure: I used an AI assistant while investigating this issue. I independently reproduced the behavior against both native CPython and my current smolagents checkout.
Checklist
- I have searched the existing issues and have not found a similar bug report.
- I have provided a minimal, reproducible example.
- No exception is raised, so there is no traceback to provide.
- I have provided my environment details.
- I am willing to work on this issue and submit a pull request.
Source: huggingface/smolagents