datetime.replace always returns datetime.datetime in pypy3

Issue #2635 resolved
Paul G created an issue

MWE:

demo_datetime_replace.py:

from datetime import datetime

class SuperDatetime(datetime):
    pass

if __name__ == "__main__":
    sdt0 = SuperDatetime(2017, 8, 1)
    sdt1 = sdt0.replace(year=2016)

    print(repr(sdt0))
    print(repr(sdt1))
#! bash
$ python2 demo_datetime_replace.py
SuperDatetime(2017, 8, 1, 0, 0)
SuperDatetime(2016, 8, 1, 0, 0)

$ python3 demo_datetime_replace.py 
SuperDatetime(2017, 8, 1, 0, 0)
SuperDatetime(2016, 8, 1, 0, 0)

$ pypy demo_datetime_replace.py
SuperDatetime(2017, 8, 1, 0, 0)
SuperDatetime(2016, 8, 1, 0, 0)

$ pypy3 demo_datetime_replace.py 
__main__.SuperDatetime(2017, 8, 1, 0, 0)
datetime.datetime(2016, 8, 1, 0, 0)

It seems that pypy3 is also the odd man out with respect to how repr is treated, but that's a separate and less important issue.

It's not clear to me how much "datetime.replace returns the same class of object" is part of the Python spec, but it seems reasonable and it's definitely a shift in behavior. There's something about subclasses of builtin types here, but it doesn't seem relevant to me, if I'm understanding it right.

Comments (4)

  1. Armin Rigo

    Thanks a lot for creating and caring for the CPython issue :-) In PyPy we simply have no C extension, so we directly use the pure Python datetime.py. For now let's fix datetime.py to return type(self)(...) in the methods replace of date, time and datetime. Done in f2d17e80c68d.

  2. Log in to comment