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

Function test_eval_restricted_syntax

xarray/tests/test_eval.py:121–141  ·  view source on GitHub ↗

Test that eval blocks certain syntax to emulate pd.eval() behavior.

()

Source from the content-addressed store, hash-verified

119
120
121def test_eval_restricted_syntax() -> None:
122 """Test that eval blocks certain syntax to emulate pd.eval() behavior."""
123 ds = Dataset({"a": ("x", [1, 2, 3])})
124
125 # Private attribute access is not allowed (consistent with pd.eval)
126 with pytest.raises(ValueError, match="Access to private attributes is not allowed"):
127 ds.eval("a.__class__")
128
129 with pytest.raises(ValueError, match="Access to private attributes is not allowed"):
130 ds.eval("a._private")
131
132 # Lambda expressions are not allowed (pd.eval: "Only named functions are supported")
133 with pytest.raises(ValueError, match="Lambda expressions are not allowed"):
134 ds.eval("(lambda x: x + 1)(a)")
135
136 # These builtins are not in the namespace
137 with pytest.raises(NameError):
138 ds.eval("__import__('os')")
139
140 with pytest.raises(NameError):
141 ds.eval("open('file.txt')")
142
143
144def test_eval_unsupported_statements() -> None:

Callers

nothing calls this directly

Calls 2

evalMethod · 0.95
DatasetClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…