{"record":{"id":"6f5caf2710099a26","repo":"huggingface/smolagents","slug":"unary-operation-expression-op-class-name","errorCode":null,"errorMessage":"Unary operation {expression.op.__class__.__name__} is not supported.","messagePattern":"Unary operation (.+?) is not supported\\.","errorType":"error_code","errorClass":"InterpreterError","httpStatus":null,"severity":"warning","filePath":"src/smolagents/local_python_executor.py","lineNumber":413,"sourceCode":"\ndef evaluate_unaryop(\n    expression: ast.UnaryOp,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str],\n) -> Any:\n    operand = evaluate_ast(expression.operand, state, static_tools, custom_tools, authorized_imports)\n    if isinstance(expression.op, ast.USub):\n        return -operand\n    elif isinstance(expression.op, ast.UAdd):\n        return operand\n    elif isinstance(expression.op, ast.Not):\n        return not operand\n    elif isinstance(expression.op, ast.Invert):\n        return ~operand\n    else:\n        raise InterpreterError(f\"Unary operation {expression.op.__class__.__name__} is not supported.\")\n\n\ndef evaluate_lambda(\n    lambda_expression: ast.Lambda,\n    state: dict[str, Any],\n    static_tools: dict[str, Callable],\n    custom_tools: dict[str, Callable],\n    authorized_imports: list[str],\n) -> Callable:\n    args = [arg.arg for arg in lambda_expression.args.args]\n\n    def lambda_func(*values: Any) -> Any:\n        new_state = state.copy()\n        for arg, value in zip(args, values):\n            new_state[arg] = value\n        return evaluate_ast(\n            lambda_expression.body,\n            new_state,","sourceCodeStart":395,"sourceCodeEnd":431,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/local_python_executor.py#L395-L431","documentation":"evaluate_unaryop supports only USub (-), UAdd (+), Not (not) and Invert (~). Any other unary operator AST node raises InterpreterError naming the unsupported op class. In practice CPython only produces those four, so this is a defensive exhaustiveness branch.","triggerScenarios":"Practically unreachable with standard Python source; would only fire if a new unary operator were added to the grammar or an AST is fed programmatically with an exotic ast.UnaryOp op node.","commonSituations":"Version skew: a future/alternate Python grammar emits a unary op node the executor doesn't recognize; hand-crafted AST passed to evaluate_ast directly instead of source code.","solutions":["Stick to the four standard unary ops (-, +, not, ~) in executed code","Upgrade smolagents if a newer Python version added an operator and the executor was updated","If feeding ASTs programmatically, normalize to supported ops before evaluation"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import ast\nfrom smolagents.local_python_executor import evaluate_ast\nfor node in ast.walk(ast.parse(code)):\n    if isinstance(node, ast.UnaryOp) and not isinstance(node.op, (ast.USub, ast.UAdd, ast.Not, ast.Invert)):\n        raise ValueError('unsupported unary operator in AST')","typeGuard":null,"tryCatchPattern":"from smolagents.local_python_executor import InterpreterError\ntry:\n    evaluate_python(code)\nexcept InterpreterError as e:\n    if 'Unary operation' in str(e):\n        raise NotImplementedError('rewrite the expression using -/+/not/~') from e","preventionTips":["Restrict executed code to the four standard unary operators","When feeding ASTs programmatically, validate op nodes first","Keep smolagents updated alongside your Python version"],"tags":["smolagents","unary-operator","unsupported-operation","interpreter-error"],"backgroundTag":"unsupported-language-construct","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}