Skip to content

Commit fd62b0d

Browse files
committed
refactor ExprNode constructor
1 parent 204652c commit fd62b0d

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

narwhals/_expression_parsing.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,15 @@ def __init__(
226226
kind: ExprKind,
227227
name: str,
228228
/,
229-
*exprs: IntoExpr | NonNestedLiteral,
229+
*,
230+
exprs: tuple[IntoExpr | NonNestedLiteral, ...] = (),
230231
str_as_lit: bool = False,
231232
allow_multi_output: bool = False,
232233
**kwargs: Any,
233234
) -> None:
234235
self.kind: ExprKind = kind
235236
self.name: str = name
236-
self.exprs: Sequence[IntoExpr | NonNestedLiteral] = exprs
237+
self.exprs: tuple[IntoExpr | NonNestedLiteral, ...] = exprs
237238
self.kwargs: dict[str, Any] = kwargs
238239
self.str_as_lit: bool = str_as_lit
239240
self.allow_multi_output: bool = allow_multi_output
@@ -268,7 +269,7 @@ def as_dict(self) -> dict[str, Any]: # pragma: no cover
268269

269270
def _with_kwargs(self, **kwargs: Any) -> ExprNode:
270271
return self.__class__(
271-
self.kind, self.name, *self.exprs, str_as_lit=self.str_as_lit, **kwargs
272+
self.kind, self.name, exprs=self.exprs, str_as_lit=self.str_as_lit, **kwargs
272273
)
273274

274275
def _push_down_over_node_in_place(

narwhals/expr.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def cast(self, dtype: IntoDType) -> Self:
191191

192192
# --- binary ---
193193
def _with_binary(self, attr: str, other: Self | Any) -> Self:
194-
node = ExprNode(ExprKind.ELEMENTWISE, attr, other, str_as_lit=True)
194+
node = ExprNode(ExprKind.ELEMENTWISE, attr, exprs=(other,), str_as_lit=True)
195195
return self._append_node(node)
196196

197197
def __eq__(self, other: Self | Any) -> Self: # type: ignore[override]
@@ -926,7 +926,7 @@ def replace_strict(
926926
node = ExprNode(
927927
ExprKind.ELEMENTWISE,
928928
"replace_strict",
929-
default,
929+
exprs=(default,),
930930
old=old,
931931
new=new,
932932
return_dtype=return_dtype,
@@ -966,7 +966,10 @@ def is_between(
966966
└──────────────────┘
967967
"""
968968
node = ExprNode(
969-
ExprKind.ELEMENTWISE, "is_between", lower_bound, upper_bound, closed=closed
969+
ExprKind.ELEMENTWISE,
970+
"is_between",
971+
exprs=(lower_bound, upper_bound),
972+
closed=closed,
970973
)
971974
return self._append_node(node)
972975

@@ -1029,7 +1032,9 @@ def filter(self, *predicates: Any) -> Self:
10291032
| 5 7 12 |
10301033
└──────────────────┘
10311034
"""
1032-
return self._append_node(ExprNode(ExprKind.FILTRATION, "filter", *predicates))
1035+
return self._append_node(
1036+
ExprNode(ExprKind.FILTRATION, "filter", exprs=predicates)
1037+
)
10331038

10341039
def is_null(self) -> Self:
10351040
"""Returns a boolean Series indicating which values are null.
@@ -1190,7 +1195,7 @@ def fill_null(
11901195
node = ExprNode(
11911196
ExprKind.ELEMENTWISE,
11921197
"fill_null",
1193-
value,
1198+
exprs=(value,),
11941199
strategy=strategy,
11951200
limit=limit,
11961201
str_as_lit=True,
@@ -1607,14 +1612,14 @@ def clip(
16071612
"""
16081613
if upper_bound is None:
16091614
return self._append_node(
1610-
ExprNode(ExprKind.ELEMENTWISE, "clip_lower", lower_bound)
1615+
ExprNode(ExprKind.ELEMENTWISE, "clip_lower", exprs=(lower_bound,))
16111616
)
16121617
if lower_bound is None:
16131618
return self._append_node(
1614-
ExprNode(ExprKind.ELEMENTWISE, "clip_upper", upper_bound)
1619+
ExprNode(ExprKind.ELEMENTWISE, "clip_upper", exprs=(upper_bound,))
16151620
)
16161621
return self._append_node(
1617-
ExprNode(ExprKind.ELEMENTWISE, "clip", lower_bound, upper_bound)
1622+
ExprNode(ExprKind.ELEMENTWISE, "clip", exprs=(lower_bound, upper_bound))
16181623
)
16191624

16201625
def first(self, order_by: str | Iterable[str] | None = None) -> Self:

narwhals/expr_str.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def replace(
7070
ExprNode(
7171
ExprKind.ELEMENTWISE,
7272
"str.replace",
73-
value,
73+
exprs=(value,),
7474
pattern=pattern,
7575
literal=literal,
7676
n=n,
@@ -106,7 +106,7 @@ def replace_all(
106106
ExprNode(
107107
ExprKind.ELEMENTWISE,
108108
"str.replace_all",
109-
value,
109+
exprs=(value,),
110110
pattern=pattern,
111111
literal=literal,
112112
str_as_lit=True,

narwhals/functions.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,9 @@ def _expr_with_horizontal_op(name: str, *exprs: IntoExpr, **kwargs: Any) -> Expr
12601260
msg = f"At least one expression must be passed to `{name}`"
12611261
raise ValueError(msg)
12621262
return Expr(
1263-
ExprNode(ExprKind.ELEMENTWISE, name, *exprs, **kwargs, allow_multi_output=True)
1263+
ExprNode(
1264+
ExprKind.ELEMENTWISE, name, exprs=exprs, **kwargs, allow_multi_output=True
1265+
)
12641266
)
12651267

12661268

@@ -1373,8 +1375,7 @@ def then(self, value: IntoExpr | NonNestedLiteral) -> Then:
13731375
ExprNode(
13741376
ExprKind.ELEMENTWISE,
13751377
"when_then",
1376-
self._predicate,
1377-
value,
1378+
exprs=(self._predicate, value),
13781379
allow_multi_output=False,
13791380
)
13801381
)
@@ -1383,7 +1384,9 @@ def then(self, value: IntoExpr | NonNestedLiteral) -> Then:
13831384
class Then(Expr):
13841385
def otherwise(self, value: IntoExpr | NonNestedLiteral) -> Expr:
13851386
node = self._nodes[0]
1386-
return Expr(ExprNode(ExprKind.ELEMENTWISE, "when_then", *node.exprs, value))
1387+
return Expr(
1388+
ExprNode(ExprKind.ELEMENTWISE, "when_then", exprs=(*node.exprs, value))
1389+
)
13871390

13881391

13891392
def when(*predicates: IntoExpr | Iterable[IntoExpr]) -> When:
@@ -1746,7 +1749,9 @@ def coalesce(
17461749
raise TypeError(msg)
17471750

17481751
return Expr(
1749-
ExprNode(ExprKind.ELEMENTWISE, "coalesce", *flat_exprs, allow_multi_output=True)
1752+
ExprNode(
1753+
ExprKind.ELEMENTWISE, "coalesce", exprs=flat_exprs, allow_multi_output=True
1754+
)
17501755
)
17511756

17521757

narwhals/selectors.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __add__(self, other: Any) -> Expr: # type: ignore[override]
2323
msg = "unsupported operand type(s) for op: ('Selector' + 'Selector')"
2424
raise TypeError(msg)
2525
return self._to_expr()._append_node(
26-
ExprNode(ExprKind.ELEMENTWISE, "__add__", other, str_as_lit=True)
26+
ExprNode(ExprKind.ELEMENTWISE, "__add__", exprs=(other,), str_as_lit=True)
2727
)
2828

2929
def __or__(self, other: Any) -> Expr: # type: ignore[override]
@@ -32,13 +32,13 @@ def __or__(self, other: Any) -> Expr: # type: ignore[override]
3232
ExprNode(
3333
ExprKind.ELEMENTWISE,
3434
"__or__",
35-
other,
35+
exprs=(other,),
3636
str_as_lit=True,
3737
allow_multi_output=True,
3838
)
3939
)
4040
return self._to_expr()._append_node(
41-
ExprNode(ExprKind.ELEMENTWISE, "__or__", other, str_as_lit=True)
41+
ExprNode(ExprKind.ELEMENTWISE, "__or__", exprs=(other,), str_as_lit=True)
4242
)
4343

4444
def __and__(self, other: Any) -> Expr: # type: ignore[override]
@@ -47,13 +47,13 @@ def __and__(self, other: Any) -> Expr: # type: ignore[override]
4747
ExprNode(
4848
ExprKind.ELEMENTWISE,
4949
"__and__",
50-
other,
50+
exprs=(other,),
5151
str_as_lit=True,
5252
allow_multi_output=True,
5353
)
5454
)
5555
return self._to_expr()._append_node(
56-
ExprNode(ExprKind.ELEMENTWISE, "__and__", other, str_as_lit=True)
56+
ExprNode(ExprKind.ELEMENTWISE, "__and__", exprs=(other,), str_as_lit=True)
5757
)
5858

5959
def __rsub__(self, other: Any) -> NoReturn:

0 commit comments

Comments
 (0)