You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you have a user-created subtype of one of the datetime types, e.g. time, date, or datetime, and you prettyprint an instance of that subtype, the prettyprinted representation will use the class name of the base type, rather than the subtype.
What I Did
importdatetimeas_dtimportprettyprinteras_prettyprinterclassTimestamp(_dt.datetime):
"""Standardized datatype for timestamps, i.e. a datetime which is always UTC"""def__new__(cls, year, month=None, day=None, hour=0, minute=0, second=0, microsecond=0, tzinfo=_dt.timezone.utc, *, fold=0):
# Call the parent method to create the objectself=super().__new__(cls, year, month, day, hour, minute, second, microsecond, tzinfo, fold=fold)
# Confirm the object is in UTC# (We have to do this after creating the object, rather than just checking the `tzinfo` arg,# because the parent method handles the case of loading a Pickled object.)ifself.tzinfo!=_dt.timezone.utc:
raiseValueError(f"Timestamp must have UTC timezone; got {self!r}")
ifself.fold:
raiseValueError(f"Timestamp are always UTC, so nonzero 'fold' is meaningless; got {self!r}")
returnselfts=Timestamp(2023, 1, 1)
_prettyprinter.pprint(ts)
The fix for this is actually pretty simple. In pretty_stdlib.py there are functions to handle the datetime classes. These functions call pretty_call_alt() and similar functions with hard-coded references to the datetime classes. These hardcoded references should be replaced with the actual type of the passed-in instance. E.g., in def pretty_datetime(dt, ctx):, calls to pretty_call_alt(ctx, datetime, ...) should be changed to pretty_call_alt(ctx, type(dt), ...).
Curiously, it is only the datetime and pytz classes that have this problem; all the other types handled in that file, e.g. UUIDs, defaultdicts, dequeues, etc., all use type(...) already.
The text was updated successfully, but these errors were encountered:
Description
If you have a user-created subtype of one of the datetime types, e.g.
time
,date
, ordatetime
, and you prettyprint an instance of that subtype, the prettyprinted representation will use the class name of the base type, rather than the subtype.What I Did
Output:
Expected output:
The Fix
The fix for this is actually pretty simple. In
pretty_stdlib.py
there are functions to handle thedatetime
classes. These functions callpretty_call_alt()
and similar functions with hard-coded references to thedatetime
classes. These hardcoded references should be replaced with the actual type of the passed-in instance. E.g., indef pretty_datetime(dt, ctx):
, calls topretty_call_alt(ctx, datetime, ...)
should be changed topretty_call_alt(ctx, type(dt), ...)
.Curiously, it is only the
datetime
andpytz
classes that have this problem; all the other types handled in that file, e.g. UUIDs, defaultdicts, dequeues, etc., all usetype(...)
already.The text was updated successfully, but these errors were encountered: