-
Notifications
You must be signed in to change notification settings - Fork 513
feat: add PaginationList for lazy page fetching in catalog list operations #3454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stark256-spec
wants to merge
16
commits into
apache:main
Choose a base branch
from
stark256-spec:feat/lazy-pagination-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
91f3640
feat: add PaginationList for lazy page fetching in catalog list opera…
stark256-spec 5a01899
fix(pagination): resolve mypy and ruff lint errors
stark256-spec 98d6900
fix(tests): resolve ruff lint errors and add len() performance tests
stark256-spec 9fbcdd3
fix(pagination): add docstrings to __getitem__ overload stubs for pyd…
stark256-spec 10c2ba0
fix(pagination): use noqa:D105 on overload stubs instead of docstrings
stark256-spec 0b08854
fix(pagination): drop @overload stubs to resolve D105/D418 pydocstyle…
stark256-spec 6a7b6d0
fix(pagination): add D105/D418 to pydocstyle ignore list; restore @ov…
stark256-spec d71317c
fix(tests): move PaginationList import to correct alphabetical positi…
stark256-spec 1780ae6
fix(tests): account for RestCatalog config endpoint call in call_coun…
stark256-spec e2bb799
refactor(pagination): move PaginationList to typedef, add PageFetchRe…
stark256-spec 392a8f5
fix: update PaginationList import in test_rest.py
stark256-spec 7ab945a
fix(lint): add D105 back to pydocstyle ignore for @overload stubs
stark256-spec 329b1bd
fix: resolve ruff import ordering and pagination cleanup bug
stark256-spec 769728d
feat(pagination): add count, index, reversed, copy, and + to Paginati…
stark256-spec 3883280
Address review nits: named FetchNextPage alias and HTTP call count as…
stark256-spec 1c3abd9
Add __mul__ and __rmul__ overrides to PaginationList
stark256-spec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """Lazy-loading pagination utilities.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable, Iterator | ||
| from typing import SupportsIndex, TypeVar, overload | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| class PaginationList(list[T]): | ||
|
Fokko marked this conversation as resolved.
Outdated
|
||
| """A list that lazily fetches subsequent pages from a paginated API. | ||
|
|
||
| The first page is pre-loaded on construction. Subsequent pages are only | ||
| fetched when the caller iterates past items already in memory. Operations | ||
| that require the complete result set — ``len()``, ``in``, slicing, | ||
| ``repr()`` — trigger a full fetch of all remaining pages. | ||
|
|
||
| Args: | ||
| first_page: Items from the first API response. | ||
| next_page_token: Pagination token returned with the first response, | ||
| or ``None`` if no further pages exist. | ||
| fetch_next_page: Callable that accepts a page token and returns a | ||
| tuple of ``(items, next_page_token_or_None)``. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| first_page: list[T], | ||
| next_page_token: str | None, | ||
| fetch_next_page: Callable[[str], tuple[list[T], str | None]], | ||
| ) -> None: | ||
| super().__init__(first_page) | ||
| self._next_page_token = next_page_token | ||
| self._fetch_next_page = fetch_next_page | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Internal helpers — use list's own methods to avoid infinite loops. | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def _fetch_all(self) -> None: | ||
| """Fetch all remaining pages into the list.""" | ||
| while self._next_page_token: | ||
| items, self._next_page_token = self._fetch_next_page(self._next_page_token) | ||
| list.extend(self, items) | ||
|
|
||
| def _fetch_through_index(self, idx: int) -> None: | ||
| """Fetch pages until the list contains at least *idx + 1* items.""" | ||
| while list.__len__(self) <= idx and self._next_page_token: | ||
| items, self._next_page_token = self._fetch_next_page(self._next_page_token) | ||
| list.extend(self, items) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Lazy iteration | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def __iter__(self) -> Iterator[T]: | ||
| """Iterate lazily, fetching pages only as the caller advances.""" | ||
| idx = 0 | ||
| while True: | ||
| if idx < list.__len__(self): | ||
| yield list.__getitem__(self, idx) | ||
| idx += 1 | ||
| elif self._next_page_token: | ||
| items, self._next_page_token = self._fetch_next_page(self._next_page_token) | ||
| list.extend(self, items) | ||
| else: | ||
| return | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Operations that require the complete result set | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def __len__(self) -> int: | ||
| """Return the total number of items, fetching all pages first.""" | ||
| self._fetch_all() | ||
| return list.__len__(self) | ||
|
|
||
| def __contains__(self, item: object) -> bool: | ||
| """Return True if item is present, fetching all pages first.""" | ||
| self._fetch_all() | ||
| return list.__contains__(self, item) | ||
|
|
||
| def __repr__(self) -> str: | ||
| """Return string representation after fetching all pages.""" | ||
| self._fetch_all() | ||
| return f"PaginationList({list.__repr__(self)})" | ||
|
|
||
| def __eq__(self, other: object) -> bool: | ||
| """Compare equality after fetching all pages.""" | ||
| self._fetch_all() | ||
| return list.__eq__(self, other) | ||
|
|
||
| def __ne__(self, other: object) -> bool: | ||
| """Compare inequality after fetching all pages.""" | ||
| return not self.__eq__(other) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Index / slice access | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| @overload | ||
| def __getitem__(self, idx: SupportsIndex) -> T: ... | ||
|
|
||
| @overload | ||
| def __getitem__(self, idx: slice) -> list[T]: ... | ||
|
|
||
| def __getitem__(self, idx: SupportsIndex | slice) -> T | list[T]: | ||
| """Fetch pages as needed before returning the requested item(s).""" | ||
| if isinstance(idx, slice): | ||
| self._fetch_all() | ||
| else: | ||
| i = idx.__index__() | ||
| if i < 0: | ||
| self._fetch_all() | ||
| else: | ||
| self._fetch_through_index(i) | ||
| return list.__getitem__(self, idx) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.