{"record":{"id":"aa9d169f39a562d3","repo":"pandas-dev/pandas","slug":"only-named-functions-are-supported","errorCode":null,"errorMessage":"Only named functions are supported","messagePattern":"Only named functions are supported","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/expr.py","lineNumber":671,"sourceCode":"            else:\n                resolved = visited_value(self.env)\n            try:\n                v = getattr(resolved, attr)\n                name = self.env.add_tmp(v)\n                return self.term_type(name, self.env)\n            except AttributeError:\n                # something like datetime.datetime where scope is overridden\n                if isinstance(value, ast.Name) and value.id == attr:\n                    return resolved\n                raise\n\n        raise ValueError(f\"Invalid Attribute context {type(ctx).__name__}\")\n\n    def visit_Call(self, node, side=None, **kwargs):\n        if isinstance(node.func, ast.Attribute) and node.func.attr != \"__call__\":\n            res = self.visit_Attribute(node.func)\n        elif not isinstance(node.func, ast.Name):\n            raise TypeError(\"Only named functions are supported\")\n        else:\n            try:\n                res = self.visit(node.func)\n            except UndefinedVariableError:\n                # Check if this is a supported function name\n                try:\n                    res = FuncNode(node.func.id)\n                except ValueError:\n                    # Raise original error\n                    raise\n\n        if res is None:\n            # error: \"expr\" has no attribute \"id\"\n            raise ValueError(\n                f\"Invalid function call {node.func.id}\"  # type: ignore[union-attr]\n            )\n        if hasattr(res, \"value\"):\n            res = res.value","sourceCodeStart":653,"sourceCodeEnd":689,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/expr.py#L653-L689","documentation":"Raised by visit_Call() when node.func is neither an ast.Attribute (method call) nor an ast.Name (bare function name). pandas eval supports calling named functions (e.g., 'sin(x)') and method calls on resolved objects (e.g., 'obj.method(x)'), but it does not support calling the result of an arbitrary sub-expression (e.g., '(lambda x: x)(5)' or calling a subscripted item like 'fns[0](x)'). The function being called must be a direct name or attribute.","triggerScenarios":"df.eval('(lambda x: x)(5)') — the callable is a lambda expression, not a Name/Attribute; df.eval('fns[0](x)') — callable is a subscript; any expression where the call target is itself a complex expression.","commonSituations":"Trying to call dynamically-selected functions or inline lambdas inside the eval string; expecting first-class function semantics from the restricted eval DSL.","solutions":["Call only named functions: df.eval('sin(col)') where 'sin' is a supported math function, or reference a named function in scope.","Move dynamic/lambda calls out of the eval string into regular Python.","For method calls, use attribute syntax: df.eval('col.sum()') is not how eval works — use df['col'].sum() directly."],"exampleFix":"// before\ndf.eval('(lambda x: x + 1)(col)')\n\n// after\ndf['col'] = df['col'].map(lambda x: x + 1)","handlingStrategy":"validation","validationCode":"import ast\ndef calls_use_named_funcs(expr: str) -> bool:\n    try:\n        tree = ast.parse(expr, mode='eval')\n    except SyntaxError:\n        return True\n    for node in ast.walk(tree):\n        if isinstance(node, ast.Call) and not isinstance(node.func, (ast.Name, ast.Attribute)):\n            return False\n    return True\nif not calls_use_named_funcs(expr):\n    raise ValueError('Only named functions or methods are callable in eval')\ndf.eval(expr)","typeGuard":null,"tryCatchPattern":"try:\n    df.eval(expr)\nexcept TypeError as e:\n    if 'named functions' in str(e):\n        # move dynamic calls to regular Python\n        ...","preventionTips":["Call only named functions (sin, abs) or methods inside eval.","Move lambda/dynamic dispatch out of eval.","Do not call subscripted expressions as functions."],"tags":["pandas","eval","function-call","type-error","unsupported-syntax"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}