Skip to content

Raise ParserError for an interval with mismatched endpoint types - #992

Open
eeshsaxena wants to merge 1 commit into
python-pendulum:masterfrom
eeshsaxena:fix/interval-mismatched-ends
Open

Raise ParserError for an interval with mismatched endpoint types#992
eeshsaxena wants to merge 1 commit into
python-pendulum:masterfrom
eeshsaxena:fix/interval-mismatched-ends

Conversation

@eeshsaxena

Copy link
Copy Markdown

pendulum.parse('2020-01-01/12:30:00') raises TypeError: unsupported operand type(s) for -: 'Time' and 'datetime.date' instead of a ParserError.

The / makes parse treat the string as an ISO 8601 interval. Here one side parses to a date and the other to a bare time, and building the interval computes end - start (Time - date), which is undefined. The same happens for time/time (12:00:00/13:00:00) and time/date. Valid intervals (datetime/datetime, date/date) are unaffected.

I wrapped the two-endpoint pendulum.interval(...) construction in _parse so a TypeError there is re-raised as ParserError, which is what parse already raises for other invalid input (and it subclasses ValueError, so existing except ValueError handlers still work).

Added a test in test_parsing.py covering the mismatched cases; it raises TypeError on master and passes with the change, and the rest of the parsing tests still pass. Found it by fuzzing parse with mutated date strings.

pendulum.parse treats 'A/B' as an ISO 8601 interval. When one side parses to a
date and the other to a bare time (e.g. '2020-01-01/12:30:00'), building the
interval does Time - date and raised a bare TypeError out of parse(). Same for
time/time and time/date. Convert that TypeError into a ParserError so callers
get the documented parse error instead.
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.

1 participant