Skip to content

Commit 881ca5c

Browse files
fix: raise ValueError for reversed page ranges in expand_page_range
A reversed page range like '5-3' would either raise a confusing 'No valid page numbers or ranges found' error (when it was the only entry) or silently drop the pages when mixed with valid ranges (e.g. ['1-3', '7-5', '8'] returned [1, 2, 3, 8] instead of raising). Add an explicit check that start <= end and raise a clear ValueError with a descriptive message identifying the problematic range. Also adds three new tests covering: - reversed range alone raises ValueError - reversed range mixed with valid ranges raises ValueError (no silent drop) - equal start and end (e.g. '3-3') is valid and returns [3]
1 parent ed472f0 commit 881ca5c

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

haystack/utils/misc.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ def expand_page_range(page_range: list[str | int]) -> list[int]:
6161
if not parts[0].isdigit() or not parts[1].isdigit():
6262
msg = "range must be a string in the format 'start-end'"
6363
raise ValueError(f"Invalid page range: {page} - {msg}")
64-
start, end = parts
65-
expanded_page_range.extend(range(int(start), int(end) + 1))
64+
start, end = int(parts[0]), int(parts[1])
65+
if start > end:
66+
msg = "start must be less than or equal to end"
67+
raise ValueError(f"Invalid page range: '{parts[0]}-{parts[1]}' - {msg}")
68+
expanded_page_range.extend(range(start, end + 1))
6669

6770
else:
6871
msg = "range must be a string in the format 'start-end' or an integer"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed `expand_page_range` silently dropping pages when a reversed range (e.g. '7-5') appeared
5+
alongside valid entries. Mixed inputs like ['1-3', '7-5', '8'] previously returned [1, 2, 3, 8]
6+
with no warning, losing pages 5-7. A reversed range now raises `ValueError` with a descriptive
7+
message identifying the offending range.

test/utils/test_misc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,16 @@ def test_invalid_string_raises_value_error(self):
195195
def test_malformed_range_with_multiple_hyphens_raises_value_error(self):
196196
with pytest.raises(ValueError, match="Invalid page range"):
197197
expand_page_range(["1-3", "5-10-15"])
198+
199+
def test_reversed_range_alone_raises_value_error(self):
200+
with pytest.raises(ValueError, match="Invalid page range.*start must be less than or equal to end"):
201+
expand_page_range(["5-3"])
202+
203+
def test_reversed_range_mixed_raises_value_error(self):
204+
# Previously, a reversed range mixed with valid entries silently dropped the reversed range.
205+
# e.g. ["1-3", "7-5", "8"] would return [1, 2, 3, 8], losing pages 5-7 with no error.
206+
with pytest.raises(ValueError, match="Invalid page range.*start must be less than or equal to end"):
207+
expand_page_range(["1-3", "7-5", "8"])
208+
209+
def test_equal_start_end_is_valid(self):
210+
assert expand_page_range(["3-3"]) == [3]

0 commit comments

Comments
 (0)