Reject negative n in generate_n_keys_between - #2
Merged
Conversation
A negative n slipped past the n == 0 / n == 1 checks: with one bound None the range() loop simply didn't run and the function silently returned a single key; with both bounds set, mid = floor(n / 2) went negative and the function recursed until RecursionError. Both the silent wrong result and the 1000-frame traceback are worse than enforcing the documented 'n >= 0' precondition with a clear FIError. The JS reference (rocicorp/fractional-indexing) has the same behaviour and only documents the precondition; this is a deliberate deviation, like validating a defaulted int_digits. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
doistbot
reviewed
Jul 31, 2026
doistbot
left a comment
Member
There was a problem hiding this comment.
This PR adds a clean guard to generate_n_keys_between() that raises FIError when n < 0, preventing both a silent single-key return (one bound None) and unbounded recursion (both bounds set). The error message is descriptive, the new parametrized test covers all four bound combinations, and the guard follows the existing inline-FIError pattern used throughout the module. No inline issues were flagged.
I also included a few optional follow-up notes in the details below.
Optional follow-up notes (2)
tests.py:259: This repeats
check_n_keys()’s existingFIErrorassertion branch. Replace thewith pytest.raisesblock withcheck_n_keys(FIError('n must be >= 0: -1'), a, b, -1)so error checks for this API use the shared helper.tests.py:252: All four bound combinations hit the
n < 0guard beforea/bare inspected, so they exercise the same branch. The two historically distinct failure modes are "one bound None" (silent single key) and "both bounds set" (unbounded recursion); one case of each already documents the intent and protects the regression. Consider trimming to two cases (e.g.(None, None)and('a0', 'a5')) unless you want explicit per-combination coverage.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
generate_n_keys_between()documentedn >= 0as a precondition but never enforced it, and a negativenslipped past then == 0/n == 1checks with two bad outcomes:None: thefor _ in range(n - 1)loop didn't run, so the call silently returned a single key — looks like success.mid = floor(n / 2)went negative and the function recursed unboundedly untilRecursionError.Now
n < 0raisesFIError('n must be >= 0: <n>')at the top of the function.This is a faithful-port bug: the JS reference behaves identically and only documents the precondition. Guarding is a deliberate deviation, consistent with the one we already made for a defaulted odd-length
int_digits— a clear error beats silent garbage or a 1000-frame traceback. The same change has been added to our upstream PR (httpie/fractional-indexing-python#3).Testing
test_negative_n_rejectedcovering all four bound combinations (None/None, one-sided ×2, both set) — 87 tests total, all passing.Release
After merge, run Actions → Release with
bump: patchto publish 1.0.1 (changelog entry included).🤖 Generated with Claude Code