MCPcopy
hub / github.com/huggingface/smolagents / evaluate_augassign

Function evaluate_augassign

src/smolagents/local_python_executor.py:638–707  ·  view source on GitHub ↗
(
    expression: ast.AugAssign,
    state: dict[str, Any],
    static_tools: dict[str, Callable],
    custom_tools: dict[str, Callable],
    authorized_imports: list[str],
)

Source from the content-addressed store, hash-verified

636
637
638def evaluate_augassign(
639 expression: ast.AugAssign,
640 state: dict[str, Any],
641 static_tools: dict[str, Callable],
642 custom_tools: dict[str, Callable],
643 authorized_imports: list[str],
644) -> Any:
645 def get_current_value(target: ast.AST) -> Any:
646 if isinstance(target, ast.Name):
647 return state.get(target.id, 0)
648 elif isinstance(target, ast.Subscript):
649 obj = evaluate_ast(target.value, state, static_tools, custom_tools, authorized_imports)
650 key = evaluate_ast(target.slice, state, static_tools, custom_tools, authorized_imports)
651 return obj[key]
652 elif isinstance(target, ast.Attribute):
653 obj = evaluate_ast(target.value, state, static_tools, custom_tools, authorized_imports)
654 return getattr(obj, target.attr)
655 elif isinstance(target, ast.Tuple):
656 return tuple(get_current_value(elt) for elt in target.elts)
657 elif isinstance(target, ast.List):
658 return [get_current_value(elt) for elt in target.elts]
659 else:
660 raise InterpreterError("AugAssign not supported for {type(target)} targets.")
661
662 current_value = get_current_value(expression.target)
663 value_to_add = evaluate_ast(expression.value, state, static_tools, custom_tools, authorized_imports)
664
665 if isinstance(expression.op, ast.Add):
666 if isinstance(current_value, list):
667 if not isinstance(value_to_add, list):
668 raise InterpreterError(f"Cannot add non-list value {value_to_add} to a list.")
669 current_value += value_to_add
670 else:
671 current_value += value_to_add
672 elif isinstance(expression.op, ast.Sub):
673 current_value -= value_to_add
674 elif isinstance(expression.op, ast.Mult):
675 current_value *= value_to_add
676 elif isinstance(expression.op, ast.Div):
677 current_value /= value_to_add
678 elif isinstance(expression.op, ast.Mod):
679 current_value %= value_to_add
680 elif isinstance(expression.op, ast.Pow):
681 current_value **= value_to_add
682 elif isinstance(expression.op, ast.FloorDiv):
683 current_value //= value_to_add
684 elif isinstance(expression.op, ast.BitAnd):
685 current_value &= value_to_add
686 elif isinstance(expression.op, ast.BitOr):
687 current_value |= value_to_add
688 elif isinstance(expression.op, ast.BitXor):
689 current_value ^= value_to_add
690 elif isinstance(expression.op, ast.LShift):
691 current_value <<= value_to_add
692 elif isinstance(expression.op, ast.RShift):
693 current_value >>= value_to_add
694 else:
695 raise InterpreterError(f"Operation {type(expression.op).__name__} is not supported.")

Callers 1

evaluate_astFunction · 0.85

Calls 4

get_current_valueFunction · 0.85
evaluate_astFunction · 0.85
InterpreterErrorClass · 0.85
set_valueFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…