Skip to content

feat(index): append #1282

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 11 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 2 additions & 7 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ from typing import (
ClassVar,
Literal,
TypeAlias,
TypeVar,
final,
overload,
)
Expand Down Expand Up @@ -66,8 +65,6 @@ from pandas._typing import (
type_t,
)

_T_INDEX = TypeVar("_T_INDEX", bound=Index) # ty: ignore[unresolved-reference]

class InvalidIndexError(Exception): ...

class Index(IndexOpsMixin[S1]):
Expand Down Expand Up @@ -408,11 +405,9 @@ class Index(IndexOpsMixin[S1]):
@overload
def append(self, other: Index[S1] | Sequence[Index[S1]]) -> Self: ...
@overload
def append(self, other: Index[S2] | Sequence[Index[S2]]) -> Index[S1 | S2]: ...
@overload
def append(self, other: Sequence[_T_INDEX]) -> Self | _T_INDEX: ...
def append(self, other: Index[S2]) -> Index[S1 | S2]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def append(self, other: Index[S2]) -> Index[S1 | S2]: ...
def append(self, other: Index[S2]) -> Index: ...

I really want to avoid having the union types here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@overload
def append(self, other: Index | Sequence) -> Index: ...
def append(self, other: Sequence[Index]) -> Index: ...
def putmask(self, mask, value): ...
def equals(self, other) -> bool: ...
@final
Expand Down
29 changes: 10 additions & 19 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,31 +1036,31 @@ def test_append_mix() -> None:
second = pd.Index(["a"])
third = pd.Index([1, "a"])
check(assert_type(first.append(second), "pd.Index[int | str]"), pd.Index)
check(assert_type(first.append([second]), "pd.Index[int | str]"), pd.Index)
check(assert_type(first.append([second]), pd.Index), pd.Index)

check(assert_type(first.append(third), "pd.Index[int | str]"), pd.Index) # type: ignore[assert-type]
check(assert_type(first.append([third]), "pd.Index[int | str]"), pd.Index) # type: ignore[assert-type]
check(
assert_type( # type: ignore[assert-type]
first.append([second, third]), # pyright: ignore[reportAssertTypeFailure]
"pd.Index[int | str]",
first.append(third), "pd.Index[int | str]"
),
pd.Index,
)
check(assert_type(first.append([third]), pd.Index), pd.Index)
check(assert_type(first.append([second, third]), pd.Index), pd.Index)

check(assert_type(third.append([]), "pd.Index[int | str]"), pd.Index) # type: ignore[assert-type]
check(
assert_type(third.append(cast("list[Index[Any]]", [])), "pd.Index[int | str]"), # type: ignore[assert-type]
assert_type( # type: ignore[assert-type]
third.append([]), "pd.Index[int | str]"
),
pd.Index,
)
check(assert_type(third.append([first]), "pd.Index[int | str]"), pd.Index) # type: ignore[assert-type]
check(
assert_type( # type: ignore[assert-type]
third.append([first, second]), # pyright: ignore[reportAssertTypeFailure]
"pd.Index[int | str]",
third.append(cast("list[Index[Any]]", [])), "pd.Index[int | str]"
),
pd.Index,
)
check(assert_type(third.append([first]), pd.Index), pd.Index)
check(assert_type(third.append([first, second]), pd.Index), pd.Index)


def test_append_int() -> None:
Expand All @@ -1081,15 +1081,6 @@ def test_append_str() -> None:
check(assert_type(first.append([second]), "pd.Index[str]"), pd.Index, str)


def test_append_list_str() -> None:
"""Test pd.Index[list[str]].append"""
first = pd.Index([["str", "rts"]])
second = pd.Index([["srt", "trs"]])
check(assert_type(first.append([]), "pd.Index[list[str]]"), pd.Index, list)
check(assert_type(first.append(second), "pd.Index[list[str]]"), pd.Index, list)
check(assert_type(first.append([second]), "pd.Index[list[str]]"), pd.Index, list)


def test_range_index_range() -> None:
"""Test that pd.RangeIndex can be initialized from range."""
iri = pd.RangeIndex(range(5))
Expand Down
Loading