refactor: adding Compliant* sub-protocols#2055
Conversation
Disagree and so does pyright > error: Covariant type variable "NativeSeriesT_co" used in protocol where invariant one is expected [misc]
There seems to be more places that `ensure_pandas_like` could be used
…ementation` @MarcoGorelli see *NOTE*
`pandas` uses this, but `pyarrow` doesn't
| def _create_compliant_series(self, value: Any) -> ReuseSeriesT: ... | ||
|
|
||
| # NOTE: Fully spec'd | ||
| def _create_expr_from_callable( | ||
| self, | ||
| func: Callable[[CompliantDataFrameT_co], Sequence[ReuseSeriesT]], | ||
| *, | ||
| depth: int, | ||
| function_name: str, | ||
| evaluate_output_names: Callable[[CompliantDataFrameT_co], Sequence[str]], | ||
| alias_output_names: Callable[[Sequence[str]], Sequence[str]] | None, | ||
| kwargs: dict[str, Any], | ||
| ) -> ReuseExpr[ReuseSeriesT]: | ||
| return self.__narwhals_expr__()( | ||
| func, | ||
| depth=depth, | ||
| function_name=function_name, | ||
| evaluate_output_names=evaluate_output_names, | ||
| alias_output_names=alias_output_names, | ||
| implementation=self._implementation, | ||
| backend_version=self._backend_version, | ||
| version=self._version, | ||
| kwargs=kwargs, | ||
| ) | ||
|
|
||
| # NOTE: Fully spec'd | ||
| def _create_expr_from_series(self, series: ReuseSeriesT) -> ReuseExpr[ReuseSeriesT]: | ||
| return self.__narwhals_expr__()( | ||
| lambda _df: [series], | ||
| depth=0, | ||
| function_name="series", | ||
| evaluate_output_names=lambda _df: [series.name], | ||
| alias_output_names=None, | ||
| implementation=self._implementation, | ||
| backend_version=self._backend_version, | ||
| version=self._version, | ||
| kwargs={}, | ||
| ) | ||
|
|
||
| def _create_series_from_scalar( | ||
| self, value: Any, *, reference_series: ReuseSeriesT | ||
| ) -> ReuseSeriesT: ... |
There was a problem hiding this comment.
Could these all be replaced like this?
ReuseNamespace._create_compliant_series()->ReuseSeries()ReuseNamespace._create_expr_from_callable()->ReuseExpr._from_callable()ReuseNamespace._create_expr_from_series()->ReuseExpr._from_series()ReuseNamespace._create_series_from_scalar()->ReuseSeries._from_scalar()
| def ensure_pandas_like(self: Self) -> None: | ||
| if self.is_pandas_like(): | ||
| return | ||
| pandas_like = {Implementation.PANDAS, Implementation.CUDF, Implementation.MODIN} | ||
| msg = f"Expected pandas-like implementation ({pandas_like}), found {self}" | ||
| raise TypeError(msg) | ||
|
|
||
| # NOTE: Not sure why this differs from `pandas_like` | ||
| def ensure_pyarrow(self: Self) -> None: | ||
| if self.is_pyarrow(): | ||
| return | ||
| msg = f"Expected pyarrow, got: {type(self)}" # pragma: no cover | ||
| raise AssertionError(msg) |
There was a problem hiding this comment.
Probably going to split this into another PR.
Would be helpful for representing this
narwhals/narwhals/_protocols.py
Lines 178 to 185 in b74faa3
narwhals/narwhals/_pandas_like/series.py
Lines 115 to 124 in dc7a0e1
But there are lots of other places this kind of check/error message is copy/pasted at the moment
narwhals/_protocols.py
Outdated
| version: Version, | ||
| ) -> None: ... | ||
|
|
||
| def __narwhals_expr__(self) -> Callable[..., ReuseExpr[ReuseSeriesT]]: ... |
There was a problem hiding this comment.
To be more permissive at this level; I've used:
Callable[..., ReuseExpr[ReuseSeriesT]]instead oftype[ReuseExpr[ReuseSeriesT]]
|
@MarcoGorelli no rush on this, just wanted to check in to see if your happy with the direction of this? Actually implementing it shouldn't be difficult from this point, but would be a huge diff |
WIll start working on resolving `evaluate_into_exprs` errors next https://github.com/narwhals-dev/narwhals/actions/runs/13483136123/job/37670892891?pr=2064#step:8:18
Beginning to merge parts of (#2055)
|
Superseded by #2149 |
What type of PR is this? (check all applicable)
Related issues
TypeVarused in(SparkLike|DuckDB)Expr#2044TypeVarused in(SparkLike|DuckDB)Expr#2044 (comment)Checklist
If you have comments or can explain your changes, please do so below
Note
Thinking about renaming
Reuse*->EagerOnly*or something like thatTODO
evaluate_into_exprs->ReuseDataFrame._evaluate_into_exprsmypycannot cope with the genericspyrightunderstands itnarwhals/narwhals/_expression_parsing.py
Lines 53 to 84 in f6a7902