Skip to content

Commit 45e0ad3

Browse files
committed
1 parent 3a34057 commit 45e0ad3

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ from typing_extensions import (
4343
from pandas._libs.interval import _OrderableT
4444
from pandas._typing import (
4545
S1,
46+
S2,
4647
AnyAll,
4748
AxesData,
4849
DropKeep,
@@ -402,7 +403,9 @@ class Index(IndexOpsMixin[S1]):
402403
@overload
403404
def __getitem__(self, idx: int | tuple[np_ndarray_anyint, ...]) -> S1: ...
404405
@overload
405-
def append(self, other: Index[S1] | Sequence[Index[S1]]) -> Index[S1]: ...
406+
def append(self, other: Index[S1] | Sequence[Index[S1]]) -> Self: ...
407+
@overload
408+
def append(self, other: Index[S2] | Sequence[Index[S2]]) -> Index[S1 | S2]: ...
406409
@overload
407410
def append(self, other: Index | Sequence) -> Index: ...
408411
def putmask(self, mask, value): ...

tests/test_indexes.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import datetime as dt
44
from typing import (
55
TYPE_CHECKING,
6+
Any,
67
Union,
8+
cast,
79
)
810

911
import numpy as np
@@ -1028,20 +1030,37 @@ def test_getitem() -> None:
10281030
check(assert_type(i0[[0, 2]], "pd.Index[str]"), pd.Index, str)
10291031

10301032

1031-
def test_append_any() -> None:
1033+
def test_append_mix() -> None:
10321034
"""Test pd.Index.append that gives pd.Index[Any]"""
10331035
first = pd.Index([1])
10341036
second = pd.Index(["a"])
10351037
third = pd.Index([1, "a"])
1036-
check(assert_type(first.append(second), pd.Index), pd.Index)
1037-
check(assert_type(first.append([second]), pd.Index), pd.Index)
1038+
check(assert_type(first.append(second), "pd.Index[int | str]"), pd.Index)
1039+
check(assert_type(first.append([second]), "pd.Index[int | str]"), pd.Index)
10381040

1039-
check(assert_type(first.append(third), pd.Index), pd.Index)
1040-
check(assert_type(first.append([third]), pd.Index), pd.Index)
1041-
check(assert_type(first.append([second, third]), pd.Index), pd.Index)
1041+
check(assert_type(first.append(third), "pd.Index[int | str]"), pd.Index) # type: ignore[assert-type]
1042+
check(assert_type(first.append([third]), "pd.Index[int | str]"), pd.Index) # type: ignore[assert-type]
1043+
check(
1044+
assert_type( # type: ignore[assert-type]
1045+
first.append([second, third]), # pyright: ignore[reportAssertTypeFailure]
1046+
"pd.Index[int | str]",
1047+
),
1048+
pd.Index,
1049+
)
10421050

1043-
check(assert_type(third.append([]), "pd.Index[str | int]"), pd.Index) # type: ignore[assert-type]
1044-
check(assert_type(third.append([first]), pd.Index), pd.Index)
1051+
check(assert_type(third.append([]), "pd.Index[int | str]"), pd.Index) # type: ignore[assert-type]
1052+
check(
1053+
assert_type(third.append(cast("list[Index[Any]]", [])), "pd.Index[int | str]"), # type: ignore[assert-type]
1054+
pd.Index,
1055+
)
1056+
check(assert_type(third.append([first]), "pd.Index[int | str]"), pd.Index) # type: ignore[assert-type]
1057+
check(
1058+
assert_type( # type: ignore[assert-type]
1059+
third.append([first, second]), # pyright: ignore[reportAssertTypeFailure]
1060+
"pd.Index[int | str]",
1061+
),
1062+
pd.Index,
1063+
)
10451064

10461065

10471066
def test_append_int() -> None:

0 commit comments

Comments
 (0)