Skip to content

Allow sorted() to work on Index #501

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 4 commits into from
Jan 8, 2023
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
7 changes: 5 additions & 2 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ ListLikeExceptSeriesAndStr = TypeVar(
)
ListLikeU: TypeAlias = Union[Sequence, np.ndarray, Series, Index]
StrLike: TypeAlias = Union[str, np.str_]
Scalar: TypeAlias = Union[
IndexIterScalar: TypeAlias = Union[
str,
bytes,
datetime.date,
Expand All @@ -174,10 +174,13 @@ Scalar: TypeAlias = Union[
bool,
int,
float,
complex,
Timestamp,
Timedelta,
]
Scalar: TypeAlias = Union[
IndexIterScalar,
complex,
]
ScalarT = TypeVar("ScalarT", bound=Scalar)
# Refine the definitions below in 3.9 to use the specialized type.
np_ndarray_int64: TypeAlias = npt.NDArray[np.int64]
Expand Down
3 changes: 2 additions & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ from pandas._typing import (
DtypeObj,
FillnaOptions,
HashableT,
IndexIterScalar,
IndexT,
Label,
Level,
Expand Down Expand Up @@ -222,7 +223,7 @@ class Index(IndexOpsMixin, PandasObject):
def shape(self) -> tuple[int, ...]: ...
# Extra methods from old stubs
def __eq__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
def __iter__(self) -> Iterator[Scalar | tuple[Hashable, ...]]: ...
def __iter__(self) -> Iterator[IndexIterScalar | tuple[Hashable, ...]]: ...
def __ne__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
def __le__(self, other: Index | Scalar) -> np_ndarray_bool: ...
def __ge__(self, other: Index | Scalar) -> np_ndarray_bool: ...
Expand Down
29 changes: 28 additions & 1 deletion tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime as dt
from typing import (
TYPE_CHECKING,
Hashable,
List,
Tuple,
Expand All @@ -16,6 +17,9 @@

from pandas._typing import Scalar

if TYPE_CHECKING:
from pandas._typing import IndexIterScalar

from tests import (
check,
pytest_warns_bounded,
Expand Down Expand Up @@ -92,7 +96,11 @@ def test_column_contains() -> None:
def test_column_sequence() -> None:
df = pd.DataFrame([1, 2, 3])
col_list = list(df.columns)
check(assert_type(col_list, List[Union[Scalar, Tuple[Hashable, ...]]]), list, int)
check(
assert_type(col_list, List[Union["IndexIterScalar", Tuple[Hashable, ...]]]),
Copy link
Member

Choose a reason for hiding this comment

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

Other tests (test_series.py) import from __future__ import annotations to use tuple/list.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It won't work at runtime on python 3.8. CI failed for me when I did that. You can do foo: list[int] and that will be accepted, but if you are doing assert_type(col_list, list[Union["IndexIterScalar", tuple[Hashable, ...]]]) that will fail at runtime on 3.8 because the runtime for 3.8 can't parse something like list[int] when it is not a typing annotation.

list,
int,
)


def test_difference_none() -> None:
Expand Down Expand Up @@ -657,3 +665,22 @@ def test_interval_index_tuples():
pd.IntervalIndex,
pd.Interval,
)


def test_sorted_and_list() -> None:
# GH 497
i1 = pd.Index([3, 2, 1])
check(
assert_type(
sorted(i1),
List[Union["IndexIterScalar", Tuple[Hashable, ...]]],
),
list,
)
check(
assert_type(
list(i1),
List[Union["IndexIterScalar", Tuple[Hashable, ...]]],
),
list,
)