Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timezone free datetime.datetime.fromtimestamp returns original datetime.datetime #536

Closed
jogo opened this issue Mar 14, 2024 · 1 comment

Comments

@jogo
Copy link

jogo commented Mar 14, 2024

import datetime
import pickle
import time

import pytz
from freezegun import freeze_time


class Foo:
    def __init__(self):
        self.now = datetime.datetime.fromtimestamp(time.time())

    def __reduce__(self):
        # https://docs.python.org/3/library/pickle.html#object.__reduce__
        return self.now.__reduce__()


with freeze_time():
    dt = datetime.datetime.fromtimestamp(time.time(), tz=pytz.utc)
    print(repr(dt))

    dt = datetime.datetime.fromtimestamp(time.time())
    print(repr(dt))

    pickle.dumps(Foo())

Results in

FakeDatetime(2024, 3, 14, 21, 12, 0, 615787, tzinfo=<UTC>)
datetime.datetime(2024, 3, 14, 21, 12, 0, 615787)
Traceback (most recent call last):
  File "FILE", line 25, in <module>
    pickle.dumps(Foo())
_pickle.PicklingError: Can't pickle <class 'datetime.datetime'>: it's not the same object as datetime.datetime

This sample shows two related issues:

  1. return real_datetime.fromtimestamp(
    returns real_datetime while everything else uses FakeDatetime
  2. In some cases real_datetime won't pickle correctly when datetime is patched.

I think this case should be addressed by #475

@bblommers
Copy link
Collaborator

Hi @jogo! #475 does indeed fix this, as far as I can tell.

That PR is merged now, so I'll close this one. Thank you for raising this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants