fix: out-of-range page wrap returns a per_page slice; validate cache length

get_page() out-of-range/negative wrap reset to page 0 but returned pages[0] (a single item) even with per_page>1, mis-shaping the page downstream; it now reroutes through get_page(0) so the normal slice logic applies. cache shorter than max_pages now raises ValueError at construction instead of an IndexError when the cache button is clicked on a later page.

Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
disqualifier 2026-06-28 17:46:20 -04:00
parent 4d52ef7f50
commit 6dae35001e
2 changed files with 10 additions and 4 deletions

View File

@ -9,13 +9,13 @@ buttons) behind previous / jump / next navigation, with an optional cache button
`requirements.txt`:
```
dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.0
dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.1
```
Direct:
```bash
pip install "dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.0"
pip install "dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.1"
```
Requires `discord.py` (pulled transitively).
@ -116,7 +116,8 @@ await ButtonPaginator(pages, cache=cache, cache_sleep=1.0).start(ctx)
```
Omit `cache` or pass `None`/`[]` and the button never appears. When set, `cache`
must have one entry per rendered page.
must have one entry per rendered page — a shorter `cache` raises `ValueError` at
construction rather than failing with an `IndexError` mid-navigation.
## Constructor options

View File

@ -176,6 +176,11 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
total_pages, left_over = divmod(len(self.pages), self.per_page)
self.max_pages: int = total_pages + (1 if left_over else 0)
if cache is not None and len(cache) < self.max_pages:
raise ValueError(
f"cache has {len(cache)} entries but there are {self.max_pages} pages; "
"cache needs one entry per page"
)
self._page_kwargs: Dict[str, Any] = self._fresh_kwargs()
def _fresh_kwargs(self) -> Dict[str, Any]:
@ -204,7 +209,7 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
"""return the content for a page index, wrapping out-of-range to 0"""
if page_number < 0 or page_number >= self.max_pages:
self.current_page = 0
return self.pages[self.current_page]
return self.get_page(0)
if self.per_page == 1:
return self.pages[page_number]
base = page_number * self.per_page