From d08d31089600a8fdc7a9a1de58b76c105f88b28d Mon Sep 17 00:00:00 2001 From: Sarthak Mohite Date: Thu, 3 Dec 2026 07:57:08 +0000 Subject: [PATCH 1/8] Add iter_pull_request_reviews to the sync and async clients --- README.md | 2 +- proofline/client.py | 80 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_http.py | 74 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 250fb32e..9918a82b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ with ProoflineClient(os.environ["PROOFLINE_TOKEN"]) as client: When no token is passed, the clients read `PROOFLINE_TOKEN` from the environment, keep the token in memory, and redact it from diagnostic output. -With `AsyncProoflineClient`, use `await` for a single result, such as `await client.list_reviews(organization_id)` for one page. Use `async for` with `iter_reviews` to read individual reviews across pages, or with `iter_export_pages` to read export pages. The async client supports asyncio and Trio. For a Trio application, install Trio alongside the SDK. +With `AsyncProoflineClient`, use `await` for a single result, such as `await client.list_reviews(organization_id)` for one page. Use `async for` with `iter_reviews` to read individual reviews across pages, with `iter_pull_request_reviews` to read every review of one pull request, or with `iter_export_pages` to read export pages. The async client supports asyncio and Trio. For a Trio application, install Trio alongside the SDK. ```python import asyncio diff --git a/proofline/client.py b/proofline/client.py index 49e1a9e1..4a7e9e00 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -539,6 +539,46 @@ def iter_reviews( params["cursor"] = cursor raise _pagination_limit() + def iter_pull_request_reviews( + self, + organization_id: UUID, + repository: str, + pull_request_number: int, + *, + page_size: int = 100, + max_pages: int = 100, + max_items: int = 10_000, + filters: ReviewFilters | None = None, + ) -> Iterator[ReviewSummary]: + """Stream every review of one pull request, following cursors across pages. + + Takes the same bounds and refusals as ``iter_reviews``. + """ + _page_limits(page_size, max_pages, max_items) + params: ReviewFilters = {} + if filters: + params.update(filters) + params["limit"] = page_size + seen: set[str] = set() + cursor = params.get("cursor") + if cursor: + seen.add(cursor) + count = 0 + for _ in range(max_pages): + page = self.list_pull_request_reviews( + organization_id, repository, pull_request_number, filters=params + ) + for item in page.items: + if count == max_items: + raise _pagination_limit() + yield item + count += 1 + cursor = _next_cursor(page, seen) + if cursor is None: + return + params["cursor"] = cursor + raise _pagination_limit() + def wait_for_review( self, organization_id: UUID, @@ -936,6 +976,46 @@ async def iter_reviews( params["cursor"] = cursor raise _pagination_limit() + async def iter_pull_request_reviews( + self, + organization_id: UUID, + repository: str, + pull_request_number: int, + *, + page_size: int = 100, + max_pages: int = 100, + max_items: int = 10_000, + filters: ReviewFilters | None = None, + ) -> AsyncIterator[ReviewSummary]: + """Stream every review of one pull request, following cursors across pages. + + Takes the same bounds and refusals as ``iter_reviews``. + """ + _page_limits(page_size, max_pages, max_items) + params: ReviewFilters = {} + if filters: + params.update(filters) + params["limit"] = page_size + seen: set[str] = set() + cursor = params.get("cursor") + if cursor: + seen.add(cursor) + count = 0 + for _ in range(max_pages): + page = await self.list_pull_request_reviews( + organization_id, repository, pull_request_number, filters=params + ) + for item in page.items: + if count == max_items: + raise _pagination_limit() + yield item + count += 1 + cursor = _next_cursor(page, seen) + if cursor is None: + return + params["cursor"] = cursor + raise _pagination_limit() + async def wait_for_review( self, organization_id: UUID, diff --git a/tests/test_http.py b/tests/test_http.py index 68c6228e..c6f2641c 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1863,6 +1863,80 @@ def respond(request: Request, index: int) -> Reply: assert raised.value.type == "urn:proofline:sdk:pagination-limit" +async def collect_pull_request(client: Client, **kwargs: Any) -> list[Any]: + iterator = client.iter_pull_request_reviews(ORG, "repo/name", 42, **kwargs) + if hasattr(iterator, "__aiter__"): + return [item async for item in iterator] + return list(iterator) + + +@MODES +async def test_pull_request_review_iterator_follows_cursors_and_preserves_filters( + mode: str, +) -> None: + def respond(request: Request, index: int) -> Reply: + url = urlsplit(request.path) + assert url.path == f"/api/v1/organizations/{ORG}/reviews/pr/repo%2Fname/42" + assert request.headers["Authorization"] == f"Bearer {TOKEN}" + query = parse_qs(url.query) + assert query["repository"] == [str(REPOSITORY)] + assert query["status"] == ["COMPLETED"] + assert query["limit"] == ["2"] + assert query["search"] == ["text & space"] + if index == 0: + assert "cursor" not in query + else: + assert query["cursor"] == ["opaque+/cursor"] + return Reply( + body=page(cursor="opaque+/cursor" if index == 0 else None, truncated=index == 0) + ) + + with Server(respond) as server: + async with session(mode, server.url) as client: + items = await collect_pull_request( + client, + page_size=2, + filters={ + "repository": REPOSITORY, + "status": "COMPLETED", + "search": "text & space", + }, + ) + assert [item.id for item in items] == [REVIEW, REVIEW] + assert len(server.requests) == 2 + + +@MODES +async def test_pull_request_review_iterator_refuses_cycles_and_bounds(mode: str) -> None: + for cursor in ["repeated", None]: + with Server( + lambda request, index: Reply(body=page(cursor=cursor, truncated=True)) + ) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await collect_pull_request(client) + assert raised.value.type == "urn:proofline:sdk:invalid-pagination" + assert len(server.requests) <= 2 + with Server( + lambda request, index: Reply(body=page(cursor=str(index), truncated=True)) + ) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await collect_pull_request(client, max_pages=2) + assert raised.value.type == "urn:proofline:sdk:pagination-limit" + assert len(server.requests) == 2 + two_items = page() + two_items["items"].append(dict(two_items["items"][0])) + with Server(lambda request, index: Reply(body=two_items)) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await collect_pull_request(client, max_items=1, filters={"cursor": "initial"}) + assert raised.value.type == "urn:proofline:sdk:pagination-limit" + async with session(mode, "https://unused.example") as client: + with pytest.raises(ValueError): + await collect_pull_request(client, page_size=0) + + def test_token_environment_and_https_origin_rejection(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("PROOFLINE_TOKEN", TOKEN) with ProoflineClient() as client: From dcf6e682e6767f94ff35b4efde2a931cd66a29b5 Mon Sep 17 00:00:00 2001 From: Sarthzk Date: Thu, 3 Dec 2026 15:29:42 +0530 Subject: [PATCH 2/8] Share pagination setup between iterators and test filters are not mutated Co-Authored-By: Claude Sonnet 5.5 --- proofline/client.py | 53 +++++++++++++++------------------------------ tests/test_http.py | 16 ++++++-------- 2 files changed, 24 insertions(+), 45 deletions(-) diff --git a/proofline/client.py b/proofline/client.py index 4a7e9e00..8c0fd2c2 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -153,6 +153,19 @@ def _page_limits(page_size: int, max_pages: int, max_items: int) -> None: ) +def _iter_start( + page_size: int, max_pages: int, max_items: int, filters: ReviewFilters | None +) -> tuple[ReviewFilters, set[str]]: + """Validate the bounds; return a private copy of the filters and the cursors already seen.""" + _page_limits(page_size, max_pages, max_items) + params: ReviewFilters = {} + if filters: + params.update(filters) + params["limit"] = page_size + cursor = params.get("cursor") + return params, {cursor} if cursor else set() + + def _head_commit(value: str | None) -> None: # Only a full SHA names one exact commit; a branch or an abbreviation does not. if value is not None and ( @@ -516,15 +529,7 @@ def iter_reviews( max_items: int = 10_000, filters: ReviewFilters | None = None, ) -> Iterator[ReviewSummary]: - _page_limits(page_size, max_pages, max_items) - params: ReviewFilters = {} - if filters: - params.update(filters) - params["limit"] = page_size - seen: set[str] = set() - cursor = params.get("cursor") - if cursor: - seen.add(cursor) + params, seen = _iter_start(page_size, max_pages, max_items, filters) count = 0 for _ in range(max_pages): page = self.list_reviews(organization_id, **params) @@ -554,15 +559,7 @@ def iter_pull_request_reviews( Takes the same bounds and refusals as ``iter_reviews``. """ - _page_limits(page_size, max_pages, max_items) - params: ReviewFilters = {} - if filters: - params.update(filters) - params["limit"] = page_size - seen: set[str] = set() - cursor = params.get("cursor") - if cursor: - seen.add(cursor) + params, seen = _iter_start(page_size, max_pages, max_items, filters) count = 0 for _ in range(max_pages): page = self.list_pull_request_reviews( @@ -953,15 +950,7 @@ async def iter_reviews( max_items: int = 10_000, filters: ReviewFilters | None = None, ) -> AsyncIterator[ReviewSummary]: - _page_limits(page_size, max_pages, max_items) - params: ReviewFilters = {} - if filters: - params.update(filters) - params["limit"] = page_size - seen: set[str] = set() - cursor = params.get("cursor") - if cursor: - seen.add(cursor) + params, seen = _iter_start(page_size, max_pages, max_items, filters) count = 0 for _ in range(max_pages): page = await self.list_reviews(organization_id, **params) @@ -991,15 +980,7 @@ async def iter_pull_request_reviews( Takes the same bounds and refusals as ``iter_reviews``. """ - _page_limits(page_size, max_pages, max_items) - params: ReviewFilters = {} - if filters: - params.update(filters) - params["limit"] = page_size - seen: set[str] = set() - cursor = params.get("cursor") - if cursor: - seen.add(cursor) + params, seen = _iter_start(page_size, max_pages, max_items, filters) count = 0 for _ in range(max_pages): page = await self.list_pull_request_reviews( diff --git a/tests/test_http.py b/tests/test_http.py index c6f2641c..7c2f31e1 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1893,15 +1893,13 @@ def respond(request: Request, index: int) -> Reply: with Server(respond) as server: async with session(mode, server.url) as client: - items = await collect_pull_request( - client, - page_size=2, - filters={ - "repository": REPOSITORY, - "status": "COMPLETED", - "search": "text & space", - }, - ) + filters: dict[str, Any] = { + "repository": REPOSITORY, + "status": "COMPLETED", + "search": "text & space", + } + items = await collect_pull_request(client, page_size=2, filters=filters) + assert set(filters) == {"repository", "status", "search"} assert [item.id for item in items] == [REVIEW, REVIEW] assert len(server.requests) == 2 From 851182e39f99b9b9dabfd1313932d2623b5cabb0 Mon Sep 17 00:00:00 2001 From: Sarthzk Date: Thu, 3 Dec 2026 15:57:16 +0530 Subject: [PATCH 3/8] Describe iter_pull_request_reviews bounds accurately in the README Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9918a82b..0a0820e3 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ with ProoflineClient(os.environ["PROOFLINE_TOKEN"]) as client: When no token is passed, the clients read `PROOFLINE_TOKEN` from the environment, keep the token in memory, and redact it from diagnostic output. -With `AsyncProoflineClient`, use `await` for a single result, such as `await client.list_reviews(organization_id)` for one page. Use `async for` with `iter_reviews` to read individual reviews across pages, with `iter_pull_request_reviews` to read every review of one pull request, or with `iter_export_pages` to read export pages. The async client supports asyncio and Trio. For a Trio application, install Trio alongside the SDK. +With `AsyncProoflineClient`, use `await` for a single result, such as `await client.list_reviews(organization_id)` for one page. Use `async for` with `iter_reviews` to read individual reviews across pages, with `iter_pull_request_reviews` to read the reviews of one pull request across pages up to the `page_size`, `max_pages` and `max_items` bounds (a pagination-limit error is raised once a bound is exceeded, after some reviews may already have been yielded), or with `iter_export_pages` to read export pages. The async client supports asyncio and Trio. For a Trio application, install Trio alongside the SDK. ```python import asyncio From 1fa2bfaa6d4cdf23bc1c2724f5df9973efaacf77 Mon Sep 17 00:00:00 2001 From: Sarthzk Date: Thu, 3 Dec 2026 15:57:16 +0530 Subject: [PATCH 4/8] Require real ints for pagination bounds bool and float values such as max_items=1.5 passed _page_limits or failed later inside range(). Reject them with the same ValueError. Co-Authored-By: Claude Sonnet 5.5 --- proofline/client.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/proofline/client.py b/proofline/client.py index 8c0fd2c2..96c6f5ee 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -147,7 +147,14 @@ def _review_url(base_url: str, organization_id: UUID, review_id: UUID) -> str: def _page_limits(page_size: int, max_pages: int, max_items: int) -> None: - if not 1 <= page_size <= 100 or not 1 <= max_pages <= 1_000 or not 1 <= max_items <= 100_000: + if ( + any( + isinstance(n, bool) or not isinstance(n, int) for n in (page_size, max_pages, max_items) + ) + or not 1 <= page_size <= 100 + or not 1 <= max_pages <= 1_000 + or not 1 <= max_items <= 100_000 + ): raise ValueError( "Pagination requires page_size 1..100, max_pages 1..1000, max_items 1..100000." ) From cae2b0f3497a0aecd29b143a20a52f7c816596f3 Mon Sep 17 00:00:00 2001 From: Sarthzk Date: Thu, 3 Dec 2026 15:57:16 +0530 Subject: [PATCH 5/8] Tighten pull request iterator tests and cover integer bounds Co-Authored-By: Claude Sonnet 5.5 --- tests/test_http.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/tests/test_http.py b/tests/test_http.py index 7c2f31e1..4314da7f 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -16,6 +16,7 @@ import traceback from collections.abc import AsyncIterator from contextlib import asynccontextmanager +from copy import deepcopy from datetime import UTC, datetime, timedelta from pathlib import Path from typing import Any @@ -1863,6 +1864,9 @@ def respond(request: Request, index: int) -> Reply: assert raised.value.type == "urn:proofline:sdk:pagination-limit" +SECOND_REVIEW = "00000000-0000-4000-8000-000000000099" + + async def collect_pull_request(client: Client, **kwargs: Any) -> list[Any]: iterator = client.iter_pull_request_reviews(ORG, "repo/name", 42, **kwargs) if hasattr(iterator, "__aiter__"): @@ -1887,9 +1891,9 @@ def respond(request: Request, index: int) -> Reply: assert "cursor" not in query else: assert query["cursor"] == ["opaque+/cursor"] - return Reply( - body=page(cursor="opaque+/cursor" if index == 0 else None, truncated=index == 0) - ) + body = page(cursor="opaque+/cursor" if index == 0 else None, truncated=index == 0) + body["items"][0]["id"] = SECOND_REVIEW if index else str(REVIEW) + return Reply(body=body) with Server(respond) as server: async with session(mode, server.url) as client: @@ -1898,15 +1902,16 @@ def respond(request: Request, index: int) -> Reply: "status": "COMPLETED", "search": "text & space", } + original = deepcopy(filters) items = await collect_pull_request(client, page_size=2, filters=filters) - assert set(filters) == {"repository", "status", "search"} - assert [item.id for item in items] == [REVIEW, REVIEW] + assert filters == original + assert [str(item.id) for item in items] == [str(REVIEW), SECOND_REVIEW] assert len(server.requests) == 2 @MODES async def test_pull_request_review_iterator_refuses_cycles_and_bounds(mode: str) -> None: - for cursor in ["repeated", None]: + for cursor, requests in [("repeated", 2), (None, 1)]: with Server( lambda request, index: Reply(body=page(cursor=cursor, truncated=True)) ) as server: @@ -1914,7 +1919,7 @@ async def test_pull_request_review_iterator_refuses_cycles_and_bounds(mode: str) with pytest.raises(ProoflineError) as raised: await collect_pull_request(client) assert raised.value.type == "urn:proofline:sdk:invalid-pagination" - assert len(server.requests) <= 2 + assert len(server.requests) == requests with Server( lambda request, index: Reply(body=page(cursor=str(index), truncated=True)) ) as server: @@ -1935,6 +1940,19 @@ async def test_pull_request_review_iterator_refuses_cycles_and_bounds(mode: str) await collect_pull_request(client, page_size=0) +@MODES +@pytest.mark.parametrize("name", ["page_size", "max_pages", "max_items"]) +@pytest.mark.parametrize("bound", [True, 1.5, 2.0, "2"]) +async def test_review_iterators_require_integer_bounds(mode: str, name: str, bound: Any) -> None: + with Server(lambda request, index: Reply(body=page())) as server: + async with session(mode, server.url) as client: + with pytest.raises(ValueError, match="Pagination requires"): + await collect(client, **{name: bound}) + with pytest.raises(ValueError, match="Pagination requires"): + await collect_pull_request(client, **{name: bound}) + assert not server.requests + + def test_token_environment_and_https_origin_rejection(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("PROOFLINE_TOKEN", TOKEN) with ProoflineClient() as client: From 901940c5e86f4424f9076001f7548e05a4e16285 Mon Sep 17 00:00:00 2001 From: Sarthzk Date: Thu, 3 Dec 2026 16:03:04 +0530 Subject: [PATCH 6/8] Describe page_size as the per-request size in the README Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0a0820e3..6d166f11 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ with ProoflineClient(os.environ["PROOFLINE_TOKEN"]) as client: When no token is passed, the clients read `PROOFLINE_TOKEN` from the environment, keep the token in memory, and redact it from diagnostic output. -With `AsyncProoflineClient`, use `await` for a single result, such as `await client.list_reviews(organization_id)` for one page. Use `async for` with `iter_reviews` to read individual reviews across pages, with `iter_pull_request_reviews` to read the reviews of one pull request across pages up to the `page_size`, `max_pages` and `max_items` bounds (a pagination-limit error is raised once a bound is exceeded, after some reviews may already have been yielded), or with `iter_export_pages` to read export pages. The async client supports asyncio and Trio. For a Trio application, install Trio alongside the SDK. +With `AsyncProoflineClient`, use `await` for a single result, such as `await client.list_reviews(organization_id)` for one page. Use `async for` with `iter_reviews` to read individual reviews across pages, with `iter_pull_request_reviews` to read the reviews of one pull request across pages. `page_size` only sets how many reviews each request asks for; `max_pages` and `max_items` are the traversal bounds, and a pagination-limit error is raised once either is exceeded, after some reviews may already have been yielded, or with `iter_export_pages` to read export pages. The async client supports asyncio and Trio. For a Trio application, install Trio alongside the SDK. ```python import asyncio From 4a30a0ecfdb47e5ab6981a885288394fe9f05ec0 Mon Sep 17 00:00:00 2001 From: Sarthzk Date: Thu, 3 Dec 2026 16:03:04 +0530 Subject: [PATCH 7/8] Test initial-cursor cycle and the exact max_items cap for pull request reviews Co-Authored-By: Claude Sonnet 5.5 --- tests/test_http.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/test_http.py b/tests/test_http.py index 4314da7f..9bb93587 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -14,7 +14,7 @@ import time import tomllib import traceback -from collections.abc import AsyncIterator +from collections.abc import AsyncIterator, Callable from contextlib import asynccontextmanager from copy import deepcopy from datetime import UTC, datetime, timedelta @@ -1928,13 +1928,31 @@ async def test_pull_request_review_iterator_refuses_cycles_and_bounds(mode: str) await collect_pull_request(client, max_pages=2) assert raised.value.type == "urn:proofline:sdk:pagination-limit" assert len(server.requests) == 2 - two_items = page() - two_items["items"].append(dict(two_items["items"][0])) - with Server(lambda request, index: Reply(body=two_items)) as server: + with Server( + lambda request, index: Reply(body=page(cursor="initial", truncated=True)) + ) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await collect_pull_request(client, filters={"cursor": "initial"}) + assert raised.value.type == "urn:proofline:sdk:invalid-pagination" + assert len(server.requests) == 1 + + def one_item_pages(last: int) -> Callable[[Request, int], Reply]: + return lambda request, index: Reply( + body=page(cursor=None if index == last else f"c{index}", truncated=index != last) + ) + + # Two items fit under max_items=2; the cap only trips on the third. + with Server(one_item_pages(1)) as server: + async with session(mode, server.url) as client: + assert len(await collect_pull_request(client, max_items=2)) == 2 + assert len(server.requests) == 2 + with Server(one_item_pages(2)) as server: async with session(mode, server.url) as client: with pytest.raises(ProoflineError) as raised: - await collect_pull_request(client, max_items=1, filters={"cursor": "initial"}) + await collect_pull_request(client, max_items=2) assert raised.value.type == "urn:proofline:sdk:pagination-limit" + assert len(server.requests) == 3 async with session(mode, "https://unused.example") as client: with pytest.raises(ValueError): await collect_pull_request(client, page_size=0) From 1690e412392ab73ee77c1f2e0c2c2b81e67c74de Mon Sep 17 00:00:00 2001 From: Sarthzk Date: Thu, 3 Dec 2026 16:07:16 +0530 Subject: [PATCH 8/8] Keep the async-for iterator list intact in the README Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d166f11..defb50ec 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ with ProoflineClient(os.environ["PROOFLINE_TOKEN"]) as client: When no token is passed, the clients read `PROOFLINE_TOKEN` from the environment, keep the token in memory, and redact it from diagnostic output. -With `AsyncProoflineClient`, use `await` for a single result, such as `await client.list_reviews(organization_id)` for one page. Use `async for` with `iter_reviews` to read individual reviews across pages, with `iter_pull_request_reviews` to read the reviews of one pull request across pages. `page_size` only sets how many reviews each request asks for; `max_pages` and `max_items` are the traversal bounds, and a pagination-limit error is raised once either is exceeded, after some reviews may already have been yielded, or with `iter_export_pages` to read export pages. The async client supports asyncio and Trio. For a Trio application, install Trio alongside the SDK. +With `AsyncProoflineClient`, use `await` for a single result, such as `await client.list_reviews(organization_id)` for one page. Use `async for` with `iter_reviews` to read individual reviews across pages, with `iter_pull_request_reviews` to read the reviews of one pull request, or with `iter_export_pages` to read export pages. `iter_reviews` and `iter_pull_request_reviews` raise a pagination-limit error once `max_pages` or `max_items` is exceeded, after some items may already have been yielded; `page_size` only sets the size of each request. The async client supports asyncio and Trio. For a Trio application, install Trio alongside the SDK. ```python import asyncio