1919class HasItems (Protocol [T ]):
2020 """Structural contract for a single page of results from a paginated API endpoint.
2121
22- Implementations must expose `items`. They may optionally expose `count` — the number of items scanned by the API for
22+ Implementations must expose `items`. They may optionally expose `count` - the number of items scanned by the API for
2323 this page, which can exceed `len(items)` when filters drop items from the response. The iterator helpers consult
2424 `count` opportunistically via `getattr` for offset bookkeeping and fall back to `len(items)` when it is absent.
2525 """
@@ -41,8 +41,11 @@ def get_items_iterator(
4141 is used for offset bookkeeping (the Apify API's `count` reflects items scanned, which can exceed items returned when
4242 filters are applied).
4343
44- Iteration stops when a page returns no items or when the user-requested `limit` is reached. The `total` field is
45- intentionally not consulted, because it can change between calls.
44+ Iteration stops when a page scans no items (`count` is `0`, or `items` is empty when `count` is absent) or when the
45+ user-requested `limit` is reached. A page can scan items while returning none - filters like `clean` drop items from
46+ `items` but still count toward `count` - so terminating on scanned rather than returned items keeps the iterator
47+ advancing across fully-filtered pages. The `total` field is intentionally not consulted, because it can change
48+ between calls.
4649
4750 Args:
4851 callback: Function returning a single page of items.
@@ -62,9 +65,10 @@ def get_items_iterator(
6265 )
6366 yield from current_page .items
6467
65- fetched_items += max (getattr (current_page , 'count' , 0 ), len (current_page .items ))
68+ page_scanned = max (getattr (current_page , 'count' , 0 ), len (current_page .items ))
69+ fetched_items += page_scanned
6670
67- if not current_page . items or (initial_limit and fetched_items >= initial_limit ):
71+ if not page_scanned or (initial_limit and fetched_items >= initial_limit ):
6872 break
6973
7074
@@ -92,9 +96,10 @@ async def get_items_iterator_async(
9296 for item in current_page .items :
9397 yield item
9498
95- fetched_items += max (getattr (current_page , 'count' , 0 ), len (current_page .items ))
99+ page_scanned = max (getattr (current_page , 'count' , 0 ), len (current_page .items ))
100+ fetched_items += page_scanned
96101
97- if not current_page . items or (initial_limit and fetched_items >= initial_limit ):
102+ if not page_scanned or (initial_limit and fetched_items >= initial_limit ):
98103 break
99104
100105
@@ -124,8 +129,11 @@ def get_cursor_iterator(
124129 """Yield individual items from cursor-paginated API responses.
125130
126131 Cursor pagination is restricted to the two API responses that expose it: `ListOfKeys` (for key-value store keys) and
127- `ListOfRequests` (for request queue requests). Iteration ends when a page returns no items, the next cursor is
128- `None`, or the user-requested `limit` is reached.
132+ `ListOfRequests` (for request queue requests). Iteration ends when the next cursor is `None` or the user-requested
133+ `limit` is reached. Emptiness alone does not stop iteration: server-side filters (such as the request-queue state
134+ `filter`) can drop every item on a page while a live cursor still points at more data, so termination relies on the
135+ cursor, not on whether a page returned items. Unlike offset responses, cursor responses expose no scanned-item
136+ `count`, so `count` cannot be used to detect a fully-filtered page here.
129137
130138 Args:
131139 callback: Function returning a single page of items. Receives `cursor` and `limit` kwargs.
@@ -144,12 +152,12 @@ def get_cursor_iterator(
144152 )
145153 yield from current_page .items
146154
147- fetched_items += max ( getattr ( current_page , 'count' , 0 ), len (current_page .items ) )
155+ fetched_items += len (current_page .items )
148156 cursor = (
149157 current_page .next_exclusive_start_key if isinstance (current_page , ListOfKeys ) else current_page .next_cursor
150158 )
151159
152- if not current_page . items or cursor is None or (initial_limit and fetched_items >= initial_limit ):
160+ if cursor is None or (initial_limit and fetched_items >= initial_limit ):
153161 break
154162
155163
@@ -189,12 +197,12 @@ async def get_cursor_iterator_async(
189197 for item in current_page .items :
190198 yield item
191199
192- fetched_items += max ( getattr ( current_page , 'count' , 0 ), len (current_page .items ) )
200+ fetched_items += len (current_page .items )
193201 cursor = (
194202 current_page .next_exclusive_start_key if isinstance (current_page , ListOfKeys ) else current_page .next_cursor
195203 )
196204
197- if not current_page . items or cursor is None or (initial_limit and fetched_items >= initial_limit ):
205+ if cursor is None or (initial_limit and fetched_items >= initial_limit ):
198206 break
199207
200208
0 commit comments