Skip to content

Commit

Permalink
Fix infinite daterange issue when start and stop is same (#302)
Browse files Browse the repository at this point in the history
Co-authored-by: Mahmoud Hashemi <mahmoud@hatnote.com>
  • Loading branch information
mezbaul-h and mahmoud authored Dec 8, 2022
1 parent 6505c6b commit 93185b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion boltons/timeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def daterange(start, stop, step=1, inclusive=False):

if stop is None:
finished = lambda now, stop: False
elif start < stop:
elif start <= stop:
finished = operator.gt if inclusive else operator.ge
else:
finished = operator.lt if inclusive else operator.le
Expand Down
20 changes: 18 additions & 2 deletions tests/test_timeutils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

from datetime import timedelta, date

import pytest

from boltons.timeutils import total_seconds, daterange


Expand Down Expand Up @@ -60,10 +63,23 @@ def test_daterange_years_step():
dates = list(daterange(start_day, end_day, step=(0, 13, 0), inclusive=False))
expected = [date(year=2012, month=12, day=25), date(year=2014, month=1, day=25), date(year=2015, month=2, day=25)]
assert dates == expected


def test_daterange_infinite():
today = date.today()
infinite_dates = daterange(today, None)
for i in range(10):
assert next(infinite_dates) == today + timedelta(days=i)


def test_daterange_with_same_start_stop():
today = date.today()

date_range = daterange(today, today)
with pytest.raises(StopIteration):
next(date_range)

date_range_inclusive = daterange(today, today, inclusive=True)
assert next(date_range_inclusive) == today
with pytest.raises(StopIteration):
next(date_range_inclusive)

0 comments on commit 93185b2

Please # to comment.