Skip to content

Preserve Duration years and months across pickle and copy - #1027

Open
fhgffy wants to merge 1 commit into
python-pendulum:masterfrom
fhgffy:fix-duration-pickle
Open

fhgffy wants to merge 1 commit into
python-pendulum:masterfrom
fhgffy:fix-duration-pickle

Conversation

@fhgffy

@fhgffy fhgffy commented Oct 4, 2026

Copy link
Copy Markdown

Fixes #1023

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

Duration subclasses timedelta, whose reducer only keeps days, seconds, and microseconds. Pickle and copy.copy therefore rebuilt the instance without years and months (2 years 3 months ... came back as weeks and days). deepcopy already rebuilt from the calendar fields.

__reduce_ex__ now reconstructs the instance the same way __deepcopy__ does, so pickle and shallow copy keep years, months, and weeks.

No documentation change: this restores the fields the constructor already documents.

Tests: pytest tests/duration/test_behavior.py

timedelta's reducer only stores days, seconds, and microseconds, so years and months were dropped on pickle and copy.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

Duration pickle and shallow copy lose year and month components

1 participant