Skip to content

Fix integer assignment in exploded text fragments - #2121

Open
FanWu-ai wants to merge 1 commit into
prompt-toolkit:mainfrom
FanWu-ai:fix-exploded-list-integer-assignment
Open

FanWu-ai wants to merge 1 commit into
prompt-toolkit:mainfrom
FanWu-ai:fix-exploded-list-integer-assignment

Conversation

@FanWu-ai

@FanWu-ai FanWu-ai commented Oct 4, 2026

Copy link
Copy Markdown

explode_text_fragments() returns a list whose integer assignments should replace an existing fragment. Currently, assigning to -1 inserts before the last fragment, and out-of-range integer assignments silently insert instead of raising IndexError.

For example:

fragments = explode_text_fragments([('', 'abc')])
fragments[-1] = ('bold', 'Z')
# Expected: [('', 'a'), ('', 'b'), ('bold', 'Z')]
# Before:   [('', 'a'), ('', 'b'), ('bold', 'Z'), ('', 'c')]

Normalize negative integer indices and check bounds before converting to a one-element slice. Explicit slice assignment and automatic expansion of replacement fragments are unchanged. Adds 23 regression cases for valid positive/negative indices, empty/single/multicharacter replacements, invalid indices, empty lists and custom __index__ objects.

Validation:

  • Complete test suite: 179 passed on Python 3.12.14 and 3.13.5.
  • The unchanged implementation fails 8 cases in the regression module; the fixed module passes all 25 cases.
  • Ruff lint/format, typos and strict mypy for Linux/Windows/macOS passed.
  • A separate deterministic check passed 1,034 cases for integer-index parity, replacement metadata and unchanged slice behavior.

This fixes public-helper behavior; a specific built-in UI failure has not been established. Other Python versions in the CI matrix were not run locally. This branch is based directly on main and is independent of #2120.

OpenAI Codex assisted with investigation, implementation, testing, automated review and this description. The exact model identifier was not available; no human-review claim is made.

@FanWu-ai
FanWu-ai marked this pull request as ready for review October 4, 2026 23:25
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