From 4c8cc1b9090fc315126a90b292b6ed17ac51deb1 Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Sun, 10 Nov 2024 11:07:55 +0100 Subject: [PATCH] merge main, test invalid method --- narwhals/expr.py | 5 ++++- narwhals/series.py | 5 ++++- tests/expr_and_series/rank_test.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/narwhals/expr.py b/narwhals/expr.py index 089e56fcf..8c6fc8b27 100644 --- a/narwhals/expr.py +++ b/narwhals/expr.py @@ -2528,7 +2528,10 @@ def rank( supported_rank_methods = {"average", "min", "max", "dense", "ordinal"} if method not in supported_rank_methods: - msg = f"Ranking method must be one of {supported_rank_methods}. Found '{method}'" + msg = ( + "Ranking method must be one of {'average', 'min', 'max', 'dense', 'ordinal'}. " + f"Found '{method}'" + ) raise ValueError(msg) return self.__class__( diff --git a/narwhals/series.py b/narwhals/series.py index b49e2d093..5f5a27c9c 100644 --- a/narwhals/series.py +++ b/narwhals/series.py @@ -2731,7 +2731,10 @@ def rank( """ supported_rank_methods = {"average", "min", "max", "dense", "ordinal"} if method not in supported_rank_methods: - msg = f"Ranking method must be one of {supported_rank_methods}. Found '{method}'" + msg = ( + "Ranking method must be one of {'average', 'min', 'max', 'dense', 'ordinal'}. " + f"Found '{method}'" + ) raise ValueError(msg) return self._from_compliant_series( diff --git a/tests/expr_and_series/rank_test.py b/tests/expr_and_series/rank_test.py index 695e4a37a..259bfbd5d 100644 --- a/tests/expr_and_series/rank_test.py +++ b/tests/expr_and_series/rank_test.py @@ -101,3 +101,19 @@ def test_rank_expr_in_over_context( result = df.select(nw.col("a").rank(method=method).over("b")) expected_data = {"a": expected_over[method]} assert_equal_data(result, expected_data) + + +def test_invalid_method_raise(constructor: Constructor) -> None: + method = "invalid_method_name" + df = nw.from_native(constructor(data_float)) + + msg = ( + "Ranking method must be one of {'average', 'min', 'max', 'dense', 'ordinal'}. " + f"Found '{method}'" + ) + + with pytest.raises(ValueError, match=msg): + df.select(nw.col("a").rank(method=method)) # type: ignore[arg-type] + + with pytest.raises(ValueError, match=msg): + df.lazy().collect()["a"].rank(method=method) # type: ignore[arg-type]