huggingface/smolagents · warning · InterpreterError

Unsupported AnnAssign target in class body: {type(target).__

Error message

Unsupported AnnAssign target in class body: {type(target).__name__}

What it means

When evaluating a class body, evaluate_class_def handles annotated assignments (AnnAssign) only to Names, Attributes and Subscripts; any other target node type triggers InterpreterError naming the unsupported target class. It is a defensive exhaustiveness check for exotic AST shapes.

Source

Thrown at src/smolagents/local_python_executor.py:594

                class_dict.setdefault("__annotations__", {})[target.id] = annotation
                # Assign value if provided
                if stmt.value:
                    class_dict[target.id] = value
            elif isinstance(target, ast.Attribute):
                # Attribute annotation like "obj.attr: int"
                obj = evaluate_ast(target.value, class_dict, static_tools, custom_tools, authorized_imports)
                # If there's a value assignment, set the attribute
                if stmt.value:
                    setattr(obj, target.attr, value)
            elif isinstance(target, ast.Subscript):
                # Subscript annotation like "dict[key]: int"
                container = evaluate_ast(target.value, class_dict, static_tools, custom_tools, authorized_imports)
                index = evaluate_ast(target.slice, state, static_tools, custom_tools, authorized_imports)
                # If there's a value assignment, set the item
                if stmt.value:
                    container[index] = value
            else:
                raise InterpreterError(f"Unsupported AnnAssign target in class body: {type(target).__name__}")
        elif isinstance(stmt, ast.Assign):
            value = evaluate_ast(stmt.value, state, static_tools, custom_tools, authorized_imports)
            for target in stmt.targets:
                if isinstance(target, ast.Name):
                    class_dict[target.id] = value
                elif isinstance(target, ast.Attribute):
                    obj = evaluate_ast(target.value, class_dict, static_tools, custom_tools, authorized_imports)
                    setattr(obj, target.attr, value)
        elif isinstance(stmt, ast.Pass):
            pass
        elif (
            isinstance(stmt, ast.Expr)
            and stmt == class_def.body[0]
            and isinstance(stmt.value, ast.Constant)
            and isinstance(stmt.value.value, str)
        ):
            # Check if it is a docstring: first statement in class body which is a string literal expression
            class_dict["__doc__"] = stmt.value.value

View on GitHub (pinned to 30bb116109)

Solutions

  1. Use only simple annotated targets (plain names) in class bodies: `x: int = 0`
  2. If building ASTs manually, ensure AnnAssign targets are ast.Name nodes
  3. Simplify the class body or move exotic constructs outside executed code
Defensive patterns

Strategy: validation

Validate before calling

import ast
for node in ast.walk(ast.parse(code)):
    if isinstance(node, ast.ClassDef):
        for stmt in node.body:
            if isinstance(stmt, ast.AnnAssign) and not isinstance(stmt.target, (ast.Name, ast.Attribute, ast.Subscript)):
                raise ValueError('unsupported AnnAssign target in class body')

Try / catch

from smolagents.local_python_executor import InterpreterError
try:
    evaluate_python(code)
except InterpreterError as e:
    if 'Unsupported AnnAssign target' in str(e):
        simplify_class_body(code)

Prevention

When it happens

Trigger: A class body contains an AnnAssign whose target is not Name/Attribute/Subscript (not producible by normal Python source since annotations only allow simple targets; only reachable by feeding hand-built AST).

Common situations: Programmatically constructed AST passed to evaluate_ast; version skew where grammar allows new AnnAssign targets; meta-programming that mutates the AST before execution.

Related errors


AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28). Data as JSON: /api/errors/ad712c17a7682857. Report an issue: GitHub.