freezegun icon indicating copy to clipboard operation
freezegun copied to clipboard

Freezing of custom datetime classes

Open sliverc opened this issue 6 years ago • 3 comments

Custom date times are not frozen when using freeze_time.

Example:

from datetime import datetime
from freezegun import freeze_time

class CustomDatetime(datetime):
  pass

with freeze_time('2018-05-30'):
  print(datetime.now())   # correctly outputs 2018-05-30 00:00:00
  print(CustomDatetime.now())   # invalid output of current timestamp

I have encountered this issue when using faker library which uses a custom datetime.

sliverc avatar May 31 '18 08:05 sliverc

Ah, I think this may have been a regression caused by https://github.com/spulec/freezegun/pull/175

It looks like we are checking the id of the module variables, but this won't catch subclasses: https://github.com/spulec/freezegun/blob/1a6c8144b1d5424fd140c93b247b564bd762cfdf/freezegun/api.py#L92-L93

spulec avatar Jun 01 '18 00:06 spulec

@spulec I bumped my version back to 0.3.10, and expected that to fix this based on your above comment and the timeframe that that fix got merged. Alas it didn't fix the issue.

Did I misread that merge timeline or was this use case never supported by freezegun?

gradybarrett avatar Feb 13 '19 22:02 gradybarrett