From 6dae35001e57d245b960af242cb4a110954b516e Mon Sep 17 00:00:00 2001 From: disqualifier Date: Sun, 28 Jun 2026 17:46:20 -0400 Subject: [PATCH] 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 --- README.md | 7 ++++--- src/dpy_paginator/dpy_paginator.py | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d628f09..c3320ca 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/dpy_paginator/dpy_paginator.py b/src/dpy_paginator/dpy_paginator.py index 1335e88..2ce0599 100644 --- a/src/dpy_paginator/dpy_paginator.py +++ b/src/dpy_paginator/dpy_paginator.py @@ -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