[New-bugs-announce] [issue32404] fromtimestamp does not call __new__ in datetime subclasses
Paul Ganssle
report at bugs.python.org
Thu Dec 21 16:54:13 EST 2017
New submission from Paul Ganssle <p.ganssle at gmail.com>:
In writing some tests for the alternate date constructors as part of my PR for issue 32403 (https://bugs.python.org/issue32403), I noticed that for `datetime`, the `fromtimestamp` bypasses the `__new__` call on the subclass:
from datetime import datetime
args = (2003, 4, 14)
ts = 1050292800.0 # Equivalent timestamp
d_ord = 731319 # Equivalent ordinal date
class DatetimeSubclass(datetime):
def __new__(cls, *args, **kwargs):
result = datetime.__new__(cls, *args, **kwargs)
result.extra = 7
return result
base_d = DatetimeSubclass(*args)
assert isinstance(base_d, DatetimeSubclass) # Passes
assert base_d.extra == 7 # Passes
ord_d = DatetimeSubclass.fromordinal(d_ord)
assert isinstance(ord_d, DatetimeSubclass) # Passes
assert ord_d.extra == 7 # Passes
ts_d = DatetimeSubclass.fromtimestamp(ts)
assert isinstance(ts_d, DatetimeSubclass) # Passes
assert ts_d.extra == 7 # Fails
Replacing `datetime` with `date` in the above code we don't get a failure, but with `datetime`, it fails with:
AttributeError: 'DatetimeSubclass' object has no attribute 'extra'
Regardless of the status of 32403, I think this should be fixed (though I can try to fix them both at the same time).
----------
messages: 308908
nosy: belopolsky, p-ganssle
priority: normal
severity: normal
status: open
title: fromtimestamp does not call __new__ in datetime subclasses
versions: Python 3.6, Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32404>
_______________________________________
More information about the New-bugs-announce
mailing list