Skip to content

Commit 1661601

Browse files
davidsbatistadevteamaegissjrl
authored
fix: pagination error too many values to unpack (#11475)
Co-authored-by: devteamaegis <devteam.aegis@gmail.com> Co-authored-by: Sebastian Husch Lee <10526848+sjrl@users.noreply.github.com>
1 parent e8ebfb4 commit 1661601

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

haystack/utils/misc.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def expand_page_range(page_range: list[str | int]) -> list[int]:
3939
:param page_range: List of page numbers and ranges
4040
:returns:
4141
An expanded list of page integers
42+
:raises ValueError:
43+
If any element is not a valid integer or a range string in the format `'start-end'`.
4244
4345
"""
4446
expanded_page_range = []
@@ -55,7 +57,11 @@ def expand_page_range(page_range: list[str | int]) -> list[int]:
5557
expanded_page_range.append(int(page))
5658

5759
elif isinstance(page, str) and "-" in page:
58-
start, end = page.split("-")
60+
parts = page.split("-", maxsplit=1)
61+
if not parts[0].isdigit() or not parts[1].isdigit():
62+
msg = "range must be a string in the format 'start-end'"
63+
raise ValueError(f"Invalid page range: {page} - {msg}")
64+
start, end = parts
5965
expanded_page_range.extend(range(int(start), int(end) + 1))
6066

6167
else:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
``expand_page_range()`` now raises a ``ValueError: too many values to unpack`` when a page range string
5+
contained more than one hyphen (e.g. ``"10-20-30"``). The parser now validates the
6+
format and raises a clear ``ValueError`` with an explanatory message for invalid inputs.

test/utils/test_misc.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
_normalize_metadata_field_name,
1414
_parse_dict_from_json,
1515
_reciprocal_rank_fusion,
16+
expand_page_range,
1617
)
1718

1819

@@ -168,3 +169,29 @@ def test_parse_dict_from_json_missing_keys_raise_false(self, mock_logger):
168169
assert "Missing expected keys in JSON: {missing_keys}" in args[0]
169170
assert kwargs["missing_keys"] == ["key2"]
170171
assert kwargs["keys"] == ["key1"]
172+
173+
174+
class TestExpandPageRange:
175+
def test_single_page_integers(self):
176+
assert expand_page_range([1, 3, 5]) == [1, 3, 5]
177+
178+
def test_single_page_strings(self):
179+
assert expand_page_range(["1", "3", "5"]) == [1, 3, 5]
180+
181+
def test_range_strings_expanded(self):
182+
assert expand_page_range(["1-3", "5", "8", "10-12"]) == [1, 2, 3, 5, 8, 10, 11, 12]
183+
184+
def test_mixed_integers_and_range_strings(self):
185+
assert expand_page_range([1, "3-5", 7]) == [1, 3, 4, 5, 7]
186+
187+
def test_empty_input_raises_value_error(self):
188+
with pytest.raises(ValueError, match="No valid page numbers"):
189+
expand_page_range([])
190+
191+
def test_invalid_string_raises_value_error(self):
192+
with pytest.raises(ValueError, match="Invalid page range"):
193+
expand_page_range(["abc"])
194+
195+
def test_malformed_range_with_multiple_hyphens_raises_value_error(self):
196+
with pytest.raises(ValueError, match="Invalid page range"):
197+
expand_page_range(["1-3", "5-10-15"])

0 commit comments

Comments
 (0)