This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Andrew.Lutomirski
Recipients Andrew.Lutomirski
Date 2014-01-23.19:47:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1390506444.15.0.286575412745.issue20371@psf.upfronthosting.co.za>
In-reply-to
Content
I'll admit that what I'm doing is possibly unhealthy.  Nonetheless, I find this behavior *extremely* surprising.  This code:

--- start code ---

import datetime

class my_dt(datetime.datetime):
    def __new__(cls, *args, **kwargs):
        print('In my_dt.__new__')
        return datetime.datetime.__new__(cls, *args, **kwargs)

    def __init__(self, *args, **kwargs):
        print('In my_dt.__init__')
        super(my_dt, self).__init__()

dt = datetime.datetime.now()

print('Create a my_dt')
t = my_dt(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)


print('Calling replace')
t2 = t.replace(tzinfo=None)
print('Got a %r' % type(t2))

--- end code ---

results in:

Create a my_dt
In my_dt.__new__
In my_dt.__init__
Calling replace
Got a <class '__main__.my_dt'>

So datetime.datetime.replace will create an object of type my_dt *without calling __new__ or __init__*.  This should (AFAIK) be impossible.

I think that datetime.datetime.replace should either return an actual datetime object or that it should invoke __new__ and probably __init__.
History
Date User Action Args
2014-01-23 19:47:24Andrew.Lutomirskisetrecipients: + Andrew.Lutomirski
2014-01-23 19:47:24Andrew.Lutomirskisetmessageid: <1390506444.15.0.286575412745.issue20371@psf.upfronthosting.co.za>
2014-01-23 19:47:24Andrew.Lutomirskilinkissue20371 messages
2014-01-23 19:47:23Andrew.Lutomirskicreate