Skip to content

Preserve calendar units when pickling and copying Duration - #1100

Open
draarivpatel-ui wants to merge 1 commit into
python-pendulum:masterfrom
draarivpatel-ui:fix-duration-pickle-calendar-units
Open

draarivpatel-ui wants to merge 1 commit into
python-pendulum:masterfrom
draarivpatel-ui:fix-duration-pickle-calendar-units

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Pull Request Check List

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

Fixes #1023

Description

Pickling or shallow-copying a Duration lost its calendar components even though its approximate total length remained equal:

original = pendulum.duration(years=2, months=3, weeks=1, days=2)
restored = pickle.loads(pickle.dumps(original))
restored.years, restored.months  # (0, 0), was (2, 3)
str(restored)  # '118 weeks 3 days', was '2 years 3 months 1 week 2 days'

The reduction inherited from timedelta serializes only the normalized day/second/microsecond constructor arguments, so the years and months the duration was built with were dropped. Duration now preserves its instance state alongside those constructor arguments, so pickling (at every protocol) and shallow copying round-trip without losing information. Interval defines its own reduction and is unaffected.

The constructor arguments still come from timedelta's own C-level reduction, which keeps the base timedelta value exact (no float round-trip through total_seconds()).

Tests

New regression cases in tests/duration/test_behavior.py cover every pickle protocol, positive, negative and mixed-sign calendar components, microsecond-only durations, str() output and shallow copying. They fail before the fix ('118 weeks 3 days' != '2 years 3 months 1 week 2 days') and pass after it. The full test suite passes with the pure-Python implementation (PENDULUM_EXTENSIONS=0), and ruff check/ruff format (pinned v0.14.11) pass on the changed files. The one unrelated failure observed locally, test_instance_with_aware_datetime_any_tzinfo, fails identically on the pristine tree.

This fix was developed with AI assistance (an autonomous coding agent); the change, tests and verification are as described above.

The reduction inherited from timedelta only keeps the normalized
day/second/microsecond constructor arguments, so pickling or shallow
copying a Duration lost the years and months it was built with even
though its approximate total length survived. The instance state is
now preserved alongside the constructor arguments so both round-trip
without losing information.

Fixes python-pendulum#1023
Copilot AI balanced review requested due to automatic review settings November 25, 2026 21:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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

2 participants