refactor: Organizing Compliant* APIs#3045
Conversation
`self: ImplExpr[CompliantFrameT, Any]` was hiding this - but eventually wanna solve this circular bug
Was never directly referenced
Can't remember when we dropped these elsewhere
| @classmethod | ||
| def from_column_indices( | ||
| cls, *column_indices: int, context: _LimitedContext | ||
| ) -> Self: ... | ||
| @classmethod | ||
| def from_column_names( | ||
| cls, | ||
| evaluate_column_names: EvalNames[CompliantFrameT], | ||
| /, | ||
| *, | ||
| context: _LimitedContext, | ||
| ) -> Self: ... |
There was a problem hiding this comment.
I'm somewhat torn on these CompliantExpr constructors.
I like that all you need to do is implement these two:
from_column_indicesfrom_column_names
And then you get these 4 for "free"
narwhals/narwhals/_compliant/namespace.py
Lines 53 to 76 in 29a3038
What I don't like is that _LimitedContext specifies you need to pass an object with:
_implementation: Implementation_version: Version
Recently, this PR removed the requirement of also having _backend_version:
I'd like to continue that trend and work towards removing _implementation as well 😏
I've been wanting to drop down to 1x Implementation per class for a while:
- refactor: Generic
CompliantSelector#2064 (comment) - chore(typing): enable typing checks for
pyspark#2051 (comment)
Which would just mean defining it on the class like e.g. Arrow*
narwhals/narwhals/_arrow/expr.py
Lines 28 to 29 in 29a3038
If we do that, then:
-
We don't need it included in any signatures
iArrowExprstill requires a sink in__init__even now 😢narwhals/narwhals/_arrow/expr.py
Lines 41 to 42 in 29a3038
-
*Likeclasses don't need to branch onImplementationinside methods
i. We just override things in subclasses -
We might be able to do something different for extensions (enh:
Implementationfor plugins #3042)
i. WhereImplementation.UNKNOWNis just a constant to satisfy typing atm
There was a problem hiding this comment.
We could also then make __native_namespace__ a @classmethod 🤯
That doesn't depend on nw.Version, so we don't need to initialize a Compliant object to access it
There was a problem hiding this comment.
We could also then make native_namespace a @classmethod 🤯
sure, if that works, why not
| def simple_select(self, *column_names: str) -> Self: | ||
| """`select` where all args are column names.""" | ||
| ... |
There was a problem hiding this comment.
Nit: I'd prefer if this were named something like:
select_by_name
select_column_names
select_names- simple requires explanation of what it means
- It is a subset of
select, so a suffix means they show up together in autocomplete
narwhals/_compliant/dataframe.py
Outdated
| # `LazySelectorNamespace._iter_columns` depends | ||
| def _iter_columns(self) -> Iterator[Any]: ... |
There was a problem hiding this comment.
Untangling this one will need to be it's own PR
Main goal of (#3045 (comment))
| CompliantColumn, | ||
| Protocol[NativeSeriesT], | ||
| ): | ||
| # NOTE: `narwhals` |
There was a problem hiding this comment.
should we split them by having all the narwhals ones be private, and and all the polars ones public?
There was a problem hiding this comment.
should we split them
I've thought about this as well in the past!
narwhals ones be private, and and all the polars ones public?
I'd prefer to define our public api using common conventions where possible 🙏
For example, enabling this pyright rule already isn't feasible for us
reportPrivateImportUsage = "none"
reportUnusedExpression = "none" # handled by (https://docs.astral.sh/ruff/rules/unused-variable/)
+reportPrivateUsage = "error"
typeCheckingMode = "basic">>> pyright
1004 errors, 0 warnings, 0 informationsIt also means we don't leave any space for extensions to define their methods - without concern for us needing that name later
There was a problem hiding this comment.
The least bad idea I've had was to define our stuff with a property accessor
Buuuuuut we're running quite short on useful names - given how far we've stretched Namespace 😂
MarcoGorelli
left a comment
There was a problem hiding this comment.
generally looks good, thanks @dangotbanned
just left a question #3045 (comment), but feel free to merge when ready
Only became visible after `__narwhals_expr__`
|
Thanks for the review @MarcoGorelli! |

What type of PR is this? (check all applicable)
Related issues
Compliant*methods #2713BaseFrame._extract_compliant#3054Checklist
If you have comments or can explain your changes, please do so below
narwhalsvspolarsNote
All the threads are follow-up ideas