-
Notifications
You must be signed in to change notification settings - Fork 179
fix(typing): Narrow TypeVar used in Series
#2347
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
11 commits
Select commit
Hold shift + click to select a range
74a03a8
fix(typing): Make `IntoSeries` non-recursive
dangotbanned ff841d1
test: Add `test_series_recursive`
dangotbanned 37549cc
chore(DRAFT): Add todo
dangotbanned 6bd47b4
fix(typing): Try a more aggressive fix
dangotbanned 50e4d13
test: tidy up
dangotbanned 253da29
fix(typing): `v1` backport
dangotbanned 51aaa8d
remove todo
dangotbanned 77565ed
Merge remote-tracking branch 'upstream/main' into narrow-series-type-var
dangotbanned 124239a
fix: Add runtime error for unknown `**kwds`
dangotbanned 8f6d90a
test: Add `test_from_native_invalid_keywords`
dangotbanned 83ca244
Merge branch 'main' into narrow-series-type-var
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 |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ | |
| from narwhals.typing import IntoLazyFrameT | ||
| from narwhals.typing import IntoSeries | ||
| from narwhals.typing import IntoSeriesT | ||
| from narwhals.typing import SeriesT | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
@@ -128,6 +129,10 @@ def to_native( | |
| return narwhals_object | ||
|
|
||
|
|
||
| @overload | ||
| def from_native(native_object: SeriesT, **kwds: Any) -> SeriesT: ... | ||
|
|
||
|
|
||
| @overload | ||
| def from_native( | ||
| native_object: IntoDataFrameT | IntoSeries, | ||
|
|
@@ -298,14 +303,15 @@ def from_native( | |
| ) -> Any: ... | ||
|
|
||
|
|
||
| def from_native( | ||
| def from_native( # noqa: D417 | ||
| native_object: IntoLazyFrameT | IntoFrameT | IntoSeriesT | IntoFrame | IntoSeries | T, | ||
| *, | ||
| strict: bool | None = None, | ||
| pass_through: bool | None = None, | ||
| eager_only: bool = False, | ||
| series_only: bool = False, | ||
| allow_series: bool | None = None, | ||
| **kwds: Any, | ||
| ) -> LazyFrame[IntoLazyFrameT] | DataFrame[IntoFrameT] | Series[IntoSeriesT] | T: | ||
| """Convert `native_object` to Narwhals Dataframe, Lazyframe, or Series. | ||
|
|
||
|
|
@@ -351,6 +357,9 @@ def from_native( | |
| pass_through = validate_strict_and_pass_though( | ||
| strict, pass_through, pass_through_default=False, emit_deprecation_warning=True | ||
| ) | ||
| if kwds: | ||
| msg = f"from_native() got an unexpected keyword argument {next(iter(kwds))!r}" | ||
| raise TypeError(msg) | ||
|
Comment on lines
+360
to
+362
Member
Author
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. Important This matches the runtime behavior of import polars as pl
import narwhals as nw
df = pl.DataFrame({"a": ["A", "B", "A"]})
nw_df = nw.from_native(df)
nw.to_native(nw_df, keyword_1="a", keyword_2="b")Only the first bad argument is ever included: ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 39
37 df = pl.DataFrame({"a": ["A", "B", "A"]})
38 nw_df = nw.from_native(df)
---> 39 nw.to_native(nw_df, keyword_1="a", keyword_2="b")
TypeError: to_native() got an unexpected keyword argument 'keyword_1' |
||
|
|
||
| return _from_native_impl( # type: ignore[no-any-return] | ||
| native_object, | ||
|
|
||
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
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.
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.
But why does this work?
By having an unconditional
@overloadwhen anw.Seriesis passed - it ensures no other@overload(s) are checked.If we were to spell out all the valid cases, they would overlap with the
NativeSeriescases (see #2347 (comment))Needing to have
**kwdsis unfortunate - butI'm open to any other solutions that can pass the new tests π
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.
shall we raise at runtime
if kwds?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.
Yeah I was thinking about that as well!
I wasn't sure
v1andmain?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.
@MarcoGorelli resolved in:
TypeVarused inSeriesΒ #2347 (comment))