Skip to content

TYP: use overload to refine return type of reset_index #37137

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 1 commit into from
Oct 23, 2020
Merged
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
29 changes: 27 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
Type,
Union,
cast,
overload,
)
import warnings

Expand Down Expand Up @@ -155,6 +156,8 @@
import pandas.plotting

if TYPE_CHECKING:
from typing import Literal

from pandas.core.groupby.generic import DataFrameGroupBy

from pandas.io.formats.style import Styler
Expand Down Expand Up @@ -4692,6 +4695,30 @@ def set_index(
if not inplace:
return frame

@overload
# https://github.com/python/mypy/issues/6580
# Overloaded function signatures 1 and 2 overlap with incompatible return types
def reset_index( # type: ignore[misc]
self,
level: Optional[Union[Hashable, Sequence[Hashable]]] = ...,
drop: bool = ...,
inplace: Literal[False] = ...,
col_level: Hashable = ...,
col_fill: Label = ...,
) -> DataFrame:
...

@overload
def reset_index(
self,
level: Optional[Union[Hashable, Sequence[Hashable]]] = ...,
drop: bool = ...,
inplace: Literal[True] = ...,
col_level: Hashable = ...,
col_fill: Label = ...,
) -> None:
...

def reset_index(
self,
level: Optional[Union[Hashable, Sequence[Hashable]]] = None,
Expand Down Expand Up @@ -7156,8 +7183,6 @@ def explode(
raise ValueError("columns must be unique")

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