-
Notifications
You must be signed in to change notification settings - Fork 170
feat(typing): Make Implementation less opaque
#3016
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 55 commits
2c36eea
2350dfc
fe80d52
123dc2e
54bfbe4
cadcdf0
5b2bc62
14974bc
685409c
0f83e44
49a10bd
fd2b93e
618ce8c
0606a14
37aaa69
141b687
cabedd4
71c5163
2b7945b
c573cfd
410b5bd
e07cbc5
c4bceed
c8dbe07
eaa43c1
5ef8103
f55cb3a
811290c
012c2bf
b0694d0
2a75529
7d42972
fd736c6
5d2f54f
05d4115
87d4439
bee6984
1c68c68
fcafec6
7157bbd
08d900c
b2aaf0d
635b5a8
5049a2a
4b78837
29daf5e
f6da9ce
fe21d09
a94a0f8
884d135
4ad081c
791ecae
a3bd3ac
043b9d1
6b59ed9
919b22f
5e2838e
6bc2c47
21800fe
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 |
|---|---|---|
|
|
@@ -58,3 +58,4 @@ | |
| - write_parquet | ||
| show_source: false | ||
| show_bases: false | ||
| inherited_members: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,3 +34,4 @@ | |
| show_root_heading: false | ||
| show_source: false | ||
| show_bases: false | ||
| inherited_members: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,14 +74,40 @@ | |
| NativeSeriesT_co, | ||
| ) | ||
| from narwhals._compliant.typing import EvalNames, NativeLazyFrameT | ||
| from narwhals._namespace import Namespace | ||
| from narwhals._namespace import ( | ||
| Namespace, | ||
| _NativeArrow, | ||
| _NativeCuDF, | ||
| _NativeDask, | ||
| _NativeDuckDB, | ||
| _NativeIbis, | ||
| _NativeModin, | ||
| _NativePandas, | ||
| _NativePandasLike, | ||
| _NativePolars, | ||
| _NativePySpark, | ||
| _NativePySparkConnect, | ||
| _NativeSQLFrame, | ||
| ) | ||
| from narwhals._translate import ArrowStreamExportable, IntoArrowTable, ToNarwhalsT_co | ||
| from narwhals._typing import ( | ||
| Backend, | ||
| IntoBackend, | ||
| _ArrowImpl, | ||
| _CudfImpl, | ||
| _DaskImpl, | ||
| _DuckDBImpl, | ||
| _EagerAllowedImpl, | ||
| _IbisImpl, | ||
| _LazyAllowedImpl, | ||
| _LazyFrameCollectImpl, | ||
| _ModinImpl, | ||
| _PandasImpl, | ||
| _PandasLikeImpl, | ||
| _PolarsImpl, | ||
| _PySparkConnectImpl, | ||
| _PySparkImpl, | ||
| _SQLFrameImpl, | ||
| ) | ||
| from narwhals.dataframe import DataFrame, LazyFrame | ||
| from narwhals.dtypes import DType | ||
|
|
@@ -142,7 +168,7 @@ def columns(self) -> Sequence[str]: ... | |
| _Constructor: TypeAlias = "Callable[Concatenate[_T, P], R2]" | ||
|
|
||
|
|
||
| class _StoresNative(Protocol[NativeT_co]): # noqa: PYI046 | ||
| class _StoresNative(Protocol[NativeT_co]): | ||
| """Provides access to a native object. | ||
|
|
||
| Native objects have types like: | ||
|
|
@@ -2035,3 +2061,91 @@ def deep_attrgetter(attr: str, *nested: str) -> attrgetter[Any]: | |
| def deep_getattr(obj: Any, name_1: str, *nested: str) -> Any: | ||
| """Perform a nested attribute lookup on `obj`.""" | ||
| return deep_attrgetter(name_1, *nested)(obj) | ||
|
|
||
|
|
||
| class Compliant( | ||
| _StoresNative[NativeT_co], _StoresImplementation, Protocol[NativeT_co] | ||
| ): ... | ||
|
|
||
|
|
||
| class Narwhals(Protocol[NativeT_co]): | ||
| """Minimal *Narwhals-level* protocol. | ||
|
|
||
| Provides access to a compliant object: | ||
|
|
||
| obj: Narwhals[NativeT_co]] | ||
| compliant: Compliant[NativeT_co] = obj._compliant | ||
|
|
||
| Which itself exposes: | ||
|
|
||
| implementation: Implementation = compliant.implementation | ||
| native: NativeT_co = compliant.native | ||
|
|
||
| This interface is used for revealing which `Implementation` member is associated with **either**: | ||
| - One or more [nominal] native type(s) | ||
| - One or more [structural] type(s) | ||
| - where the true native type(s) are [assignable to] *at least* one of them | ||
|
|
||
| These relationships are defined in the `@overload`s of `_Implementation.__get__(...)`. | ||
|
|
||
| [nominal]: https://typing.python.org/en/latest/spec/glossary.html#term-nominal | ||
| [structural]: https://typing.python.org/en/latest/spec/glossary.html#term-structural | ||
| [assignable to]: https://typing.python.org/en/latest/spec/glossary.html#term-assignable | ||
| """ | ||
|
|
||
| @property | ||
| def _compliant(self) -> Compliant[NativeT_co]: ... | ||
|
|
||
|
|
||
| class _Implementation: | ||
dangotbanned marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Descriptor for matching an opaque `Implementation` on a generic class. | ||
|
|
||
| Based on [pyright comment](https://github.com/microsoft/pyright/issues/3071#issuecomment-1043978070) | ||
| """ | ||
|
|
||
| def __set_name__(self, owner: type[Any], name: str) -> None: | ||
| self.__name__: str = name | ||
|
|
||
| @overload | ||
| def __get__(self, instance: Narwhals[_NativePolars], owner: Any) -> _PolarsImpl: ... | ||
| @overload | ||
| def __get__(self, instance: Narwhals[_NativePandas], owner: Any) -> _PandasImpl: ... | ||
| @overload | ||
| def __get__(self, instance: Narwhals[_NativeModin], owner: Any) -> _ModinImpl: ... | ||
| @overload # TODO @dangotbanned: Rename `_typing` `*Cudf*` aliases to `*CuDF*` | ||
| def __get__(self, instance: Narwhals[_NativeCuDF], owner: Any) -> _CudfImpl: ... | ||
| @overload | ||
| def __get__( | ||
| self, instance: Narwhals[_NativePandasLike], owner: Any | ||
| ) -> _PandasLikeImpl: ... | ||
| @overload | ||
| def __get__(self, instance: Narwhals[_NativeArrow], owner: Any) -> _ArrowImpl: ... | ||
| @overload | ||
| def __get__( | ||
| self, instance: Narwhals[_NativePolars | _NativeArrow | _NativePandas], owner: Any | ||
| ) -> _PolarsImpl | _PandasImpl | _ArrowImpl: ... | ||
| @overload | ||
| def __get__(self, instance: Narwhals[_NativeDuckDB], owner: Any) -> _DuckDBImpl: ... | ||
| @overload | ||
| def __get__( | ||
| self, instance: Narwhals[_NativeSQLFrame], owner: Any | ||
| ) -> _SQLFrameImpl: ... | ||
| @overload | ||
| def __get__(self, instance: Narwhals[_NativeDask], owner: Any) -> _DaskImpl: ... | ||
| @overload | ||
| def __get__(self, instance: Narwhals[_NativeIbis], owner: Any) -> _IbisImpl: ... | ||
| @overload | ||
| def __get__( | ||
| self, instance: Narwhals[_NativePySpark | _NativePySparkConnect], owner: Any | ||
| ) -> _PySparkImpl | _PySparkConnectImpl: ... | ||
| # NOTE: https://docs.python.org/3/howto/descriptor.html#invocation-from-a-class | ||
| @overload | ||
| def __get__(self, instance: None, owner: type[Narwhals[Any]]) -> Self: ... | ||
|
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. ππΌ |
||
| @overload | ||
| def __get__( | ||
| self, instance: DataFrame[Any] | Series[Any], owner: Any | ||
| ) -> _EagerAllowedImpl: ... | ||
| @overload | ||
| def __get__(self, instance: LazyFrame[Any], owner: Any) -> _LazyAllowedImpl: ... | ||
| def __get__(self, instance: Narwhals[Any] | None, owner: Any) -> Any: | ||
| return self if instance is None else instance._compliant._implementation | ||
FBruzzesi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |||
| from narwhals._utils import ( | ||||
| Implementation, | ||||
| Version, | ||||
| _Implementation, | ||||
| can_lazyframe_collect, | ||||
| check_columns_exist, | ||||
| flatten, | ||||
|
|
@@ -108,6 +109,31 @@ class BaseFrame(Generic[_FrameT]): | |||
| _compliant_frame: Any | ||||
| _level: Literal["full", "lazy", "interchange"] | ||||
|
|
||||
| implementation: _Implementation = _Implementation() | ||||
| """Return [`narwhals.Implementation`][] of native frame. | ||||
|
|
||||
| This can be useful when you need to use special-casing for features outside of | ||||
| Narwhals' scope - for example, when dealing with pandas' Period Dtype. | ||||
|
Comment on lines
+112
to
+113
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. True (and I know this was the description that was already here) - but this is not the only case in which 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. Yeah I agree tbf, I also thought the first line could do with some tweaking narwhals/narwhals/dataframe.py Line 113 in 635b5a8
If you suggest something, I'm 95% sure I'll accept it π 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. Could we look at improving these docs in a follow-up? 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. Yes sure! Sorry I didn't mean it as a blocker |
||||
|
|
||||
| Examples: | ||||
| >>> import narwhals as nw | ||||
| >>> import pandas as pd | ||||
| >>> df_native = pd.DataFrame({"a": [1, 2, 3]}) | ||||
| >>> df = nw.from_native(df_native) | ||||
| >>> df.implementation | ||||
| <Implementation.PANDAS: 'pandas'> | ||||
| >>> df.implementation.is_pandas() | ||||
| True | ||||
| >>> df.implementation.is_pandas_like() | ||||
| True | ||||
| >>> df.implementation.is_polars() | ||||
| False | ||||
| """ | ||||
|
|
||||
| @property | ||||
| @abstractmethod | ||||
| def _compliant(self) -> Any: ... | ||||
|
|
||||
| def __native_namespace__(self) -> ModuleType: | ||||
| return self._compliant_frame.__native_namespace__() # type: ignore[no-any-return] | ||||
|
|
||||
|
|
@@ -443,6 +469,10 @@ class DataFrame(BaseFrame[DataFrameT]): | |||
|
|
||||
| _version: ClassVar[Version] = Version.MAIN | ||||
|
|
||||
| @property | ||||
| def _compliant(self) -> CompliantDataFrame[Any, Any, DataFrameT, Self]: | ||||
| return self._compliant_frame | ||||
|
|
||||
| def _extract_compliant(self, arg: Any) -> Any: | ||||
| from narwhals.expr import Expr | ||||
| from narwhals.series import Series | ||||
|
|
@@ -668,29 +698,6 @@ def from_numpy( | |||
| ) | ||||
| raise ValueError(msg) | ||||
|
|
||||
| @property | ||||
| def implementation(self) -> Implementation: | ||||
| """Return implementation of native frame. | ||||
|
|
||||
| This can be useful when you need to use special-casing for features outside of | ||||
| Narwhals' scope - for example, when dealing with pandas' Period Dtype. | ||||
|
|
||||
| Examples: | ||||
| >>> import narwhals as nw | ||||
| >>> import pandas as pd | ||||
| >>> df_native = pd.DataFrame({"a": [1, 2, 3]}) | ||||
| >>> df = nw.from_native(df_native) | ||||
| >>> df.implementation | ||||
| <Implementation.PANDAS: 'pandas'> | ||||
| >>> df.implementation.is_pandas() | ||||
| True | ||||
| >>> df.implementation.is_pandas_like() | ||||
| True | ||||
| >>> df.implementation.is_polars() | ||||
| False | ||||
| """ | ||||
| return self._compliant_frame._implementation | ||||
|
|
||||
| def __len__(self) -> int: | ||||
| return self._compliant_frame.__len__() | ||||
|
|
||||
|
|
@@ -2292,6 +2299,10 @@ class LazyFrame(BaseFrame[LazyFrameT]): | |||
| ``` | ||||
| """ | ||||
|
|
||||
| @property | ||||
| def _compliant(self) -> CompliantLazyFrame[Any, LazyFrameT, Self]: | ||||
| return self._compliant_frame | ||||
|
|
||||
| def _extract_compliant(self, arg: Any) -> Any: | ||||
| from narwhals.expr import Expr | ||||
| from narwhals.series import Series | ||||
|
|
@@ -2355,22 +2366,6 @@ def __init__(self, df: Any, *, level: Literal["full", "lazy", "interchange"]) -> | |||
| def __repr__(self) -> str: # pragma: no cover | ||||
| return generate_repr("Narwhals LazyFrame", self.to_native().__repr__()) | ||||
|
|
||||
| @property | ||||
| def implementation(self) -> Implementation: | ||||
| """Return implementation of native frame. | ||||
|
|
||||
| This can be useful when you need to use special-casing for features outside of | ||||
| Narwhals' scope - for example, when dealing with pandas' Period Dtype. | ||||
|
|
||||
| Examples: | ||||
| >>> import narwhals as nw | ||||
| >>> import dask.dataframe as dd | ||||
| >>> lf_native = dd.from_dict({"a": [1, 2]}, npartitions=1) | ||||
| >>> nw.from_native(lf_native).implementation | ||||
| <Implementation.DASK: 'dask'> | ||||
| """ | ||||
| return self._compliant_frame._implementation | ||||
|
|
||||
| def __getitem__(self, item: str | slice) -> NoReturn: | ||||
| msg = "Slicing is not supported on LazyFrame" | ||||
| raise TypeError(msg) | ||||
|
|
||||
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.
Follow-up PR
Loooooooooong overdue at this stage, but I'm gonna move most of this stuff and some others into a new
narwhals._nativemodule which has:Protocols and aliases like:
narwhals/narwhals/_namespace.py
Lines 119 to 128 in 5d2f54f
Their corresponding new and re-aliased guards like:
narwhals/narwhals/_namespace.py
Lines 379 to 384 in 5d2f54f
And the
typing.Native*protocols as well:narwhals/narwhals/typing.py
Lines 24 to 39 in 5d2f54f
Beyond just organizing things, it'll mean we can deduplicate the definitions that appear in 3x typing modules π
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.
The type used for
sessionin (#3032 (comment)) would also make sense to be defined in this new module