Skip to content

Commit daaeaf9

Browse files
authored
chore: always set CompliantExpr._metadata (narwhals-dev#2282)
1 parent e67ae25 commit daaeaf9

File tree

17 files changed

+27
-91
lines changed

17 files changed

+27
-91
lines changed

.github/workflows/extremes.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
cache-suffix: ${{ matrix.python-version }}
6363
cache-dependency-glob: "pyproject.toml"
6464
- name: install-pretty-old-versions
65-
run: uv pip install pipdeptree tox virtualenv setuptools pandas==1.1.5 polars==0.20.3 numpy==1.17.5 pyarrow==11.0.0 "pyarrow-stubs<17" pyspark==3.5.0 scipy==1.5.0 scikit-learn==1.1.0 duckdb==1.0 tzdata backports.zoneinfo --system
65+
run: uv pip install pipdeptree tox virtualenv setuptools pandas==1.1.5 polars==0.20.3 numpy==1.17.5 pyarrow==11.0.0 "pyarrow-stubs<17" scipy==1.5.0 scikit-learn==1.1.0 duckdb==1.0 tzdata backports.zoneinfo --system
6666
- name: install-reqs
6767
run: uv pip install -e . --group tests --system
6868
- name: show-deps
@@ -76,7 +76,6 @@ jobs:
7676
echo "$DEPS" | grep 'polars==0.20.3'
7777
echo "$DEPS" | grep 'numpy==1.17.5'
7878
echo "$DEPS" | grep 'pyarrow==11.0.0'
79-
echo "$DEPS" | grep 'pyspark==3.5.0'
8079
echo "$DEPS" | grep 'scipy==1.5.0'
8180
echo "$DEPS" | grep 'scikit-learn==1.1.0'
8281
echo "$DEPS" | grep 'duckdb==1.0'
@@ -101,7 +100,7 @@ jobs:
101100
cache-suffix: ${{ matrix.python-version }}
102101
cache-dependency-glob: "pyproject.toml"
103102
- name: install-not-so-old-versions
104-
run: uv pip install tox virtualenv setuptools pandas==2.0.3 polars==0.20.8 numpy==1.24.4 pyarrow==15.0.0 "pyarrow-stubs<17" pyspark==3.5.0 scipy==1.8.0 scikit-learn==1.3.0 duckdb==1.0 dask[dataframe]==2024.10 tzdata --system
103+
run: uv pip install tox virtualenv setuptools pandas==2.0.3 polars==0.20.8 numpy==1.24.4 pyarrow==15.0.0 "pyarrow-stubs<17" scipy==1.8.0 scikit-learn==1.3.0 duckdb==1.0 dask[dataframe]==2024.10 tzdata --system
105104
- name: install-reqs
106105
run: uv pip install -e . --group tests --system
107106
- name: show-deps
@@ -113,7 +112,6 @@ jobs:
113112
echo "$DEPS" | grep 'polars==0.20.8'
114113
echo "$DEPS" | grep 'numpy==1.24.4'
115114
echo "$DEPS" | grep 'pyarrow==15.0.0'
116-
echo "$DEPS" | grep 'pyspark==3.5.0'
117115
echo "$DEPS" | grep 'scipy==1.8.0'
118116
echo "$DEPS" | grep 'scikit-learn==1.3.0'
119117
echo "$DEPS" | grep 'dask==2024.10'

.github/workflows/pytest.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
- name: install duckdb nightly
8888
run: uv pip install -U --pre duckdb --system
8989
- name: install pyspark
90-
run: uv pip install -e ".[pyspark]" --system
90+
run: echo "setuptools<78" | uv pip install -b - -e ".[pyspark]" --system
9191
# PySpark is not yet available on Python3.12+
9292
if: matrix.python-version != '3.13'
9393
- name: install ibis

.github/workflows/typing.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
# TODO: add more dependencies/backends incrementally
3333
run: |
3434
source .venv/bin/activate
35-
uv pip install -e ".[pyspark]" --group core --group typing
35+
echo "setuptools<78" | uv pip install -b - -e ".[pyspark]" --group core --group typing
3636
- name: show-deps
3737
run: |
3838
source .venv/bin/activate

narwhals/_arrow/namespace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def len(self: Self) -> ArrowExpr:
6868
version=self._version,
6969
)
7070

71-
def lit(self: Self, value: Any, dtype: DType | None) -> ArrowExpr:
71+
def lit(self: Self, value: Any, dtype: DType | type[DType] | None) -> ArrowExpr:
7272
def _lit_arrow_series(_: ArrowDataFrame) -> ArrowSeries:
7373
arrow_series = ArrowSeries.from_iterable(
7474
data=[value], name="literal", context=self

narwhals/_compliant/expr.py

-17
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ def from_column_indices(
106106
cls: type[Self], *column_indices: int, context: _FullContext
107107
) -> Self: ...
108108

109-
def _with_metadata(self, metadata: ExprMetadata) -> Self: ...
110-
111109
def is_null(self) -> Self: ...
112110
def abs(self) -> Self: ...
113111
def all(self) -> Self: ...
@@ -353,21 +351,6 @@ def __narwhals_namespace__(
353351
) -> EagerNamespace[EagerDataFrameT, EagerSeriesT, Self]: ...
354352
def __narwhals_expr__(self) -> None: ...
355353

356-
def _with_metadata(self, metadata: ExprMetadata) -> Self:
357-
expr = self.__class__(
358-
self._call,
359-
function_name=self._function_name,
360-
evaluate_output_names=self._evaluate_output_names,
361-
alias_output_names=self._alias_output_names,
362-
backend_version=self._backend_version,
363-
version=self._version,
364-
depth=self._depth,
365-
implementation=self._implementation,
366-
call_kwargs=self._call_kwargs,
367-
)
368-
expr._metadata = metadata
369-
return expr
370-
371354
@classmethod
372355
def _from_callable(
373356
cls,

narwhals/_compliant/namespace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def nth(self, *column_indices: int) -> CompliantExprT:
5555
return self._expr.from_column_indices(*column_indices, context=self)
5656

5757
def len(self) -> CompliantExprT: ...
58-
def lit(self, value: Any, dtype: DType | None) -> CompliantExprT: ...
58+
def lit(self, value: Any, dtype: DType | type[DType] | None) -> CompliantExprT: ...
5959
def all_horizontal(self, *exprs: CompliantExprT) -> CompliantExprT: ...
6060
def any_horizontal(self, *exprs: CompliantExprT) -> CompliantExprT: ...
6161
def sum_horizontal(self, *exprs: CompliantExprT) -> CompliantExprT: ...

narwhals/_dask/expr.py

-14
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ def __narwhals_namespace__(self) -> DaskNamespace: # pragma: no cover
8383

8484
return DaskNamespace(backend_version=self._backend_version, version=self._version)
8585

86-
def _with_metadata(self, metadata: ExprMetadata) -> Self:
87-
expr = self.__class__(
88-
self._call,
89-
function_name=self._function_name,
90-
evaluate_output_names=self._evaluate_output_names,
91-
alias_output_names=self._alias_output_names,
92-
backend_version=self._backend_version,
93-
version=self._version,
94-
depth=self._depth,
95-
call_kwargs=self._call_kwargs,
96-
)
97-
expr._metadata = metadata
98-
return expr
99-
10086
def broadcast(self, kind: Literal[ExprKind.AGGREGATION, ExprKind.LITERAL]) -> Self:
10187
def func(df: DaskLazyFrame) -> list[dx.Series]:
10288
return [result[0] for result in self(df)]

narwhals/_dask/namespace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(
5555
self._backend_version = backend_version
5656
self._version = version
5757

58-
def lit(self: Self, value: Any, dtype: DType | None) -> DaskExpr:
58+
def lit(self: Self, value: Any, dtype: DType | type[DType] | None) -> DaskExpr:
5959
def func(df: DaskLazyFrame) -> list[dx.Series]:
6060
if dtype is not None:
6161
native_dtype = narwhals_to_native_dtype(dtype, self._version)

narwhals/_duckdb/expr.py

-13
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,6 @@ def __narwhals_namespace__(self) -> DuckDBNamespace: # pragma: no cover
7979
backend_version=self._backend_version, version=self._version
8080
)
8181

82-
def _with_metadata(self, metadata: ExprMetadata) -> Self:
83-
expr = self.__class__(
84-
self._call,
85-
evaluate_output_names=self._evaluate_output_names,
86-
alias_output_names=self._alias_output_names,
87-
backend_version=self._backend_version,
88-
version=self._version,
89-
)
90-
if func := self._window_function:
91-
expr = expr._with_window_function(func)
92-
expr._metadata = metadata
93-
return expr
94-
9582
def broadcast(self, kind: Literal[ExprKind.AGGREGATION, ExprKind.LITERAL]) -> Self:
9683
if kind is ExprKind.LITERAL:
9784
return self

narwhals/_duckdb/namespace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def func(df: DuckDBLazyFrame) -> list[duckdb.Expression]:
213213
def when(self: Self, predicate: DuckDBExpr) -> DuckDBWhen:
214214
return DuckDBWhen.from_expr(predicate, context=self)
215215

216-
def lit(self: Self, value: Any, dtype: DType | None) -> DuckDBExpr:
216+
def lit(self: Self, value: Any, dtype: DType | type[DType] | None) -> DuckDBExpr:
217217
def func(_df: DuckDBLazyFrame) -> list[duckdb.Expression]:
218218
if dtype is not None:
219219
return [

narwhals/_pandas_like/namespace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(
5757
self._backend_version = backend_version
5858
self._version = version
5959

60-
def lit(self: Self, value: Any, dtype: DType | None) -> PandasLikeExpr:
60+
def lit(self: Self, value: Any, dtype: DType | type[DType] | None) -> PandasLikeExpr:
6161
def _lit_pandas_series(df: PandasLikeDataFrame) -> PandasLikeSeries:
6262
pandas_series = self._series.from_iterable(
6363
data=[value],

narwhals/_polars/expr.py

-9
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ def _from_native_expr(self: Self, expr: pl.Expr) -> Self:
4040
expr, version=self._version, backend_version=self._backend_version
4141
)
4242

43-
def _with_metadata(self, metadata: ExprMetadata) -> Self:
44-
expr = self.__class__(
45-
self._native_expr,
46-
backend_version=self._backend_version,
47-
version=self._version,
48-
)
49-
expr._metadata = metadata
50-
return expr
51-
5243
@classmethod
5344
def _from_series(cls, series: Any) -> Self:
5445
return cls(

narwhals/_polars/namespace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def concat(
111111
result, backend_version=items[0]._backend_version, version=items[0]._version
112112
)
113113

114-
def lit(self: Self, value: Any, dtype: DType | None) -> PolarsExpr:
114+
def lit(self: Self, value: Any, dtype: DType | type[DType] | None) -> PolarsExpr:
115115
if dtype is not None:
116116
return self._expr(
117117
pl.lit(

narwhals/_spark_like/expr.py

-14
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,6 @@ def __narwhals_namespace__(self: Self) -> SparkLikeNamespace: # pragma: no cove
119119
implementation=self._implementation,
120120
)
121121

122-
def _with_metadata(self, metadata: ExprMetadata) -> Self:
123-
expr = self.__class__(
124-
self._call,
125-
evaluate_output_names=self._evaluate_output_names,
126-
alias_output_names=self._alias_output_names,
127-
backend_version=self._backend_version,
128-
version=self._version,
129-
implementation=self._implementation,
130-
)
131-
if func := self._window_function:
132-
expr = expr._with_window_function(func)
133-
expr._metadata = metadata
134-
return expr
135-
136122
def _with_window_function(
137123
self: Self,
138124
window_function: WindowFunction,

narwhals/_spark_like/namespace.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def selectors(self: Self) -> SparkLikeSelectorNamespace:
4646
def _expr(self) -> type[SparkLikeExpr]:
4747
return SparkLikeExpr
4848

49-
def lit(self: Self, value: object, dtype: DType | None) -> SparkLikeExpr:
49+
def lit(
50+
self: Self, value: object, dtype: DType | type[DType] | None
51+
) -> SparkLikeExpr:
5052
def _lit(df: SparkLikeLazyFrame) -> list[Column]:
5153
column = df._F.lit(value)
5254
if dtype:

narwhals/expr.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@
4949

5050
class Expr:
5151
def __init__(
52-
self: Self,
53-
to_compliant_expr: Callable[[Any], Any],
54-
metadata: ExprMetadata,
52+
self: Self, to_compliant_expr: _ToCompliant, metadata: ExprMetadata
5553
) -> None:
5654
# callable from CompliantNamespace to CompliantExpr
57-
self._to_compliant_expr: _ToCompliant = to_compliant_expr
55+
def func(plx: CompliantNamespace[Any, Any]) -> CompliantExpr[Any, Any]:
56+
result = to_compliant_expr(plx)
57+
result._metadata = self._metadata
58+
return result
59+
60+
self._to_compliant_expr: _ToCompliant = func
5861
self._metadata = metadata
5962

6063
def _from_callable(self, to_compliant_expr: Callable[[Any], Any]) -> Self:
@@ -1598,9 +1601,9 @@ def over(
15981601
)
15991602

16001603
return self.__class__(
1601-
lambda plx: self._to_compliant_expr(plx)
1602-
._with_metadata(current_meta)
1603-
.over(flat_partition_by, flat_order_by),
1604+
lambda plx: self._to_compliant_expr(plx).over(
1605+
flat_partition_by, flat_order_by
1606+
),
16041607
next_meta,
16051608
)
16061609

narwhals/group_by.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def agg(self: Self, *aggs: Expr | Iterable[Expr], **named_aggs: Expr) -> DataFra
8484
raise InvalidOperationError(msg)
8585
plx = self._df.__narwhals_namespace__()
8686
compliant_aggs = (
87-
*(x._to_compliant_expr(plx)._with_metadata(x._metadata) for x in flat_aggs),
87+
*(x._to_compliant_expr(plx) for x in flat_aggs),
8888
*(
89-
value._to_compliant_expr(plx).alias(key)._with_metadata(value._metadata)
89+
value.alias(key)._to_compliant_expr(plx)
9090
for key, value in named_aggs.items()
9191
),
9292
)
@@ -174,9 +174,9 @@ def agg(self: Self, *aggs: Expr | Iterable[Expr], **named_aggs: Expr) -> LazyFra
174174
raise InvalidOperationError(msg)
175175
plx = self._df.__narwhals_namespace__()
176176
compliant_aggs = (
177-
*(x._to_compliant_expr(plx)._with_metadata(x._metadata) for x in flat_aggs),
177+
*(x._to_compliant_expr(plx) for x in flat_aggs),
178178
*(
179-
value._to_compliant_expr(plx).alias(key)._with_metadata(value._metadata)
179+
value.alias(key)._to_compliant_expr(plx)
180180
for key, value in named_aggs.items()
181181
),
182182
)

0 commit comments

Comments
 (0)