Skip to content

Commit ed6f6b2

Browse files
committed
fix(typing): Align lambda_expr to cpp implementation
1 parent 64f6b4f commit ed6f6b2

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

narwhals/_duckdb/typing.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
22

33
from collections.abc import Sequence
4-
from typing import TYPE_CHECKING, Any, Literal, Protocol, TypedDict, overload
4+
from typing import TYPE_CHECKING, Any, Literal, Protocol, TypedDict, Union, overload
55

66
import duckdb
7+
from duckdb import Expression
78

89
from narwhals._typing_compat import TypeVar
910

@@ -12,13 +13,19 @@
1213

1314
import numpy as np
1415
import pandas as pd
15-
from duckdb import DuckDBPyConnection, Expression
16+
from duckdb import DuckDBPyConnection
1617
from typing_extensions import TypeAlias, TypeIs
1718

1819
from narwhals.typing import Into1DArray, PythonLiteral
1920

2021

21-
__all__ = ["BaseType", "WindowExpressionKwargs", "has_children", "is_dtype"]
22+
__all__ = [
23+
"BaseType",
24+
"IntoColumnExpr",
25+
"WindowExpressionKwargs",
26+
"has_children",
27+
"is_dtype",
28+
]
2229

2330
IntoDuckDBLiteral: TypeAlias = """
2431
PythonLiteral
@@ -60,6 +67,8 @@ class WindowExpressionKwargs(TypedDict, total=False):
6067
_Enum: TypeAlias = Literal["enum"]
6168
_Decimal: TypeAlias = Literal["decimal"]
6269
_TimestampTZ: TypeAlias = Literal["timestamp with time zone"]
70+
IntoColumnExpr: TypeAlias = Union[str, Expression]
71+
"""A column name, or the result of calling `duckdb.ColumnExpression`."""
6372

6473

6574
class BaseType(Protocol[_ID_co]):

narwhals/_duckdb/utils.py

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

9-
from narwhals._duckdb.typing import BaseType, IntoDuckDBLiteral, has_children, is_dtype
9+
from narwhals._duckdb.typing import (
10+
BaseType,
11+
IntoColumnExpr,
12+
IntoDuckDBLiteral,
13+
has_children,
14+
is_dtype,
15+
)
1016
from narwhals._utils import Implementation, Version, isinstance_or_issubclass, zip_strict
1117
from narwhals.exceptions import ColumnNotFoundError, UnsupportedDTypeError
1218

@@ -54,6 +60,7 @@
5460

5561

5662
# TODO @dangotbanned: Raise an issue upstream on `Expression | str` too narrow
63+
# NOTE: https://github.com/duckdb/duckdb-python/blob/df7789cbd31b2d2b8d03d012f14331bc3297fb2d/src/duckdb_py/native/python_conversion.cpp#L916-L1069
5764
def lit(value: IntoDuckDBLiteral | Expression) -> Expression:
5865
"""Alias for `duckdb.ConstantExpression`."""
5966
lit_: Incomplete = duckdb.ConstantExpression
@@ -67,10 +74,10 @@ def lit(value: IntoDuckDBLiteral | Expression) -> Expression:
6774
"""Alias for `duckdb.FunctionExpression`."""
6875

6976

70-
# TODO @dangotbanned: Investigate `lhs: Expression | str | tuple[str]`
71-
# Seems incorrect
77+
# TODO @dangotbanned: Raise an issue upstream on `Expression | str | tuple[str` too narrow
78+
# NOTE: https://github.com/duckdb/duckdb-python/blob/df7789cbd31b2d2b8d03d012f14331bc3297fb2d/src/duckdb_py/pyexpression.cpp#L361-L413
7279
def lambda_expr(
73-
params: str | Expression | tuple[Expression, ...], expr: Expression, /
80+
params: IntoColumnExpr | tuple[IntoColumnExpr, ...], expr: Expression, /
7481
) -> Expression:
7582
"""Wraps [`duckdb.LambdaExpression`].
7683

0 commit comments

Comments
 (0)