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 4 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
58 changes: 47 additions & 11 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5119,9 +5119,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 @@ -5134,22 +5132,60 @@ def reset_index( # type: ignore[misc]
@overload
def reset_index(
self,
level: Optional[Union[Hashable, Sequence[Hashable]]] = ...,
drop: bool = ...,
inplace: Literal[True] = ...,
level: Optional[Union[Hashable, Sequence[Hashable]]],
drop: bool,
inplace: Literal[True],
col_level: Hashable = ...,
col_fill: Hashable = ...,
) -> None:
...

@overload
def reset_index(
self,
level: Optional[Union[Hashable, Sequence[Hashable]]] = None,
drop: bool = False,
inplace: bool = False,
col_level: Hashable = 0,
col_fill: Hashable = "",
*,
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: bool = ...,
col_level: Hashable = ...,
col_fill: Hashable = ...,
) -> Optional[DataFrame]:
...

def reset_index(
self, level=None, drop=False, inplace=False, col_level=0, col_fill=""
Copy link
Member

Choose a reason for hiding this comment

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

removing the type annotations from the function means that we lose the type checking for the body of the function.

so if you reveal_type on say level you will get Revealed type is 'Any'

IIUC correctly, the type annotations on the function are used to check the function itself, while the overloads are used when checking other functions that call this one.

so both are needed.

):
"""
Reset the index, or a level of it.

Expand Down