Skip to content

Fix Interval.range hang on a zero or wrong-sign amount - #1081

Open
ekanshul wants to merge 1 commit into
python-pendulum:masterfrom
ekanshul:fix-1080-interval-range-zero-amount
Open

ekanshul wants to merge 1 commit into
python-pendulum:masterfrom
ekanshul:fix-1080-interval-range-zero-amount

Conversation

@ekanshul

@ekanshul ekanshul commented Nov 2, 2026

Copy link
Copy Markdown

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

Fixes #1080

Interval.range(unit, amount) used amount as both the step and the loop increment with no validation. amount=0 therefore yielded the start forever (start.add(days=0) never moves, so start <= end stays true). A negative amount on a forward interval walked away from the end instead of terminating.

Reject a zero amount with ValueError. A negative amount whose sign does not match the interval direction now yields an empty iterator, the same way Python's range() does. Existing positive-amount ranges are unchanged.

range() treated amount as both the step and the termination
increment with no validation, so amount=0 yielded the start
forever and a mismatched sign walked away from the end.

Reject a zero amount with ValueError. A negative amount whose
sign does not match the interval direction now yields an empty
iterator, matching Python range().
Comment thread docs/docs/interval.md
```

`amount` must be a non-zero integer. A zero amount raises `ValueError`.
A negative amount whose sign does not match the interval direction

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we clarify this for inverted intervals? If dt2 is later than dt1, interval(dt2, dt1).range("days", -1) returns an empty iterator even though the step points in the same direction as the interval. A positive amount already iterates backwards there.

For equal endpoints, the loop yields that endpoint once even with -1. A short example for each case would make the intended behavior clearer.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interval.range() hangs forever on a zero (or wrong-sign) step

2 participants