Skip to content

Honor the index protocol in positional operations - #261

Open
repzakske wants to merge 1 commit into
grantjenks:masterfrom
repzakske:codex/fix-scalar-index-protocol
Open

repzakske wants to merge 1 commit into
grantjenks:masterfrom
repzakske:codex/fix-scalar-index-protocol

Conversation

@repzakske

Copy link
Copy Markdown

Positional operations reject valid objects implementing __index__, while the endpoint fast paths accept invalid indices such as 0.0 and -1.0. For example, SortedList([10, 20]).pop(0.0) removes an item instead of raising TypeError.

Normalize scalar indices with operator.index() before lookup, deletion, and pop. This supports integer-like index objects without accepting values that only implement __int__. Use a single list pop for scalar SortedSet deletion so an index object's conversion is evaluated once and the membership set stays synchronized. SortedKeyList and the positional SortedDict APIs inherit the corrected behavior.

Related to #192, which reported the float-index behavior and was closed as low priority; this also covers valid index-protocol objects.

Validation on Windows / CPython 3.14:

  • Added 134 parametrized regression cases: 110 fail on the unchanged base, and all pass with the fix.
  • Full configured pytest suite, including doctests and stress tests: 500 passed.
  • Ruff lint (targeting Python 3.8) and formatting checks pass for the three changed files; git diff --check passes.

Tests cover both list implementations, positive/negative indices across internal blocks, empty and out-of-range access, invalid inputs without mutation, booleans, conversion count, and delegated set/dictionary operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant