Skip to content

Commit

Permalink
simplify definitions of rhs expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 4, 2025
1 parent 2dcfd26 commit 054a592
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 199 deletions.
36 changes: 0 additions & 36 deletions narwhals/_arrow/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,66 +163,30 @@ def __lt__(self: Self, other: ArrowExpr | Any) -> Self:
def __and__(self: Self, other: ArrowExpr | bool | Any) -> Self:
return reuse_series_implementation(self, "__and__", other=other)

def __rand__(self: Self, other: ArrowExpr | bool | Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__and__(self) # type: ignore[return-value]

def __or__(self: Self, other: ArrowExpr | bool | Any) -> Self:
return reuse_series_implementation(self, "__or__", other=other)

def __ror__(self: Self, other: ArrowExpr | bool | Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__or__(self) # type: ignore[return-value]

def __add__(self: Self, other: ArrowExpr | Any) -> Self:
return reuse_series_implementation(self, "__add__", other=other)

def __radd__(self: Self, other: ArrowExpr | Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__add__(self) # type: ignore[return-value]

def __sub__(self: Self, other: ArrowExpr | Any) -> Self:
return reuse_series_implementation(self, "__sub__", other=other)

def __rsub__(self: Self, other: ArrowExpr | Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__sub__(self) # type: ignore[return-value]

def __mul__(self: Self, other: ArrowExpr | Any) -> Self:
return reuse_series_implementation(self, "__mul__", other=other)

def __rmul__(self: Self, other: ArrowExpr | Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__mul__(self) # type: ignore[return-value]

def __pow__(self: Self, other: ArrowExpr | Any) -> Self:
return reuse_series_implementation(self, "__pow__", other=other)

def __rpow__(self: Self, other: ArrowExpr | Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__pow__(self) # type: ignore[return-value]

def __floordiv__(self: Self, other: ArrowExpr | Any) -> Self:
return reuse_series_implementation(self, "__floordiv__", other=other)

def __rfloordiv__(self: Self, other: ArrowExpr | Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__floordiv__(self) # type: ignore[return-value]

def __truediv__(self: Self, other: ArrowExpr | Any) -> Self:
return reuse_series_implementation(self, "__truediv__", other=other)

def __rtruediv__(self: Self, other: ArrowExpr | Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__truediv__(self) # type: ignore[return-value]

def __mod__(self: Self, other: ArrowExpr | Any) -> Self:
return reuse_series_implementation(self, "__mod__", other=other)

def __rmod__(self: Self, other: ArrowExpr | Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__mod__(self) # type: ignore[return-value]

def __invert__(self: Self) -> Self:
return reuse_series_implementation(self, "__invert__")

Expand Down
72 changes: 0 additions & 72 deletions narwhals/_dask/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,6 @@ def __add__(self, other: Any) -> Self:
returns_scalar=binary_operation_returns_scalar(self, other),
)

def __radd__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__radd__(other),
"__radd__",
other=other,
returns_scalar=binary_operation_returns_scalar(self, other),
).alias("literal")

def __sub__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__sub__(other),
Expand All @@ -226,14 +218,6 @@ def __sub__(self, other: Any) -> Self:
returns_scalar=binary_operation_returns_scalar(self, other),
)

def __rsub__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__rsub__(other),
"__rsub__",
other=other,
returns_scalar=binary_operation_returns_scalar(self, other),
).alias("literal")

def __mul__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__mul__(other),
Expand All @@ -242,14 +226,6 @@ def __mul__(self, other: Any) -> Self:
returns_scalar=binary_operation_returns_scalar(self, other),
)

def __rmul__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__rmul__(other),
"__rmul__",
other=other,
returns_scalar=binary_operation_returns_scalar(self, other),
).alias("literal")

def __truediv__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__truediv__(other),
Expand All @@ -258,14 +234,6 @@ def __truediv__(self, other: Any) -> Self:
returns_scalar=binary_operation_returns_scalar(self, other),
)

def __rtruediv__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__rtruediv__(other),
"__rtruediv__",
other=other,
returns_scalar=binary_operation_returns_scalar(self, other),
).alias("literal")

def __floordiv__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__floordiv__(other),
Expand All @@ -274,14 +242,6 @@ def __floordiv__(self, other: Any) -> Self:
returns_scalar=binary_operation_returns_scalar(self, other),
)

def __rfloordiv__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__rfloordiv__(other),
"__rfloordiv__",
other=other,
returns_scalar=binary_operation_returns_scalar(self, other),
).alias("literal")

def __pow__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__pow__(other),
Expand All @@ -290,14 +250,6 @@ def __pow__(self, other: Any) -> Self:
returns_scalar=binary_operation_returns_scalar(self, other),
)

def __rpow__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__rpow__(other),
"__rpow__",
other=other,
returns_scalar=binary_operation_returns_scalar(self, other),
).alias("literal")

def __mod__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__mod__(other),
Expand All @@ -306,14 +258,6 @@ def __mod__(self, other: Any) -> Self:
returns_scalar=binary_operation_returns_scalar(self, other),
)

def __rmod__(self, other: Any) -> Self:
return self._from_call(
lambda _input, other: _input.__rmod__(other),
"__rmod__",
other=other,
returns_scalar=binary_operation_returns_scalar(self, other),
).alias("literal")

def __eq__(self, other: DaskExpr) -> Self: # type: ignore[override]
return self._from_call(
lambda _input, other: _input.__eq__(other),
Expand Down Expand Up @@ -370,14 +314,6 @@ def __and__(self, other: DaskExpr) -> Self:
returns_scalar=binary_operation_returns_scalar(self, other),
)

def __rand__(self, other: DaskExpr) -> Self:
return self._from_call(
lambda _input, other: _input.__rand__(other),
"__rand__",
other=other,
returns_scalar=binary_operation_returns_scalar(self, other),
).alias("literal")

def __or__(self, other: DaskExpr) -> Self:
return self._from_call(
lambda _input, other: _input.__or__(other),
Expand All @@ -386,14 +322,6 @@ def __or__(self, other: DaskExpr) -> Self:
returns_scalar=binary_operation_returns_scalar(self, other),
)

def __ror__(self, other: DaskExpr) -> Self:
return self._from_call(
lambda _input, other: _input.__ror__(other),
"__ror__",
other=other,
returns_scalar=binary_operation_returns_scalar(self, other),
).alias("literal")

def __invert__(self: Self) -> Self:
return self._from_call(
lambda _input: _input.__invert__(),
Expand Down
37 changes: 0 additions & 37 deletions narwhals/_pandas_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,68 +179,31 @@ def __lt__(self, other: PandasLikeExpr | Any) -> Self:
def __and__(self, other: PandasLikeExpr | bool | Any) -> Self:
return reuse_series_implementation(self, "__and__", other=other)

def __rand__(self, other: Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__and__(self) # type: ignore[no-any-return]

def __or__(self, other: PandasLikeExpr | bool | Any) -> Self:
return reuse_series_implementation(self, "__or__", other=other)

def __ror__(self, other: Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__or__(self) # type: ignore[no-any-return]

def __add__(self, other: PandasLikeExpr | Any) -> Self:
return reuse_series_implementation(self, "__add__", other=other)

def __radd__(self, other: Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__add__(self) # type: ignore[no-any-return]

def __sub__(self, other: PandasLikeExpr | Any) -> Self:
return reuse_series_implementation(self, "__sub__", other=other)

def __rsub__(self, other: Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__sub__(self) # type: ignore[no-any-return]

def __mul__(self, other: PandasLikeExpr | Any) -> Self:
return reuse_series_implementation(self, "__mul__", other=other)

def __rmul__(self, other: Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__mul__(self) # type: ignore[no-any-return]

def __truediv__(self, other: PandasLikeExpr | Any) -> Self:
return reuse_series_implementation(self, "__truediv__", other=other)

def __rtruediv__(self, other: Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__truediv__(self) # type: ignore[no-any-return]

def __floordiv__(self, other: PandasLikeExpr | Any) -> Self:
return reuse_series_implementation(self, "__floordiv__", other=other)

def __rfloordiv__(self, other: Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__floordiv__(self) # type: ignore[no-any-return]

def __pow__(self, other: PandasLikeExpr | Any) -> Self:
return reuse_series_implementation(self, "__pow__", other=other)

def __rpow__(self, other: Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__pow__(self) # type: ignore[no-any-return]

def __mod__(self, other: PandasLikeExpr | Any) -> Self:
return reuse_series_implementation(self, "__mod__", other=other)

def __rmod__(self, other: Any) -> Self:
other = self.__narwhals_namespace__().lit(other, dtype=None)
return other.__mod__(self) # type: ignore[no-any-return]

# Unary

def __invert__(self) -> Self:
return reuse_series_implementation(self, "__invert__")

Expand Down
12 changes: 0 additions & 12 deletions narwhals/_polars/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,15 @@ def __or__(self: Self, other: PolarsExpr | bool | Any) -> Self:
def __add__(self: Self, other: Any) -> Self:
return self._from_native_expr(self._native_expr.__add__(extract_native(other)))

def __radd__(self: Self, other: Any) -> Self:
return self._from_native_expr(self._native_expr.__radd__(extract_native(other)))

def __sub__(self: Self, other: Any) -> Self:
return self._from_native_expr(self._native_expr.__sub__(extract_native(other)))

def __rsub__(self: Self, other: Any) -> Self:
return self._from_native_expr(self._native_expr.__rsub__(extract_native(other)))

def __mul__(self: Self, other: Any) -> Self:
return self._from_native_expr(self._native_expr.__mul__(extract_native(other)))

def __rmul__(self: Self, other: Any) -> Self:
return self._from_native_expr(self._native_expr.__rmul__(extract_native(other)))

def __pow__(self: Self, other: Any) -> Self:
return self._from_native_expr(self._native_expr.__pow__(extract_native(other)))

def __rpow__(self: Self, other: Any) -> Self:
return self._from_native_expr(self._native_expr.__rpow__(extract_native(other)))

def __invert__(self: Self) -> Self:
return self._from_native_expr(self._native_expr.__invert__())

Expand Down
2 changes: 1 addition & 1 deletion narwhals/_spark_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __native_namespace__(self) -> Any: # pragma: no cover
def __narwhals_namespace__(self) -> SparkLikeNamespace:
from narwhals._spark_like.namespace import SparkLikeNamespace

return SparkLikeNamespace(
return SparkLikeNamespace( # type: ignore[abstract]
backend_version=self._backend_version, version=self._version
)

Expand Down
2 changes: 1 addition & 1 deletion narwhals/_spark_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __narwhals_namespace__(self) -> SparkLikeNamespace: # pragma: no cover
# Unused, just for compatibility with PandasLikeExpr
from narwhals._spark_like.namespace import SparkLikeNamespace

return SparkLikeNamespace(
return SparkLikeNamespace( # type: ignore[abstract]
backend_version=self._backend_version, version=self._version
)

Expand Down
Loading

0 comments on commit 054a592

Please sign in to comment.