Skip to content
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

Add datetime.date and datetime.datetime specializations #1003

Merged
merged 4 commits into from
Oct 14, 2024
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
21 changes: 14 additions & 7 deletions pandas-stubs/_libs/tslibs/offsets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ from typing import (

from dateutil.relativedelta import weekday as WeekdayClass
import numpy as np
from pandas import Timestamp
from pandas.core.indexes.datetimes import DatetimeIndex
from typing_extensions import Self

Expand All @@ -22,7 +23,7 @@ from pandas._typing import npt

from pandas.tseries.holiday import AbstractHolidayCalendar

_DatetimeT = TypeVar("_DatetimeT", bound=date)
_DatetimeT = TypeVar("_DatetimeT", bound=datetime)
_TimedeltaT = TypeVar("_TimedeltaT", bound=timedelta)

prefix_mapping: dict[str, type]
Expand All @@ -42,26 +43,32 @@ class BaseOffset:
@overload
def __add__(self, other: npt.NDArray[np.object_]) -> npt.NDArray[np.object_]: ...
@overload
def __add__(self, other: BaseOffset) -> Self: ...
def __add__(self, other: _DatetimeT) -> _DatetimeT: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __add__(self, other: date) -> Timestamp: ...
@overload
def __add__(self, other: _DatetimeT) -> _DatetimeT: ...
def __add__(self, other: BaseOffset) -> Self: ...
@overload
def __add__(self, other: _TimedeltaT) -> _TimedeltaT: ...
@overload
def __radd__(self, other: npt.NDArray[np.object_]) -> npt.NDArray[np.object_]: ...
@overload
def __radd__(self, other: BaseOffset) -> Self: ...
def __radd__(self, other: _DatetimeT) -> _DatetimeT: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __radd__(self, other: _DatetimeT) -> _DatetimeT: ...
def __radd__(self, other: date) -> Timestamp: ...
@overload
def __radd__(self, other: BaseOffset) -> Self: ...
@overload
def __radd__(self, other: _TimedeltaT) -> _TimedeltaT: ...
def __sub__(self, other: BaseOffset) -> Self: ...
@overload
def __rsub__(self, other: npt.NDArray[np.object_]) -> npt.NDArray[np.object_]: ...
@overload
def __rsub__(self, other: BaseOffset) -> Self: ...
def __rsub__(self, other: _DatetimeT) -> _DatetimeT: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __rsub__(self, other: _DatetimeT) -> _DatetimeT: ...
def __rsub__(self, other: date) -> Timestamp: ...
@overload
def __rsub__(self, other: BaseOffset) -> Self: ...
@overload
def __rsub__(self, other: _TimedeltaT) -> _TimedeltaT: ...
def __call__(self, other): ...
Expand Down
9 changes: 7 additions & 2 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,13 @@ def test_some_offsets() -> None:
),
pd.DatetimeIndex,
)
# GH 224
check(assert_type(dt.date.today() - Day(), dt.date), dt.date)
# GH 755
check(assert_type(dt.date.today() - Day(), pd.Timestamp), pd.Timestamp)
check(assert_type(dt.date.today() + Day(), pd.Timestamp), pd.Timestamp)
check(assert_type(Day() + dt.date.today(), pd.Timestamp), pd.Timestamp)
check(assert_type(dt.datetime.now() - Day(), dt.datetime), dt.datetime)
check(assert_type(dt.datetime.now() + Day(), dt.datetime), dt.datetime)
check(assert_type(Day() + dt.datetime.now(), dt.datetime), dt.datetime)
# GH 235
check(
assert_type(
Expand Down
Loading