-
Notifications
You must be signed in to change notification settings - Fork 170
chore(typing): Fill out CompliantNamespace protocol
#2202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
4eb66a0
8319d0f
c7a9609
70170da
dad85d7
5168ae5
934ba38
554ef8d
5d609a7
420e4cc
a621fa3
70e15ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,43 +2,63 @@ | |
|
|
||
| from typing import TYPE_CHECKING | ||
| from typing import Any | ||
| from typing import Container | ||
| from typing import Iterable | ||
| from typing import Literal | ||
| from typing import Protocol | ||
|
|
||
| from narwhals._compliant.typing import CompliantExprT | ||
| from narwhals._compliant.typing import CompliantFrameT | ||
| from narwhals._compliant.typing import CompliantSeriesOrNativeExprT_co | ||
| from narwhals._compliant.typing import EagerDataFrameT | ||
| from narwhals._compliant.typing import EagerExprT | ||
| from narwhals._compliant.typing import EagerSeriesT_co | ||
| from narwhals.utils import deprecated | ||
|
|
||
| if TYPE_CHECKING: | ||
| from narwhals._compliant.expr import CompliantExpr | ||
| from narwhals._compliant.selectors import CompliantSelectorNamespace | ||
| from narwhals.dtypes import DType | ||
|
|
||
| __all__ = ["CompliantNamespace", "EagerNamespace"] | ||
|
|
||
|
|
||
| class CompliantNamespace(Protocol[CompliantFrameT, CompliantSeriesOrNativeExprT_co]): | ||
| def col( | ||
| self, *column_names: str | ||
| ) -> CompliantExpr[CompliantFrameT, CompliantSeriesOrNativeExprT_co]: ... | ||
| def lit( | ||
| self, value: Any, dtype: DType | None | ||
| ) -> CompliantExpr[CompliantFrameT, CompliantSeriesOrNativeExprT_co]: ... | ||
| class CompliantNamespace(Protocol[CompliantFrameT, CompliantExprT]): | ||
| def col(self, *column_names: str) -> CompliantExprT: ... | ||
| def lit(self, value: Any, dtype: DType | None) -> CompliantExprT: ... | ||
| def exclude(self, excluded_names: Container[str]) -> CompliantExprT: ... | ||
| def nth(self, *column_indices: int) -> CompliantExprT: ... | ||
| def len(self) -> CompliantExprT: ... | ||
| def all(self) -> CompliantExprT: ... | ||
| def all_horizontal(self, *exprs: CompliantExprT) -> CompliantExprT: ... | ||
| def any_horizontal(self, *exprs: CompliantExprT) -> CompliantExprT: ... | ||
| def sum_horizontal(self, *exprs: CompliantExprT) -> CompliantExprT: ... | ||
| def mean_horizontal(self, *exprs: CompliantExprT) -> CompliantExprT: ... | ||
| def min_horizontal(self, *exprs: CompliantExprT) -> CompliantExprT: ... | ||
| def max_horizontal(self, *exprs: CompliantExprT) -> CompliantExprT: ... | ||
| def concat( | ||
| self, | ||
| items: Iterable[CompliantFrameT], | ||
| *, | ||
| how: Literal["horizontal", "vertical", "diagonal"], | ||
| ) -> CompliantFrameT: ... | ||
| def when(self, predicate: CompliantExprT) -> Any: ... | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| def concat_str( | ||
| self, | ||
| *exprs: CompliantExprT, | ||
| separator: str, | ||
| ignore_nulls: bool, | ||
| ) -> CompliantExprT: ... | ||
| @property | ||
| def selectors(self) -> CompliantSelectorNamespace[Any, Any]: ... | ||
| @property | ||
| def _expr(self) -> type[CompliantExprT]: ... | ||
|
|
||
|
|
||
| class EagerNamespace( | ||
| CompliantNamespace[EagerDataFrameT, EagerSeriesT_co], | ||
| CompliantNamespace[EagerDataFrameT, EagerExprT], | ||
| Protocol[EagerDataFrameT, EagerSeriesT_co, EagerExprT], | ||
| ): | ||
| @property | ||
| def _expr(self) -> type[EagerExprT]: ... | ||
| @property | ||
| def _series(self) -> type[EagerSeriesT_co]: ... | ||
| def all_horizontal(self, *exprs: EagerExprT) -> EagerExprT: ... | ||
|
|
||
| @deprecated( | ||
| "Internally used for `numpy.ndarray` -> `CompliantSeries`\n" | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,7 +32,7 @@ | |||||||||||||||||||||||||||||||||||||||||
| from narwhals.utils import Version | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| class SparkLikeNamespace(CompliantNamespace["SparkLikeLazyFrame", "Column"]): | ||||||||||||||||||||||||||||||||||||||||||
| class SparkLikeNamespace(CompliantNamespace["SparkLikeLazyFrame", "SparkLikeExpr"]): | ||||||||||||||||||||||||||||||||||||||||||
| def __init__( | ||||||||||||||||||||||||||||||||||||||||||
| self: Self, | ||||||||||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -48,6 +48,10 @@ def __init__( | |||||||||||||||||||||||||||||||||||||||||
| def selectors(self: Self) -> SparkLikeSelectorNamespace: | ||||||||||||||||||||||||||||||||||||||||||
| return SparkLikeSelectorNamespace(self) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||
| def _expr(self) -> type[SparkLikeExpr]: | ||||||||||||||||||||||||||||||||||||||||||
| return SparkLikeExpr | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def all(self: Self) -> SparkLikeExpr: | ||||||||||||||||||||||||||||||||||||||||||
| return SparkLikeExpr.from_column_names( | ||||||||||||||||||||||||||||||||||||||||||
| get_column_names, | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| # --- selection --- | |
| def col(self: Self, *column_names: str) -> PandasLikeExpr: | |
| return self._expr.from_column_names( | |
| passthrough_column_names(column_names), function_name="col", context=self | |
| ) | |
| def exclude(self: Self, excluded_names: Container[str]) -> PandasLikeExpr: | |
| return self._expr.from_column_names( | |
| partial(exclude_column_names, names=excluded_names), | |
| function_name="exclude", | |
| context=self, | |
| ) | |
| def nth(self: Self, *column_indices: int) -> PandasLikeExpr: | |
| return self._expr.from_column_indices(*column_indices, context=self) | |
| def all(self: Self) -> PandasLikeExpr: | |
| return self._expr.from_column_names( | |
| get_column_names, function_name="all", context=self | |
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@FBruzzesi ignore this if you're not fully back yet π
Thought you might like to see this new possibility - following the recent spec-ing of the Compliant* protocols
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went ahead with that in (5d609a7)
We now have default implementations for 4x CompliantNamespace methods π
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only got flagged after filling out the protocol.