Skip to content

Commit 1c75df6

Browse files
committed
fix: constraint instead of bound to get rid of Any python/mypy#19525 (comment)
1 parent fe5f1a9 commit 1c75df6

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

‎pandas-stubs/_typing.pyi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,26 @@ S1 = TypeVar("S1", bound=SeriesDType, default=Any)
847847
S2 = TypeVar("S2", bound=SeriesDType)
848848
S3 = TypeVar("S3", bound=SeriesDType)
849849

850+
# Constraint, instead of bound
851+
C2 = TypeVar(
852+
"C2",
853+
str,
854+
bytes,
855+
datetime.date,
856+
datetime.time,
857+
bool,
858+
int,
859+
float,
860+
complex,
861+
Dtype,
862+
datetime.datetime, # includes pd.Timestamp
863+
datetime.timedelta, # includes pd.Timedelta
864+
Period,
865+
Interval,
866+
CategoricalDtype,
867+
BaseOffset,
868+
)
869+
850870
IndexingInt: TypeAlias = (
851871
int | np.int_ | np.integer | np.unsignedinteger | np.signedinteger | np.int8
852872
)

‎pandas-stubs/core/indexes/base.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ from typing_extensions import (
4242

4343
from pandas._libs.interval import _OrderableT
4444
from pandas._typing import (
45+
C2,
4546
S1,
4647
AnyAll,
4748
AxesData,
@@ -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]]) -> Self: ...
406+
def append(
407+
self: Index[C2], other: Index[C2] | Sequence[Index[C2]]
408+
) -> Index[C2]: ...
406409
@overload
407410
def append(self, other: Index | Sequence[Index]) -> Index: ...
408411
def putmask(self, mask, value): ...

‎tests/test_indexes.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,20 +1042,8 @@ def test_append_mix() -> None:
10421042
check(assert_type(first.append([third]), pd.Index), pd.Index)
10431043
check(assert_type(first.append([second, third]), pd.Index), pd.Index)
10441044

1045-
# mypy thinks the following two are of the type `Index[Any]`, whereas
1046-
# pyright recognises them as `Index[int | str]`
1047-
check(
1048-
assert_type( # type: ignore[assert-type]
1049-
third.append([]), "pd.Index[int | str]"
1050-
),
1051-
pd.Index,
1052-
)
1053-
check(
1054-
assert_type( # type: ignore[assert-type]
1055-
third.append(cast("list[Index[Any]]", [])), "pd.Index[int | str]"
1056-
),
1057-
pd.Index,
1058-
)
1045+
check(assert_type(third.append([]), pd.Index), pd.Index)
1046+
check(assert_type(third.append(cast("list[Index[Any]]", [])), pd.Index), pd.Index)
10591047
check(assert_type(third.append([first]), pd.Index), pd.Index)
10601048
check(assert_type(third.append([first, second]), pd.Index), pd.Index)
10611049

0 commit comments

Comments
 (0)