From 5dca2a9890314b93757c79a7f8dfe8be68f7e470 Mon Sep 17 00:00:00 2001 From: Francesco Bruzzesi <42817048+FBruzzesi@users.noreply.github.com> Date: Wed, 8 Jan 2025 16:44:36 +0100 Subject: [PATCH] docs: fix `is_between` type hint in signature (#1766) * docs: fix is_between signature type hint * rm default in _dask * test typing --- narwhals/_arrow/expr.py | 7 ++++++- narwhals/_arrow/series.py | 5 ++++- narwhals/_dask/expr.py | 7 +++---- narwhals/_pandas_like/expr.py | 5 ++++- narwhals/_pandas_like/series.py | 5 ++++- narwhals/_spark_like/expr.py | 3 ++- narwhals/expr.py | 4 ++-- narwhals/series.py | 5 ++++- tests/expr_and_series/is_between_test.py | 12 ++++++++++-- tests/spark_like_test.py | 5 ++++- 10 files changed, 43 insertions(+), 15 deletions(-) diff --git a/narwhals/_arrow/expr.py b/narwhals/_arrow/expr.py index 1c0d0734e..df5c95367 100644 --- a/narwhals/_arrow/expr.py +++ b/narwhals/_arrow/expr.py @@ -283,7 +283,12 @@ def is_null(self: Self) -> Self: def is_nan(self: Self) -> Self: return reuse_series_implementation(self, "is_nan") - def is_between(self: Self, lower_bound: Any, upper_bound: Any, closed: str) -> Self: + def is_between( + self: Self, + lower_bound: Any, + upper_bound: Any, + closed: Literal["left", "right", "none", "both"], + ) -> Self: return reuse_series_implementation( self, "is_between", diff --git a/narwhals/_arrow/series.py b/narwhals/_arrow/series.py index 1e8d09827..193fc25a2 100644 --- a/narwhals/_arrow/series.py +++ b/narwhals/_arrow/series.py @@ -490,7 +490,10 @@ def all(self: Self, *, _return_py_scalar: bool = True) -> bool: ) def is_between( - self, lower_bound: Any, upper_bound: Any, closed: str = "both" + self, + lower_bound: Any, + upper_bound: Any, + closed: Literal["left", "right", "none", "both"], ) -> Self: import pyarrow.compute as pc diff --git a/narwhals/_dask/expr.py b/narwhals/_dask/expr.py index cb20fa616..40e7eff9c 100644 --- a/narwhals/_dask/expr.py +++ b/narwhals/_dask/expr.py @@ -413,10 +413,9 @@ def is_between( self, lower_bound: Self | Any, upper_bound: Self | Any, - closed: str = "both", + closed: Literal["left", "right", "none", "both"], ) -> Self: - if closed == "none": - closed = "neither" + closed_ = "neither" if closed == "none" else closed return self._from_call( lambda _input, lower_bound, upper_bound, closed: _input.between( lower_bound, upper_bound, closed @@ -424,7 +423,7 @@ def is_between( "is_between", lower_bound=lower_bound, upper_bound=upper_bound, - closed=closed, + closed=closed_, returns_scalar=self._returns_scalar, ) diff --git a/narwhals/_pandas_like/expr.py b/narwhals/_pandas_like/expr.py index 34d05b7eb..c694b3420 100644 --- a/narwhals/_pandas_like/expr.py +++ b/narwhals/_pandas_like/expr.py @@ -262,7 +262,10 @@ def clip(self, lower_bound: Any, upper_bound: Any) -> Self: ) def is_between( - self, lower_bound: Any, upper_bound: Any, closed: str = "both" + self, + lower_bound: Any, + upper_bound: Any, + closed: Literal["left", "right", "none", "both"], ) -> Self: return reuse_series_implementation( self, diff --git a/narwhals/_pandas_like/series.py b/narwhals/_pandas_like/series.py index e5c5e771e..35ec672e4 100644 --- a/narwhals/_pandas_like/series.py +++ b/narwhals/_pandas_like/series.py @@ -263,7 +263,10 @@ def to_list(self) -> Any: return self._native_series.to_list() def is_between( - self, lower_bound: Any, upper_bound: Any, closed: str = "both" + self, + lower_bound: Any, + upper_bound: Any, + closed: Literal["left", "right", "none", "both"], ) -> PandasLikeSeries: ser = self._native_series _, lower_bound = broadcast_align_and_extract_native(self, lower_bound) diff --git a/narwhals/_spark_like/expr.py b/narwhals/_spark_like/expr.py index 66826a6ab..03529ca96 100644 --- a/narwhals/_spark_like/expr.py +++ b/narwhals/_spark_like/expr.py @@ -3,6 +3,7 @@ from typing import TYPE_CHECKING from typing import Any from typing import Callable +from typing import Literal from typing import Sequence from narwhals._expression_parsing import infer_new_root_output_names @@ -276,7 +277,7 @@ def is_between( self, lower_bound: Any, upper_bound: Any, - closed: str, + closed: Literal["left", "right", "none", "both"], ) -> Self: def _is_between(_input: Column, lower_bound: Any, upper_bound: Any) -> Column: if closed == "both": diff --git a/narwhals/expr.py b/narwhals/expr.py index 653300da8..807a7f04b 100644 --- a/narwhals/expr.py +++ b/narwhals/expr.py @@ -1792,10 +1792,10 @@ def sort(self, *, descending: bool = False, nulls_last: bool = False) -> Self: # --- transform --- def is_between( - self, + self: Self, lower_bound: Any | IntoExpr, upper_bound: Any | IntoExpr, - closed: str = "both", + closed: Literal["left", "right", "none", "both"] = "both", ) -> Self: """Check if this expression is between the given lower and upper bounds. diff --git a/narwhals/series.py b/narwhals/series.py index 7b4cfbf6e..8385b43ad 100644 --- a/narwhals/series.py +++ b/narwhals/series.py @@ -2605,7 +2605,10 @@ def fill_null( ) def is_between( - self, lower_bound: Any | Self, upper_bound: Any | Self, closed: str = "both" + self: Self, + lower_bound: Any | Self, + upper_bound: Any | Self, + closed: Literal["left", "right", "none", "both"] = "both", ) -> Self: """Get a boolean mask of the values that are between the given lower/upper bounds. diff --git a/tests/expr_and_series/is_between_test.py b/tests/expr_and_series/is_between_test.py index 57ad545c0..a24277fa5 100644 --- a/tests/expr_and_series/is_between_test.py +++ b/tests/expr_and_series/is_between_test.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Literal + import pytest import narwhals.stable.v1 as nw @@ -17,7 +19,11 @@ ("none", [False, True, True, False]), ], ) -def test_is_between(constructor: Constructor, closed: str, expected: list[bool]) -> None: +def test_is_between( + constructor: Constructor, + closed: Literal["left", "right", "none", "both"], + expected: list[bool], +) -> None: data = {"a": [1, 4, 2, 5]} df = nw.from_native(constructor(data)) result = df.select(nw.col("a").is_between(1, 5, closed=closed)) @@ -43,7 +49,9 @@ def test_is_between_expressified(constructor: Constructor) -> None: ], ) def test_is_between_series( - constructor_eager: ConstructorEager, closed: str, expected: list[bool] + constructor_eager: ConstructorEager, + closed: Literal["left", "right", "none", "both"], + expected: list[bool], ) -> None: data = {"a": [1, 4, 2, 5]} df = nw.from_native(constructor_eager(data), eager_only=True) diff --git a/tests/spark_like_test.py b/tests/spark_like_test.py index c929f4f85..30610be45 100644 --- a/tests/spark_like_test.py +++ b/tests/spark_like_test.py @@ -9,6 +9,7 @@ from contextlib import nullcontext as does_not_raise from typing import TYPE_CHECKING from typing import Any +from typing import Literal import pandas as pd import pytest @@ -991,7 +992,9 @@ def test_clip(pyspark_constructor: Constructor) -> None: ], ) def test_is_between( - pyspark_constructor: Constructor, closed: str, expected: list[bool] + pyspark_constructor: Constructor, + closed: Literal["left", "right", "none", "both"], + expected: list[bool], ) -> None: data = {"a": [1, 4, 2, 5]} df = nw.from_native(pyspark_constructor(data))