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

Function test_eval_data_variable_priority

xarray/tests/test_eval.py:259–304  ·  view source on GitHub ↗

Test that data variables take priority over builtin functions. Users may have data variables named 'sum', 'abs', 'min', etc. When they reference these in eval(), they should get their data, not the Python builtins. The builtins should still be accessible via the np namespace (np.sum, np

()

Source from the content-addressed store, hash-verified

257
258
259def test_eval_data_variable_priority() -> None:
260 """Test that data variables take priority over builtin functions.
261
262 Users may have data variables named 'sum', 'abs', 'min', etc. When they
263 reference these in eval(), they should get their data, not the Python builtins.
264 The builtins should still be accessible via the np namespace (np.sum, np.abs).
265 """
266 # Create dataset with data variables that shadow builtins
267 ds = Dataset(
268 {
269 "sum": ("x", [10.0, 20.0, 30.0]), # shadows builtin sum
270 "abs": ("x", [1.0, 2.0, 3.0]), # shadows builtin abs
271 "min": ("x", [100.0, 200.0, 300.0]), # shadows builtin min
272 "other": ("x", [5.0, 10.0, 15.0]),
273 }
274 )
275
276 # Data variables should take priority - user data wins
277 result = ds.eval("sum + other")
278 expected = ds["sum"] + ds["other"]
279 assert_equal(result, expected)
280
281 # Should get the data variable, not builtin sum applied to something
282 result = ds.eval("sum * 2")
283 expected = ds["sum"] * 2
284 assert_equal(result, expected)
285
286 # abs as data variable should work
287 result = ds.eval("abs + 1")
288 expected = ds["abs"] + 1
289 assert_equal(result, expected)
290
291 # min as data variable should work
292 result = ds.eval("min - 50")
293 expected = ds["min"] - 50
294 assert_equal(result, expected)
295
296 # np namespace should still provide access to actual functions
297 result = ds.eval("np.abs(other - 10)")
298 expected = abs(ds["other"] - 10)
299 assert_equal(result, expected)
300
301 # np.sum should work even when 'sum' is a data variable
302 result = ds.eval("np.sum(other)")
303 expected = np.sum(ds["other"])
304 assert result == expected
305
306
307def test_eval_coordinate_priority() -> None:

Callers

nothing calls this directly

Calls 4

evalMethod · 0.95
DatasetClass · 0.90
assert_equalFunction · 0.90
sumMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…