Title: Preserve the fold when pickling DateTime by Duo-ee · Pull Request #1073 · python-pendulum/pendulum · GitHub
Open Graph Title: Preserve the fold when pickling DateTime by Duo-ee · Pull Request #1073 · python-pendulum/pendulum
X Title: Preserve the fold when pickling DateTime by Duo-ee · Pull Request #1073 · python-pendulum/pendulum
Description: Pull Request Check List Added tests for changed code. Updated documentation for changed code — pickling is not described in docs/, nothing to update. Fixes #908. What was wrong DateTime.__reduce_ex__() handed the class plus the 8-tuple from _getstate() to pickle, so the fold flag was never serialized. Unpickling a DateTime that sits on the second occurrence of an ambiguous wall time gave back the first occurrence instead: different offset, timestamp off by an hour. import pickle import pendulum chicago = pendulum.timezone("America/Chicago") after = pendulum.datetime(2024, 11, 3, 7, tz="utc").astimezone(chicago) after.timestamp() # 1730617200.0, 01:00 -06:00, fold=1 pickle.loads(pickle.dumps(after)).timestamp() # master: 1730613600.0 (01:00 -05:00, fold=0); this branch: 1730617200.0 Why not restore through create() #909 (self.create plus a 9th element in _getstate()) does fix the timestamp above, but routing the state through create() re-normalizes the instance. Measured on master with that approach: a zoneinfo.ZoneInfo tzinfo comes back as FixedTimezone/Timezone, and a wall time that does not exist (2024-03-10 02:30 in Chicago, which can only be built with the constructor) moves to 01:30 and changes its timestamp. This branch restores from the raw state instead: a module-level _restore_datetime() builds cls(*state, fold=fold) with no timezone conversion and the original tzinfo. fold has to be passed as a keyword because it is keyword-only for datetime.__new__() — which is also why the class itself cannot stay the callable. copy.copy() goes through the same __reduce_ex__() and is fixed by this too (copy.deepcopy() was already correct). Compatibility Pickles written by older versions still load: they store the class plus the 8-tuple, which still builds a valid instance, they only lack the fold they never contained. Checked by dumping with master and loading with this branch for 6 cases × 6 protocols (36 files): 36/36 load with identical type/value/timestamp, except the ambiguous fold=1 case where an old stream has nothing to restore from. Tests Added to tests/datetime/test_behavior.py: fold 0 and 1 across protocols 0-5, shallow copy, and tzinfo preservation. On master 8 of the 14 new cases fail (six fold=1 pickle cases, shallow copy, and the tzinfo case through its offset/timestamp assertions); all pass here. Local run, Python 3.12.14 with the versions pinned in poetry.lock / prek.toml (python-dateutil 2.9.0.post0, pytest 9.0.3, ruff 0.14.11): PENDULUM_EXTENSIONS=0 python -m pytest -q tests → 1882 passed, 5 skipped (master: 1868 passed, 5 skipped) python -m mypy → no issues in 228 files ruff check / ruff format --check → clean Not covered locally: the maturin develop extension build and the free-threaded 3.14t / 3.15t jobs. The change is pure Python and does not touch parsing. Side note, not changed here pendulum.Time._getstate() has the same shape and also loses fold when pickled (Time(1, 30, tzinfo=timezone, fold=1) comes back with fold=0). Happy to extend this PR or send a separate one if you want it fixed too. AI assistance disclosure: developed with AI assistance, as disclosed in #1039. Every number above was produced by running the commands listed.
Open Graph Description: Pull Request Check List Added tests for changed code. Updated documentation for changed code — pickling is not described in docs/, nothing to update. Fixes #908. What was wrong DateTime.__reduc...
X Description: Pull Request Check List Added tests for changed code. Updated documentation for changed code — pickling is not described in docs/, nothing to update. Fixes #908. What was wrong DateTime.__reduc...
Opengraph URL: https://github.com/python-pendulum/pendulum/pull/1073
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:faa00c8a-2d85-9dd9-1937-07cd405e362c |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | DEFA:208411:1EB3CF:2A2AA3:6B0466E5 |
| html-safe-nonce | a961752e51bdadaf80ebeeb163f6209b540a05fefee234cdc859f8c26a13a417 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERUZBOjIwODQxMToxRUIzQ0Y6MkEyQUEzOjZCMDQ2NkU1IiwidmlzaXRvcl9pZCI6IjEwMzgzODQwMTAwNDIzMDQyMzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c8f0758db99da4949a912d0ef2d840dc730bf59a3bb2fe2104915ab2ac56ca93 |
| hovercard-subject-tag | pull_request:5051038513 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python-pendulum/pendulum/pull/1073/files |
| twitter:image | https://avatars.githubusercontent.com/u/113885053?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/113885053?s=400&v=4 |
| og:image:alt | Pull Request Check List Added tests for changed code. Updated documentation for changed code — pickling is not described in docs/, nothing to update. Fixes #908. What was wrong DateTime.__reduc... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python-pendulum/pendulum git https://github.com/python-pendulum/pendulum.git |
| octolytics-dimension-user_id | 191041900 |
| octolytics-dimension-user_login | python-pendulum |
| octolytics-dimension-repository_id | 62095504 |
| octolytics-dimension-repository_nwo | python-pendulum/pendulum |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 62095504 |
| octolytics-dimension-repository_network_root_nwo | python-pendulum/pendulum |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ca4d07a7cef4e4b75f821dc25e7300d80c1450a9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width