MCPcopy
hub / github.com/pydata/xarray / validate_expression

Function validate_expression

xarray/core/eval.py:122–138  ·  view source on GitHub ↗

Validate that an AST doesn't contain patterns we don't support. These restrictions emulate pd.eval() behavior for consistency.

(tree: ast.AST)

Source from the content-addressed store, hash-verified

120
121
122def 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 )

Callers 1

_eval_expressionMethod · 0.90

Calls 1

startswithMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…