cloudflare-python icon indicating copy to clipboard operation
cloudflare-python copied to clipboard

Fix/pagination infinite loop

Open AzmainMahatab opened this issue 10 months ago • 0 comments

  • [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:

  1. Updated Sync and Async paginators to strictly stop iteration when current_page >= total_pages.
  2. Updated V4PagePaginationArrayResultInfo model to include the missing total_pages, total_count, and count fields 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.py to 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:

  • ImportError in radar tests (circular imports upstream).
  • AuthenticationError in cloudforce_one tests (upstream mock server auth).

My specific unit tests for pagination pass, and the formatting checks are green.

AzmainMahatab avatar Jan 09 '26 08:01 AzmainMahatab