Improve __sub__ and __add__ for datetime and date (#6323)

This commit is contained in:
Alex Waygood
2021-11-18 05:37:41 +00:00
committed by GitHub
parent 48cfe5d2f4
commit f6702e3871

View File

@@ -1,9 +1,10 @@
import sys
from time import struct_time
from typing import ClassVar, NamedTuple, SupportsAbs, Type, TypeVar, overload
from typing import ClassVar, NamedTuple, NoReturn, SupportsAbs, Type, TypeVar, overload
from typing_extensions import final
_S = TypeVar("_S")
_D = TypeVar("_D", bound=date)
MINYEAR: int
MAXYEAR: int
@@ -68,13 +69,22 @@ class date:
if sys.version_info >= (3, 8):
def __add__(self: _S, other: timedelta) -> _S: ...
def __radd__(self: _S, other: timedelta) -> _S: ...
@overload
def __sub__(self: _D, other: timedelta) -> _D: ...
@overload
def __sub__(self, other: datetime) -> NoReturn: ...
@overload
def __sub__(self: _D, other: _D) -> timedelta: ...
else:
# Prior to Python 3.8, arithmetic operations always returned `date`, even in subclasses
def __add__(self, other: timedelta) -> date: ...
def __radd__(self, other: timedelta) -> date: ...
@overload
def __sub__(self, other: timedelta) -> date: ...
@overload
def __sub__(self, other: date) -> timedelta: ...
@overload
def __sub__(self, other: timedelta) -> date: ...
@overload
def __sub__(self, other: datetime) -> NoReturn: ...
@overload
def __sub__(self, other: date) -> timedelta: ...
def __hash__(self) -> int: ...
def weekday(self) -> int: ...
def isoweekday(self) -> int: ...
@@ -267,13 +277,19 @@ class datetime(date):
def __lt__(self, other: datetime) -> bool: ... # type: ignore
def __ge__(self, other: datetime) -> bool: ... # type: ignore
def __gt__(self, other: datetime) -> bool: ... # type: ignore
if sys.version_info < (3, 8):
if sys.version_info >= (3, 8):
@overload # type: ignore[override]
def __sub__(self: _D, other: timedelta) -> _D: ...
@overload
def __sub__(self: _D, other: _D) -> timedelta: ...
else:
# Prior to Python 3.8, arithmetic operations always returned `datetime`, even in subclasses
def __add__(self, other: timedelta) -> datetime: ...
def __radd__(self, other: timedelta) -> datetime: ...
@overload # type: ignore
def __sub__(self, other: datetime) -> timedelta: ...
@overload
def __sub__(self, other: timedelta) -> datetime: ...
@overload # type: ignore[override]
def __sub__(self, other: datetime) -> timedelta: ...
@overload
def __sub__(self, other: timedelta) -> datetime: ...
if sys.version_info >= (3, 9):
def isocalendar(self) -> _IsoCalendarDate: ...
else: