-
Notifications
You must be signed in to change notification settings - Fork 185
fix: missing __getitem__ type fixes
#1963
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
Changes from all commits
6272946
89983fe
2517334
88a0ca5
932a2f9
733ab52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -861,16 +861,19 @@ def estimated_size(self: Self, unit: SizeUnit = "b") -> int | float: | |||
|
|
||||
| @overload | ||||
| def __getitem__( # type: ignore[overload-overlap] | ||||
| self: Self, key: str | tuple[slice | Sequence[int] | np.ndarray, int | str] | ||||
| self: Self, | ||||
| item: str | tuple[slice | Sequence[int] | np.ndarray, int | str], | ||||
| ) -> Series[Any]: ... | ||||
|
|
||||
| @overload | ||||
| def __getitem__( | ||||
| self: Self, | ||||
| key: ( | ||||
| slice | ||||
| item: ( | ||||
| int | ||||
|
Collaborator
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. Hey @dangotbanned thanks for fixing! super quick I didn't have the chance to see the comments :D Anyway I think that I think we shouldn't add it in typing here, but ignore the Having said that, it is weird that after my changes
Member
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. @EdAbati interesting π€ So should this line in the doc be rewritten? narwhals/narwhals/dataframe.py Line 928 in 733ab52
I do appreciate that what you're saying is documented, but writing always reads to me as something that belongs in the annotation?
Collaborator
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. Mmm oh yeah actually good catch, you are right! then it makes a lot of sense
Member
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. @EdAbati marking as unresolved just to show another option if a reviewer wanted it: diff --git a/narwhals/dataframe.py b/narwhals/dataframe.py
index 6b673a80..207ef7b2 100644
--- a/narwhals/dataframe.py
+++ b/narwhals/dataframe.py
@@ -859,6 +859,8 @@ class DataFrame(BaseFrame[DataFrameT]):
"""
return self._compliant_frame.estimated_size(unit=unit) # type: ignore[no-any-return]
+ @overload
+ def __getitem__(self: DataFrame[pd.DataFrame], item: int) -> Any: ...
@overload
def __getitem__( # type: ignore[overload-overlap]
self: Self,
diff --git a/narwhals/stable/v1/__init__.py b/narwhals/stable/v1/__init__.py
index 5aefefe2..af8aed47 100644
--- a/narwhals/stable/v1/__init__.py
+++ b/narwhals/stable/v1/__init__.py
@@ -84,6 +84,7 @@ if TYPE_CHECKING:
from types import ModuleType
import numpy as np
+ import pandas as pd
from typing_extensions import Self
from narwhals.dtypes import DType
@@ -131,6 +132,8 @@ class DataFrame(NwDataFrame[IntoDataFrameT]):
def _lazyframe(self: Self) -> type[LazyFrame[Any]]:
return LazyFrame
+ @overload
+ def __getitem__(self: DataFrame[pd.DataFrame], item: int) -> Any: ...
@overload
def __getitem__( # type: ignore[overload-overlap]
self: Self,
I think this describes the |
||||
| | slice | ||||
| | Sequence[int] | ||||
| | Sequence[str] | ||||
| | np.ndarray | ||||
EdAbati marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| | tuple[ | ||||
| slice | Sequence[int] | np.ndarray, slice | Sequence[int] | Sequence[str] | ||||
| ] | ||||
|
|
@@ -880,9 +883,11 @@ def __getitem__( | |||
| self: Self, | ||||
| item: ( | ||||
| str | ||||
| | int | ||||
| | slice | ||||
| | Sequence[int] | ||||
| | Sequence[str] | ||||
| | np.ndarray | ||||
| | tuple[slice | Sequence[int] | np.ndarray, int | str] | ||||
| | tuple[ | ||||
| slice | Sequence[int] | np.ndarray, slice | Sequence[int] | Sequence[str] | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,15 +55,15 @@ def test_slice_lazy_fails() -> None: | |
|
|
||
|
|
||
| def test_slice_int(constructor_eager: ConstructorEager) -> None: | ||
| result = nw.from_native(constructor_eager(data), eager_only=True)[1] # type: ignore[call-overload] | ||
| result = nw.from_native(constructor_eager(data), eager_only=True)[1] | ||
| assert_equal_data(result, {"a": [2], "b": [12]}) | ||
|
|
||
|
|
||
| def test_slice_fails(constructor_eager: ConstructorEager) -> None: | ||
| class Foo: ... | ||
|
|
||
| with pytest.raises(TypeError, match="Expected str or slice, got:"): | ||
| nw.from_native(constructor_eager(data), eager_only=True)[Foo()] # type: ignore[call-overload] | ||
| nw.from_native(constructor_eager(data), eager_only=True)[Foo()] # type: ignore[call-overload, unused-ignore] | ||
|
Member
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. π€ ignoring unused-ignore seems a bit strange?
Member
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. @MarcoGorelli see #1963 (comment) It is to ignore
Member
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. thanks! |
||
|
|
||
|
|
||
| def test_gather(constructor_eager: ConstructorEager) -> None: | ||
|
|
||

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.
silly mistake (I blame copilot) π
I wonder why it was not picked up though