[issue20371] datetime.datetime.replace bypasses a subclass's __new__

Edward O report at bugs.python.org
Wed Feb 11 23:19:31 CET 2015


Edward O added the comment:

Here is a workaround for subclasses (2&3-compatible):

--- start code ---

class MyDate(datetime):

    @classmethod
    def fromDT(cls, d):
        """ :type d: datetime """
        return cls(d.year, d.month, d.day, d.hour, d.minute, d.second, d.microsecond, d.tzinfo)

    def replace(self, *args, **kw):
        """ :type other: datetime.timedelta
            :rtype: MyDate """
        return self.fromDT(super(MyDate, self).replace(*args, **kw))

--- end code ---

This is really a bug and not a misuse as datetime was specifically adapted to be subclassing-friendly. From a look at the (python) source, this seems to be the only bit that was forgotten.

The real fix is as per yselivanov comment [and no, this has nothing to do with pickle or copy AFAIK]

----------
versions: +Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20371>
_______________________________________


More information about the Python-bugs-list mailing list