Fix parse() crash on an ISO 8601 date + duration interval#982
Open
vineethsaivs wants to merge 1 commit into
Open
Fix parse() crash on an ISO 8601 date + duration interval#982vineethsaivs wants to merge 1 commit into
vineethsaivs wants to merge 1 commit into
Conversation
When parse() is given an interval made of a duration and a date-only endpoint (e.g. "2021-01-01/P1DT1H" or "P1Y/2021-01-01"), base_parse returns a datetime.date for the endpoint, so pendulum.instance() returns a Date. _parse then calls Date.add()/Date.subtract() with the duration's hours/minutes/seconds/microseconds, which Date does not accept, raising 'TypeError: add() got an unexpected keyword argument 'hours''. Endpoints that carry a time component return a DateTime and worked fine. Promote a date-only endpoint to a midnight datetime before instance(), so the interval has DateTime endpoints (matching the time-bearing case and the function's Interval[DateTime] return type) and add()/subtract() accept the time components. Fixes python-pendulum#881.
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 #881.
Problem
parse()raises on an ISO 8601 interval built from a duration and a date-only endpoint:base_parsereturns adatetime.datefor a date-only endpoint, sopendulum.instance()returns aDate._parsethen callsDate.add()/Date.subtract()with the duration'shours/minutes/seconds/microseconds, whichDatedoes not accept. Endpoints that carry a time component (e.g.2021-01-01T00:00/P1D) return aDateTimeand already work.Fix
Promote a date-only endpoint to a midnight
datetimebeforependulum.instance()in the two duration branches of_parse. The interval then hasDateTimeendpoints, matching the time-bearing case and the function'sInterval[DateTime]return annotation, andadd()/subtract()accept the duration's time components. Endpoints that are already datetimes are unchanged (the helper returns them as-is), and this also replaces thet.cast("datetime.datetime", parsed.end)on the end branch, which was papering over exactly this case.Test
test_parse_interval_with_date_and_durationcovers both the forward (add) and reverse (subtract) forms and asserts the resultingIntervalhas the expectedDateTimeendpoints. It raises theTypeErrorabove on the current code and passes with this change; the existingtest_parse_intervaland the rest oftest_parsing.pystill pass (validated withPENDULUM_EXTENSIONS=0, the pure-Python parser). Happy to add a CHANGELOG entry if you'd like one.