diff --git a/docs/docs/interval.md b/docs/docs/interval.md index fc70fb3c4..83c1a082b 100644 --- a/docs/docs/interval.md +++ b/docs/docs/interval.md @@ -151,6 +151,23 @@ You can pass an amount for the passed unit to control the length of the gap: '2000-01-09T00:00:00+00:00' ``` +`amount` must be a non-zero integer. A zero amount raises `ValueError`. +A positive amount follows the interval direction, including backwards for an +inverted interval. A negative amount walks away from the end, so it yields no +values when the endpoints differ. When both endpoints are equal, either sign +yields that endpoint once. + +```python +>>> start = pendulum.datetime(2000, 1, 1) +>>> end = pendulum.datetime(2000, 1, 3) +>>> [dt.day for dt in pendulum.interval(end, start).range('days', 1)] +[3, 2, 1] +>>> list(pendulum.interval(end, start).range('days', -1)) +[] +>>> [dt.day for dt in pendulum.interval(start, start).range('days', -1)] +[1] +``` + You can also directly iterate over the `Interval` instance, the unit will be `days` in this case: diff --git a/src/pendulum/interval.py b/src/pendulum/interval.py index c5e0713a7..3fc5287b7 100644 --- a/src/pendulum/interval.py +++ b/src/pendulum/interval.py @@ -288,12 +288,23 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str: return separator.join(parts) def range(self, unit: str, amount: int = 1) -> Iterator[_T]: + if amount == 0: + raise ValueError("range() requires a non-zero amount") + method = "add" op = operator.le if not self._absolute and self.invert: method = "subtract" op = operator.ge + # A negative amount walks the opposite direction of the interval. + # Flip the comparison too, so unequal endpoints yield no values + # instead of looping forever. Equal endpoints are yielded once. + if amount < 0: + amount = -amount + method = "subtract" if method == "add" else "add" + op = operator.ge if op is operator.le else operator.le + start, end = self.start, self.end i = amount diff --git a/tests/interval/test_range.py b/tests/interval/test_range.py index 0b883e583..4c4c2bab6 100644 --- a/tests/interval/test_range.py +++ b/tests/interval/test_range.py @@ -1,5 +1,7 @@ from __future__ import annotations +import pytest + import pendulum from pendulum.interval import Interval @@ -117,3 +119,31 @@ def test_range_amount(): assert_datetime(r[1], 2016, 10, 16, 1, 0, 0) assert_datetime(r[2], 2016, 10, 18, 0, 0, 0) assert_datetime(r[3], 2016, 10, 20, 0, 0, 0) + + +def test_range_zero_amount_raises(): + dt1 = pendulum.datetime(2000, 1, 1) + dt2 = pendulum.datetime(2000, 1, 10) + p = pendulum.interval(dt1, dt2) + + with pytest.raises(ValueError, match="non-zero amount"): + list(p.range("days", 0)) + + +def test_range_wrong_sign_amount_is_empty(): + dt1 = pendulum.datetime(2000, 1, 1) + dt2 = pendulum.datetime(2000, 1, 10) + + forward = pendulum.interval(dt1, dt2) + assert list(forward.range("days", -1)) == [] + + inverted = pendulum.interval(dt2, dt1) + assert list(inverted.range("days", -1)) == [] + + +@pytest.mark.parametrize("amount", [-1, 1]) +def test_range_equal_endpoints(amount): + endpoint = pendulum.datetime(2000, 1, 1) + assert list(pendulum.interval(endpoint, endpoint).range("days", amount)) == [ + endpoint + ]