Skip to content

Commit a8f5477

Browse files
TYP: use overload to refine return type of reset_index (#37137)
1 parent 9fd30b7 commit a8f5477

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

pandas/core/frame.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
Type,
3535
Union,
3636
cast,
37+
overload,
3738
)
3839
import warnings
3940

@@ -155,6 +156,8 @@
155156
import pandas.plotting
156157

157158
if TYPE_CHECKING:
159+
from typing import Literal
160+
158161
from pandas.core.groupby.generic import DataFrameGroupBy
159162

160163
from pandas.io.formats.style import Styler
@@ -4706,6 +4709,30 @@ def set_index(
47064709
if not inplace:
47074710
return frame
47084711

4712+
@overload
4713+
# https://github.com/python/mypy/issues/6580
4714+
# Overloaded function signatures 1 and 2 overlap with incompatible return types
4715+
def reset_index( # type: ignore[misc]
4716+
self,
4717+
level: Optional[Union[Hashable, Sequence[Hashable]]] = ...,
4718+
drop: bool = ...,
4719+
inplace: Literal[False] = ...,
4720+
col_level: Hashable = ...,
4721+
col_fill: Label = ...,
4722+
) -> DataFrame:
4723+
...
4724+
4725+
@overload
4726+
def reset_index(
4727+
self,
4728+
level: Optional[Union[Hashable, Sequence[Hashable]]] = ...,
4729+
drop: bool = ...,
4730+
inplace: Literal[True] = ...,
4731+
col_level: Hashable = ...,
4732+
col_fill: Label = ...,
4733+
) -> None:
4734+
...
4735+
47094736
def reset_index(
47104737
self,
47114738
level: Optional[Union[Hashable, Sequence[Hashable]]] = None,
@@ -7185,8 +7212,6 @@ def explode(
71857212
raise ValueError("columns must be unique")
71867213

71877214
df = self.reset_index(drop=True)
7188-
# TODO: use overload to refine return type of reset_index
7189-
assert df is not None # needed for mypy
71907215
result = df[column].explode()
71917216
result = df.drop([column], axis=1).join(result)
71927217
if ignore_index:

0 commit comments

Comments
 (0)