1
1
from __future__ import annotations
2
2
3
+ from typing import TYPE_CHECKING
3
4
from typing import Any
4
5
from typing import Callable
5
6
6
7
from narwhals .pandas_like .series import Series
8
+ from narwhals .pandas_like .utils import register_expression_call
7
9
from narwhals .spec import DataFrame as DataFrameT
8
10
from narwhals .spec import Expr as ExprT
9
11
from narwhals .spec import ExprStringNamespace as ExprStringNamespaceT
10
12
from narwhals .spec import LazyFrame as LazyFrameProtocol
11
13
from narwhals .spec import Namespace as NamespaceProtocol
12
14
from 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
14
18
15
19
16
20
class Expr (ExprT ):
@@ -44,8 +48,8 @@ def __repr__(self) -> str:
44
48
45
49
@classmethod
46
50
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 :
49
53
return cls (
50
54
lambda df : [
51
55
Series (
@@ -70,124 +74,124 @@ def __expr_namespace__(self) -> NamespaceProtocol:
70
74
implementation = self ._implementation , # type: ignore[attr-defined]
71
75
)
72
76
73
- def __eq__ (self , other : Expr | Any ) -> ExprT : # type: ignore[override]
77
+ def __eq__ (self , other : Expr | Any ) -> Self : # type: ignore[override]
74
78
return register_expression_call (self , "__eq__" , other )
75
79
76
- def __ne__ (self , other : Expr | Any ) -> ExprT : # type: ignore[override]
80
+ def __ne__ (self , other : Expr | Any ) -> Self : # type: ignore[override]
77
81
return register_expression_call (self , "__ne__" , other )
78
82
79
- def __ge__ (self , other : Expr | Any ) -> ExprT :
83
+ def __ge__ (self , other : Expr | Any ) -> Self :
80
84
return register_expression_call (self , "__ge__" , other )
81
85
82
- def __gt__ (self , other : Expr | Any ) -> ExprT :
86
+ def __gt__ (self , other : Expr | Any ) -> Self :
83
87
return register_expression_call (self , "__gt__" , other )
84
88
85
- def __le__ (self , other : Expr | Any ) -> ExprT :
89
+ def __le__ (self , other : Expr | Any ) -> Self :
86
90
return register_expression_call (self , "__le__" , other )
87
91
88
- def __lt__ (self , other : Expr | Any ) -> ExprT :
92
+ def __lt__ (self , other : Expr | Any ) -> Self :
89
93
return register_expression_call (self , "__lt__" , other )
90
94
91
- def __and__ (self , other : Expr | bool | Any ) -> ExprT :
95
+ def __and__ (self , other : Expr | bool | Any ) -> Self :
92
96
return register_expression_call (self , "__and__" , other )
93
97
94
- def __rand__ (self , other : Any ) -> ExprT :
98
+ def __rand__ (self , other : Any ) -> Self :
95
99
return register_expression_call (self , "__rand__" , other )
96
100
97
- def __or__ (self , other : Expr | bool | Any ) -> ExprT :
101
+ def __or__ (self , other : Expr | bool | Any ) -> Self :
98
102
return register_expression_call (self , "__or__" , other )
99
103
100
- def __ror__ (self , other : Any ) -> ExprT :
104
+ def __ror__ (self , other : Any ) -> Self :
101
105
return register_expression_call (self , "__ror__" , other )
102
106
103
- def __add__ (self , other : Expr | Any ) -> ExprT : # type: ignore[override]
107
+ def __add__ (self , other : Expr | Any ) -> Self : # type: ignore[override]
104
108
return register_expression_call (self , "__add__" , other )
105
109
106
- def __radd__ (self , other : Any ) -> ExprT :
110
+ def __radd__ (self , other : Any ) -> Self :
107
111
return register_expression_call (self , "__radd__" , other )
108
112
109
- def __sub__ (self , other : Expr | Any ) -> ExprT :
113
+ def __sub__ (self , other : Expr | Any ) -> Self :
110
114
return register_expression_call (self , "__sub__" , other )
111
115
112
- def __rsub__ (self , other : Any ) -> ExprT :
116
+ def __rsub__ (self , other : Any ) -> Self :
113
117
return register_expression_call (self , "__rsub__" , other )
114
118
115
- def __mul__ (self , other : Expr | Any ) -> ExprT :
119
+ def __mul__ (self , other : Expr | Any ) -> Self :
116
120
return register_expression_call (self , "__mul__" , other )
117
121
118
- def __rmul__ (self , other : Any ) -> ExprT :
122
+ def __rmul__ (self , other : Any ) -> Self :
119
123
return self .__mul__ (other )
120
124
121
- def __truediv__ (self , other : Expr | Any ) -> ExprT :
125
+ def __truediv__ (self , other : Expr | Any ) -> Self :
122
126
return register_expression_call (self , "__truediv__" , other )
123
127
124
- def __rtruediv__ (self , other : Any ) -> ExprT :
128
+ def __rtruediv__ (self , other : Any ) -> Self :
125
129
raise NotImplementedError
126
130
127
- def __floordiv__ (self , other : Expr | Any ) -> ExprT :
131
+ def __floordiv__ (self , other : Expr | Any ) -> Self :
128
132
return register_expression_call (self , "__floordiv__" , other )
129
133
130
- def __rfloordiv__ (self , other : Any ) -> ExprT :
134
+ def __rfloordiv__ (self , other : Any ) -> Self :
131
135
raise NotImplementedError
132
136
133
- def __pow__ (self , other : Expr | Any ) -> ExprT :
137
+ def __pow__ (self , other : Expr | Any ) -> Self :
134
138
return register_expression_call (self , "__pow__" , other )
135
139
136
- def __rpow__ (self , other : Any ) -> ExprT : # pragma: no cover
140
+ def __rpow__ (self , other : Any ) -> Self : # pragma: no cover
137
141
raise NotImplementedError
138
142
139
- def __mod__ (self , other : Expr | Any ) -> ExprT :
143
+ def __mod__ (self , other : Expr | Any ) -> Self :
140
144
return register_expression_call (self , "__mod__" , other )
141
145
142
- def __rmod__ (self , other : Any ) -> ExprT : # pragma: no cover
146
+ def __rmod__ (self , other : Any ) -> Self : # pragma: no cover
143
147
raise NotImplementedError
144
148
145
149
# Unary
146
150
147
- def __invert__ (self ) -> ExprT :
151
+ def __invert__ (self ) -> Self :
148
152
return register_expression_call (self , "__invert__" )
149
153
150
154
# Reductions
151
155
152
- def sum (self ) -> ExprT :
156
+ def sum (self ) -> Self :
153
157
return register_expression_call (self , "sum" )
154
158
155
- def mean (self ) -> ExprT :
159
+ def mean (self ) -> Self :
156
160
return register_expression_call (self , "mean" )
157
161
158
- def max (self ) -> ExprT :
162
+ def max (self ) -> Self :
159
163
return register_expression_call (self , "max" )
160
164
161
- def min (self ) -> ExprT :
165
+ def min (self ) -> Self :
162
166
return register_expression_call (self , "min" )
163
167
164
168
# Other
165
169
def is_between (
166
170
self , lower_bound : Any , upper_bound : Any , closed : str = "both"
167
- ) -> ExprT :
171
+ ) -> Self :
168
172
return register_expression_call (
169
173
self , "is_between" , lower_bound , upper_bound , closed
170
174
)
171
175
172
- def is_null (self ) -> ExprT :
176
+ def is_null (self ) -> Self :
173
177
return register_expression_call (self , "is_null" )
174
178
175
- def is_in (self , other : Any ) -> ExprT :
179
+ def is_in (self , other : Any ) -> Self :
176
180
return register_expression_call (self , "is_in" , other )
177
181
178
- def drop_nulls (self ) -> ExprT :
182
+ def drop_nulls (self ) -> Self :
179
183
return register_expression_call (self , "drop_nulls" )
180
184
181
- def n_unique (self ) -> ExprT :
185
+ def n_unique (self ) -> Self :
182
186
return register_expression_call (self , "n_unique" )
183
187
184
- def unique (self ) -> ExprT :
188
+ def unique (self ) -> Self :
185
189
return register_expression_call (self , "unique" )
186
190
187
- def sample (self , n : int , fraction : float , * , with_replacement : bool ) -> ExprT :
191
+ def sample (self , n : int , fraction : float , * , with_replacement : bool ) -> Self :
188
192
return register_expression_call (self , "sample" , n , fraction , with_replacement )
189
193
190
- def alias (self , name : str ) -> ExprT :
194
+ def alias (self , name : str ) -> Self :
191
195
# Define this one manually, so that we can
192
196
# override `output_names` and not increase depth
193
197
if self ._depth is None :
@@ -211,7 +215,7 @@ class ExprStringNamespace(ExprStringNamespaceT):
211
215
def __init__ (self , expr : ExprT ) -> None :
212
216
self ._expr = expr
213
217
214
- def ends_with (self , suffix : str ) -> ExprT :
218
+ def ends_with (self , suffix : str ) -> Expr :
215
219
# TODO make a register_expression_call for namespaces
216
220
return Expr (
217
221
lambda df : [
@@ -229,7 +233,7 @@ def ends_with(self, suffix: str) -> ExprT:
229
233
implementation = self ._expr ._implementation , # type: ignore[attr-defined]
230
234
)
231
235
232
- def strip_chars (self , characters : str = " " ) -> ExprT :
236
+ def strip_chars (self , characters : str = " " ) -> Expr :
233
237
return Expr (
234
238
lambda df : [
235
239
Series (
0 commit comments