Repository navigation
fix: refuse page tokens that aren't strings - #829
Conversation
PageKey.after() raised TypeError, so a 500, for tokens of other types, e.g., from JSON bodies; they are now invalid-format, as other malformed tokens. Document what the pre-release review of 0.23.0 found missing: the ValueError of find_page() for items without a valid token and why to sort by values the app sets, skipped items without sort fields, collations, empty tokens of generated clients, TokenPage, and the ValueErrors of the helpers' own arguments. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
PageKey("name") sorted by the characters of the name, so lists with
more than one page failed. Follow the review: wrap the docs, say when
find_page() raises and how items without sort fields sort in either
order, drop the collation note, accept only empty tokens as the first
page, and assert that other types of tokens aren't quoted.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
PageKey((1,)) raised TypeError and PageKey(["a"]) broke hashing; both are now ValueErrors, as the docs say. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Reviewer's GuideAPIMake
DocumentationExplain page-token handling, pagination edge cases, and helper argument errors, and clarify
TestsCover invalid page-key field shapes and ensure non-string tokens produce the expected invalid-format field error without echoing the token.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #829 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 76 76
Lines 10831 10831
=========================================
Hits 10831 10831
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/utils/pagination.py" line_range="403-404" />
<code_context>
+ # A string, e.g., `("name")`, would sort by its characters
+ not isinstance(self.fields, tuple)
+ or not self.fields
+ or not all(isinstance(field, str) for field in self.fields)
or len(set(self.fields)) != len(self.fields)
or not all(_FIELD.fullmatch(field) for field in self.fields)
):
</code_context>
<issue_to_address>
**Unhashable fields raise TypeError**
When a tuple contains a `str` subclass with `__hash__ = None`, `isinstance(field, str)` accepts it, then `len(set(self.fields))` raises `TypeError` instead of the documented `ValueError`.
Reject unhashable fields before deduplicating them, and raise the documented `ValueError`.
Also at `foca/utils/pagination.py:402`.
</issue_to_address>
### Comment 2
<location path="docs/utilities.md" line_range="153-155" />
<code_context>
-the same order, keeps pages fast.
+`find_page()` raises a `ValueError` (so the app answers `500`) for a page
+that has a next page and whose last item has no valid token. Items without
+a sort field sort as `null`: first in ascending order (a `500` once they
+fill a page), and last in descending order, where the filter of each next
+page leaves them out, so they are skipped once a page ends before them. So sort by values that the app
+sets itself (e.g., times of creation and identifiers), not by values that
+users write (e.g., names), unless the app checks their type and length when
</code_context>
<issue_to_address>
**Missing fields aren't always skipped**
When a `PageKey` has multiple fields and an item lacks a later sort field, `PageKey.after()` builds a lexicographic `$or` filter, so a document missing a later sort field can still match an earlier-field comparison and appear on a subsequent descending page, contrary to the blanket claim that such items are left out and skipped.
Describe missing values as null in the affected sort-field component, and qualify the ordering and skipping behavior for compound keys.
</issue_to_address>
### Comment 3
<location path="docs/utilities.md" line_range="163-166" />
<code_context>
+to find the items after a token.
+
+Errors of the app's own calls raise `ValueError` (see the [API
+reference](reference.md) for each): e.g., page keys without fields, with a
+string or list instead of a tuple of fields, with fields named twice, or
+with names that have empty parts or parts starting with `$`; a `default` of
+`get_page_size()` larger than its `maximum` (with a `maximum` below 100,
+pass a `default` too).
</code_context>
<issue_to_address>
**Pagination API reference is missing**
When a reader follows the new API-reference direction for a pagination helper, the sentence directs readers to the API reference for each helper, but `docs/reference.md` has no pagination entries, so readers cannot find the promised per-helper reference there.
Add the pagination helpers to `docs/reference.md`, or change the direction to point to a reference that documents them.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: foca/utils/pagination.py:404, docs/utilities.md:155
With several sort fields, items without a later one are compared by the earlier ones first, so they are skipped only among items with the same earlier values. Rewrap the paragraph. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
#826 is deferred out of 0.23.0: say in the migration guide that the endpoints for managing roles and the permissions API still accept bare users, which apply to nobody once an issuer namespaces its subjects. Add #829 (#830) to the release notes. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Linked issuesChecked at commit 992ec71 #830 fix: refuse page tokens that aren't strings · 5 of 5 met
|
Summary
Follow-up of #828 from the pre-release review of 0.23.0 (before the helpers ship):
PageKey.after()(and sofind_page()) refuses page tokens that aren't strings (e.g.,123from a JSON body) asinvalid-format(400); before,TypeError(500).PageKeyrefuses fields that aren't a tuple of strings:PageKey(("name"))(a string) sorted by the characters of the name and failed on every list with a next page;PageKey((1,))raisedTypeError; a list broke hashing.docs/utilities.md("Page tokens"): whenfind_page()raisesValueError(only for a page with a next page), how items without a sort field sort (first when ascending, skipped when descending), why to sort by values the app sets rather than values users write, how to accept empty tokens of generated clients (None if pageToken == "" else pageToken),TokenPage, and theValueErrors of the helpers' own arguments. Reflowed thePageKeybullet.Closes #830
Verification
test_tokens_of_other_typesandtest_fields_not_tuple_of_stringsfailed before the fixes, pass after.pytest tests/utils/test_pagination_tokens.py tests/utils/test_pagination.py: 146 passed;coverage report --include=foca/utils/pagination.py: 100%.ruff check .,ruff format --check .,mypy(192 files),ast-grep scan --error,semgrep scan --config .semgrep --error: pass.mkdocs build --strict: pass.mongodhere); CI runs them.Review
Two rounds of the adversarial-reviewer and quality-reviewer subagents; no high findings. Fixed: the medium of round 1 (docs reflow and claims checked against the code) and the lows that affect behaviour (non-string tokens, string/list/non-string fields, docs precision on
ValueErrors and null ordering; the collation note was dropped as not supported by the code path).Declined review findings
get_page_size(maximum=50)withoutdefaultraisesValueError(unlikeget_page(), which clamps): kept as a programmer error, documented ("with amaximumbelow 100, pass adefaulttoo").find_page()keeps raisingValueErrorfor items without a valid token instead of a problem: the cause is the app's data or key, which a client can't fix; documented, with the advice to sort by values the app sets.PageKeynot run in CI and shared integer parsing with_get_integer: out of scope for a release fix.Open questions
None.
🤖 Generated with Claude Code
https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Summary by Sourcery
Harden pagination token and page-key validation while documenting pagination edge cases and helper errors.
Bug Fixes:
Enhancements:
Documentation:
Tests: