-
Notifications
You must be signed in to change notification settings - Fork 150
chore: Add EagerSeries*Namespace
protocols
#2294
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
18 commits
Select commit
Hold shift + click to select a range
524c390
chore(typing): Add `EagerSeries*Namespace` protocols
dangotbanned 9713b10
chore: Add to `EagerSeries` +1
dangotbanned f4c0cd6
feat(DRAFT): Implement `PandasLikeSeries.cat`
dangotbanned 1321d94
Merge remote-tracking branch 'upstream/main' into series-ns-cleanup
dangotbanned 6a39d56
try `Protocol38` on `EagerSeriesNamespace` only
dangotbanned 38ea7d0
fix: Try mixing differently
dangotbanned 799cd28
Merge remote-tracking branch 'upstream/main' into series-ns-cleanup
dangotbanned cca17e7
refactor: Move `*Namespace` protocols to `_compliant`
dangotbanned d213710
refactor: `PandasLikeSeriesDateTimeNamespace`
dangotbanned afd9fc6
fix(typing): Fill incorrect `Any`
dangotbanned db4fc57
refactor: `PandasLikeSeriesStructNamespace`
dangotbanned 9969051
refactor: `PandasLikeSeriesListNamespace`
dangotbanned bfa7001
refactor: `PandasLikeSeriesStringNamespace`
dangotbanned 7daa8f6
refactor: Just make that a method
dangotbanned 3ff77a0
fix: Use `aggregate_function` in backend completeness
dangotbanned 1336f10
Merge branch 'main' into series-ns-cleanup
dangotbanned 38619d1
Merge branch 'main' into series-ns-cleanup
dangotbanned 147cd6b
Merge branch 'main' into series-ns-cleanup
MarcoGorelli 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
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
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.
Now that these are in the right place, I can collect my thoughts on the 3 solutions we can use for
Protocol
definintions with an__init__
.Problem
Defining a constructor in a
Protocol
can produce two different bugs.I've documented that in (#2064 (comment)) - but the short of it is
3.8
and3.(9|10)
have issues.Solutions
1.
Protocol38
This was the first one I discovered (for #2064) and I really dislike it due to the complexity.
"Fixing"
3.8
requires tricking the type checker into thinkingtyping.Generic
istyping.Protocol
.Imports
narwhals/narwhals/_compliant/expr.py
Lines 40 to 48 in 697bb2c
The side-effect is every sub-protocol is then required to use that
Protocol38
definition or face runtime errors - that can no-longer be caught statically"Fixing"
3.(9|10)
requires redefining__init__
anyway in the impl.Which mostly negates the benefit of defining in the
Protocol
π€¦ββοΈRepeat
__init__
narwhals/narwhals/_compliant/expr.py
Lines 332 to 345 in 697bb2c
narwhals/narwhals/_arrow/expr.py
Lines 33 to 55 in 697bb2c
narwhals/narwhals/_pandas_like/expr.py
Lines 69 to 91 in 697bb2c
2. Define the constructor another way
(#2261) introduced a different solution - but still needed to use
Protocol38
to avoid the metaclass issues of the first solution.I like this a lot more as it feels more intuitive from the outside - given the clear chain of
@classmethod
and@property
definitions.when-then
narwhals/narwhals/_arrow/namespace.py
Lines 241 to 242 in 697bb2c
narwhals/narwhals/_arrow/namespace.py
Lines 282 to 285 in 697bb2c
narwhals/narwhals/_compliant/when_then.py
Lines 72 to 82 in 697bb2c
narwhals/narwhals/_compliant/when_then.py
Lines 63 to 64 in 697bb2c
narwhals/narwhals/_compliant/when_then.py
Lines 67 to 70 in 697bb2c
narwhals/narwhals/_compliant/when_then.py
Lines 94 to 115 in 697bb2c
The only downside I could see is
__new__
being a less-understood method than__init__
.Personally it took me a while to understand when it makes sense to use.
Even now that I do - I didn't think of it as a solution until a month after (#2064) merged π
3. Avoid constructors entirely in
Protocol
(s)That is the solution this comment is attached to.
It might be easy to miss, but
_SeriesNamespace
is aProtocol
andEagerSeriesNamespace
is aGeneric
class.The interesting aspect of that is the
Protocol
(s) represent the interface - but the concrete impl is separate.This approach makes sense when the interface and implementation differ greatly.
Here it is
ArrowSeries
andPandasLikeSeries
- but we go fully structural for all ofPolars