Repository navigation
Preserve calendar units when pickling and copying Duration - #1100
Open
draarivpatel-ui wants to merge 1 commit into
Open
draarivpatel-ui wants to merge 1 commit into
draarivpatel-ui wants to merge 1 commit into
Conversation
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
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Check List
Fixes #1023
Description
Pickling or shallow-copying a
Durationlost its calendar components even though its approximate total length remained equal:The reduction inherited from
timedeltaserializes only the normalized day/second/microsecond constructor arguments, so the years and months the duration was built with were dropped.Durationnow preserves its instance state alongside those constructor arguments, so pickling (at every protocol) and shallow copying round-trip without losing information.Intervaldefines its own reduction and is unaffected.The constructor arguments still come from
timedelta's own C-level reduction, which keeps the basetimedeltavalue exact (no float round-trip throughtotal_seconds()).Tests
New regression cases in
tests/duration/test_behavior.pycover 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), andruff 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.