Content-Length: 300994 | pFad | https://github.com/python/cpython/issues/107773

C9 datetime subclass repr varies by implementation · Issue #107773 · python/cpython · GitHub
Skip to content

datetime subclass repr varies by implementation #107773

@jaraco

Description

@jaraco

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/python/cpython/issues/107773

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy