Skip to content

Commit 64f6b4f

Browse files
committed
fix: It found a bug!!!
#3189 (comment)
1 parent 7b43748 commit 64f6b4f

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

narwhals/_duckdb/expr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ def is_finite(self) -> Self:
258258
return self._with_elementwise(lambda expr: F("isfinite", expr))
259259

260260
def is_in(self, other: Sequence[Any]) -> Self:
261-
return self._with_elementwise(lambda expr: F("contains", lit(other), expr))
261+
other_ = tuple(other) if not isinstance(other, (tuple, list)) else other
262+
return self._with_elementwise(lambda expr: F("contains", lit(other_), expr))
262263

263264
def fill_null(
264265
self,

narwhals/_duckdb/typing.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,31 @@
88
from narwhals._typing_compat import TypeVar
99

1010
if TYPE_CHECKING:
11+
import uuid
12+
13+
import numpy as np
14+
import pandas as pd
1115
from duckdb import DuckDBPyConnection, Expression
1216
from typing_extensions import TypeAlias, TypeIs
1317

18+
from narwhals.typing import Into1DArray, PythonLiteral
19+
20+
1421
__all__ = ["BaseType", "WindowExpressionKwargs", "has_children", "is_dtype"]
1522

23+
IntoDuckDBLiteral: TypeAlias = """
24+
PythonLiteral
25+
| dict[Any, Any]
26+
| uuid.UUID
27+
| bytearray
28+
| memoryview
29+
| Into1DArray
30+
| pd.api.typing.NaTType
31+
| pd.api.typing.NAType
32+
| np.ma.MaskedArray
33+
| duckdb.Value
34+
"""
35+
1636

1737
class WindowExpressionKwargs(TypedDict, total=False):
1838
partition_by: Sequence[str | Expression]

narwhals/_duckdb/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import duckdb
77
from duckdb import Expression
88

9-
from narwhals._duckdb.typing import BaseType, has_children, is_dtype
9+
from narwhals._duckdb.typing import BaseType, IntoDuckDBLiteral, has_children, is_dtype
1010
from narwhals._utils import Implementation, Version, isinstance_or_issubclass, zip_strict
1111
from narwhals.exceptions import ColumnNotFoundError, UnsupportedDTypeError
1212

@@ -22,7 +22,7 @@
2222
from narwhals._duckdb.dataframe import DuckDBLazyFrame
2323
from narwhals._duckdb.expr import DuckDBExpr
2424
from narwhals.dtypes import DType
25-
from narwhals.typing import IntoDType, NonNestedLiteral, TimeUnit
25+
from narwhals.typing import IntoDType, TimeUnit
2626

2727
Incomplete: TypeAlias = Any
2828

@@ -54,7 +54,7 @@
5454

5555

5656
# TODO @dangotbanned: Raise an issue upstream on `Expression | str` too narrow
57-
def lit(value: Expression | NonNestedLiteral | Sequence[Any]) -> Expression:
57+
def lit(value: IntoDuckDBLiteral | Expression) -> Expression:
5858
"""Alias for `duckdb.ConstantExpression`."""
5959
lit_: Incomplete = duckdb.ConstantExpression
6060
return lit_(value)

tests/expr_and_series/is_in_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ def test_expr_is_in_empty_list(constructor: Constructor) -> None:
2626
assert_equal_data(result, expected)
2727

2828

29+
def test_expr_is_in_iterable(
30+
constructor: Constructor, request: pytest.FixtureRequest
31+
) -> None:
32+
if any(x in str(constructor) for x in ("sqlframe", "polars")):
33+
request.applymarker(pytest.mark.xfail)
34+
df = nw.from_native(constructor(data))
35+
sequence = 4, 2
36+
result = df.select(nw.col("a").is_in(iter(sequence)))
37+
expected = {"a": [False, True, True, False]}
38+
assert_equal_data(result, expected)
39+
40+
2941
def test_ser_is_in(constructor_eager: ConstructorEager) -> None:
3042
ser = nw.from_native(constructor_eager(data), eager_only=True)["a"]
3143
result = {"a": ser.is_in([4, 5])}

0 commit comments

Comments
 (0)