Skip to content

Commit 364d2ee

Browse files
committed
fix(typing): Use Self
I did things generic to match `_reuse_series*` - but I couldv'e used `Self` there 🤦
1 parent e16e942 commit 364d2ee

File tree

1 file changed

+69
-86
lines changed

1 file changed

+69
-86
lines changed

narwhals/_compliant/expr.py

Lines changed: 69 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,13 @@ def from_column_indices(
325325

326326
# https://github.com/narwhals-dev/narwhals/blob/35cef0b1e2c892fb24aa730902b08b6994008c18/narwhals/_protocols.py#L135
327327
def _reuse_series_implementation(
328-
self: EagerExpr[EagerDataFrameT, EagerSeriesT],
328+
self: Self,
329329
attr: str,
330330
*,
331331
returns_scalar: bool = False,
332332
call_kwargs: dict[str, Any] | None = None,
333333
**expressifiable_args: Any,
334-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
334+
) -> Self:
335335
func = partial(
336336
self._reuse_series_inner,
337337
method_name=attr,
@@ -392,11 +392,8 @@ def _reuse_series_inner(
392392
return out
393393

394394
def _reuse_series_namespace_implementation(
395-
self: EagerExpr[EagerDataFrameT, EagerSeriesT],
396-
series_namespace: str,
397-
attr: str,
398-
**kwargs: Any,
399-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
395+
self: Self, series_namespace: str, attr: str, **kwargs: Any
396+
) -> Self:
400397
return self._from_callable(
401398
lambda df: [
402399
getattr(getattr(series, series_namespace), attr)(**kwargs)
@@ -433,171 +430,161 @@ def func(df: EagerDataFrameT) -> list[EagerSeriesT]:
433430
call_kwargs=self._call_kwargs,
434431
)
435432

436-
def cast(
437-
self, dtype: DType | type[DType]
438-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
433+
def cast(self, dtype: DType | type[DType]) -> Self:
439434
return self._reuse_series_implementation("cast", dtype=dtype)
440435

441-
def __eq__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]: # type: ignore[override]
436+
def __eq__(self, other: Self | Any) -> Self: # type: ignore[override]
442437
return self._reuse_series_implementation("__eq__", other=other)
443438

444-
def __ne__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]: # type: ignore[override]
439+
def __ne__(self, other: Self | Any) -> Self: # type: ignore[override]
445440
return self._reuse_series_implementation("__ne__", other=other)
446441

447-
def __ge__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
442+
def __ge__(self, other: Self | Any) -> Self:
448443
return self._reuse_series_implementation("__ge__", other=other)
449444

450-
def __gt__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
445+
def __gt__(self, other: Self | Any) -> Self:
451446
return self._reuse_series_implementation("__gt__", other=other)
452447

453-
def __le__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
448+
def __le__(self, other: Self | Any) -> Self:
454449
return self._reuse_series_implementation("__le__", other=other)
455450

456-
def __lt__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
451+
def __lt__(self, other: Self | Any) -> Self:
457452
return self._reuse_series_implementation("__lt__", other=other)
458453

459-
def __and__(
460-
self, other: Self | bool | Any
461-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
454+
def __and__(self, other: Self | bool | Any) -> Self:
462455
return self._reuse_series_implementation("__and__", other=other)
463456

464-
def __or__(
465-
self, other: Self | bool | Any
466-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
457+
def __or__(self, other: Self | bool | Any) -> Self:
467458
return self._reuse_series_implementation("__or__", other=other)
468459

469-
def __add__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
460+
def __add__(self, other: Self | Any) -> Self:
470461
return self._reuse_series_implementation("__add__", other=other)
471462

472-
def __sub__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
463+
def __sub__(self, other: Self | Any) -> Self:
473464
return self._reuse_series_implementation("__sub__", other=other)
474465

475-
def __rsub__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
466+
def __rsub__(self, other: Self | Any) -> Self:
476467
return self.alias("literal")._reuse_series_implementation("__rsub__", other=other)
477468

478-
def __mul__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
469+
def __mul__(self, other: Self | Any) -> Self:
479470
return self._reuse_series_implementation("__mul__", other=other)
480471

481-
def __truediv__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
472+
def __truediv__(self, other: Self | Any) -> Self:
482473
return self._reuse_series_implementation("__truediv__", other=other)
483474

484-
def __rtruediv__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
475+
def __rtruediv__(self, other: Self | Any) -> Self:
485476
return self.alias("literal")._reuse_series_implementation(
486477
"__rtruediv__", other=other
487478
)
488479

489-
def __floordiv__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
480+
def __floordiv__(self, other: Self | Any) -> Self:
490481
return self._reuse_series_implementation("__floordiv__", other=other)
491482

492-
def __rfloordiv__(
493-
self, other: Self | Any
494-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
483+
def __rfloordiv__(self, other: Self | Any) -> Self:
495484
return self.alias("literal")._reuse_series_implementation(
496485
"__rfloordiv__", other=other
497486
)
498487

499-
def __pow__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
488+
def __pow__(self, other: Self | Any) -> Self:
500489
return self._reuse_series_implementation("__pow__", other=other)
501490

502-
def __rpow__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
491+
def __rpow__(self, other: Self | Any) -> Self:
503492
return self.alias("literal")._reuse_series_implementation("__rpow__", other=other)
504493

505-
def __mod__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
494+
def __mod__(self, other: Self | Any) -> Self:
506495
return self._reuse_series_implementation("__mod__", other=other)
507496

508-
def __rmod__(self, other: Self | Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
497+
def __rmod__(self, other: Self | Any) -> Self:
509498
return self.alias("literal")._reuse_series_implementation("__rmod__", other=other)
510499

511500
# Unary
512-
def __invert__(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
501+
def __invert__(self) -> Self:
513502
return self._reuse_series_implementation("__invert__")
514503

515504
# Reductions
516-
def null_count(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
505+
def null_count(self) -> Self:
517506
return self._reuse_series_implementation("null_count", returns_scalar=True)
518507

519-
def n_unique(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
508+
def n_unique(self) -> Self:
520509
return self._reuse_series_implementation("n_unique", returns_scalar=True)
521510

522-
def sum(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
511+
def sum(self) -> Self:
523512
return self._reuse_series_implementation("sum", returns_scalar=True)
524513

525-
def mean(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
514+
def mean(self) -> Self:
526515
return self._reuse_series_implementation("mean", returns_scalar=True)
527516

528-
def median(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
517+
def median(self) -> Self:
529518
return self._reuse_series_implementation("median", returns_scalar=True)
530519

531-
def std(self, *, ddof: int) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
520+
def std(self, *, ddof: int) -> Self:
532521
return self._reuse_series_implementation(
533522
"std", returns_scalar=True, call_kwargs={"ddof": ddof}
534523
)
535524

536-
def var(self, *, ddof: int) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
525+
def var(self, *, ddof: int) -> Self:
537526
return self._reuse_series_implementation(
538527
"var", returns_scalar=True, call_kwargs={"ddof": ddof}
539528
)
540529

541-
def skew(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
530+
def skew(self) -> Self:
542531
return self._reuse_series_implementation("skew", returns_scalar=True)
543532

544-
def any(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
533+
def any(self) -> Self:
545534
return self._reuse_series_implementation("any", returns_scalar=True)
546535

547-
def all(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
536+
def all(self) -> Self:
548537
return self._reuse_series_implementation("all", returns_scalar=True)
549538

550-
def max(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
539+
def max(self) -> Self:
551540
return self._reuse_series_implementation("max", returns_scalar=True)
552541

553-
def mix(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
542+
def mix(self) -> Self:
554543
return self._reuse_series_implementation("min", returns_scalar=True)
555544

556-
def arg_min(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
545+
def arg_min(self) -> Self:
557546
return self._reuse_series_implementation("arg_min", returns_scalar=True)
558547

559-
def arg_max(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
548+
def arg_max(self) -> Self:
560549
return self._reuse_series_implementation("arg_max", returns_scalar=True)
561550

562551
# Other
563552

564-
def clip(
565-
self, lower_bound: Any, upper_bound: Any
566-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
553+
def clip(self, lower_bound: Any, upper_bound: Any) -> Self:
567554
return self._reuse_series_implementation(
568555
"clip", lower_bound=lower_bound, upper_bound=upper_bound
569556
)
570557

571-
def is_null(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
558+
def is_null(self) -> Self:
572559
return self._reuse_series_implementation("is_null")
573560

574-
def is_nan(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
561+
def is_nan(self) -> Self:
575562
return self._reuse_series_implementation("is_nan")
576563

577564
def fill_null(
578565
self,
579566
value: Any | None,
580567
strategy: Literal["forward", "backward"] | None,
581568
limit: int | None,
582-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
569+
) -> Self:
583570
return self._reuse_series_implementation(
584571
"fill_null", value=value, strategy=strategy, limit=limit
585572
)
586573

587-
def is_in(self, other: Any) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
574+
def is_in(self, other: Any) -> Self:
588575
return self._reuse_series_implementation("is_in", other="other")
589576

590-
def arg_true(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
577+
def arg_true(self) -> Self:
591578
return self._reuse_series_implementation("arg_true")
592579

593580
# NOTE: `ewm_mean` not implemented `pyarrow`
594581

595-
def filter(self, *predicates: Self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
582+
def filter(self, *predicates: Self) -> Self:
596583
plx = self.__narwhals_namespace__()
597584
other = plx.all_horizontal(*predicates)
598585
return self._reuse_series_implementation("filter", other=other)
599586

600-
def drop_nulls(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
587+
def drop_nulls(self) -> Self:
601588
return self._reuse_series_implementation("drop_nulls")
602589

603590
def replace_strict(
@@ -606,25 +593,23 @@ def replace_strict(
606593
new: Sequence[Any],
607594
*,
608595
return_dtype: DType | type[DType] | None,
609-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
596+
) -> Self:
610597
return self._reuse_series_implementation(
611598
"replace_strict", old=old, new=new, return_dtype=return_dtype
612599
)
613600

614-
def sort(
615-
self, *, descending: bool, nulls_last: bool
616-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
601+
def sort(self, *, descending: bool, nulls_last: bool) -> Self:
617602
return self._reuse_series_implementation(
618603
"sort", descending=descending, nulls_last=nulls_last
619604
)
620605

621-
def abs(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
606+
def abs(self) -> Self:
622607
return self._reuse_series_implementation("abs")
623608

624-
def unique(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
609+
def unique(self) -> Self:
625610
return self._reuse_series_implementation("unique", maintain_order=False)
626611

627-
def diff(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
612+
def diff(self) -> Self:
628613
return self._reuse_series_implementation("diff")
629614

630615
# NOTE: `shift` differs
@@ -636,7 +621,7 @@ def sample(
636621
fraction: float | None,
637622
with_replacement: bool,
638623
seed: int | None,
639-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
624+
) -> Self:
640625
return self._reuse_series_implementation(
641626
"sample", n=n, fraction=fraction, with_replacement=with_replacement, seed=seed
642627
)
@@ -664,57 +649,55 @@ def alias_output_names(names: Sequence[str]) -> Sequence[str]:
664649

665650
# NOTE: `over` differs
666651

667-
def is_unique(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
652+
def is_unique(self) -> Self:
668653
return self._reuse_series_implementation("is_unique")
669654

670-
def is_first_distinct(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
655+
def is_first_distinct(self) -> Self:
671656
return self._reuse_series_implementation("is_first_distinct")
672657

673-
def is_last_distinct(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
658+
def is_last_distinct(self) -> Self:
674659
return self._reuse_series_implementation("is_last_distinct")
675660

676661
def quantile(
677662
self,
678663
quantile: float,
679664
interpolation: Literal["nearest", "higher", "lower", "midpoint", "linear"],
680-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
665+
) -> Self:
681666
return self._reuse_series_implementation(
682667
"quantile",
683668
quantile=quantile,
684669
interpolation=interpolation,
685670
returns_scalar=True,
686671
)
687672

688-
def head(self, n: int) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
673+
def head(self, n: int) -> Self:
689674
return self._reuse_series_implementation("head", n=n)
690675

691-
def tail(self, n: int) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
676+
def tail(self, n: int) -> Self:
692677
return self._reuse_series_implementation("tail", n=n)
693678

694-
def round(self, decimals: int) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
679+
def round(self, decimals: int) -> Self:
695680
return self._reuse_series_implementation("round", decimals=decimals)
696681

697-
def len(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
682+
def len(self) -> Self:
698683
return self._reuse_series_implementation("len", returns_scalar=True)
699684

700-
def gather_every(
701-
self, n: int, offset: int
702-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
685+
def gather_every(self, n: int, offset: int) -> Self:
703686
return self._reuse_series_implementation("gather_every", n=n, offset=offset)
704687

705-
def mode(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
688+
def mode(self) -> Self:
706689
return self._reuse_series_implementation("mode")
707690

708691
# NOTE: `map_batches` differs
709692

710-
def is_finite(self) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
693+
def is_finite(self) -> Self:
711694
return self._reuse_series_implementation("is_finite")
712695

713696
# NOTE: `cum_(sum|count|min|max|prod)` differ
714697

715698
def rolling_mean(
716699
self, window_size: int, *, min_samples: int | None, center: bool
717-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
700+
) -> Self:
718701
return self._reuse_series_implementation(
719702
"rolling_mean",
720703
window_size=window_size,
@@ -724,7 +707,7 @@ def rolling_mean(
724707

725708
def rolling_std(
726709
self, window_size: int, *, min_samples: int | None, center: bool, ddof: int
727-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
710+
) -> Self:
728711
return self._reuse_series_implementation(
729712
"rolling_std",
730713
window_size=window_size,
@@ -735,14 +718,14 @@ def rolling_std(
735718

736719
def rolling_sum(
737720
self, window_size: int, *, min_samples: int | None, center: bool
738-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
721+
) -> Self:
739722
return self._reuse_series_implementation(
740723
"rolling_sum", window_size=window_size, min_samples=min_samples, center=center
741724
)
742725

743726
def rolling_var(
744727
self, window_size: int, *, min_samples: int | None, center: bool, ddof: int
745-
) -> EagerExpr[EagerDataFrameT, EagerSeriesT]:
728+
) -> Self:
746729
return self._reuse_series_implementation(
747730
"rolling_var",
748731
window_size=window_size,

0 commit comments

Comments
 (0)