Skip to content

make sub of timedelta work #759

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 6 commits into from
Aug 2, 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
10 changes: 10 additions & 0 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,16 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
@overload
def __rxor__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ... # type: ignore[misc]
@overload
def __sub__(
self: Series[Timestamp],
other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64,
) -> TimestampSeries: ...
@overload
def __sub__(
self: Series[Timedelta],
other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64,
) -> TimedeltaSeries: ...
@overload
def __sub__(
self, other: Timestamp | datetime | TimestampSeries
) -> TimedeltaSeries: ...
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ reportPrivateUsage = false
# enable optional checks
reportMissingModuleSource = true
useLibraryCodeForTypes = false
defineConstant = { MYPY_CHECKING = false }

[tool.codespell]
ignore-words-list = "indext, mose, sav, ser"
15 changes: 14 additions & 1 deletion tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
)

if TYPE_CHECKING:
MYPY_CHECKING: bool = True
# See test_sorted_and_list() where mypy and pyright do different
# inference on sorted(pd.Index)
if MYPY_CHECKING:
from typing import Any

from typing_extensions import TypeAlias

SupportsRichComparison: TypeAlias = Any
else:
from _typeshed import SupportsRichComparison # noqa: F401
Copy link
Member

Choose a reason for hiding this comment

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

Do you mind referencing an issue or describing the discrepancy? Is it just that mypy has an older version of typestubs?

Copy link
Member

Choose a reason for hiding this comment

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

Does this also work for poe mypy --mypy_nightly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added comments in 5310eb6

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Does this also work for poe mypy --mypy_nightly?

Looks like it did!

We may want to use this pattern (MYPY_CHECKING) elsewhere when mypy and pyright make different inferences. Can't put it in the stubs themselves - that would require users to define MYPY_CHECKING as false if using pyright, but we can use it in our tests.

from pandas.core.indexes.base import (
_ComplexIndexType,
_FloatIndexType,
Expand Down Expand Up @@ -719,10 +730,12 @@ def test_interval_index_tuples():
def test_sorted_and_list() -> None:
# GH 497
i1 = pd.Index([3, 2, 1])
# mypy infers sorted(i1) as list[Any], while pyright infers sorted(i1) as
# list[SupportsRichComparison]
check(
assert_type(
sorted(i1),
list,
"list[SupportsRichComparison]",
),
list,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ def test_merge_ordered() -> None:
)
pd.merge_ordered( # type: ignore[call-overload]
ls,
rf, # pyright: ignore[reportGeneralTypeIssues]
rf,
left_on="left",
right_on="b",
left_by="left", # pyright: ignore[reportGeneralTypeIssues]
Expand Down
9 changes: 8 additions & 1 deletion tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,14 @@ def test_timedelta64_and_arithmatic_operator() -> None:
s1 = pd.Series(data=pd.date_range("1/1/2020", "2/1/2020"))
s2 = pd.Series(data=pd.date_range("1/1/2021", "2/1/2021"))
s3 = s2 - s1
# https://github.com/pandas-dev/pandas/issues/54059 needs to be fixed
check(assert_type(s3, "TimedeltaSeries"), pd.Series, pd.Timedelta)
td1 = pd.Timedelta(1, "D")
check(assert_type(s2 - td1, "TimestampSeries"), pd.Series, pd.Timestamp)
# GH 758
s4 = s1.astype(object)
check(assert_type(s4 - td1, "TimestampSeries"), pd.Series, pd.Timestamp)

# https://github.com/pandas-dev/pandas/issues/54059 says this is invalid
if PD_LTE_20:
td = np.timedelta64(1, "M")
check(assert_type((s1 - td), "TimestampSeries"), pd.Series, pd.Timestamp)
Expand Down