8000 TYP: use overload to refine return type of set_axis by MarcoGorelli · Pull Request #40197 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

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 1 commit
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
Next Next commit
try typing set_axis
  • Loading branch information
MarcoGorelli committed Mar 2, 2021
commit 3ffbc78ab3f12bb702f1aa77bf165732e636fd35
23 changes: 21 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4407,6 +4407,18 @@ def align(
broadcast_axis=broadcast_axis,
)

@overload
# https://github.com/python/mypy/issues/6580
# Overloaded function signatures 1 and 2 overlap with incompatible return types
def set_axis( # type: ignore[misc]
self, labels, axis: Axis = ..., inplace: Literal[False] = ...
) -> DataFrame:
...

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

@Appender(
"""
Examples
Expand Down Expand Up @@ -4446,8 +4458,15 @@ def align(
see_also_sub=" or columns",
)
@Appender(NDFrame.set_axis.__doc__)
def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
return super().set_axis(labels, axis=axis, inplace=inplace)
# Signature of "set_axis" incompatible with supertype "NDFrame"
def set_axis( # type: ignore[override]
self, labels, axis: Axis = 0, inplace: bool = False
) -> Optional[DataFrame]:
# No overload variant of "set_axis" of "NDFrame" matches argument types "Any",
# "Union[str, int]", "bool"
return super().set_axis( # type: ignore[call-overload]
labels, axis=axis, inplace=inplace
)

@Substitution(**_shared_doc_kwargs)
@Appender(NDFrame.reindex.__doc__)
Expand Down
21 changes: 20 additions & 1 deletion 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 @@ -159,6 +160,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 @@ -662,7 +665,23 @@ def _obj_with_exclusions(self: FrameOrSeries) -> FrameOrSeries:
""" internal compat with SelectionMixin """
return self

def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
@overload
# https://github.com/python/mypy/issues/6580
# Overloaded function signatures 1 and 2 overlap with incompatible return types
def set_axis( # type: ignore[misc]
self: FrameOrSeries, labels, axis: Axis = ..., inplace: Literal[False] = ...
) -> FrameOrSeries:
...

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

def set_axis(
self: FrameOrSeries, labels, axis: Axis = 0, inplace: bool_t = False
) -> Optional[FrameOrSeries]:
"""
Assign desired index to given axis.

Expand Down
26 changes: 24 additions & 2 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 @@ -138,6 +139,8 @@
import pandas.plotting

if TYPE_CHECKING:
from typing import Literal

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

@overload
# https://github.com/python/mypy/issues/6580
# Overloaded function signatures 1 and 2 overlap with incompatible return types
def set_axis( # type: ignore[misc]
self, labels, axis: Axis = ..., inplace: Literal[False] = ...
) -> Series:
...

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

@Appender(
"""
Examples
Expand All @@ -4319,8 +4334,15 @@ def rename(
see_also_sub="",
)
@Appender(generic.NDFrame.set_axis.__doc__)
def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
return super().set_axis(labels, axis=axis, inplace=inplace)
# Signature of "set_axis" incompatible with supertype "NDFrame"
def set_axis( # type: ignore[override]
self, labels, axis: Axis = 0, inplace: bool = False
) -> Optional[Series]:
# No overload variant of "set_axis" of "NDFrame" matches argument types "Any",
# "Union[str, int]", "bool"
return super().set_axis( # type: ignore[call-overload]
labels, axis=axis, inplace=inplace
)

@doc(
NDFrame.reindex,
Expand Down
0