Skip to content

TYP: sort_index #46300

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 12 commits into from
Mar 18, 2022
15 changes: 9 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Iterator,
Literal,
Sequence,
TypeVar,
cast,
overload,
)
Expand Down Expand Up @@ -222,6 +223,8 @@

from pandas.io.formats.style import Styler

DataFrameT = TypeVar("DataFrameT", bound="DataFrame")

# ---------------------------------------------------------------------
# Docstring templates

Expand Down Expand Up @@ -6446,7 +6449,7 @@ def sort_index(

@overload
def sort_index(
self,
self: DataFrameT,
Copy link
Member Author

Choose a reason for hiding this comment

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

as far as I remember, pyright doesn't like self annotations that are broader than the class itself (cannot use NDFrameT).

*,
axis: Axis = ...,
level: Level | None = ...,
Expand All @@ -6457,12 +6460,12 @@ def sort_index(
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> DataFrame:
) -> DataFrameT:
...

@overload
def sort_index(
self,
self: DataFrameT,
*,
axis: Axis = ...,
level: Level | None = ...,
Expand All @@ -6473,13 +6476,13 @@ def sort_index(
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> DataFrame | None:
) -> DataFrameT | None:
...

# error: Signature of "sort_index" incompatible with supertype "NDFrame"
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
def sort_index( # type: ignore[override]
self,
self: DataFrameT,
axis: Axis = 0,
level: Level | None = None,
ascending: bool | int | Sequence[bool | int] = True,
Expand All @@ -6489,7 +6492,7 @@ def sort_index( # type: ignore[override]
sort_remaining: bool = True,
ignore_index: bool = False,
key: IndexKeyFunc = None,
) -> DataFrame | None:
) -> DataFrameT | None:
"""
Sort object by labels (along an axis).

Expand Down
15 changes: 9 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Iterable,
Literal,
Sequence,
TypeVar,
Union,
cast,
overload,
Expand Down Expand Up @@ -162,6 +163,8 @@
from pandas.core.groupby.generic import SeriesGroupBy
from pandas.core.resample import Resampler

SeriesT = TypeVar("SeriesT", bound="Series")

__all__ = ["Series"]

_shared_doc_kwargs = {
Expand Down Expand Up @@ -3597,7 +3600,7 @@ def sort_index(

@overload
def sort_index(
self,
self: SeriesT,
*,
axis: Literal[0] = ...,
level: Level | None = ...,
Expand All @@ -3608,12 +3611,12 @@ def sort_index(
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> Series:
) -> SeriesT:
...

@overload
def sort_index(
self,
self: SeriesT,
*,
axis: Literal[0] = ...,
level: Level | None = ...,
Expand All @@ -3624,14 +3627,14 @@ def sort_index(
sort_remaining: bool = ...,
ignore_index: bool = ...,
key: IndexKeyFunc = ...,
) -> Series | None:
) -> SeriesT | None:
...

# error: Argument 1 of "sort_index" is incompatible with supertype "NDFrame";
# supertype defines the argument type as "Union[str, int]"
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
def sort_index( # type: ignore[override]
self,
self: SeriesT,
axis: Literal[0] = 0,
level: Level | None = None,
ascending: bool | int | Sequence[bool | int] = True,
Expand All @@ -3641,7 +3644,7 @@ def sort_index( # type: ignore[override]
sort_remaining: bool = True,
ignore_index: bool = False,
key: IndexKeyFunc = None,
):
) -> SeriesT | None:
"""
Sort Series by index labels.

Expand Down