← 输入框|github.com|刷新
Skip to content

Raise IndexError for slices selecting undefined version parts - #535

Open
SonicHedghog wants to merge 1 commit into
python-semver:masterfrom
SonicHedghog:fix-529-slice-indexerror
Open

SonicHedghog wants to merge 1 commit into
python-semver:masterfrom
SonicHedghog:fix-529-slice-indexerror

Conversation

@SonicHedghog

Copy link
Copy Markdown
Contributor

Fixes #529

Summary

Version slicing silently dropped undefined parts instead of raising the documented IndexError:

>>> Version.parse("1.2.3")[0:10]      # before: silently truncated
(1, 2, 3)
>>> Version.parse("1.2.3-rc.1")[3:5]  # before: scalar prerelease, build silently dropped
'rc.1'

Following the direction confirmed in the issue, a slice now applies normal Python slicing semantics to determine the selected positions — including clamping of out-of-range bounds — and raises IndexError when any selected position is undefined:

>>> Version.parse("1.2.3")[0:10]       # clamps to [0:5], selects undefined positions
IndexError: Version part undefined
>>> Version.parse("1.2.3-rc.1+build.1")[0:10]   # fully populated: all five parts
(1, 2, 3, 'rc.1', 'build.1')
>>> Version.parse("1.2.3")[0:4:2]      # stepped slices inspect only selected positions
(1, 3)
>>> Version.parse("1.2.3")[0:5:2]      # selects 0, 2, 4 — build undefined
IndexError: Version part undefined

Scalar indexing behavior is unchanged: out-of-range and undefined positions raise exactly as before, and negative indices remain unsupported.

Compatibility notes

  • The existing assertion Version.parse("1.2.3")[0:10] == (1, 2, 3) in tests/test_index.py encoded the undocumented dropping behavior; it is updated to the documented raising behavior, with new regression coverage for partially undefined selections, over-long slices, and stepped slices in both directions.
  • One-element slices keep returning a scalar (e.g. v[3:4] on a version with a prerelease), preserving the current API; as noted in the issue this is not idiomatic, but changing it would be a separate breaking change and can be decided independently.

Tests

  • Full test suite passes: 589 passed, 1 skipped, with 100% coverage.

Version slicing silently dropped undefined parts from the result,
contradicting the documented behavior that requesting an undefined
part of a range raises IndexError. A slice now applies normal Python
slicing semantics to determine the selected positions and raises
IndexError when any selected position is undefined, consistent with
scalar indexing. Stepped slices inspect only the positions they
actually select.

Fixes python-semver#529

This branch has not been deployed

No deployments
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.

Version slicing silently drops undefined parts instead of raising IndexError

1 participant