Skip to content

Commit 1797a49

Browse files
committed
Fix return type of __iter__() and tuple-handling for __getitem__()
1 parent 09553b0 commit 1797a49

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

pandas-stubs/core/frame.pyi

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from typing import (
1111
Mapping,
1212
Pattern,
1313
Sequence,
14+
TypeVar,
1415
overload,
1516
)
1617

@@ -115,6 +116,7 @@ from pandas.plotting import PlotAccessor
115116

116117
_str = str
117118
_bool = bool
119+
_ScalarOrTupleT = TypeVar("_ScalarOrTupleT", bound=Scalar | tuple[Hashable, ...])
118120

119121
class _iLocIndexerFrame(_iLocIndexer):
120122
@overload
@@ -491,21 +493,18 @@ class DataFrame(NDFrame, OpsMixin):
491493
def T(self) -> DataFrame: ...
492494
def __getattr__(self, name: str) -> Series: ...
493495
@overload
494-
def __getitem__(self, idx: Scalar) -> Series: ...
496+
def __getitem__(self, idx: Scalar | tuple[Hashable, ...]) -> Series: ...
495497
@overload
496498
def __getitem__(self, rows: slice) -> DataFrame: ...
497499
@overload
498500
def __getitem__(
499501
self,
500-
idx: tuple
501-
| Series[_bool]
502+
idx: Series[_bool]
502503
| DataFrame
503-
| list[_str]
504-
| list[ScalarT]
505504
| Index
506505
| np_ndarray_str
507506
| np_ndarray_bool
508-
| Sequence[tuple[Scalar, ...]],
507+
| list[_ScalarOrTupleT],
509508
) -> DataFrame: ...
510509
def isetitem(
511510
self, loc: int | Sequence[int], value: Scalar | ArrayLike | list[Any]

pandas-stubs/core/indexes/base.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class Index(IndexOpsMixin, PandasObject):
220220
def shape(self) -> tuple[int, ...]: ...
221221
# Extra methods from old stubs
222222
def __eq__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
223-
def __iter__(self) -> Iterator: ...
223+
def __iter__(self) -> Iterator[Scalar | tuple[Hashable, ...]]: ...
224224
def __ne__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
225225
def __le__(self, other: Index | Scalar) -> np_ndarray_bool: ...
226226
def __ge__(self, other: Index | Scalar) -> np_ndarray_bool: ...

tests/test_frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,9 @@ def test_getmultiindex_columns() -> None:
14651465
res3: pd.DataFrame = df[
14661466
[(i, s) for i in [1] for s in df.columns.get_level_values(1)]
14671467
]
1468-
ndf: pd.DataFrame = df[[df.columns[0]]]
1468+
res4: pd.DataFrame = df[[df.columns[0]]]
1469+
check(assert_type(df[df.columns[0]], pd.Series), pd.Series)
1470+
check(assert_type(df[li[0]], pd.Series), pd.Series)
14691471

14701472

14711473
def test_frame_getitem_isin() -> None:

tests/test_indexes.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
from __future__ import annotations
22

33
import datetime as dt
4-
from typing import Union
4+
from typing import (
5+
Hashable,
6+
List,
7+
Tuple,
8+
Union,
9+
)
510

611
import numpy as np
712
from numpy import typing as npt
813
import pandas as pd
914
from pandas.core.indexes.numeric import NumericIndex
1015
from typing_extensions import assert_type
1116

17+
from pandas._typing import Scalar
18+
1219
from tests import (
1320
check,
1421
pytest_warns_bounded,
@@ -67,6 +74,7 @@ def test_column_getitem() -> None:
6774
df = pd.DataFrame([[1, 2, 3]], columns=["a", "b", "c"])
6875

6976
column = df.columns[0]
77+
check(assert_type(column, Scalar), str)
7078
check(assert_type(df[column], pd.Series), pd.Series, int)
7179

7280

@@ -81,6 +89,12 @@ def test_column_contains() -> None:
8189
length = len(df.columns[df.columns.str.contains("A|B")])
8290

8391

92+
def test_column_sequence() -> None:
93+
df = pd.DataFrame([1, 2, 3])
94+
col_list = list(df.columns)
95+
check(assert_type(col_list, List[Union[Scalar, Tuple[Hashable, ...]]]), list, int)
96+
97+
8498
def test_difference_none() -> None:
8599
# https://github.com/pandas-dev/pandas-stubs/issues/17
86100
ind = pd.Index([1, 2, 3])

0 commit comments

Comments
 (0)