mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
bpo-32417: Make timedelta arithmetic respect subclasses (#10902)
* Make timedelta return subclass types Previously timedelta would always return the `date` and `datetime` types, regardless of what it is added to. This makes it return an object of the type it was added to. * Add tests for timedelta arithmetic on subclasses * Make pure python timedelta return subclass types * Add test for fromtimestamp with tz argument * Add tests for subclass behavior in now * Add news entry. Fixes: bpo-32417 bpo-35364 * More descriptive variable names in tests Addresses Victor's comments
This commit is contained in:
parent
ca7d2933a3
commit
89427cd0fe
4 changed files with 90 additions and 19 deletions
|
@ -1014,7 +1014,7 @@ class date:
|
|||
if isinstance(other, timedelta):
|
||||
o = self.toordinal() + other.days
|
||||
if 0 < o <= _MAXORDINAL:
|
||||
return date.fromordinal(o)
|
||||
return type(self).fromordinal(o)
|
||||
raise OverflowError("result out of range")
|
||||
return NotImplemented
|
||||
|
||||
|
@ -2024,10 +2024,10 @@ class datetime(date):
|
|||
hour, rem = divmod(delta.seconds, 3600)
|
||||
minute, second = divmod(rem, 60)
|
||||
if 0 < delta.days <= _MAXORDINAL:
|
||||
return datetime.combine(date.fromordinal(delta.days),
|
||||
time(hour, minute, second,
|
||||
delta.microseconds,
|
||||
tzinfo=self._tzinfo))
|
||||
return type(self).combine(date.fromordinal(delta.days),
|
||||
time(hour, minute, second,
|
||||
delta.microseconds,
|
||||
tzinfo=self._tzinfo))
|
||||
raise OverflowError("result out of range")
|
||||
|
||||
__radd__ = __add__
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue