Repository navigation
Return empty single-item slices for negative out-of-range indexes - #387
Merged
dgilland merged 2 commits intoOct 29, 2026
Merged
Conversation
Clamp the implicit end of a negative single-item slice to zero so that Python does not interpret a negative computed end relative to the sequence a second time. Preserve explicit endpoint behavior. Add regression cases for lists, tuples, strings and very large negative indexes, clarify the docstring, and record the fix in the changelog. Co-authored-by: OpenAI Codex <noreply@openai.com>
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.
When
endis omitted,slice_is documented to select the item atstart. Out-of-range negative indexes can instead return multiple items:slice_([1, 2, 3], -5)returns[1, 2], and-6returns[1].The computed implicit end is negative, so Python interprets it relative to the sequence a second time. Clamp that end to zero. Explicit endpoints keep their normal slicing behavior. Added list, tuple, string and huge-index regressions, clarified the docstring, and updated the changelog and contributor list.
Validation on the recorded base
2b851532dea82df13623d2731d2eacf68708c398:git diff --checkpasses. The build emits setuptools' existing experimentaltool.distutilswarning.inv cicould not start because its runner requests a PTY unsupported on Windows. Its underlying build, documentation, lint, typing, chain-generation and runtime-test commands were run directly. The repository's standard runtime test task excludestests/pytest_mypy_testing; that separate optional collection and Python 3.11/3.13/3.14 were not run locally.AI assistance: OpenAI Codex helped investigate the bug and write the patch, tests, and this description. The commit includes attribution.