Skip to content

Commit 410a84f

Browse files
Validate async paginated callbacks return awaitables
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 1793f5d commit 410a84f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

hyperbrowser/client/polling.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,13 @@ async def collect_paginated_results_async(
420420
should_sleep = True
421421
try:
422422
previous_page_batch = current_page_batch
423-
page_response = await get_next_page(current_page_batch + 1)
423+
page_result = get_next_page(current_page_batch + 1)
424+
page_awaitable = _ensure_awaitable(
425+
page_result,
426+
callback_name="get_next_page",
427+
operation_name=operation_name,
428+
)
429+
page_response = await page_awaitable
424430
on_page_success(page_response)
425431
current_page_batch = get_current_page_batch(page_response)
426432
total_page_batches = get_total_page_batches(page_response)

tests/test_polling.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,25 @@ async def run() -> None:
332332
asyncio.run(run())
333333

334334

335+
def test_collect_paginated_results_async_rejects_non_awaitable_page_callback_result():
336+
async def run() -> None:
337+
with pytest.raises(
338+
HyperbrowserError, match="get_next_page must return an awaitable"
339+
):
340+
await collect_paginated_results_async(
341+
operation_name="async paginated awaitable validation",
342+
get_next_page=lambda page: {"current": 1, "total": 1, "items": []}, # type: ignore[return-value]
343+
get_current_page_batch=lambda response: response["current"],
344+
get_total_page_batches=lambda response: response["total"],
345+
on_page_success=lambda response: None,
346+
max_wait_seconds=1.0,
347+
max_attempts=2,
348+
retry_delay_seconds=0.0001,
349+
)
350+
351+
asyncio.run(run())
352+
353+
335354
def test_collect_paginated_results_async_allows_single_page_on_zero_max_wait():
336355
async def run() -> None:
337356
collected = []

0 commit comments

Comments
 (0)