Skip to content

remove types from Index.__iter__()` #532

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 2 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ from pandas._typing import (
DtypeObj,
FillnaOptions,
HashableT,
IndexIterScalar,
IndexT,
Label,
Level,
Expand Down Expand Up @@ -223,7 +222,7 @@ class Index(IndexOpsMixin, PandasObject):
def shape(self) -> tuple[int, ...]: ...
# Extra methods from old stubs
def __eq__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
def __iter__(self) -> Iterator[IndexIterScalar | tuple[Hashable, ...]]: ...
def __iter__(self) -> Iterator: ...
Copy link
Member

Choose a reason for hiding this comment

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

If we were to type the return type, shouldn't this simply have been Iterator[Hashable] (tuple and everything in IndexIterScalar) is hashable?

(Having mypy_primer could have been helpful to see the false-positives earlier.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we were to type the return type, shouldn't this simply have been Iterator[Hashable] (tuple and everything in IndexIterScalar) is hashable?

(Having mypy_primer could have been helpful to see the false-positives earlier.)

If I change it to Iterator[Hashable], then I get this with the test (and example above) from pyright:

Operator "in" not supported for types "Literal['at']" and "Hashable"
  Operator "in" not supported for types "Literal['at']" and "Hashable"

def __ne__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
def __le__(self, other: Index | Scalar) -> np_ndarray_bool: ... # type: ignore[override]
def __ge__(self, other: Index | Scalar) -> np_ndarray_bool: ... # type: ignore[override]
Expand Down
11 changes: 10 additions & 1 deletion tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1813,7 +1813,6 @@ def test_frame_index_numpy() -> None:


def test_frame_stack() -> None:

multicol2 = pd.MultiIndex.from_tuples([("weight", "kg"), ("height", "m")])
df_multi_level_cols2 = pd.DataFrame(
[[1.0, 2.0], [3.0, 4.0]], index=["cat", "dog"], columns=multicol2
Expand Down Expand Up @@ -2403,3 +2402,13 @@ def get_NDArray(df: pd.DataFrame, key: npt.NDArray[np.uint64]) -> pd.DataFrame:

a: npt.NDArray[np.uint64] = np.array([10, 30], dtype="uint64")
check(assert_type(get_NDArray(df, a), pd.DataFrame), pd.DataFrame)


def test_in_columns() -> None:
# GH 532 (PR)
df = pd.DataFrame(np.random.random((3, 4)), columns=["cat", "dog", "rat", "pig"])
cols = [c for c in df.columns if "at" in c]
check(assert_type(cols, list), list, str)
check(assert_type(df.loc[:, cols], pd.DataFrame), pd.DataFrame)
check(assert_type(df[cols], pd.DataFrame), pd.DataFrame)
check(assert_type(df.groupby(by=cols).sum(), pd.DataFrame), pd.DataFrame)
9 changes: 3 additions & 6 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import datetime as dt
from typing import (
TYPE_CHECKING,
Hashable,
List,
Tuple,
Union,
)
Expand All @@ -29,7 +27,6 @@
if TYPE_CHECKING:
from pandas.core.indexes.numeric import NumericIndex

from pandas._typing import IndexIterScalar
else:
if not PD_LTE_15:
from pandas import Index as NumericIndex
Expand Down Expand Up @@ -108,7 +105,7 @@ def test_column_sequence() -> None:
df = pd.DataFrame([1, 2, 3])
col_list = list(df.columns)
check(
assert_type(col_list, List[Union["IndexIterScalar", Tuple[Hashable, ...]]]),
assert_type(col_list, list),
list,
int,
)
Expand Down Expand Up @@ -684,14 +681,14 @@ def test_sorted_and_list() -> None:
check(
assert_type(
sorted(i1),
List[Union["IndexIterScalar", Tuple[Hashable, ...]]],
list,
),
list,
)
check(
assert_type(
list(i1),
List[Union["IndexIterScalar", Tuple[Hashable, ...]]],
list,
),
list,
)
Expand Down