diff --git a/narwhals/_arrow/expr.py b/narwhals/_arrow/expr.py index d0bc49b08..8ab13a75c 100644 --- a/narwhals/_arrow/expr.py +++ b/narwhals/_arrow/expr.py @@ -15,6 +15,7 @@ from narwhals._expression_parsing import reuse_series_implementation from narwhals.dependencies import get_numpy from narwhals.dependencies import is_numpy_array +from narwhals.exceptions import AnonymousExprError from narwhals.exceptions import ColumnNotFoundError from narwhals.typing import CompliantExpr from narwhals.utils import Implementation @@ -390,12 +391,9 @@ def clip(self: Self, lower_bound: Any | None, upper_bound: Any | None) -> Self: def over(self: Self, keys: list[str]) -> Self: def func(df: ArrowDataFrame) -> list[ArrowSeries]: if self._output_names is None: - msg = ( - "Anonymous expressions are not supported in over.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".over" + raise AnonymousExprError.from_expr_name(msg) + tmp = df.group_by(*keys, drop_null_keys=False).agg(self) tmp = df.select(*keys).join( tmp, how="left", left_on=keys, right_on=keys, suffix="_right" diff --git a/narwhals/_arrow/expr_name.py b/narwhals/_arrow/expr_name.py index 7a2fbedef..4c4991bfe 100644 --- a/narwhals/_arrow/expr_name.py +++ b/narwhals/_arrow/expr_name.py @@ -3,6 +3,8 @@ from typing import TYPE_CHECKING from typing import Callable +from narwhals.exceptions import AnonymousExprError + if TYPE_CHECKING: from typing_extensions import Self @@ -17,12 +19,8 @@ def keep(self: Self) -> ArrowExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.keep`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.keep" + raise AnonymousExprError.from_expr_name(msg) return self._compliant_expr.__class__( lambda df: [ @@ -42,12 +40,8 @@ def map(self: Self, function: Callable[[str], str]) -> ArrowExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.map`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.map" + raise AnonymousExprError.from_expr_name(msg) output_names = [function(str(name)) for name in root_names] @@ -68,12 +62,8 @@ def map(self: Self, function: Callable[[str], str]) -> ArrowExpr: def prefix(self: Self, prefix: str) -> ArrowExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.prefix`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.prefix" + raise AnonymousExprError.from_expr_name(msg) output_names = [prefix + str(name) for name in root_names] return self._compliant_expr.__class__( @@ -93,12 +83,8 @@ def prefix(self: Self, prefix: str) -> ArrowExpr: def suffix(self: Self, suffix: str) -> ArrowExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.suffix`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.suffix" + raise AnonymousExprError.from_expr_name(msg) output_names = [str(name) + suffix for name in root_names] @@ -120,12 +106,8 @@ def to_lowercase(self: Self) -> ArrowExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.to_lowercase`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.to_lowercase" + raise AnonymousExprError.from_expr_name(msg) output_names = [str(name).lower() for name in root_names] return self._compliant_expr.__class__( @@ -146,12 +128,8 @@ def to_uppercase(self: Self) -> ArrowExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.to_uppercase`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.to_uppercase" + raise AnonymousExprError.from_expr_name(msg) output_names = [str(name).upper() for name in root_names] return self._compliant_expr.__class__( diff --git a/narwhals/_arrow/group_by.py b/narwhals/_arrow/group_by.py index 11ed914fe..aac1decbe 100644 --- a/narwhals/_arrow/group_by.py +++ b/narwhals/_arrow/group_by.py @@ -9,6 +9,7 @@ from narwhals._expression_parsing import is_simple_aggregation from narwhals._expression_parsing import parse_into_exprs +from narwhals.exceptions import AnonymousExprError from narwhals.utils import generate_temporary_column_name from narwhals.utils import remove_prefix @@ -61,12 +62,8 @@ def agg( ) for expr in exprs: if expr._output_names is None: - msg = ( - "Anonymous expressions are not supported in group_by.agg.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = "group_by.agg" + raise AnonymousExprError.from_expr_name(msg) return agg_arrow( self._grouped, diff --git a/narwhals/_dask/expr.py b/narwhals/_dask/expr.py index 951222c35..04e49cab0 100644 --- a/narwhals/_dask/expr.py +++ b/narwhals/_dask/expr.py @@ -16,6 +16,7 @@ from narwhals._dask.utils import narwhals_to_native_dtype from narwhals._expression_parsing import infer_new_root_output_names from narwhals._pandas_like.utils import native_to_narwhals_dtype +from narwhals.exceptions import AnonymousExprError from narwhals.exceptions import ColumnNotFoundError from narwhals.exceptions import InvalidOperationError from narwhals.typing import CompliantExpr @@ -690,12 +691,8 @@ def gather_every(self: Self, n: int, offset: int = 0) -> NoReturn: def over(self: Self, keys: list[str]) -> Self: def func(df: DaskLazyFrame) -> list[Any]: if self._output_names is None: - msg = ( - "Anonymous expressions are not supported in over.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = "over" + raise AnonymousExprError.from_expr_name(msg) if df._native_frame.npartitions == 1: # pragma: no cover tmp = df.group_by(*keys, drop_null_keys=False).agg(self) diff --git a/narwhals/_dask/expr_name.py b/narwhals/_dask/expr_name.py index 3f5a0e4a2..bbed6addc 100644 --- a/narwhals/_dask/expr_name.py +++ b/narwhals/_dask/expr_name.py @@ -3,6 +3,8 @@ from typing import TYPE_CHECKING from typing import Callable +from narwhals.exceptions import AnonymousExprError + if TYPE_CHECKING: from typing_extensions import Self @@ -17,12 +19,8 @@ def keep(self: Self) -> DaskExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.keep`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.keep" + raise AnonymousExprError.from_expr_name(msg) return self._compliant_expr.__class__( lambda df: [ @@ -43,12 +41,8 @@ def map(self: Self, function: Callable[[str], str]) -> DaskExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.map`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.map" + raise AnonymousExprError.from_expr_name(msg) output_names = [function(str(name)) for name in root_names] @@ -70,12 +64,8 @@ def map(self: Self, function: Callable[[str], str]) -> DaskExpr: def prefix(self: Self, prefix: str) -> DaskExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.prefix`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.prefix" + raise AnonymousExprError.from_expr_name(msg) output_names = [prefix + str(name) for name in root_names] return self._compliant_expr.__class__( @@ -96,12 +86,8 @@ def prefix(self: Self, prefix: str) -> DaskExpr: def suffix(self: Self, suffix: str) -> DaskExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.suffix`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.suffix" + raise AnonymousExprError.from_expr_name(msg) output_names = [str(name) + suffix for name in root_names] @@ -124,12 +110,8 @@ def to_lowercase(self: Self) -> DaskExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.to_lowercase`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.to_lowercase" + raise AnonymousExprError.from_expr_name(msg) output_names = [str(name).lower() for name in root_names] return self._compliant_expr.__class__( @@ -151,12 +133,8 @@ def to_uppercase(self: Self) -> DaskExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.to_uppercase`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.to_uppercase" + raise AnonymousExprError.from_expr_name(msg) output_names = [str(name).upper() for name in root_names] return self._compliant_expr.__class__( diff --git a/narwhals/_dask/group_by.py b/narwhals/_dask/group_by.py index 60086efa2..2fbb4edb6 100644 --- a/narwhals/_dask/group_by.py +++ b/narwhals/_dask/group_by.py @@ -8,6 +8,7 @@ from narwhals._expression_parsing import is_simple_aggregation from narwhals._expression_parsing import parse_into_exprs +from narwhals.exceptions import AnonymousExprError from narwhals.utils import remove_prefix if TYPE_CHECKING: @@ -110,12 +111,8 @@ def agg( output_names: list[str] = copy(self._keys) for expr in exprs: if expr._output_names is None: - msg = ( - "Anonymous expressions are not supported in group_by.agg.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = "group_by.agg" + raise AnonymousExprError.from_expr_name(msg) output_names.extend(expr._output_names) diff --git a/narwhals/_duckdb/group_by.py b/narwhals/_duckdb/group_by.py index 0b312ff03..ce81a1579 100644 --- a/narwhals/_duckdb/group_by.py +++ b/narwhals/_duckdb/group_by.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING from narwhals._expression_parsing import parse_into_exprs +from narwhals.exceptions import AnonymousExprError if TYPE_CHECKING: from narwhals._duckdb.dataframe import DuckDBLazyFrame @@ -33,12 +34,8 @@ def agg( output_names: list[str] = copy(self._keys) for expr in exprs: if expr._output_names is None: # pragma: no cover - msg = ( - "Anonymous expressions are not supported in group_by.agg.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = "group_by.agg" + raise AnonymousExprError.from_expr_name(msg) output_names.extend(expr._output_names) diff --git a/narwhals/_pandas_like/expr.py b/narwhals/_pandas_like/expr.py index 3519674d2..6eceaaac7 100644 --- a/narwhals/_pandas_like/expr.py +++ b/narwhals/_pandas_like/expr.py @@ -16,6 +16,7 @@ from narwhals._pandas_like.utils import rename from narwhals.dependencies import get_numpy from narwhals.dependencies import is_numpy_array +from narwhals.exceptions import AnonymousExprError from narwhals.exceptions import ColumnNotFoundError from narwhals.typing import CompliantExpr @@ -399,12 +400,8 @@ def func(df: PandasLikeDataFrame) -> list[PandasLikeSeries]: self._output_names is None or self._root_names is None ): # pragma: no cover # Technically unreachable, but keep this for safety - msg = ( - "Anonymous expressions are not supported in over.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = "over" + raise AnonymousExprError.from_expr_name(msg) reverse = self._kwargs.get("reverse", False) if reverse: @@ -452,12 +449,8 @@ def func(df: PandasLikeDataFrame) -> list[PandasLikeSeries]: def func(df: PandasLikeDataFrame) -> list[PandasLikeSeries]: if self._output_names is None: - msg = ( - "Anonymous expressions are not supported in over.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = "over" + raise AnonymousExprError.from_expr_name(msg) tmp = df.group_by(*keys, drop_null_keys=False).agg(self) tmp = df.select(*keys).join( tmp, how="left", left_on=keys, right_on=keys, suffix="_right" diff --git a/narwhals/_pandas_like/expr_name.py b/narwhals/_pandas_like/expr_name.py index 0050e698b..fb56c944a 100644 --- a/narwhals/_pandas_like/expr_name.py +++ b/narwhals/_pandas_like/expr_name.py @@ -3,6 +3,8 @@ from typing import TYPE_CHECKING from typing import Callable +from narwhals.exceptions import AnonymousExprError + if TYPE_CHECKING: from typing_extensions import Self @@ -17,12 +19,8 @@ def keep(self: Self) -> PandasLikeExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.keep`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.keep" + raise AnonymousExprError.from_expr_name(msg) return self._compliant_expr.__class__( lambda df: [ @@ -43,12 +41,8 @@ def map(self: Self, function: Callable[[str], str]) -> PandasLikeExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.map`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.map" + raise AnonymousExprError.from_expr_name(msg) output_names = [function(str(name)) for name in root_names] @@ -70,12 +64,8 @@ def map(self: Self, function: Callable[[str], str]) -> PandasLikeExpr: def prefix(self: Self, prefix: str) -> PandasLikeExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.prefix`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.prefix" + raise AnonymousExprError.from_expr_name(msg) output_names = [prefix + str(name) for name in root_names] return self._compliant_expr.__class__( @@ -96,12 +86,8 @@ def prefix(self: Self, prefix: str) -> PandasLikeExpr: def suffix(self: Self, suffix: str) -> PandasLikeExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.suffix`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.suffix" + raise AnonymousExprError.from_expr_name(msg) output_names = [str(name) + suffix for name in root_names] @@ -124,12 +110,9 @@ def to_lowercase(self: Self) -> PandasLikeExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.to_lowercase`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.to_lowercase" + raise AnonymousExprError.from_expr_name(msg) + output_names = [str(name).lower() for name in root_names] return self._compliant_expr.__class__( @@ -151,12 +134,9 @@ def to_uppercase(self: Self) -> PandasLikeExpr: root_names = self._compliant_expr._root_names if root_names is None: - msg = ( - "Anonymous expressions are not supported in `.name.to_uppercase`.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = ".name.to_uppercase" + raise AnonymousExprError.from_expr_name(msg) + output_names = [str(name).upper() for name in root_names] return self._compliant_expr.__class__( diff --git a/narwhals/_pandas_like/group_by.py b/narwhals/_pandas_like/group_by.py index a1eca5b5d..51efb1d82 100644 --- a/narwhals/_pandas_like/group_by.py +++ b/narwhals/_pandas_like/group_by.py @@ -15,6 +15,7 @@ from narwhals._pandas_like.utils import native_series_from_iterable from narwhals._pandas_like.utils import select_columns_by_name from narwhals._pandas_like.utils import set_columns +from narwhals.exceptions import AnonymousExprError from narwhals.utils import Implementation from narwhals.utils import find_stacklevel from narwhals.utils import remove_prefix @@ -92,12 +93,9 @@ def agg( output_names: list[str] = copy(self._keys) for expr in exprs: if expr._output_names is None: - msg = ( - "Anonymous expressions are not supported in group_by.agg.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = "group_by.agg" + raise AnonymousExprError.from_expr_name(msg) + output_names.extend(expr._output_names) return agg_pandas( diff --git a/narwhals/_spark_like/group_by.py b/narwhals/_spark_like/group_by.py index 66f4bf2b8..575163fe4 100644 --- a/narwhals/_spark_like/group_by.py +++ b/narwhals/_spark_like/group_by.py @@ -11,6 +11,7 @@ from narwhals._expression_parsing import parse_into_exprs from narwhals._spark_like.utils import _std from narwhals._spark_like.utils import _var +from narwhals.exceptions import AnonymousExprError from narwhals.utils import parse_version from narwhals.utils import remove_prefix @@ -55,12 +56,8 @@ def agg( output_names: list[str] = copy(self._keys) for expr in exprs: if expr._output_names is None: # pragma: no cover - msg = ( - "Anonymous expressions are not supported in group_by.agg.\n" - "Instead of `nw.all()`, try using a named expression, such as " - "`nw.col('a', 'b')`\n" - ) - raise ValueError(msg) + msg = "group_by.agg" + raise AnonymousExprError.from_expr_name(msg) output_names.extend(expr._output_names) diff --git a/narwhals/exceptions.py b/narwhals/exceptions.py index ee4b79b6a..18a225c8e 100644 --- a/narwhals/exceptions.py +++ b/narwhals/exceptions.py @@ -66,5 +66,22 @@ def from_invalid_type(cls, invalid_type: type) -> InvalidIntoExprError: return InvalidIntoExprError(message) +class AnonymousExprError(ValueError): + """Exception raised when trying to perform operations on anonymous expressions.""" + + def __init__(self, message: str) -> None: + self.message = message + super().__init__(self.message) + + @classmethod + def from_expr_name(cls, expr_name: str) -> AnonymousExprError: + message = ( + f"Anonymous expressions are not supported in `{expr_name}`.\n" + "Instead of `nw.all()`, try using a named expression, such as " + "`nw.col('a', 'b')`" + ) + return AnonymousExprError(message) + + class NarwhalsUnstableWarning(UserWarning): """Warning issued when a method or function is considered unstable in the stable api.""" diff --git a/tests/expr_and_series/name/keep_test.py b/tests/expr_and_series/name/keep_test.py index e382db733..54219dae6 100644 --- a/tests/expr_and_series/name/keep_test.py +++ b/tests/expr_and_series/name/keep_test.py @@ -6,6 +6,7 @@ import pytest import narwhals.stable.v1 as nw +from narwhals.exceptions import AnonymousExprError from tests.utils import Constructor from tests.utils import assert_equal_data @@ -47,7 +48,7 @@ def test_keep_raise_anonymous( does_not_raise() if isinstance(df_raw, (pl.LazyFrame, pl.DataFrame)) else pytest.raises( - ValueError, + AnonymousExprError, match="Anonymous expressions are not supported in `.name.keep`.", ) ) diff --git a/tests/expr_and_series/name/map_test.py b/tests/expr_and_series/name/map_test.py index 276138ef9..d86a12fbe 100644 --- a/tests/expr_and_series/name/map_test.py +++ b/tests/expr_and_series/name/map_test.py @@ -6,6 +6,7 @@ import pytest import narwhals.stable.v1 as nw +from narwhals.exceptions import AnonymousExprError from tests.utils import Constructor from tests.utils import assert_equal_data @@ -51,7 +52,7 @@ def test_map_raise_anonymous( does_not_raise() if isinstance(df_raw, (pl.LazyFrame, pl.DataFrame)) else pytest.raises( - ValueError, + AnonymousExprError, match="Anonymous expressions are not supported in `.name.map`.", ) ) diff --git a/tests/expr_and_series/name/prefix_test.py b/tests/expr_and_series/name/prefix_test.py index 934d1d664..0f6ff2f61 100644 --- a/tests/expr_and_series/name/prefix_test.py +++ b/tests/expr_and_series/name/prefix_test.py @@ -6,6 +6,7 @@ import pytest import narwhals.stable.v1 as nw +from narwhals.exceptions import AnonymousExprError from tests.utils import Constructor from tests.utils import assert_equal_data @@ -48,7 +49,7 @@ def test_prefix_raise_anonymous( does_not_raise() if isinstance(df_raw, (pl.LazyFrame, pl.DataFrame)) else pytest.raises( - ValueError, + AnonymousExprError, match="Anonymous expressions are not supported in `.name.prefix`.", ) ) diff --git a/tests/expr_and_series/name/to_lowercase_test.py b/tests/expr_and_series/name/to_lowercase_test.py index 1b39fc726..191e72275 100644 --- a/tests/expr_and_series/name/to_lowercase_test.py +++ b/tests/expr_and_series/name/to_lowercase_test.py @@ -6,6 +6,7 @@ import pytest import narwhals.stable.v1 as nw +from narwhals.exceptions import AnonymousExprError from tests.utils import Constructor from tests.utils import assert_equal_data @@ -47,7 +48,7 @@ def test_to_lowercase_raise_anonymous( does_not_raise() if isinstance(df_raw, (pl.LazyFrame, pl.DataFrame)) else pytest.raises( - ValueError, + AnonymousExprError, match="Anonymous expressions are not supported in `.name.to_lowercase`.", ) ) diff --git a/tests/expr_and_series/name/to_uppercase_test.py b/tests/expr_and_series/name/to_uppercase_test.py index e6703212d..b0e49c7d5 100644 --- a/tests/expr_and_series/name/to_uppercase_test.py +++ b/tests/expr_and_series/name/to_uppercase_test.py @@ -6,6 +6,7 @@ import pytest import narwhals.stable.v1 as nw +from narwhals.exceptions import AnonymousExprError from tests.utils import Constructor from tests.utils import assert_equal_data @@ -44,7 +45,7 @@ def test_to_uppercase_raise_anonymous( does_not_raise() if isinstance(df_raw, (pl.LazyFrame, pl.DataFrame)) else pytest.raises( - ValueError, + AnonymousExprError, match="Anonymous expressions are not supported in `.name.to_uppercase`.", ) ) diff --git a/tests/group_by_test.py b/tests/group_by_test.py index 911cc473e..40acd142b 100644 --- a/tests/group_by_test.py +++ b/tests/group_by_test.py @@ -8,6 +8,7 @@ import pytest import narwhals.stable.v1 as nw +from narwhals.exceptions import AnonymousExprError from tests.utils import PANDAS_VERSION from tests.utils import PYARROW_VERSION from tests.utils import Constructor @@ -49,7 +50,8 @@ def test_invalid_group_by_dask() -> None: nw.from_native(df_dask).group_by("a").agg(nw.col("b")) with pytest.raises( - ValueError, match=r"Anonymous expressions are not supported in group_by\.agg" + AnonymousExprError, + match=r"Anonymous expressions are not supported in `group_by\.agg`", ): nw.from_native(df_dask).group_by("a").agg(nw.all().mean()) @@ -60,11 +62,13 @@ def test_invalid_group_by() -> None: with pytest.raises(ValueError, match="does your"): df.group_by("a").agg(nw.col("b")) with pytest.raises( - ValueError, match=r"Anonymous expressions are not supported in group_by\.agg" + AnonymousExprError, + match=r"Anonymous expressions are not supported in `group_by\.agg`", ): df.group_by("a").agg(nw.all().mean()) with pytest.raises( - ValueError, match=r"Anonymous expressions are not supported in group_by\.agg" + AnonymousExprError, + match=r"Anonymous expressions are not supported in `group_by\.agg`", ): nw.from_native(pa.table({"a": [1, 2, 3]})).group_by("a").agg(nw.all().mean()) with pytest.raises(ValueError, match=r"Non-trivial complex aggregation found"):