Skip to content

Commit

Permalink
refactor(rust): Map Polars AssertionError to pyo3's `AssertionError…
Browse files Browse the repository at this point in the history
…` and improve macro flexibility (#21495)
  • Loading branch information
Kevin-Patyk authored Feb 28, 2025
1 parent abe2a57 commit 7360d15
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
8 changes: 4 additions & 4 deletions crates/polars-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,12 @@ on startup."#.trim_start())
$dtype,
)
};
(assert_eq = $lhs:expr, $rhs:expr) => {
(assertion_error = $objects:expr, $detail:expr, $lhs:expr, $rhs:expr) => {
$crate::polars_err!(
AssertionError: "equality assertion failed: {} != {}",
$lhs, $rhs
AssertionError: "{} are different ({})\n[left]: {}\n[right]: {}",
$objects, $detail, $lhs, $rhs
)
}
};
}

#[macro_export]
Expand Down
6 changes: 4 additions & 2 deletions crates/polars-python/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use pyo3::prelude::*;
use pyo3::PyTypeInfo;

use crate::exceptions::{
AssertionError, CategoricalRemappingWarning, ColumnNotFoundError, ComputeError, DuplicateError,
CategoricalRemappingWarning, ColumnNotFoundError, ComputeError, DuplicateError,
InvalidOperationError, MapWithoutReturnDtypeWarning, NoDataError, OutOfBoundsError,
SQLInterfaceError, SQLSyntaxError, SchemaError, SchemaFieldNotFoundError, ShapeError,
StringCacheMismatchError, StructFieldNotFoundError,
Expand Down Expand Up @@ -68,7 +68,9 @@ impl From<PyPolarsErr> for PyErr {
use PyPolarsErr::*;
match err {
Polars(err) => match err {
PolarsError::AssertionError(err) => AssertionError::new_err(err.to_string()),
PolarsError::AssertionError(err) => {
pyo3::exceptions::PyAssertionError::new_err(err.to_string())
},
PolarsError::ColumnNotFound(name) => ColumnNotFoundError::new_err(name.to_string()),
PolarsError::ComputeError(err) => ComputeError::new_err(err.to_string()),
PolarsError::Duplicate(err) => DuplicateError::new_err(err.to_string()),
Expand Down
1 change: 0 additions & 1 deletion crates/polars-python/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use pyo3::exceptions::{PyException, PyWarning};

// Errors
create_exception!(polars.exceptions, PolarsError, PyException);
create_exception!(polars.exceptions, AssertionError, PolarsError);
create_exception!(polars.exceptions, ColumnNotFoundError, PolarsError);
create_exception!(polars.exceptions, ComputeError, PolarsError);
create_exception!(polars.exceptions, DuplicateError, PolarsError);
Expand Down

0 comments on commit 7360d15

Please sign in to comment.