Skip to content

Commit 78c98cd

Browse files
committed
Fix tests and types
1 parent 969322f commit 78c98cd

3 files changed

Lines changed: 15 additions & 16 deletions

File tree

‎src/apify_client/_iterable_list_page.py‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def build_iterable_list_page(
144144
The `total` field from the first page is not trusted for stopping iteration because it may change between calls;
145145
iteration stops when a page has no items or when the user-requested `limit` has been reached.
146146
147-
The `count` field does not count objects returned, but object scanned by the API. For example when using filters,
147+
The `count` field does not count objects returned, but objects scanned by the API. For example when using filters,
148148
returned items can be smaller than `count`. Therefore, `count` should be used for correct offset calculation if
149149
available.
150150
@@ -213,9 +213,9 @@ async def async_iterator() -> AsyncIterator[Any]:
213213
yield item
214214
fetched_items += getattr(current_page, 'count', len(current_page.items))
215215

216-
async def wrap_first_page() -> IterableListPage[Any]:
216+
async def wrap_first_page() -> ListPage[Any]:
217217
first_page = await fetch_first_page
218-
return IterableListPage(first_page, iter(first_page.items))
218+
return ListPage(first_page)
219219

220220
return IterableListPageAsync(wrap_first_page, async_iterator())
221221

@@ -261,7 +261,7 @@ def iterator() -> Iterator[Any]:
261261

262262

263263
def build_cursor_iterable_list_page_async(
264-
callback: Callable[..., Awaitable[HasItems[T]]],
264+
callback: Callable[..., Coroutine[Any, Any, HasItems[T]]],
265265
*,
266266
cursor_param: str,
267267
initial_cursor: Any = None,
@@ -279,11 +279,11 @@ def build_cursor_iterable_list_page_async(
279279
user_limit = limit or 0
280280
first_limit = _min_for_limit_param(limit, effective_chunk)
281281

282-
async def fetch_first_page() -> Any:
283-
return await callback(**{**kwargs, cursor_param: initial_cursor, 'limit': first_limit})
282+
# Can be awaited multiple times with same result, but not scheduled at this time yet, as it might be pre-emptive.
283+
fetch_first_page = _LazyTask(callback(**{**kwargs, cursor_param: initial_cursor, 'limit': first_limit}))
284284

285285
async def async_iterator() -> AsyncIterator[Any]:
286-
current_page = await fetch_first_page()
286+
current_page = await fetch_first_page
287287
for item in current_page.items:
288288
yield item
289289

@@ -299,8 +299,7 @@ async def async_iterator() -> AsyncIterator[Any]:
299299
fetched += len(current_page.items)
300300
next_cursor = getattr(current_page, f'next_{cursor_param}')
301301

302-
async def wrap_first_page() -> IterableListPage[Any]:
303-
first_page = await fetch_first_page()
304-
return IterableListPage(first_page, iter(first_page.items))
302+
async def wrap_first_page() -> ListPage[Any]:
303+
return ListPage(await fetch_first_page)
305304

306305
return IterableListPageAsync(wrap_first_page, async_iterator())

‎tests/integration/_utils.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ async def maybe_await(value: Coroutine[Any, Any, T]) -> T: ...
8989
async def maybe_await(value: T) -> T: ...
9090

9191

92-
async def maybe_await(value: Any) -> Any:
92+
async def maybe_await(value: T | Coroutine[Any, Any, T]) -> T:
9393
"""Await coroutines, pass through other values.
9494
9595
Enables unified test code for both sync and async clients:
9696
result = await maybe_await(client.datasets().list())
9797
"""
9898
if hasattr(value, '__await__'):
99-
return await value
99+
return await value # ty: ignore[invalid-await]
100100
return value
101101

102102

‎tests/integration/test_request_queue.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ async def test_request_queue_list_requests(client: ApifyClient | ApifyClientAsyn
256256
)
257257

258258
# Poll until all requests are available (eventual consistency)
259-
list_response: ListPage[Request] | None = None
260259
for _ in range(5):
261260
await maybe_sleep(1, is_async=is_async)
262261
list_response = await maybe_await(rq_client.list_requests())
262+
assert isinstance(list_response, ListPage)
263263
if list_response.items and len(list_response.items) == 5:
264264
break
265265

@@ -326,10 +326,10 @@ async def test_request_queue_batch_add_requests(client: ApifyClient | ApifyClien
326326
assert len(batch_response.unprocessed_requests) == 0
327327

328328
# Poll until all requests are available (eventual consistency)
329-
list_response: ListPage[Request] | None = None
330329
for _ in range(5):
331330
await maybe_sleep(1, is_async=is_async)
332331
list_response = await maybe_await(rq_client.list_requests())
332+
assert isinstance(list_response, ListPage)
333333
if list_response.items and len(list_response.items) == 10:
334334
break
335335

@@ -357,10 +357,10 @@ async def test_request_queue_batch_delete_requests(client: ApifyClient | ApifyCl
357357
)
358358

359359
# Poll until all requests are available (eventual consistency)
360-
list_response: ListPage[Request] | None = None
361360
for _ in range(5):
362361
await maybe_sleep(1, is_async=is_async)
363362
list_response = await maybe_await(rq_client.list_requests())
363+
assert isinstance(list_response, ListPage)
364364
if list_response.items and len(list_response.items) == 10:
365365
break
366366

@@ -379,10 +379,10 @@ async def test_request_queue_batch_delete_requests(client: ApifyClient | ApifyCl
379379
assert len(delete_response.processed_requests) == 5
380380

381381
# Poll until deletions are reflected (eventual consistency)
382-
remaining: ListPage[Request] | None = None
383382
for _ in range(5):
384383
await maybe_sleep(1, is_async=is_async)
385384
remaining = await maybe_await(rq_client.list_requests())
385+
assert isinstance(remaining, ListPage)
386386
if remaining.items and len(remaining.items) == 5:
387387
break
388388

0 commit comments

Comments
 (0)