Skip to content

chore: refactor EagerWhen._if_then_else #2662

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 14 commits into from
Jun 11, 2025

Conversation

MarcoGorelli
Copy link
Member

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

  • Related issue #<issue number>
  • Closes #<issue number>

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

) -> EagerWhen[EagerDataFrameT, EagerSeriesT, EagerExprT, NativeSeriesT]: ...
) -> EagerWhen[EagerDataFrameT, EagerSeriesT, EagerExprT]: ...
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling to follow this

If I remove NativeSeriesT from EagerWhen, and hence from the return type, then mypy / pyright tell me:

   /home/runner/work/narwhals/narwhals/narwhals/_compliant/namespace.py:133:7 - warning: Type variable "NativeSeriesT" used in generic Protocol "EagerNamespace" should be covariant (reportInvalidTypeVarUse)

But, I can't make it covariant, because it's used as an argument to from_native. If I did, type checkers would complain. I can't tell what they're expecting me to do πŸ˜„

@dangotbanned sorry for the ping, any tips would be appreciated πŸ™

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @MarcoGorelli, I'll try to take a look at this today πŸ™

Would you be able to give some more background on what you're trying to accomplish with this PR please?

I'm not following the motivation from either the title or commit messages πŸ€”

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, it's to enable #2652 and #2645

i spent about 2 hours trying different things and wasn't able to get anywhere with this 😳 , based on #2662 (comment) it looks to me like there were already issues with these protocols. happy for some parts to be brought back, so long as we're able to move towards the linked issues

@MarcoGorelli MarcoGorelli marked this pull request as ready for review June 10, 2025 20:24
@MarcoGorelli
Copy link
Member Author

I think we may just need to remove NativeSeriesT from EagerNamespace? It's used as an argument, but due to how EagerNamespace is used (probably?), pyright/mypy expect EagerNamespace to be covariant in NativeSeriesT. But, it can't be covariant in NativeSeriesT because NativeSeriesT appears as a function argument, so it should be contravariant.

One trick could be to add def _no_op(self, s: NativeSeriesT) -> NativeSeriesT: return s to trick type checkers into thinking that EagerNamespace is invariant in NativeSeriesT. But, I think that would look extremely confusing. IMHO this is a chance to just remove NativeSeriesT from EagerNamespace protocol and simplify things a little

@MarcoGorelli MarcoGorelli merged commit e831be1 into narwhals-dev:main Jun 11, 2025
32 checks passed
Comment on lines -145 to -159
@overload
def from_native(self, data: NativeFrameT, /) -> EagerDataFrameT: ...
@overload
def from_native(self, data: NativeSeriesT, /) -> EagerSeriesT: ...
# TODO @dangotbanned: Align `PandasLike` typing with `_namespace`, then drop this `@overload`
# - Using the guards there introduces `_NativeModin`, `_NativeCuDF`
# - These types haven't been integrated into the backend
# - Most of the `pandas` stuff is still untyped
@overload
def from_native(
self, data: NativeFrameT | NativeSeriesT | Any, /
) -> EagerDataFrameT | EagerSeriesT: ...
def from_native(
self, data: NativeFrameT | NativeSeriesT | Any, /
) -> EagerDataFrameT | EagerSeriesT:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should have been merged

Comment on lines +452 to 456
def _gather(self, rows: SizedMultiIndexSelector[Any]) -> Self: ...
def _gather_slice(self, rows: _SliceIndex | range) -> Self: ...
def _select_multi_index(
self, columns: SizedMultiIndexSelector[NativeSeriesT]
) -> Self: ...
def _select_multi_name(
self, columns: SizedMultiNameSelector[NativeSeriesT]
) -> Self: ...
def _select_multi_index(self, columns: SizedMultiIndexSelector[Any]) -> Self: ...
def _select_multi_name(self, columns: SizedMultiNameSelector[Any]) -> Self: ...
def _select_slice_index(self, columns: _SliceIndex | range) -> Self: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah can you revert this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A refactor on when shouldn't have to remove typing from other parts of narwhals

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants