Skip to content
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

Tighten-up NativeSeries Protocol #2111

Closed
dangotbanned opened this issue Feb 27, 2025 · 0 comments · Fixed by #2159
Closed

Tighten-up NativeSeries Protocol #2111

dangotbanned opened this issue Feb 27, 2025 · 0 comments · Fixed by #2159
Labels

Comments

@dangotbanned
Copy link
Member

dangotbanned commented Feb 27, 2025

Description

Noticed during (#2110 (comment)) that the current definition is pretty prone to false positives

class NativeSeries(Protocol):
def __len__(self) -> int: ...

All of these will error at runtime, but list[int] will pass statically as list.__len__ exists:

image

Solution

For all the cases we support in nw.from_native, we can use __array__ to narrow things further

Diff

diff --git a/narwhals/typing.py b/narwhals/typing.py
index 9c868694..c13fcbb7 100644
--- a/narwhals/typing.py
+++ b/narwhals/typing.py
@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING
 from typing import Any
 from typing import Callable
 from typing import Generic
+from typing import Iterator
 from typing import Literal
 from typing import Protocol
 from typing import Sequence
@@ -38,6 +39,9 @@ if TYPE_CHECKING:
 
     class NativeSeries(Protocol):
         def __len__(self) -> int: ...
+        def __getitem__(self, key: Any, /) -> Any: ...
+        def __iter__(self) -> Iterator[Any]: ...
+        def __array__(self, *args: Any, **kwds: Any) -> Any: ...
 
     class DataFrameLike(Protocol):
         def __dataframe__(self, *args: Any, **kwargs: Any) -> Any: ...

image

Open questions

  • Would __array__ be too numpy-centric?
  • Are there any other widely-used, third-party Series-like classes we should be aware of?

Other common methods

  • filter
  • unique
  • value_counts
  • equals
  • take
  • to_numpy
dangotbanned added a commit to vega/altair that referenced this issue Feb 28, 2025
Have a better understanding *now* of what the issue is in (https://github.com/narwhals-dev/narwhals/actions/runs/13577097131/job/37955718895).

- This patch is *functionally* the same - but removes the need for ignoring a (typing) warning following (narwhals-dev/narwhals@62f8a1f).
- Should also mean we don't need to bump our `narwhals` minimum [again](#3807 (comment)).
- **Bonus**: reduced overhead for `<3.11` (python/cpython#84403)

## Related
- narwhals-dev/narwhals#2110
- #3811
- narwhals-dev/narwhals#2110 (comment)
- narwhals-dev/narwhals#2110 (comment)
- narwhals-dev/narwhals#2111
dangotbanned added a commit that referenced this issue Mar 6, 2025
MarcoGorelli pushed a commit that referenced this issue Mar 7, 2025
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 a pull request may close this issue.

1 participant