Skip to content

TYP: use overload to refine return type of set_axis #40197

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 8 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4541,6 +4541,26 @@ def align(
broadcast_axis=broadcast_axis,
)

@overload
def set_axis(
self, labels, axis: Axis = ..., inplace: Literal[False] = ...
) -> DataFrame:
...

@overload
def set_axis(self, labels, axis: Axis, inplace: Literal[True]) -> None:
...

@overload
def set_axis(self, labels, *, inplace: Literal[True]) -> None:
...

@overload
def set_axis(
self, labels, axis: Axis = ..., inplace: bool = ...
) -> Optional[DataFrame]:
...

@Appender(
"""
Examples
Expand Down
25 changes: 25 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Type,
Union,
cast,
overload,
)
import warnings
import weakref
Expand Down Expand Up @@ -162,6 +163,8 @@
from pandas.io.formats.printing import pprint_thing

if TYPE_CHECKING:
from typing import Literal

from pandas._libs.tslibs import BaseOffset

from pandas.core.frame import DataFrame
Expand Down Expand Up @@ -682,6 +685,28 @@ def _obj_with_exclusions(self: FrameOrSeries) -> FrameOrSeries:
""" internal compat with SelectionMixin """
return self

@overload
def set_axis(
self: FrameOrSeries, labels, axis: Axis = ..., inplace: Literal[False] = ...
) -> FrameOrSeries:
...

@overload
def set_axis(
self: FrameOrSeries, labels, axis: Axis, inplace: Literal[True]
) -> None:
...

@overload
def set_axis(self: FrameOrSeries, labels, *, inplace: Literal[True]) -> None:
...

@overload
def set_axis(
self: FrameOrSeries, labels, axis: Axis = ..., inplace: bool = ...
) -> Optional[FrameOrSeries]:
...

def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
"""
Assign desired index to given axis.
Expand Down
23 changes: 23 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Type,
Union,
cast,
overload,
)
import warnings

Expand Down Expand Up @@ -142,6 +143,8 @@
import pandas.plotting

if TYPE_CHECKING:
from typing import Literal

from pandas._typing import (
TimedeltaConvertibleTypes,
TimestampConvertibleTypes,
Expand Down Expand Up @@ -4342,6 +4345,26 @@ def rename(
else:
return self._set_name(index, inplace=inplace)

@overload
def set_axis(
self, labels, axis: Axis = ..., inplace: Literal[False] = ...
) -> Series:
...

@overload
def set_axis(self, labels, axis: Axis, inplace: Literal[True]) -> None:
...

@overload
def set_axis(self, labels, *, inplace: Literal[True]) -> None:
...

@overload
def set_axis(
self, labels, axis: Axis = ..., inplace: bool = ...
) -> Optional[Series]:
...

@Appender(
"""
Examples
Expand Down