Conversation
|
| backward = page._direction == "backward" | ||
| while True: | ||
| yield from page.data | ||
| items = reversed(page.data) if backward else page.data |
There was a problem hiding this comment.
Backward pages lose requested order When a caller uses
order="normal" with a before cursor, results are documented as descending even though before fetches older records. This reversal yields each page in ascending order instead, so sync and async iteration no longer preserve the requested order.
Knowledge Base Used: Pagination and shared types
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/workos/_pagination.py
Line: 75
Comment:
**Backward pages lose requested order** When a caller uses `order="normal"` with a `before` cursor, results are documented as descending even though `before` fetches older records. This reversal yields each page in ascending order instead, so sync and async iteration no longer preserve the requested order.
**Knowledge Base Used:** [Pagination and shared types](https://app.greptile.com/workos/-/custom-context/knowledge-base/workos/workos-python/-/docs/pagination-and-shared-types.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| break | ||
| page = page._fetch_page(after=page.after) | ||
| if backward: | ||
| if page.before is None or page._fetch_page is None: |
There was a problem hiding this comment.
Events pagination stops early
list_events(before=...) returns pagination metadata with only an after cursor. This new backward branch requires page.before, so it reverses the first page and stops without fetching further results. The async iterator has the same behavior.
Knowledge Base Used: Pagination and shared types
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/workos/_pagination.py
Line: 80
Comment:
**Events pagination stops early** `list_events(before=...)` returns pagination metadata with only an `after` cursor. This new backward branch requires `page.before`, so it reverses the first page and stops without fetching further results. The async iterator has the same behavior.
**Knowledge Base Used:** [Pagination and shared types](https://app.greptile.com/workos/-/custom-context/knowledge-base/workos/workos-python/-/docs/pagination-and-shared-types.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
I confirmed that
auto_paging_iter()cannot page backward: after a call likelist_users(before="u21"), the follow-up request keeps the originalbeforeand addsafter, and the WorkOS API rejects any request carrying both cursors (Please provide either "after" or "before" parameters.). Backward pagination fails on page two; this fixes it.auto_paging_iter()now follows the direction of the initial request: a page first fetched withbefore(and noafter) keeps paging backward withbefore; anything else pages forward withafterexactly as before.beforeis dropped going forward, andafteris dropped going backward. The caller's params dict is never modified.list(list_users(before="u21", limit=10).auto_paging_iter())returns u20 down to u1.Test plan
Red-first evidence, recorded against mocked transports that capture every request's query string:
origin/main(9bbc82d),mise x uv -- uv run pytest tests/test_pagination.py -q→ 2 failed, 13 passed: the new backward tests (sync + async) fail because items come back in forward order, and a probe of the same path shows the second request going out as?limit=10&before=u21&order=desc&after=u21— the both-cursors shape the API rejects.before=u21thenbefore=u11and noafterin any request; forward iteration yields u1..u100 withafter=u10,after=u20, …; the caller's params dict is asserted unchanged after full iteration.