{"record":{"id":"f2bb8fcae06d8e74","repo":"pandas-dev/pandas","slug":"invalid-attribute-context-type-ctx-name","errorCode":null,"errorMessage":"Invalid Attribute context {type(ctx).__name__}","messagePattern":"Invalid Attribute context (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/expr.py","lineNumber":665,"sourceCode":"        ctx = node.ctx\n        if isinstance(ctx, ast.Load):\n            # resolve the value\n            visited_value = self.visit(value)\n            if hasattr(visited_value, \"value\"):\n                resolved = visited_value.value\n            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:","sourceCodeStart":647,"sourceCodeEnd":683,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/expr.py#L647-L683","documentation":"Raised by visit_Attribute() when the attribute node's context (node.ctx) is not an ast.Load instance. pandas eval only supports reading attributes (Load context, e.g., 'df.col' to read a value); attribute deletion (ast.Del) or attribute storage/assignment (ast.Store) are not supported inside the eval DSL. The message includes the actual context type name for diagnostics.","triggerScenarios":"Expressions that attempt attribute assignment or deletion inside eval, producing an ast.Store or ast.Del context rather than ast.Load. Rare since most such attempts are caught earlier by visit_Assign's ast.Name check, but reachable through certain complex attribute expressions.","commonSituations":"Constructing expressions that use attribute syntax in a storing/deleting context; programmatic AST manipulation that produces non-Load attribute nodes.","solutions":["Use attribute access only for reading: df.eval('col_a + df.col_b') where attributes are read-only.","For attribute assignment, do it outside eval: df.col_c = df['col_a'] + df['col_b'].","Restrict eval expressions to Load-context attribute access on resolvable objects."],"exampleFix":"// before\n# expression that triggers a non-Load attribute context inside eval\n\n// after\ndf['col_c'] = df['col_a'] + df['col_b']  # do assignment outside eval","handlingStrategy":"try-catch","validationCode":"import ast\ndef attributes_are_load_only(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.Attribute) and not isinstance(node.ctx, ast.Load):\n            return False\n    return True\nif not attributes_are_load_only(expr):\n    raise ValueError('Attribute store/delete not supported in eval')\ndf.eval(expr)","typeGuard":null,"tryCatchPattern":"try:\n    df.eval(expr)\nexcept ValueError as e:\n    if 'Invalid Attribute context' in str(e):\n        # perform attribute assignment outside eval\n        ...","preventionTips":["Use attributes only for reading values in eval.","Do attribute assignment/modification outside the eval string.","Keep eval expressions read-only on attributes."],"tags":["pandas","eval","attribute","syntax-error","context"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}