cloudflare-python
cloudflare-python copied to clipboard
Fix/pagination infinite loop
- [x] I understand that this repository is auto-generated, and my pull request may not be merged
Changes being requested
This PR fixes a critical logic bug in pagination.py that causes infinite loops when iterating over results.
The Bug:
The next_page_info iterator blindly increments the page number even when the API returns the last page's data repeatedly for out-of-bounds requests (instead of 404s).
The Fix:
- Updated
SyncandAsyncpaginators to strictly stop iteration whencurrent_page >= total_pages. - Updated
V4PagePaginationArrayResultInfomodel to include the missingtotal_pages,total_count, andcountfields so the SDK can correctly read the metadata.
Verification:
- Verified locally with a reproduction script against the live
client.accounts.list()endpoint. - Added unit tests in
tests/test_pagination_fix.pyto verify the logic in isolation.
Additional context & links
Example codes to test:
from cloudflare import Cloudflare, AsyncCloudflare
api_token = "redacted"
cloudflare = Cloudflare(api_token=api_token)
for account in cloudflare.accounts.list():
print(account.name)
async_cloudflare = AsyncCloudflare(api_token=api_token)
async for account in async_cloudflare.accounts.list():
print(account .name)
It was looping indefinitely by printing the same account, but with my fix, the loop properly ends.
Note on CI: I noticed unrelated CI failures:
ImportErrorinradartests (circular imports upstream).AuthenticationErrorincloudforce_onetests (upstream mock server auth).
My specific unit tests for pagination pass, and the formatting checks are green.