movermeyer/backports.datetime_fromisoformat

Cannot pickle datetime object with timezone

jappievw opened this issue · 5 comments

Thanks for providing this awesome backport! Today I ran into the issue that a datetime object can't be pickled when it includes a timezone. Please find the example below:

$ python
Python 3.6.5 (default, Mar 30 2018, 06:41:53)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> import datetime
>>> from backports.datetime_fromisoformat import MonkeyPatch
>>> MonkeyPatch.patch_fromisoformat()
>>> moment = datetime.datetime.fromisoformat('2018-11-01 20:42:09.058000+00:00')
>>> pickle.dumps(moment)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class 'FixedOffset_type'>: attribute lookup FixedOffset_type on builtins failed
>>> moment = datetime.datetime.fromisoformat('2018-11-01 20:42:09.058000')
>>> pickle.dumps(moment)
b'\x80\x03cdatetime\ndatetime\nq\x00C\n\x07\xe2\x0b\x01\x14*\t\x00\xe2\x90q\x01\x85q\x02Rq\x03.'

Unfortunately, I lack the skills to create a PR to resolve this.

Hey @jappievw, thanks for the bug report.

I can reproduce this behaviour.

Unfortunately, I also lack the skills to know how to resolve this.

The problem exists in the underlying library I copied the timezone code from.

I've raised an issue with them, so hopefully I can learn some more.

Thanks for your update @movermeyer. I will subscribe to that issue as well!

I've gone and learned how pickling/copying works.
The fix has been made, and a new version released.

This problem should be fixed.

Implemented it, thanks for your effort @movermeyer!!