From 3c9bdc9b289ecca4712004dcf2230a6068872f88 Mon Sep 17 00:00:00 2001 From: Rajdip K <62926121+rajdipk@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:15:27 +0530 Subject: [PATCH 1/4] Add iter_export_rows to read export rows across pages Adds iter_export_rows to ProoflineClient and AsyncProoflineClient. It reuses iter_export_pages for cursors, filters and max_pages, and adds a max_rows bound that raises pagination-limit instead of truncating. Co-Authored-By: Claude Opus 5.5 --- proofline/client.py | 87 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/proofline/client.py b/proofline/client.py index 49e1a9e1..c1fbaff7 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -194,6 +194,15 @@ def _export_page_limit(max_pages: int) -> None: raise ValueError("Export pagination requires max_pages 1..1000.") +def _export_row_limit(max_rows: int) -> None: + if ( + isinstance(max_rows, bool) + or not isinstance(max_rows, int) + or not 1 <= max_rows <= 1_000_000 + ): + raise ValueError("Export row iteration requires max_rows 1..1000000.") + + def _export_next_cursor(page: ExportPage, seen: set[str]) -> str | None: cursor = page.next_cursor if cursor is None: @@ -336,6 +345,45 @@ def iter_export_pages( return raise _pagination_limit("Export") + def iter_export_rows( + self, + organization_id: UUID, + dataset: ExportDataset, + *, + from_time: str, + to_time: str, + format: ExportFormat = "ndjson", + cursor: str | None = None, + group_by: str | None = None, + max_pages: int = 100, + max_rows: int = 100_000, + ) -> Iterator[dict[str, Any]]: + """Stream decoded export rows across pages, within the page and row bounds. + + Rows keep the types their page decoded: CSV cells are strings and NDJSON values keep + their JSON types. A traversal that would pass max_pages or max_rows raises a + pagination-limit error instead of stopping silently. To resume a traversal, use + iter_export_pages, whose pages carry next_cursor. + """ + _export_page_limit(max_pages) + _export_row_limit(max_rows) + count = 0 + for page in self.iter_export_pages( + organization_id, + dataset, + from_time=from_time, + to_time=to_time, + format=format, + cursor=cursor, + group_by=group_by, + max_pages=max_pages, + ): + for row in page.rows: + if count == max_rows: + raise _pagination_limit("Export") + yield row + count += 1 + def list_reviews(self, organization_id: UUID, **filters: Unpack[ReviewFilters]) -> ReviewPage: return self._call(list_reviews.sync_detailed, ReviewPage, organization_id, **filters) @@ -708,6 +756,45 @@ async def iter_export_pages( return raise _pagination_limit("Export") + async def iter_export_rows( + self, + organization_id: UUID, + dataset: ExportDataset, + *, + from_time: str, + to_time: str, + format: ExportFormat = "ndjson", + cursor: str | None = None, + group_by: str | None = None, + max_pages: int = 100, + max_rows: int = 100_000, + ) -> AsyncIterator[dict[str, Any]]: + """Stream decoded export rows across pages, within the page and row bounds. + + Rows keep the types their page decoded: CSV cells are strings and NDJSON values keep + their JSON types. A traversal that would pass max_pages or max_rows raises a + pagination-limit error instead of stopping silently. To resume a traversal, use + iter_export_pages, whose pages carry next_cursor. + """ + _export_page_limit(max_pages) + _export_row_limit(max_rows) + count = 0 + async for page in self.iter_export_pages( + organization_id, + dataset, + from_time=from_time, + to_time=to_time, + format=format, + cursor=cursor, + group_by=group_by, + max_pages=max_pages, + ): + for row in page.rows: + if count == max_rows: + raise _pagination_limit("Export") + yield row + count += 1 + async def list_reviews( self, organization_id: UUID, **filters: Unpack[ReviewFilters] ) -> ReviewPage: From a3d571289adb80fcc199c7c7fb7ec0eb0fc9171b Mon Sep 17 00:00:00 2001 From: Rajdip K <62926121+rajdipk@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:16:21 +0530 Subject: [PATCH 2/4] Register iter_export_rows as a client helper in the contract check Co-Authored-By: Claude Opus 5.5 --- scripts/contracts.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/contracts.py b/scripts/contracts.py index 6e6255a2..e73b597b 100644 --- a/scripts/contracts.py +++ b/scripts/contracts.py @@ -42,7 +42,14 @@ def main() -> None: raise SystemExit("ReviewFilters differs from the list_reviews query parameters.") for client in (ProoflineClient, AsyncProoflineClient): - helpers = {"close", "aclose", "iter_export_pages", "iter_reviews", "wait_for_review"} + helpers = { + "close", + "aclose", + "iter_export_pages", + "iter_export_rows", + "iter_reviews", + "wait_for_review", + } exposed = { name for name in dir(client) From 0d1ddfe10a1d53eb274d6d3b993cf27871048f27 Mon Sep 17 00:00:00 2001 From: Rajdip K <62926121+rajdipk@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:16:56 +0530 Subject: [PATCH 3/4] Document iter_export_rows in the README export section Co-Authored-By: Claude Opus 5.5 --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 250fb32e..e7e27b6a 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,23 @@ with ProoflineClient() as client: Use `async for` with `AsyncProoflineClient`. Each yielded page retains its raw text, decoded rows, and metadata; CSV pages retain their individual headers. Pass `cursor=` to resume an export. Iteration stops only when the server omits the next cursor, including when an intermediate page has no rows. Empty or repeated continuation cursors raise `ProoflineError` with type `urn:proofline:sdk:invalid-pagination`, and the page that carries such a cursor is never yielded. The default bound is 100 pages; set `max_pages` to an integer from 1 to 1000. Exhausting that bound while a cursor remains raises `urn:proofline:sdk:pagination-limit`, so a partial export cannot silently appear complete. Stop iteration early when only a prefix is needed. +`iter_export_rows` yields the rows of those pages directly, for a caller that needs every row in the window rather than the pages that carry them: + +```python +with ProoflineClient() as client: + rows = list( + client.iter_export_rows( + UUID(os.environ["PROOFLINE_ORGANIZATION_ID"]), + "reviews", + from_time="2026-10-01T00:00:00Z", + to_time="2026-10-25T00:00:00Z", + ) + ) +print(len(rows)) +``` + +It follows the same cursors, filters and `max_pages` bound as `iter_export_pages`, and rows keep their page's decoding: CSV cells stay strings and NDJSON values keep their JSON types. `max_rows` bounds the rows it yields; the default is 100,000, and it accepts an integer from 1 to 1,000,000. A window with more rows than the bound raises `urn:proofline:sdk:pagination-limit` after yielding `max_rows` rows, so a partial window cannot silently appear complete. Use `async for` with `AsyncProoflineClient`. Rows carry no cursor, so use `iter_export_pages` to resume an interrupted export. + ## Coverage uploads To upload coverage reports from CI, use a repository-restricted token with `coverage:write` and current repository write access. Name the full commit SHA that the tests ran on. Send 1 to 50 LCOV, Cobertura XML, JaCoCo XML, or Go cover reports in a JSON body of at most 30 MiB. From 44120e1e4b45b983944c9d0630d5f879125a9cd9 Mon Sep 17 00:00:00 2001 From: Rajdip K <62926121+rajdipk@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:18:09 +0530 Subject: [PATCH 4/4] Test iter_export_rows on the sync and async clients Co-Authored-By: Claude Opus 5.5 --- tests/test_http.py | 185 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) diff --git a/tests/test_http.py b/tests/test_http.py index 68c6228e..cb7c97da 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -63,6 +63,7 @@ from proofline._generated.models.reviews_export_row import ReviewsExportRow from proofline._generated.types import UNSET from proofline._transport import REFUSAL_SHAPES +from proofline.exports import ExportFormat from .fixtures import ( ASSESSMENT, @@ -2159,6 +2160,190 @@ def respond(request: Request, index: int) -> Reply: assert len(server.requests) == yielded + 1 +def export_reply(body: bytes, format: str = "ndjson", next_cursor: str | None = None) -> Reply: + headers = { + "Content-Type": "text/csv" if format == "csv" else "application/x-ndjson", + "X-Proofline-Export-Version": "1", + "X-Proofline-Export-Settled-Until": "2026-10-25T00:00:00Z", + } + if next_cursor is not None: + headers["X-Proofline-Export-Next-Cursor"] = next_cursor + return Reply(body=body, headers=headers) + + +async def collect_rows(iterator: Any) -> list[dict[str, Any]]: + if hasattr(iterator, "__aiter__"): + return [row async for row in iterator] + return list(iterator) + + +@MODES +@pytest.mark.parametrize("format", ["csv", "ndjson"]) +async def test_export_rows_read_a_whole_window_across_pages( + mode: str, format: ExportFormat +) -> None: + # A caller summing a month of finding outcomes per repository should not have to + # handle cursors: rows arrive in page order, empty pages included, filters intact. + pages = { + "csv": [ + b"dataset_version,repository,accepted\r\n1,api,3\r\n1,web,1\r\n", + b"dataset_version,repository,accepted\r\n", + b"dataset_version,repository,accepted\r\n1,api,2\r\n", + ], + "ndjson": [ + b'{"dataset_version":1,"repository":"api","accepted":3}\n' + b'{"dataset_version":1,"repository":"web","accepted":1}\n', + b"", + b'{"dataset_version":1,"repository":"api","accepted":2}\n', + ], + }[format] + cursors = ["page/2", "page/3", None] + + def respond(request: Request, index: int) -> Reply: + assert request.path.startswith(f"/api/v1/organizations/{ORG}/exports/finding_outcomes?") + query = parse_qs(urlsplit(request.path).query) + expected = { + "from": ["2026-10-01T00:00:00Z"], + "to": ["2026-11-01T00:00:00Z"], + "format": [format], + "group_by": ["repository"], + } + if index: + expected["cursor"] = [f"page/{index + 1}"] + assert query == expected + return export_reply(pages[index], format, cursors[index]) + + with Server(respond) as server: + async with session(mode, server.url) as client: + rows = client.iter_export_rows( + ORG, + "finding_outcomes", + from_time="2026-10-01T00:00:00Z", + to_time="2026-11-01T00:00:00Z", + format=format, + group_by="repository", + ) + assert not server.requests + accepted: dict[str, int] = {} + for row in await collect_rows(rows): + accepted[row["repository"]] = accepted.get(row["repository"], 0) + int( + row["accepted"] + ) + assert accepted == {"api": 5, "web": 1} + assert len(server.requests) == 3 + + +@MODES +async def test_export_rows_keep_each_format_types(mode: str) -> None: + def respond(request: Request, index: int) -> Reply: + if "format=csv" in request.path: + return export_reply(b"dataset_version,count\r\n1,7\r\n", "csv") + return export_reply(b'{"dataset_version":1,"count":7}\n') + + with Server(respond) as server: + async with session(mode, server.url) as client: + start, end = "2026-10-01T00:00:00Z", "2026-11-01T00:00:00Z" + csv_rows = await collect_rows( + client.iter_export_rows(ORG, "reviews", format="csv", from_time=start, to_time=end) + ) + json_rows = await collect_rows( + client.iter_export_rows(ORG, "reviews", from_time=start, to_time=end) + ) + assert csv_rows == [{"dataset_version": "1", "count": "7"}] + assert json_rows == [{"dataset_version": 1, "count": 7}] + + +@MODES +@pytest.mark.parametrize("max_rows", [2, 3]) +async def test_export_rows_refuse_to_truncate_at_the_row_bound(mode: str, max_rows: int) -> None: + # Two pages of two rows: a bound below four must fail loudly, whether it falls on a + # page boundary (2) or inside a page (3), after yielding exactly max_rows rows. + def respond(request: Request, index: int) -> Reply: + body = b"".join(f'{{"dataset_version":1,"n":{index * 2 + n}}}\n'.encode() for n in range(2)) + return export_reply(body, next_cursor="page/2" if index == 0 else None) + + received: list[int] = [] + with Server(respond) as server: + async with session(mode, server.url) as client: + rows = client.iter_export_rows( + ORG, + "reviews", + from_time="2026-10-01T00:00:00Z", + to_time="2026-11-01T00:00:00Z", + max_rows=max_rows, + ) + with pytest.raises(ProoflineError) as raised: + while True: + received.append((await next_export(rows))["n"]) + assert raised.value.type == "urn:proofline:sdk:pagination-limit" + assert received == list(range(max_rows)) + assert len(server.requests) == 2 + + +@MODES +async def test_export_rows_end_exactly_at_the_row_bound(mode: str) -> None: + with Server(lambda request, index: export_reply(b'{"dataset_version":1}\n' * 3)) as server: + async with session(mode, server.url) as client: + rows = await collect_rows( + client.iter_export_rows( + ORG, + "reviews", + from_time="2026-10-01T00:00:00Z", + to_time="2026-11-01T00:00:00Z", + max_rows=3, + ) + ) + assert len(rows) == 3 + + +@MODES +async def test_export_rows_never_yield_rows_from_a_page_with_an_invalid_cursor(mode: str) -> None: + def respond(request: Request, index: int) -> Reply: + body = f'{{"dataset_version":1,"page":{index}}}\n'.encode() + return export_reply(body, next_cursor="same") + + received: list[int] = [] + with Server(respond) as server: + async with session(mode, server.url) as client: + rows = client.iter_export_rows( + ORG, "reviews", from_time="2026-10-01T00:00:00Z", to_time="2026-11-01T00:00:00Z" + ) + with pytest.raises(ProoflineError) as raised: + while True: + received.append((await next_export(rows))["page"]) + assert raised.value.type == "urn:proofline:sdk:invalid-pagination" + assert received == [0] + assert len(server.requests) == 2 + + +@MODES +@pytest.mark.parametrize( + ("bounds", "message"), + [ + ({"max_rows": 0}, "max_rows"), + ({"max_rows": 1_000_001}, "max_rows"), + ({"max_rows": True}, "max_rows"), + ({"max_rows": 1.5}, "max_rows"), + ({"max_pages": 0}, "max_pages"), + ], +) +async def test_export_rows_validate_bounds_before_requests( + mode: str, bounds: dict[str, Any], message: str +) -> None: + with Server(lambda request, index: Reply(body={})) as server: + async with session(mode, server.url) as client: + rows = client.iter_export_rows( + ORG, + "reviews", + from_time="2026-10-01T00:00:00Z", + to_time="2026-11-01T00:00:00Z", + **bounds, + ) + with pytest.raises(ValueError, match=message): + await next_export(rows) + assert not server.requests + + @MODES @pytest.mark.parametrize( ("format", "content_type"),