-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
2c36eea
feat(typing): Make `Implementation` less opaque
dangotbanned 2350dfc
ci(typing): fix pyright coverage
dangotbanned fe80d52
ci: Handle descriptors in API reference
dangotbanned 123dc2e
Merge branch 'main' into implementation-typing
dangotbanned 54bfbe4
Merge remote-tracking branch 'upstream/main' into implementation-typing
dangotbanned cadcdf0
cov
dangotbanned 5b2bc62
add typing tests, tweak overloads
dangotbanned 14974bc
refactor(typing): Switch most overloads to `BaseFrame`
dangotbanned 685409c
feat(typing): Get basic `LazyFrame.implementation` working
dangotbanned 0f83e44
kinda support dask
dangotbanned 49a10bd
ci: try include `dask` in typing?
dangotbanned fd2b93e
aaaaand `modin` as well
dangotbanned 618ce8c
feat(typing): `duckdb` & `sqlframe` work!
dangotbanned 0606a14
kinda support `ibis`
dangotbanned 37aaa69
test(typing): Simplify Any/Into, also test lazy
dangotbanned 141b687
test(typing): Add `DataFrame.lazy` suite
dangotbanned cabedd4
refactor: Prepare for `Series` support
dangotbanned 71c5163
extend this overload abomination
dangotbanned 2b7945b
refactor: Move `_ImplDescriptor` to `_utils`
dangotbanned c573cfd
feat(typing): Add (new) `Series.implementation`
dangotbanned 410b5bd
oop
dangotbanned e07cbc5
test(typing): Add `Series` tests
dangotbanned c4bceed
test: Redo everything, check collect as well
dangotbanned c8dbe07
Merge branch 'main' into implementation-typing
dangotbanned eaa43c1
docs: Ensure `BaseFrame.implementation` shows in api ref
dangotbanned 5ef8103
fix(typing): `ibis`, `dask` work!!!
dangotbanned f55cb3a
fix(typing): Unbreak `modin`
dangotbanned 811290c
test(typing): Check `mpd.Series` too
dangotbanned 012c2bf
typo
dangotbanned b0694d0
fix `mpd.Series`
dangotbanned 2a75529
chore: Add overload for pyspark
dangotbanned 7d42972
simplify, add notes
dangotbanned fd736c6
Merge branch 'main' into implementation-typing
dangotbanned 5d2f54f
Merge branch 'main' into implementation-typing
dangotbanned 05d4115
rename, add brief doc to `_Implementation`
dangotbanned 87d4439
refactor: Rename `NarwhalsObj` -> `Narwhals`
dangotbanned bee6984
tighten up `Narwhals` w/ `Compliant`
dangotbanned 1c68c68
docs(typing): Add `Narwhals` explainer
dangotbanned fcafec6
docs: Add crossref to `Implementation`
dangotbanned 7157bbd
refactor: shrinking
dangotbanned 08d900c
Merge branch 'main' into implementation-typing
dangotbanned b2aaf0d
Merge branch 'main' into implementation-typing
dangotbanned 635b5a8
Merge branch 'main' into implementation-typing
dangotbanned 5049a2a
docs: Explain typing test structure
dangotbanned 4b78837
Update narwhals/_utils.py
dangotbanned 29daf5e
Merge branch 'main' into implementation-typing
dangotbanned f6da9ce
Merge branch 'main' into implementation-typing
dangotbanned fe21d09
Merge remote-tracking branch 'upstream/main' into implementation-typing
dangotbanned a94a0f8
test(typing): Update for (#3032)
dangotbanned 884d135
Merge branch 'main' into implementation-typing
dangotbanned 4ad081c
Merge branch 'main' into implementation-typing
dangotbanned 791ecae
ci: Exclude `OrderedDict` methods from `check-api-reference`
dangotbanned a3bd3ac
Merge branch 'main' into implementation-typing
dangotbanned 043b9d1
Merge branch 'main' into implementation-typing
dangotbanned 6b59ed9
Merge branch 'main' into implementation-typing
dangotbanned 919b22f
Merge remote-tracking branch 'upstream/main' into implementation-typing
dangotbanned 5e2838e
Merge branch 'main' into implementation-typing
dangotbanned 6bc2c47
Merge branch 'main' into implementation-typing
dangotbanned 21800fe
ci: Try adding `--group 'typing-ci'`
dangotbanned File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,14 +73,40 @@ | |
| NativeSeriesT_co, | ||
| ) | ||
| from narwhals._compliant.typing import EvalNames, NativeDataFrameT, 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 | ||
|
|
@@ -141,7 +167,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: | ||
|
|
@@ -2034,3 +2060,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
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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