Skip to content

Commit 5a25d07

Browse files
committed
refactor: Move ExprDispatch to column.py
1 parent 5318254 commit 5a25d07

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

narwhals/_plan/arrow/expr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from narwhals._plan.arrow import functions as fn
1010
from narwhals._plan.arrow.series import ArrowSeries as Series
1111
from narwhals._plan.arrow.typing import ChunkedOrScalarAny, NativeScalar, StoresNativeT_co
12+
from narwhals._plan.compliant.column import ExprDispatch
1213
from narwhals._plan.compliant.typing import namespace
1314
from narwhals._plan.expressions import NamedIR
14-
from narwhals._plan.protocols import EagerExpr, EagerScalar, ExprDispatch
15+
from narwhals._plan.protocols import EagerExpr, EagerScalar
1516
from narwhals._utils import (
1617
Implementation,
1718
Version,

narwhals/_plan/compliant/column.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
from __future__ import annotations
22

3-
from collections.abc import Sequence, Sized
3+
from collections.abc import Sized
44
from typing import TYPE_CHECKING, Protocol
55

66
from narwhals._plan.common import flatten_hash_safe
7-
from narwhals._plan.compliant.typing import LengthT, SeriesT
7+
from narwhals._plan.compliant.typing import (
8+
FrameT_contra,
9+
LengthT,
10+
NamespaceT_co,
11+
R_co,
12+
SeriesT,
13+
StoresVersion,
14+
)
815

916
if TYPE_CHECKING:
1017
from collections.abc import Iterator, Sequence
1118

1219
from typing_extensions import Self
1320

21+
from narwhals._plan import expressions as ir
1422
from narwhals._plan.typing import OneOrIterable
1523

1624

@@ -74,3 +82,18 @@ def _length_required(
7482
max_length = cls._length_max(lengths)
7583
required = any(len_ != max_length for len_ in lengths)
7684
return max_length if required else None
85+
86+
87+
class ExprDispatch(StoresVersion, Protocol[FrameT_contra, R_co, NamespaceT_co]):
88+
@classmethod
89+
def from_ir(cls, node: ir.ExprIR, frame: FrameT_contra, name: str) -> R_co:
90+
obj = cls.__new__(cls)
91+
obj._version = frame.version
92+
return node.dispatch(obj, frame, name)
93+
94+
@classmethod
95+
def from_named_ir(cls, named_ir: ir.NamedIR[ir.ExprIR], frame: FrameT_contra) -> R_co:
96+
return cls.from_ir(named_ir.expr, frame, named_ir.name)
97+
98+
# NOTE: Needs to stay `covariant` and never be used as a parameter
99+
def __narwhals_namespace__(self) -> NamespaceT_co: ...

narwhals/_plan/compliant/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
if TYPE_CHECKING:
88
from typing_extensions import TypeAlias
99

10+
from narwhals._plan.compliant.column import ExprDispatch
1011
from narwhals._plan.compliant.dataframe import (
1112
CompliantBaseFrame,
1213
CompliantDataFrame,
@@ -20,7 +21,6 @@
2021
CompliantScalar,
2122
EagerExpr,
2223
EagerScalar,
23-
ExprDispatch,
2424
LazyExpr,
2525
LazyScalar,
2626
)

narwhals/_plan/protocols.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from narwhals._plan.compliant.typing import (
99
FrameT_contra,
1010
LengthT,
11-
NamespaceT_co,
12-
R_co,
1311
SeriesT,
1412
SeriesT_co,
1513
StoresVersion,
@@ -23,7 +21,6 @@
2321
from narwhals._plan.expressions import (
2422
BinaryExpr,
2523
FunctionExpr,
26-
NamedIR,
2724
aggregation as agg,
2825
boolean,
2926
functions as F,
@@ -32,21 +29,6 @@
3229
from narwhals.typing import IntoDType, PythonLiteral
3330

3431

35-
class ExprDispatch(StoresVersion, Protocol[FrameT_contra, R_co, NamespaceT_co]):
36-
@classmethod
37-
def from_ir(cls, node: ir.ExprIR, frame: FrameT_contra, name: str) -> R_co:
38-
obj = cls.__new__(cls)
39-
obj._version = frame.version
40-
return node.dispatch(obj, frame, name)
41-
42-
@classmethod
43-
def from_named_ir(cls, named_ir: NamedIR[ir.ExprIR], frame: FrameT_contra) -> R_co:
44-
return cls.from_ir(named_ir.expr, frame, named_ir.name)
45-
46-
# NOTE: Needs to stay `covariant` and never be used as a parameter
47-
def __narwhals_namespace__(self) -> NamespaceT_co: ...
48-
49-
5032
class CompliantExpr(StoresVersion, Protocol[FrameT_contra, SeriesT_co]):
5133
"""Everything common to `Expr`/`Series` and `Scalar` literal values."""
5234

0 commit comments

Comments
 (0)