Skip to content

TYP fixup overloads for reset_index #40200

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 6 commits into from
Mar 10, 2021
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
51 changes: 46 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5145,9 +5145,7 @@ def set_index(
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]
def reset_index(
self,
level: Optional[Union[Hashable, Sequence[Hashable]]] = ...,
drop: bool = ...,
Expand All @@ -5157,15 +5155,58 @@ def reset_index( # type: ignore[misc]
) -> DataFrame:
...

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

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

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

@overload
def reset_index(
self,
*,
inplace: Literal[True],
col_level: Hashable = ...,
col_fill: Hashable = ...,
) -> None:
...
Copy link
Member

@simonjayhawkins simonjayhawkins Mar 9, 2021

Choose a reason for hiding this comment

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

Is the last overload needed now? won't it fall back to the function definition if no overloads match?

updated: comment should have been on the next one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Pretty sure it's needed, see here for a toy example (uncomment the commented-out part and it'll pass)

Copy link
Member Author

Choose a reason for hiding this comment

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

Commenting out the last overload leads to the same errors observed on master in the typing test from #40202 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

ah ok, and Revealed type is 'Any'. I thought that it fell back to the non-overloaded function with no matching overloads. maybe something changed in mypy or only does so under different conditions.

Copy link
Member Author

Choose a reason for hiding this comment

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

only does so under different conditions

If I've understood correctly, this is it - see #39501 (comment)


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

def reset_index(
Expand Down