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: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## Unreleased

### Fixed
- `Interval.range()` now raises `ValueError` for a non-positive `amount` instead of silently producing an infinite (amount <= 0... zero repeats the start forever, negative runs away from the end) or unbounded generator [#1080](https://github.com/python-pendulum/pendulum/issues/1080)

## [3.3.0] - 2026-10-12

### Added
Expand Down
4 changes: 4 additions & 0 deletions src/pendulum/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ 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:
msg = f"amount must be a positive integer, got {amount!r}"
raise ValueError(msg)

method = "add"
op = operator.le
if not self._absolute and self.invert:
Expand Down
14 changes: 14 additions & 0 deletions tests/interval/test_range.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import pytest

import pendulum

from pendulum.interval import Interval
Expand Down Expand Up @@ -117,3 +119,15 @@ 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():
p = Interval(pendulum.datetime(2016, 1, 1), pendulum.datetime(2016, 2, 1))
with pytest.raises(ValueError, match="positive integer"):
list(p.range("days", 0))


def test_range_negative_amount_raises():
p = Interval(pendulum.datetime(2016, 1, 1), pendulum.datetime(2016, 2, 1))
with pytest.raises(ValueError, match="positive integer"):
list(p.range("days", -1))