Skip to content

Add auto-pagination helpers and client iterators - #124

Open
Sh4reef wants to merge 1 commit into
prooflinedev:mainfrom
Sh4reef:feat/auto-pagination-iterators
Open

Sh4reef wants to merge 1 commit into
prooflinedev:mainfrom
Sh4reef:feat/auto-pagination-iterators

Conversation

@Sh4reef

@Sh4reef Sh4reef commented Nov 27, 2026 •

Copy link
Copy Markdown

Who this helps

  • Platform & DevEx Engineers building dashboards, compliance reports, or analytics pipelines that aggregate reviews across organizations and pull requests.
  • CI/CD & Automation Authors writing scripts that scan pull request reviews and stop early as soon as a target condition (such as a failed review) is reached.
  • SDK Consumers using ProoflineClient or AsyncProoflineClient who want idiomatic for / async for lazy iteration and bounded eager collection without writing manual next_cursor loops.

Before (Manual Cursor Tracking) vs. After (Auto-Pagination)

Before — manual while cursor loop with truncated, cycle, and bound handling:

from uuid import UUID
from proofline import ProoflineClient, ProoflineError

with ProoflineClient() as client:
    cursor: str | None = None
    seen_cursors: set[str] = set()
    pages_fetched = 0
    items_checked = 0
    failed_review = None

    while pages_fetched < 10 and items_checked < 200:
        page = client.list_pull_request_reviews(
            UUID(organization_id),
            "owner/repo",
            42,
            filters={"limit": 50, "cursor": cursor},
        )
        pages_fetched += 1
        for review in page.items:
            items_checked += 1
            if review.status == "FAILED":
                failed_review = review
                break
            if items_checked >= 200:
                break
        if failed_review is not None or not page.truncated:
            break
        if not page.next_cursor or page.next_cursor in seen_cursors:
            raise ProoflineError(
                0,
                "urn:proofline:sdk:invalid-pagination",
                "The review page has no new continuation cursor.",
            )
        seen_cursors.add(page.next_cursor)
        cursor = page.next_cursor

After — lazy auto-pagination with early break, eager bounded collection, and generic paginate():

from uuid import UUID
from proofline import PageResponse, ProoflineClient, paginate

with ProoflineClient() as client:
    # Lazy iteration: stops fetching pages immediately on early break,
    # while handling truncated flags, cursor cycle detection, and bounds.
    failed_review = None
    for review in client.iter_pull_request_reviews(
        UUID(organization_id),
        "owner/repo",
        42,
        page_size=50,
        max_pages=10,
        max_items=200,
    ):
        if review.status == "FAILED":
            failed_review = review
            break

    # Eager bounded collection when all matching items are needed
    all_pr_reviews = client.list_all_pull_request_reviews(
        UUID(organization_id),
        "owner/repo",
        42,
        max_items=200,
    )

# Or use the transport-agnostic paginate() helper for custom cursor endpoints
items = list(
    paginate(
        lambda cursor: PageResponse(items=["a", "b"], next_cursor=None),
        max_items=200,
        max_pages=10,
    )
)

Design & Edge-Case Trade-offs

  • Transport-agnostic pagination primitives (proofline/pagination.py): Added PageResponse[T], paginate, async_paginate, collect_all, and async_collect_all (re-exported from proofline/__init__.py) with zero new runtime dependencies.
  • Client wrappers (proofline/client.py): Added iter_pull_request_reviews, list_all_reviews, and list_all_pull_request_reviews on both ProoflineClient and AsyncProoflineClient, keeping existing iter_reviews and iter_export_pages behavior backward-compatible.
  • Generated code isolation: Zero modifications under proofline/_generated/. Updated scripts/contracts.py to cover the new hand-written pagination exports.
  • Strict lazy evaluation & early termination: Page N + 1 is never fetched until all items from page N are consumed; breaking out of a loop or closing/aclose()ing the generator stops further HTTP requests immediately.
  • Eager bound validation: Invalid max_items or max_pages (non-positive integers, bool, float, etc.) raise ValueError eagerly when constructing the iterator—before any network call is issued.
  • Graceful bounds vs. infinite-loop protection: Reaching max_items or max_pages stops iteration cleanly; empty intermediate pages with a valid next_cursor continue up to max_pages; empty or repeated/cyclic cursors raise ProoflineError(0, "urn:proofline:sdk:invalid-pagination", ...).
  • Error & cancellation propagation: Transport/HTTP errors and asyncio.CancelledError propagate directly to the caller's loop without being swallowed.
  • Documentation (README.md): Added an "Auto-Pagination" section with sync, async, early-termination, and custom paginate examples.

How it was tested

Full SDK check suite (scripts/check.py)

Ran the full repository verification script in sdk-python/ (venv, CPython 3.13.18), covering OpenAPI client regeneration verification, contracts, ruff check, ruff format --check, mypy, full pytest suite, sdist/wheel build, and wheel/sdist installed sync/async HTTP smoke tests:

$ source venv/bin/activate && python scripts/check.py
Using CPython 3.13.18 interpreter at: venv/bin/python
...
Python operation and outcome-vocabulary contracts passed.
All checks passed!
239 files already formatted
Success: no issues found in 230 source files
........................................................................ [ 27%]
........................................................................ [ 55%]
........................................................................ [ 83%]
...........................................                              [100%]
259 passed in 27.67s
* Getting build dependencies for sdist...
* Building sdist...
* Building wheel from sdist
* Getting build dependencies for wheel...
* Building wheel...
Successfully built proofline_sdk-10.2.2.tar.gz and proofline_sdk-10.2.2-py3-none-any.whl
...
Installed Python SDK sync/async HTTP smoke passed.
...
Installed Python SDK sync/async HTTP smoke passed.
Python SDK generation, HTTP contracts, wheel and source installs passed.

Visual review (AI agent browser)

  • Verified auto-pagination behavior visually in Chrome via the AI agent browser (1440×900, 0 console errors, 0 failed HTTP requests)
    • Prompt used: /browser do a visual review of the implemented auto-pagination feature, build the visual review example with a production grade style for best visualization.
    • Built a local interactive visual review dashboard backed by the real proofline Python SDK (ProoflineClient.iter_pull_request_reviews, ProoflineClient.list_all_pull_request_reviews, proofline.paginate) against a loopback Proofline HTTP server, and verified all 6 scenarios:
      • Multi-Page Lazy: 3/3 pages fetched, 6 items yielded, completed
      • Early Break: 1/5 pages fetched, 2 items yielded, 4 requests avoided (early_break, pages 2–5 not fetched)
      • max_items=4: 2/3 pages fetched, 4 items yielded, 1 request avoided (max_items)
      • max_pages=2: 2/5 pages fetched, 4 items yielded, 3 requests avoided (max_pages)
      • Empty Page Skip: 2/2 pages fetched, skipped empty page 1 with next_cursor="c1", yielded 2 items (completed)
      • Cycle Guard: 2/2 pages fetched, repeated cursor "c1" raised ProoflineError (urn:proofline:sdk:invalid-pagination)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant