Skip to content

Commit 5e3bd35

Browse files
fix: pendulum.parse('2007-12-13/14:30') raised TypeError instead of uniform ValueError
Time object was falling through type checks in Interval.__new__
1 parent ae4c405 commit 5e3bd35

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/pendulum/interval.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ class Interval(Duration, Generic[_T]):
3939
"""
4040

4141
def __new__(cls, start: _T, end: _T, absolute: bool = False) -> Self:
42-
if (isinstance(start, datetime) and not isinstance(end, datetime)) or (
43-
not isinstance(start, datetime) and isinstance(end, datetime)
42+
if (isinstance(start, datetime) and not isinstance(end, datetime) or
43+
not isinstance(start, datetime) and isinstance(end, datetime) or
44+
not (isinstance(start, date) and isinstance(end, date)) # Time not allowed
4445
):
4546
raise ValueError(
4647
"Both start and end of an Interval must have the same type"

tests/test_parsing.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import pytest
4+
35
import pendulum
46

57
from tests.conftest import assert_date
@@ -127,6 +129,19 @@ def test_parse_interval() -> None:
127129
assert interval.end.offset == 0
128130

129131

132+
def test_parse_interval_mixed_types():
133+
error = 'Both start and end of an Interval must have the same type'
134+
135+
# date / time is not allowed in ISO 8601
136+
with pytest.raises(ValueError, match=error):
137+
pendulum.parse('2007-12-13/14:30')
138+
139+
# datetime / time is allowed in some lenient ISO 8601 implementations (e.g. aniso8601 package).
140+
# Not allowed in pendulum for strictness and consistency.
141+
with pytest.raises(ValueError, match=error):
142+
pendulum.parse('2007-12-13T14:30/15:45')
143+
144+
130145
def test_parse_now() -> None:
131146
assert pendulum.parse("now").timezone_name == "UTC"
132147
assert (

0 commit comments

Comments
 (0)