Repository navigation
Fix multi-digit decimal fractions in the pure Python duration parser - #1107
Open
farisbasha wants to merge 1 commit into
Open
farisbasha wants to merge 1 commit into
farisbasha wants to merge 1 commit into
Conversation
The pure Python ISO 8601 duration parser divided the digits after the decimal mark by 10 whatever their count, so PT1.25H became 3h30m instead of 1h15m and long fractions raised OverflowError. Fractional weeks also dropped everything below whole hours. Parse the fraction as 0.<digits>, which matches the Rust parser. Fixes python-pendulum#534
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.
Fixes #534
Pull Request Check List
Bug
The pure Python ISO 8601 duration parser (used when
PENDULUM_EXTENSIONS=0or the Rust extension is not available) turns the digits after the decimal mark into a fraction by dividing by 10, whatever the number of digits:A fractional week also dropped everything below whole hours:
P1.1WgaveDuration(weeks=1, hours=16)instead of 1 week 16h 48m.The Rust parser already returns the expected values for all of these.
Fix
Read the fraction as
0.<digits>(float(f"0.{digits}")) for weeks, days, hours and minutes, and pass the fractional days of a week duration on toDurationinstead of truncating them to whole hours.Tests
Added cases to
test_parse_iso8601_duration(P1.1W,P1.25W,P1.25D,P1.05D,PT1.25H,PT16.48M). WithPENDULUM_EXTENSIONS=0all six fail on master and pass with the fix. With the Rust extension they pass before and after.On the server, the full suite gives 1876 passed, 3 skipped in both modes. One test fails in both modes on master too (
test_instance_with_aware_datetime_any_tzinfo), because of the machine's local time zone.ruff check,ruff format --check(0.14.11) andmypyon the changed file are clean.This PR was prepared with AI assistance (Claude).