Skip to content

Commit 8957328

Browse files
authored
make sub of timedelta work (#759)
* make sub of timedelta work * fix issues due to pyright 1.1.320 * fix for older python with list subscript * change order of test so ruff does not remove import from typeshed * get ruff to ignore a line * add comments about why testing is special for mypy vs pyright
1 parent 6769e81 commit 8957328

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

pandas-stubs/core/series.pyi

+10
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,16 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
15331533
@overload
15341534
def __rxor__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # type: ignore[misc]
15351535
@overload
1536+
def __sub__(
1537+
self: Series[Timestamp],
1538+
other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64,
1539+
) -> TimestampSeries: ...
1540+
@overload
1541+
def __sub__(
1542+
self: Series[Timedelta],
1543+
other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64,
1544+
) -> TimedeltaSeries: ...
1545+
@overload
15361546
def __sub__(
15371547
self, other: Timestamp | datetime | TimestampSeries
15381548
) -> TimedeltaSeries: ...

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ reportPrivateUsage = false
197197
# enable optional checks
198198
reportMissingModuleSource = true
199199
useLibraryCodeForTypes = false
200+
defineConstant = { MYPY_CHECKING = false }
200201

201202
[tool.codespell]
202203
ignore-words-list = "indext, mose, sav, ser"

tests/test_indexes.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
)
2525

2626
if TYPE_CHECKING:
27+
MYPY_CHECKING: bool = True
28+
# See test_sorted_and_list() where mypy and pyright do different
29+
# inference on sorted(pd.Index)
30+
if MYPY_CHECKING:
31+
from typing import Any
32+
33+
from typing_extensions import TypeAlias
34+
35+
SupportsRichComparison: TypeAlias = Any
36+
else:
37+
from _typeshed import SupportsRichComparison # noqa: F401
2738
from pandas.core.indexes.base import (
2839
_ComplexIndexType,
2940
_FloatIndexType,
@@ -719,10 +730,12 @@ def test_interval_index_tuples():
719730
def test_sorted_and_list() -> None:
720731
# GH 497
721732
i1 = pd.Index([3, 2, 1])
733+
# mypy infers sorted(i1) as list[Any], while pyright infers sorted(i1) as
734+
# list[SupportsRichComparison]
722735
check(
723736
assert_type(
724737
sorted(i1),
725-
list,
738+
"list[SupportsRichComparison]",
726739
),
727740
list,
728741
)

tests/test_pandas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ def test_merge_ordered() -> None:
12721272
)
12731273
pd.merge_ordered( # type: ignore[call-overload]
12741274
ls,
1275-
rf, # pyright: ignore[reportGeneralTypeIssues]
1275+
rf,
12761276
left_on="left",
12771277
right_on="b",
12781278
left_by="left", # pyright: ignore[reportGeneralTypeIssues]

tests/test_timefuncs.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,14 @@ def test_timedelta64_and_arithmatic_operator() -> None:
11301130
s1 = pd.Series(data=pd.date_range("1/1/2020", "2/1/2020"))
11311131
s2 = pd.Series(data=pd.date_range("1/1/2021", "2/1/2021"))
11321132
s3 = s2 - s1
1133-
# https://github.com/pandas-dev/pandas/issues/54059 needs to be fixed
1133+
check(assert_type(s3, "TimedeltaSeries"), pd.Series, pd.Timedelta)
1134+
td1 = pd.Timedelta(1, "D")
1135+
check(assert_type(s2 - td1, "TimestampSeries"), pd.Series, pd.Timestamp)
1136+
# GH 758
1137+
s4 = s1.astype(object)
1138+
check(assert_type(s4 - td1, "TimestampSeries"), pd.Series, pd.Timestamp)
1139+
1140+
# https://github.com/pandas-dev/pandas/issues/54059 says this is invalid
11341141
if PD_LTE_20:
11351142
td = np.timedelta64(1, "M")
11361143
check(assert_type((s1 - td), "TimestampSeries"), pd.Series, pd.Timestamp)

0 commit comments

Comments
 (0)