FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Raise ParserError for an interval with mismatched endpoint types by eeshsaxena · Pull Request #992 · python-pendulum/pendulum · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
24 changes: 16 additions & 8 deletions src/pendulum/parser.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pendulum.duration import Duration
from pendulum.parsing import _Interval
from pendulum.parsing import parse as base_parse
from pendulum.parsing.exceptions import ParserError
from pendulum.tz.timezone import UTC


Expand Down Expand Up @@ -109,14 +110,21 @@ def _parse(
dt,
)

return pendulum.interval(
pendulum.instance(
t.cast("datetime.datetime", parsed.start), tz=options.get("tz", UTC)
),
pendulum.instance(
t.cast("datetime.datetime", parsed.end), tz=options.get("tz", UTC)
),
)
try:
return pendulum.interval(
pendulum.instance(
t.cast("datetime.datetime", parsed.start), tz=options.get("tz", UTC)
),
pendulum.instance(
t.cast("datetime.datetime", parsed.end), tz=options.get("tz", UTC)
),
)
except TypeError as e:
# An interval whose ends are not both dates or both datetimes (e.g.
# "2020-01-01/12:30:00", a date and a bare time) has an undefined
# difference; report it as an invalid string instead of letting a
# TypeError escape parse().
raise ParserError(f"Invalid interval string: {text!r}") from e

if isinstance(parsed, Duration):
return parsed
Expand Down
15 changes: 15 additions & 0 deletions tests/test_parsing.py
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import annotations

import pytest

import pendulum

from pendulum.parsing.exceptions import ParserError
from tests.conftest import assert_date
from tests.conftest import assert_datetime
from tests.conftest import assert_duration
Expand Down Expand Up @@ -127,6 +130,18 @@ def test_parse_interval() -> None:
assert interval.end.offset == 0


def test_parse_interval_mismatched_ends() -> None:
# An interval whose ends are not both dates or both datetimes has an
# undefined difference; this used to raise a bare TypeError.
for text in (
"2020-01-01/12:30:00",
"12:30:00/2020-01-01",
"12:00:00/13:00:00",
):
with pytest.raises(ParserError):
pendulum.parse(text)


def test_parse_now() -> None:
assert pendulum.parse("now").timezone_name == "UTC"
assert (
Expand Down

Back | FazBrowse Home | New Git URL