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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/pendulum/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class Interval(Duration, Generic[_T]):
"""

def __new__(cls, start: _T, end: _T, absolute: bool = False) -> Self:
if (isinstance(start, datetime) and not isinstance(end, datetime)) or (
not isinstance(start, datetime) and isinstance(end, datetime)
if (isinstance(start, datetime) and not isinstance(end, datetime) or
not isinstance(start, datetime) and isinstance(end, datetime) or
not (isinstance(start, date) and isinstance(end, date)) # Time not allowed
):
raise ValueError(
"Both start and end of an Interval must have the same type"
Expand Down
15 changes: 15 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import pytest

import pendulum

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


def test_parse_interval_mixed_types():
error = 'Both start and end of an Interval must have the same type'

# date / time is not allowed in ISO 8601
with pytest.raises(ValueError, match=error):
pendulum.parse('2007-12-13/14:30')

# datetime / time is allowed in some lenient ISO 8601 implementations (e.g. aniso8601 package).
# Not allowed in pendulum for strictness and consistency.
with pytest.raises(ValueError, match=error):
pendulum.parse('2007-12-13T14:30/15:45')


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