Validate that an AST doesn't contain patterns we don't support. These restrictions emulate pd.eval() behavior for consistency.
(tree: ast.AST)
| 120 | |
| 121 | |
| 122 | def validate_expression(tree: ast.AST) -> None: |
| 123 | """Validate that an AST doesn't contain patterns we don't support. |
| 124 | |
| 125 | These restrictions emulate pd.eval() behavior for consistency. |
| 126 | """ |
| 127 | for node in ast.walk(tree): |
| 128 | # Block lambda expressions (pd.eval: "Only named functions are supported") |
| 129 | if isinstance(node, ast.Lambda): |
| 130 | raise ValueError( |
| 131 | "Lambda expressions are not allowed in eval(). " |
| 132 | "Use direct operations on data variables instead." |
| 133 | ) |
| 134 | # Block private/dunder attributes (consistent with pd.eval restrictions) |
| 135 | if isinstance(node, ast.Attribute) and node.attr.startswith("_"): |
| 136 | raise ValueError( |
| 137 | f"Access to private attributes is not allowed: '{node.attr}'" |
| 138 | ) |
no test coverage detected
searching dependent graphs…