Skip to content

Conversation

@dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Mar 5, 2025

Closes #2044

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

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

Related issues

Checklist

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

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

Direct follow-up to (#2119), aiming to make space for similar changes for other Compliant* protocols.

This PR spiralled a bit, but tackles a few quite-related issues.

Lazy-only typing

#2044 has been resolved through the use of the new protocol NativeExpr.

class NativeExpr(Protocol):
"""An `Expr`-like object from a package with [Lazy-only support](https://narwhals-dev.github.io/narwhals/extending/#levels-of-support).
Protocol members are chosen *purely* for matching statically - as they
are common to all currently supported packages.
"""
def between(self, *args: Any, **kwds: Any) -> Any: ...
def isin(self, *args: Any, **kwds: Any) -> Any: ...

(Arrow|PandasLike)* deduplication

All of the new classes prefixed with Eager now provide the common functionality between the two Eager-only backends.
The big one is EagerExpr

class EagerExpr(
CompliantExpr[EagerDataFrameT, EagerSeriesT],
Protocol38[EagerDataFrameT, EagerSeriesT],
):

A very unexpected part of that was that they could also share *ExprNamespace implementations

Probably the most visual part of the PR as it removes the need for 12 modules

*Namespace protocols

Working on the Eager namespaces led to the new generic protocols for both (expr|series)_* namespaces https://github.com/narwhals-dev/narwhals/blob/b3bdb3b3ba52dada9cadde3c50d68520f1270a09/narwhals/_compliant/any_namespace.py

These can be re-used for all backends πŸ˜„

Misc

I followed up on (#2055 (comment)) and converted some methods to simpler .from_ variants.
The only remaining one (_create_compliant_series) is discussed in (#2149 (comment))

I also opened (#2169) for following up some strange pandas behavior

Note

There might be some other bits in commit descriptions I've missed

@dangotbanned dangotbanned changed the title refactor(DRAFT): adds _compliant sub-package refactor: adds _compliant sub-package Mar 6, 2025
@dangotbanned dangotbanned marked this pull request as ready for review March 6, 2025 16:04
@dangotbanned dangotbanned marked this pull request as draft March 6, 2025 17:22
@dangotbanned dangotbanned linked an issue Mar 6, 2025 that may be closed by this pull request
Comment on lines +27 to +31
CompliantSeriesOrNativeExprT_co = TypeVar(
"CompliantSeriesOrNativeExprT_co",
bound="CompliantSeries | NativeExpr",
covariant=True,
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Really want something that rolls off the tongue better than this πŸ€”

```log
error: All protocol members must have explicitly declared types  [misc]
```
@dangotbanned
Copy link
Member Author

dangotbanned commented Mar 10, 2025

Still managing to get more LOC (and module) reductions in (40d718f) πŸ˜‰

@MarcoGorelli
Copy link
Member

thanks - gonna review/merge tomorrow πŸ‘

@dangotbanned
Copy link
Member Author

thanks - gonna review/merge tomorrow πŸ‘

perfect, thanks @MarcoGorelli

The points I'm trying to make are:
- the method isn't self-explanatory from its name
- `Find All References` won't show every use
- This will be replaced soon - but still gathering the details on how/with what
@dangotbanned
Copy link
Member Author

dangotbanned commented Mar 11, 2025

Thanks for the review @MarcoGorelli - sorry that this one spiralled!

pyright failures are not related

pytest failures also seems to be sqlframe, but only an [XPASS(strict)]

@dangotbanned dangotbanned mentioned this pull request Mar 11, 2025
10 tasks
@MarcoGorelli
Copy link
Member

MarcoGorelli commented Mar 11, 2025

i've put up a PR for the xfail, but I think we need to resolve the pyright failures (in a separate PR?) before merging this if that's OK

this could be a chance to address #2185 too

@dangotbanned
Copy link
Member Author

dangotbanned commented Mar 11, 2025

#2149 (comment)

All good - taking a look now

Notes (Updating)

  • 24 errors
  • only impacts _spark_like.(dataframe|group_by)

Comment on lines +43 to +51
@deprecated(
"Internally used for `numpy.ndarray` -> `CompliantSeries`\n"
"Also referenced in untyped `nw.dataframe.DataFrame._extract_compliant`\n"
"See Also:\n"
" - https://github.com/narwhals-dev/narwhals/pull/2149#discussion_r1986283345\n"
" - https://github.com/narwhals-dev/narwhals/issues/2116\n"
" - https://github.com/narwhals-dev/narwhals/pull/2169"
)
def _create_compliant_series(self, value: Any) -> EagerSeriesT_co: ...
Copy link
Member

Choose a reason for hiding this comment

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

why is this deprecated?

Copy link
Member Author

Choose a reason for hiding this comment

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

"Deprecated" is probably the wrong word.

I'm trying to mark it as something I want to replace with something like:

CompliantNamespace._series.from_numpy
CompliantNamespace._series.from_iterable

There are still some parts I wanna clear up first in the linked issues - hoping to explore that more in a future PR 🀞

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 don't think the current name is helpful in hinting that it is used for numpy - or at least it surprised me when I followed through the references

Copy link
Member

Choose a reason for hiding this comment

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

cool, thanks! ok to move forwards then

Copy link
Member Author

Choose a reason for hiding this comment

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

New usage of the method will show up in VSCode with a strikethrough and that message on hover

Copy link
Member Author

Choose a reason for hiding this comment

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

But there isn't a runtime effect - purely for internal docs

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

thanks @dangotbanned , great refactor here!

approving so we can move forwards with other work, I just left a question about why an internal method is deprecated

@dangotbanned
Copy link
Member Author

Thanks again for making it through this one @MarcoGorelli 😍

@dangotbanned dangotbanned merged commit 1d8bc31 into main Mar 11, 2025
28 of 29 checks passed
@dangotbanned dangotbanned deleted the compliant-package branch March 11, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix TypeVar used in (SparkLike|DuckDB)Expr

3 participants