From 07b239ef3aadd1b0b100ce4c42dce71fb159146d Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Wed, 18 Sep 2024 13:37:28 +0200 Subject: [PATCH] fix: Fixed some error/assertion types --- crates/polars-expr/src/expressions/binary.rs | 2 +- crates/polars-expr/src/expressions/sortby.rs | 4 ++-- py-polars/polars/testing/asserts/series.py | 10 +++++++++- py-polars/tests/unit/expr/test_exprs.py | 1 + py-polars/tests/unit/operations/test_sort.py | 2 +- py-polars/tests/unit/test_errors.py | 10 +++------- py-polars/tests/unit/test_scalar.py | 1 + 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/crates/polars-expr/src/expressions/binary.rs b/crates/polars-expr/src/expressions/binary.rs index c7e89132bc2e..d76db8db59f8 100644 --- a/crates/polars-expr/src/expressions/binary.rs +++ b/crates/polars-expr/src/expressions/binary.rs @@ -201,7 +201,7 @@ impl PhysicalExpr for BinaryExpr { polars_ensure!( lhs.len() == rhs.len() || lhs.len() == 1 || rhs.len() == 1, expr = self.expr, - ComputeError: "cannot evaluate two Series of different lengths ({} and {})", + ShapeMismatch: "cannot evaluate two Series of different lengths ({} and {})", lhs.len(), rhs.len(), ); apply_operator_owned(lhs, rhs, self.op) diff --git a/crates/polars-expr/src/expressions/sortby.rs b/crates/polars-expr/src/expressions/sortby.rs index 43c8f0a0cf70..f966e4cbb544 100644 --- a/crates/polars-expr/src/expressions/sortby.rs +++ b/crates/polars-expr/src/expressions/sortby.rs @@ -239,7 +239,7 @@ impl PhysicalExpr for SortByExpr { for i in 1..s_sort_by.len() { polars_ensure!( s_sort_by[0].len() == s_sort_by[i].len(), - expr = self.expr, ComputeError: + expr = self.expr, ShapeMismatch: "`sort_by` produced different length ({}) than earlier Series' length in `by` ({})", s_sort_by[0].len(), s_sort_by[i].len() ); @@ -254,7 +254,7 @@ impl PhysicalExpr for SortByExpr { let (sorted_idx, series) = (sorted_idx?, series?); polars_ensure!( sorted_idx.len() == series.len(), - expr = self.expr, ComputeError: + expr = self.expr, ShapeMismatch: "`sort_by` produced different length ({}) than the Series that has to be sorted ({})", sorted_idx.len(), series.len() ); diff --git a/py-polars/polars/testing/asserts/series.py b/py-polars/polars/testing/asserts/series.py index e85e90790bd4..f7c522315d03 100644 --- a/py-polars/polars/testing/asserts/series.py +++ b/py-polars/polars/testing/asserts/series.py @@ -12,7 +12,7 @@ unpack_dtypes, ) from polars.datatypes.group import FLOAT_DTYPES -from polars.exceptions import ComputeError, InvalidOperationError +from polars.exceptions import ComputeError, InvalidOperationError, ShapeError from polars.series import Series from polars.testing.asserts.utils import raise_assertion_error @@ -157,6 +157,14 @@ def _assert_series_values_equal( right=right.dtype, cause=exc, ) + except ShapeError as exc: + raise_assertion_error( + "Series", + "incompatible lengths", + left=left, + right=right, + cause=exc, + ) # Check nested dtypes in separate function if _comparing_nested_floats(left.dtype, right.dtype): diff --git a/py-polars/tests/unit/expr/test_exprs.py b/py-polars/tests/unit/expr/test_exprs.py index a8f874e42548..b97b3c8f1288 100644 --- a/py-polars/tests/unit/expr/test_exprs.py +++ b/py-polars/tests/unit/expr/test_exprs.py @@ -289,6 +289,7 @@ def test_power_by_expression() -> None: assert out["pow_op_left"].to_list() == [2.0, 4.0, None, 16.0, None, 64.0] +@pytest.mark.may_fail_auto_streaming def test_expression_appends() -> None: df = pl.DataFrame({"a": [1, 1, 2]}) diff --git a/py-polars/tests/unit/operations/test_sort.py b/py-polars/tests/unit/operations/test_sort.py index 6d67e83a418a..6b2060e8305f 100644 --- a/py-polars/tests/unit/operations/test_sort.py +++ b/py-polars/tests/unit/operations/test_sort.py @@ -808,7 +808,7 @@ def test_sort_string_nulls() -> None: def test_sort_by_unequal_lengths_7207() -> None: df = pl.DataFrame({"a": [0, 1, 1, 0], "b": [3, 2, 3, 2]}) - with pytest.raises(pl.exceptions.ComputeError): + with pytest.raises(pl.exceptions.ShapeError): df.select(pl.col.a.sort_by(["a", 1])) diff --git a/py-polars/tests/unit/test_errors.py b/py-polars/tests/unit/test_errors.py index 07b98d9d8111..f875c0953798 100644 --- a/py-polars/tests/unit/test_errors.py +++ b/py-polars/tests/unit/test_errors.py @@ -13,6 +13,7 @@ from polars.datatypes.convert import dtype_to_py_type from polars.exceptions import ( ColumnNotFoundError, + ShapeError, ComputeError, InvalidOperationError, OutOfBoundsError, @@ -293,10 +294,7 @@ def test_invalid_sort_by() -> None: ) # `select a where b order by c desc` - with pytest.raises( - ComputeError, - match=r"`sort_by` produced different length \(5\) than the Series that has to be sorted \(3\)", - ): + with pytest.raises(ShapeError): df.select(pl.col("a").filter(pl.col("b") == "M").sort_by("c", descending=True)) @@ -447,9 +445,7 @@ def test_compare_different_len() -> None: ) s = pl.Series([2, 5, 8]) - with pytest.raises( - ComputeError, match=r"cannot evaluate two Series of different lengths" - ): + with pytest.raises(ShapeError): df.filter(pl.col("idx") == s) diff --git a/py-polars/tests/unit/test_scalar.py b/py-polars/tests/unit/test_scalar.py index 68632e7bfdd4..f0a845dd43ab 100644 --- a/py-polars/tests/unit/test_scalar.py +++ b/py-polars/tests/unit/test_scalar.py @@ -3,6 +3,7 @@ import polars as pl +@pytest.mark.may_fail_auto_streaming def test_invalid_broadcast() -> None: df = pl.DataFrame( {