11from __future__ import annotations
22
3+ from typing import TYPE_CHECKING
34from typing import Any
45from typing import Callable
56
67from narwhals .pandas_like .series import Series
8+ from narwhals .pandas_like .utils import register_expression_call
79from narwhals .spec import DataFrame as DataFrameT
810from narwhals .spec import Expr as ExprT
911from narwhals .spec import ExprStringNamespace as ExprStringNamespaceT
1012from narwhals .spec import LazyFrame as LazyFrameProtocol
1113from narwhals .spec import Namespace as NamespaceProtocol
1214from narwhals .spec import Series as SeriesProtocol
13- from narwhals .utils import register_expression_call
15+
16+ if TYPE_CHECKING :
17+ from typing_extensions import Self
1418
1519
1620class Expr (ExprT ):
@@ -44,8 +48,8 @@ def __repr__(self) -> str:
4448
4549 @classmethod
4650 def from_column_names (
47- cls : type [Expr ], * column_names : str , implementation : str
48- ) -> ExprT :
51+ cls : type [Self ], * column_names : str , implementation : str
52+ ) -> Self :
4953 return cls (
5054 lambda df : [
5155 Series (
@@ -70,124 +74,124 @@ def __expr_namespace__(self) -> NamespaceProtocol:
7074 implementation = self ._implementation , # type: ignore[attr-defined]
7175 )
7276
73- def __eq__ (self , other : Expr | Any ) -> ExprT : # type: ignore[override]
77+ def __eq__ (self , other : Expr | Any ) -> Self : # type: ignore[override]
7478 return register_expression_call (self , "__eq__" , other )
7579
76- def __ne__ (self , other : Expr | Any ) -> ExprT : # type: ignore[override]
80+ def __ne__ (self , other : Expr | Any ) -> Self : # type: ignore[override]
7781 return register_expression_call (self , "__ne__" , other )
7882
79- def __ge__ (self , other : Expr | Any ) -> ExprT :
83+ def __ge__ (self , other : Expr | Any ) -> Self :
8084 return register_expression_call (self , "__ge__" , other )
8185
82- def __gt__ (self , other : Expr | Any ) -> ExprT :
86+ def __gt__ (self , other : Expr | Any ) -> Self :
8387 return register_expression_call (self , "__gt__" , other )
8488
85- def __le__ (self , other : Expr | Any ) -> ExprT :
89+ def __le__ (self , other : Expr | Any ) -> Self :
8690 return register_expression_call (self , "__le__" , other )
8791
88- def __lt__ (self , other : Expr | Any ) -> ExprT :
92+ def __lt__ (self , other : Expr | Any ) -> Self :
8993 return register_expression_call (self , "__lt__" , other )
9094
91- def __and__ (self , other : Expr | bool | Any ) -> ExprT :
95+ def __and__ (self , other : Expr | bool | Any ) -> Self :
9296 return register_expression_call (self , "__and__" , other )
9397
94- def __rand__ (self , other : Any ) -> ExprT :
98+ def __rand__ (self , other : Any ) -> Self :
9599 return register_expression_call (self , "__rand__" , other )
96100
97- def __or__ (self , other : Expr | bool | Any ) -> ExprT :
101+ def __or__ (self , other : Expr | bool | Any ) -> Self :
98102 return register_expression_call (self , "__or__" , other )
99103
100- def __ror__ (self , other : Any ) -> ExprT :
104+ def __ror__ (self , other : Any ) -> Self :
101105 return register_expression_call (self , "__ror__" , other )
102106
103- def __add__ (self , other : Expr | Any ) -> ExprT : # type: ignore[override]
107+ def __add__ (self , other : Expr | Any ) -> Self : # type: ignore[override]
104108 return register_expression_call (self , "__add__" , other )
105109
106- def __radd__ (self , other : Any ) -> ExprT :
110+ def __radd__ (self , other : Any ) -> Self :
107111 return register_expression_call (self , "__radd__" , other )
108112
109- def __sub__ (self , other : Expr | Any ) -> ExprT :
113+ def __sub__ (self , other : Expr | Any ) -> Self :
110114 return register_expression_call (self , "__sub__" , other )
111115
112- def __rsub__ (self , other : Any ) -> ExprT :
116+ def __rsub__ (self , other : Any ) -> Self :
113117 return register_expression_call (self , "__rsub__" , other )
114118
115- def __mul__ (self , other : Expr | Any ) -> ExprT :
119+ def __mul__ (self , other : Expr | Any ) -> Self :
116120 return register_expression_call (self , "__mul__" , other )
117121
118- def __rmul__ (self , other : Any ) -> ExprT :
122+ def __rmul__ (self , other : Any ) -> Self :
119123 return self .__mul__ (other )
120124
121- def __truediv__ (self , other : Expr | Any ) -> ExprT :
125+ def __truediv__ (self , other : Expr | Any ) -> Self :
122126 return register_expression_call (self , "__truediv__" , other )
123127
124- def __rtruediv__ (self , other : Any ) -> ExprT :
128+ def __rtruediv__ (self , other : Any ) -> Self :
125129 raise NotImplementedError
126130
127- def __floordiv__ (self , other : Expr | Any ) -> ExprT :
131+ def __floordiv__ (self , other : Expr | Any ) -> Self :
128132 return register_expression_call (self , "__floordiv__" , other )
129133
130- def __rfloordiv__ (self , other : Any ) -> ExprT :
134+ def __rfloordiv__ (self , other : Any ) -> Self :
131135 raise NotImplementedError
132136
133- def __pow__ (self , other : Expr | Any ) -> ExprT :
137+ def __pow__ (self , other : Expr | Any ) -> Self :
134138 return register_expression_call (self , "__pow__" , other )
135139
136- def __rpow__ (self , other : Any ) -> ExprT : # pragma: no cover
140+ def __rpow__ (self , other : Any ) -> Self : # pragma: no cover
137141 raise NotImplementedError
138142
139- def __mod__ (self , other : Expr | Any ) -> ExprT :
143+ def __mod__ (self , other : Expr | Any ) -> Self :
140144 return register_expression_call (self , "__mod__" , other )
141145
142- def __rmod__ (self , other : Any ) -> ExprT : # pragma: no cover
146+ def __rmod__ (self , other : Any ) -> Self : # pragma: no cover
143147 raise NotImplementedError
144148
145149 # Unary
146150
147- def __invert__ (self ) -> ExprT :
151+ def __invert__ (self ) -> Self :
148152 return register_expression_call (self , "__invert__" )
149153
150154 # Reductions
151155
152- def sum (self ) -> ExprT :
156+ def sum (self ) -> Self :
153157 return register_expression_call (self , "sum" )
154158
155- def mean (self ) -> ExprT :
159+ def mean (self ) -> Self :
156160 return register_expression_call (self , "mean" )
157161
158- def max (self ) -> ExprT :
162+ def max (self ) -> Self :
159163 return register_expression_call (self , "max" )
160164
161- def min (self ) -> ExprT :
165+ def min (self ) -> Self :
162166 return register_expression_call (self , "min" )
163167
164168 # Other
165169 def is_between (
166170 self , lower_bound : Any , upper_bound : Any , closed : str = "both"
167- ) -> ExprT :
171+ ) -> Self :
168172 return register_expression_call (
169173 self , "is_between" , lower_bound , upper_bound , closed
170174 )
171175
172- def is_null (self ) -> ExprT :
176+ def is_null (self ) -> Self :
173177 return register_expression_call (self , "is_null" )
174178
175- def is_in (self , other : Any ) -> ExprT :
179+ def is_in (self , other : Any ) -> Self :
176180 return register_expression_call (self , "is_in" , other )
177181
178- def drop_nulls (self ) -> ExprT :
182+ def drop_nulls (self ) -> Self :
179183 return register_expression_call (self , "drop_nulls" )
180184
181- def n_unique (self ) -> ExprT :
185+ def n_unique (self ) -> Self :
182186 return register_expression_call (self , "n_unique" )
183187
184- def unique (self ) -> ExprT :
188+ def unique (self ) -> Self :
185189 return register_expression_call (self , "unique" )
186190
187- def sample (self , n : int , fraction : float , * , with_replacement : bool ) -> ExprT :
191+ def sample (self , n : int , fraction : float , * , with_replacement : bool ) -> Self :
188192 return register_expression_call (self , "sample" , n , fraction , with_replacement )
189193
190- def alias (self , name : str ) -> ExprT :
194+ def alias (self , name : str ) -> Self :
191195 # Define this one manually, so that we can
192196 # override `output_names` and not increase depth
193197 if self ._depth is None :
@@ -211,7 +215,7 @@ class ExprStringNamespace(ExprStringNamespaceT):
211215 def __init__ (self , expr : ExprT ) -> None :
212216 self ._expr = expr
213217
214- def ends_with (self , suffix : str ) -> ExprT :
218+ def ends_with (self , suffix : str ) -> Expr :
215219 # TODO make a register_expression_call for namespaces
216220 return Expr (
217221 lambda df : [
@@ -229,7 +233,7 @@ def ends_with(self, suffix: str) -> ExprT:
229233 implementation = self ._expr ._implementation , # type: ignore[attr-defined]
230234 )
231235
232- def strip_chars (self , characters : str = " " ) -> ExprT :
236+ def strip_chars (self , characters : str = " " ) -> Expr :
233237 return Expr (
234238 lambda df : [
235239 Series (
0 commit comments