You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Replace pandas.eval with native implementation
This commit removes the dependency on pandas.eval() and implements a native
expression evaluator in Dataset.eval() using Python's ast module. The new
implementation provides better support for multi-dimensional arrays and
maintains backward compatibility with deprecated operators through automatic
transformation.
Key changes:
- Remove pd.eval() call and replace with custom _eval_expression() method
- Add _LogicalOperatorTransformer to convert deprecated operators (and/or/not)
to bitwise operators (&/|/~) that work element-wise on arrays
- Implement automatic transformation of chained comparisons to explicit
bitwise AND operations
- Add security validation to block lambda expressions and private attributes
- Emit FutureWarning for deprecated constructs (logical operators, chained
comparisons, parser= argument)
- Support assignment statements (target = expression) in eval()
- Make data variables and coordinates take priority in namespace resolution
- Provide safe builtins (abs, min, max, round, len, sum, pow, any, all, type
constructors, iteration helpers) while blocking __import__, open, etc.
- Add comprehensive test coverage including edge cases, error messages, dask
compatibility, and security validation
* Fix mypy errors in eval tests
- Use pd.isna(ds["a"].values) instead of pd.isna(ds["a"]) since pandas
type stubs don't have overloads for DataArray
- Use abs() instead of np.abs() to get DataArray return type
Co-authored-by: Claude <noreply@anthropic.com>
* Remove security framing, frame restrictions as pd.eval() compatibility
The lambda and dunder restrictions emulate pd.eval() behavior rather than
providing security guarantees. Pandas explicitly doesn't claim these as
security measures.
Co-authored-by: Claude <noreply@anthropic.com>
* Move eval implementation to dedicated module
Extract AST-based expression evaluation code to xarray/core/eval.py:
- EVAL_BUILTINS dict
- LogicalOperatorTransformer class
- validate_expression function
This addresses the review feedback to keep the Dataset class focused.
Co-authored-by: Claude <noreply@anthropic.com>
* Move eval tests to dedicated test_eval.py module
Extract eval tests from test_dataset.py to test_eval.py:
- 35 tests covering basic functionality, error messages, edge cases, and dask
- Mirrors the implementation structure (core/eval.py <-> tests/test_eval.py)
- Reduces test_dataset.py by 574 lines
Co-authored-by: Claude <noreply@anthropic.com>
* Refactor eval tests: convert classes to standalone functions
Address review feedback:
- Convert TestEvalErrorMessages class to test_eval_error_* functions
- Convert TestEvalEdgeCases class to test_eval_* functions
- Convert TestEvalDask class to test_eval_dask_* functions
This follows xarray's preference for standalone test functions over classes.
Co-authored-by: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>
0 commit comments