Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ This is a Python port of the original JavaScript implementation by [@rocicorp](h

## Changelog

### 1.0.1

- `generate_n_keys_between()` now raises `FIError` for a negative `n`, instead of silently returning a single key
(one bound `None`) or hitting `RecursionError` (both bounds set). Deviation from the JS reference, which only
documents the `n >= 0` precondition.

### 0.2.0

Brings the library to parity with [rocicorp/fractional-indexing](https://github.com/rocicorp/fractional-indexing)
Expand Down
6 changes: 5 additions & 1 deletion fractional_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,18 @@ def generate_n_keys_between(
) -> List[str]:
"""
same preconditions as generate_key_between().
n >= 0.
n must be >= 0 (raises FIError otherwise).
Returns an array of n distinct keys in sorted order.
If a and b are both null, returns [a0, a1, ...]
If one or the other is null, returns consecutive "integer"
keys. Otherwise, returns relatively short keys between `a` and `b`.

"""
digits, int_digits = _resolve_alphabets(digits, int_digits)
if n < 0:
# Without this guard a negative n silently returns a single key when
# one bound is None, and recurses without bound when both are set.
raise FIError(f'n must be >= 0: {n}')
if n == 0:
return []
if n == 1:
Expand Down
14 changes: 14 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ def test_equal_bounds_rejected() -> None:
generate_key_between('a0', 'a0')


# A negative n must raise, not silently return a single key (one bound None)
# or recurse without bound (both bounds set).
@pytest.mark.parametrize(['a', 'b'], [
(None, None),
('a0', None),
(None, 'a1'),
('a0', 'a5'),
])
def test_negative_n_rejected(a: Optional[str], b: Optional[str]) -> None:
with pytest.raises(FIError) as e:
generate_n_keys_between(a, b, -1)
assert e.value.args[0] == 'n must be >= 0: -1'


def test_readme_examples_single_key():
# Insert at the beginning
first = generate_key_between(None, None)
Expand Down
Loading