Fix infinite daterange issue when start and stop is same (#302)

Co-authored-by: Mahmoud Hashemi <mahmoud@hatnote.com>
This commit is contained in:
Mezbaul Haque 2022-12-08 15:51:37 +06:00 committed by GitHub
parent 6505c6b556
commit 93185b224e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -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

View File

@ -1,5 +1,8 @@
from datetime import timedelta, date
import pytest
from boltons.timeutils import total_seconds, daterange
@ -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)