jaraco · GitHub

In pypy/pypy#3980, I reported an issue I'd encountered where the repr of a datetime.datetime subclass shows a different repr depending on which implementation (C or Python) of the datetime module is used:

import sys
import importlib
c_impl = importlib.import_module('_datetime')
sys.modules['_datetime'] = None
py_impl = importlib.import_module('datetime')
assert repr(py_impl.datetime(2000,1,1)) == repr(c_impl.datetime(2000,1,1))
class Fake(py_impl.datetime):
  pass
fpy = Fake(2000,1,1)
class Fake(c_impl.datetime):
  pass
fc = Fake(2000,1,1)
assert repr(fpy) == repr(fc), f'{fpy!r} != {fc!r}'

Emits as output:

Traceback (most recent call last):
  File "/Users/jaraco/draft/dt.py", line 18, in <module>
    assert repr(fpy) == repr(fc), f'{fpy!r} != {fc!r}'
AssertionError: __main__.Fake(2000, 1, 1, 0, 0) != Fake(2000, 1, 1, 0, 0)

This inconsistency wreaks havoc on doctests when using a library like freezegun.

Is there a way for these two implementations to share a common repr?

Linked PRs

Read the original on github.com ↗