Skip to content

Commit 247ad11

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 4aae069 commit 247ad11

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

narwhals/_sql/expr.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,38 @@
33
from typing import TYPE_CHECKING, Any, Callable, Literal, Protocol, cast
44

55
from narwhals._compliant.expr import LazyExpr
6-
from narwhals._compliant.typing import (
7-
AliasNames,
8-
EvalNames,
9-
EvalSeries,
10-
NativeExprT,
11-
WindowFunction,
12-
)
6+
from narwhals._compliant.typing import AliasNames, WindowFunction
137
from narwhals._compliant.window import WindowInputs
148
from narwhals._expression_parsing import (
159
combine_alias_output_names,
1610
combine_evaluate_output_names,
1711
)
18-
from narwhals._sql.typing import SQLLazyFrameT, NativeSQLExprT
12+
from narwhals._sql.typing import NativeSQLExprT, SQLLazyFrameT
1913
from narwhals._utils import Implementation, Version, not_implemented
2014

2115
if TYPE_CHECKING:
2216
from collections.abc import Iterable, Sequence
2317

2418
from typing_extensions import Self, TypeIs
2519

26-
from narwhals._compliant.typing import AliasNames, WindowFunction
20+
from narwhals._compliant.typing import (
21+
AliasNames,
22+
EvalNames,
23+
EvalSeries,
24+
NativeExprT,
25+
WindowFunction,
26+
)
2727
from narwhals._expression_parsing import ExprMetadata
2828
from narwhals._sql.namespace import SQLNamespace
2929
from narwhals.typing import NumericLiteral, PythonLiteral, RankMethod, TemporalLiteral
3030

31-
# am I right in thinking we need to pass NativeSQLExprT here, not NativeExprT? since NativeSQLExprT
32-
# inherits from it, I can use that througout in the code and it will also have its parents functionality.
31+
32+
# am I right in thinking we need to pass NativeSQLExprT here, not NativeExprT? since NativeSQLExprT
33+
# inherits from it, I can use that througout in the code and it will also have its parents functionality.
3334
# though at the moment there are still problems with the class..
34-
class SQLExpr(LazyExpr[SQLLazyFrameT, NativeSQLExprT], Protocol[SQLLazyFrameT, NativeSQLExprT]):
35+
class SQLExpr(
36+
LazyExpr[SQLLazyFrameT, NativeSQLExprT], Protocol[SQLLazyFrameT, NativeSQLExprT]
37+
):
3538
_call: EvalSeries[SQLLazyFrameT, NativeSQLExprT]
3639
_evaluate_output_names: EvalNames[SQLLazyFrameT]
3740
_alias_output_names: AliasNames | None
@@ -277,7 +280,7 @@ def func(
277280
}
278281
return [
279282
self._when(
280-
self._window_expression(
283+
self._window_expression(
281284
self._function("count", expr), **window_kwargs
282285
)
283286
>= self._lit(min_samples),
@@ -498,13 +501,12 @@ def round(self, decimals: int) -> Self:
498501
return self._with_elementwise(
499502
lambda expr: self._function("round", expr, self._lit(decimals))
500503
)
501-
# WIP: trying new NativeSQLExprT
504+
505+
# WIP: trying new NativeSQLExprT
502506
def sqrt(self) -> Self:
503507
def _sqrt(expr: NativeSQLExprT) -> NativeSQLExprT:
504508
return self._when(
505-
expr < self._lit(0),
506-
self._lit(float("nan")),
507-
self._function("sqrt", expr),
509+
expr < self._lit(0), self._lit(float("nan")), self._function("sqrt", expr)
508510
)
509511

510512
return self._with_elementwise(_sqrt)
@@ -515,12 +517,12 @@ def exp(self) -> Self:
515517
def log(self, base: float) -> Self:
516518
def _log(expr: NativeSQLExprT) -> NativeSQLExprT:
517519
return self._when(
518-
expr < self._lit(0),
520+
expr < self._lit(0),
519521
self._lit(float("nan")),
520522
self._when(
521523
cast("NativeSQLExprT", expr == self._lit(0)),
522524
self._lit(float("-inf")),
523-
self._function("log", expr) / self._function("log", self._lit(base)),
525+
self._function("log", expr) / self._function("log", self._lit(base)),
524526
),
525527
)
526528

@@ -668,19 +670,19 @@ def _rank(
668670
count_window_kwargs: dict[str, Any] = {"partition_by": (*partition_by, expr)}
669671
if method == "max":
670672
rank_expr = (
671-
self._window_expression(func, **window_kwargs)
673+
self._window_expression(func, **window_kwargs)
672674
+ self._window_expression(count_expr, **count_window_kwargs)
673675
- self._lit(1)
674676
)
675677
elif method == "average":
676678
rank_expr = self._window_expression(func, **window_kwargs) + (
677-
self._window_expression(count_expr, **count_window_kwargs)
679+
self._window_expression(count_expr, **count_window_kwargs)
678680
- self._lit(1)
679681
) / self._lit(2.0)
680682
else:
681683
rank_expr = self._window_expression(func, **window_kwargs)
682-
# TODO: @mp, thought I added this to NativeSQLExprT but not working?
683-
return self._when(~self._function("isnull", expr), rank_expr) # type: ignore[operator]
684+
# TODO: @mp, thought I added this to NativeSQLExprT but not working?
685+
return self._when(~self._function("isnull", expr), rank_expr) # type: ignore[operator]
684686

685687
def _unpartitioned_rank(expr: NativeSQLExprT) -> NativeSQLExprT:
686688
return _rank(expr, descending=[descending], nulls_last=[True])

narwhals/_sql/typing.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from narwhals._compliant.expr import NativeExpr
66

7+
78
class NativeSQLExpr(NativeExpr):
8-
# both Self because we're comparing an expression with an expression?
9+
# both Self because we're comparing an expression with an expression?
910
def __gt__(self, value: Self) -> Self: ...
1011

1112
def __lt__(self, value: Self) -> Self: ...
@@ -28,16 +29,17 @@ def __mul__(self, value: Self) -> Self: ...
2829

2930
def __invert__(self, value: Self) -> Self: ...
3031

32+
3133
if TYPE_CHECKING:
34+
from typing_extensions import Self
35+
3236
from narwhals._sql.dataframe import SQLLazyFrame
3337
from narwhals._sql.expr import SQLExpr
34-
from narwhals.dtypes import Boolean
35-
from typing_extensions import Self
3638

3739
# TODO: @mp, understand why these are here & if we need one for NativeSQLExprT;
3840
# seem to reflect number of different 'catgories' each of the parent class has
3941
# tbc! since NativeExpr only has Protocol, I don't think we need this for NativeSQLExpr
40-
# NativeSQLExpr isn't accepting Any arguments :) I need to go back to the reading on
42+
# NativeSQLExpr isn't accepting Any arguments :) I need to go back to the reading on
4143
# cov-, contra- & invariance
4244
SQLExprAny = SQLExpr[Any, Any]
4345
SQLLazyFrameAny = SQLLazyFrame[Any, Any, Any]
@@ -47,5 +49,4 @@ def __invert__(self, value: Self) -> Self: ...
4749
SQLExprT_contra = TypeVar("SQLExprT_contra", bound="SQLExprAny", contravariant=True)
4850
SQLLazyFrameT = TypeVar("SQLLazyFrameT", bound="SQLLazyFrameAny")
4951
# TODO: @mp, should this be contravariant as to do with function arguments? think through!
50-
NativeSQLExprT = TypeVar("NativeSQLExprT", bound="NativeSQLExprAny")
51-
52+
NativeSQLExprT = TypeVar("NativeSQLExprT", bound="NativeSQLExprAny")

0 commit comments

Comments
 (0)