Skip to content

Return empty single-item slices for negative out-of-range indexes - #387

Merged
dgilland merged 2 commits into
dgilland:developfrom
jwalith4418-Mj:fix/negative-single-item-slices
Oct 29, 2026
Merged

dgilland merged 2 commits into
dgilland:developfrom
jwalith4418-Mj:fix/negative-single-item-slices

Conversation

@jwalith4418-Mj

Copy link
Copy Markdown
Contributor

When end is omitted, slice_ is documented to select the item at start. Out-of-range negative indexes can instead return multiple items: slice_([1, 2, 3], -5) returns [1, 2], and -6 returns [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:

  • Before the fix: 6 failed, 6 passed among the new regressions and controls.
  • After: all 22 selected slice tests pass.
  • Full unit/doctest suite on Windows / Python 3.12.14: 3,002 passed, 100% coverage.
  • Full unit/doctest suite on Ubuntu / Python 3.10.12: 3,002 passed; coverage was not measured on that run.
  • Ruff lint/format and mypy pass; generated chain stubs match after normalizing platform line endings.
  • Wheel/sdist and documentation builds succeed; git diff --check passes. The build emits setuptools' existing experimental tool.distutils warning.

inv ci could 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 excludes tests/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.

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>
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — jwalith4418-Mj:fix/negative-single-item-slices into dgilland:develop

@dgilland
dgilland merged commit fe3c898 into dgilland:develop Oct 29, 2026
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.

3 participants