You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Version slicing silently drops undefined parts instead of raising IndexError #529
What OS are you using? (Add more in the Environment section)
Linux
Situation
Version.__getitem__ documents that an IndexError is raised when “a part of the range requested is undefined”. A slice that spans an undefined prerelease or build position instead silently filters that missing part out and returns only the defined parts. This can also change the result shape because a single remaining element is unwrapped to a scalar.
How to reproduce
fromsemverimportVersionv=Version.parse("1.2.3")
print(v[0:4]) # includes undefined prerelease index 3print(v[0:5]) # includes undefined prerelease and build indexespre=Version.parse("1.2.3-rc.1")
print(pre[3:5]) # includes undefined build index 4
Output on semver 3.2.0:
(1, 2, 3)
(1, 2, 3)
rc.1
Expected behavior
Each of these slices should raise IndexError, because at least one part in the requested range is undefined. That is the behavior documented in both the __getitem__ docstring and the “Access Parts through Index” usage documentation.
The silent filtering is surprising because it loses information about which positions were requested. In the third example, pre[3:5] returns the scalar prerelease rather than reporting that build index 4 is absent.
A possible resolution would be to check the requested range for None parts before filtering them, and raise IndexError when any requested position is undefined. I would be happy to implement this with regression tests if you agree with the documented reading.
Environment
Reproduced with the semver 3.2.0 wheel from PyPI on CPython 3.10/Linux. The same implementation is present on current master.
Before sending a fix: tests/test_index.py currently asserts Version.parse("1.2.3")[0:10] == (1, 2, 3), which relies on undefined parts being dropped. Should only slices that stay within the 5-slot tuple and hit a None part ([0:4], [0:5], [3:5] on 1.2.3) raise IndexError, with over-long slices like [0:10] still returning the defined parts? Or should every slice that touches an undefined part raise, which would mean changing that test and adding a note to the docs? Happy to implement whichever you prefer, including how stepped slices such as [0:5:2] should behave.
good catch. my reading: treat the version as a 5-slot sequence, use normal slice semantics for the bounds, and raise if any selected position is undefined. so [0:10] on 1.2.3 clamps to [0:5] and raises (positions 3 and 4 undefined), [0:5:2] raises because it selects position 4, [0:4:2] is fine since it only selects 0 and 2. that does mean the existing [0:10] assertion has to change and the docs need a note, so worth a maintainer confirming before either of us writes the patch.
Thank you @Beehtrix and @SonicHedghog for raising this. I agree, we should follow the documented behavior and they should match: if a slice selects any undefined version part, it should raise IndexError.
I'd apply normal Python slice semantics to determine the selected positions.
This means:
v[0:4] → raises when prerelease is absent.
v[0:5] → raises when prerelease/build is absent.
v[0:10] on 1.2.3 → also raises, because Python clamps the stop to the tuple length, and the resulting selection still contains the undefined positions.
A fully populated 1.2.3-rc.1+build.1[0:10] → returns all five parts.
Stepped slices should inspect the elements actually selected:
v[0:4:2] selects indexes 0, 2 and succeeds.
v[0:5:2] selects 0, 2, 4 and raises if build is absent.
v[4:0:-2] selects existing positions according to normal slice semantics.
v[3:4] → str. This part of the current API is not idiomatic. It's returning a scalar from a one-element slice.
The PR is ready for review: #535 — it implements the confirmed semantics, including the stepped-slice and clamping cases discussed above. The one-element slice scalar return is preserved as-is and noted in the PR, since changing that would be a separate breaking change.
Activity
Beehtrix commentedon Dec 3, 2026
Before sending a fix:
tests/test_index.pycurrently assertsVersion.parse("1.2.3")[0:10] == (1, 2, 3), which relies on undefined parts being dropped. Should only slices that stay within the 5-slot tuple and hit aNonepart ([0:4],[0:5],[3:5]on1.2.3) raiseIndexError, with over-long slices like[0:10]still returning the defined parts? Or should every slice that touches an undefined part raise, which would mean changing that test and adding a note to the docs? Happy to implement whichever you prefer, including how stepped slices such as[0:5:2]should behave.SonicHedghog commentedon Dec 3, 2026
good catch. my reading: treat the version as a 5-slot sequence, use normal slice semantics for the bounds, and raise if any selected position is undefined. so
[0:10]on 1.2.3 clamps to[0:5]and raises (positions 3 and 4 undefined),[0:5:2]raises because it selects position 4,[0:4:2]is fine since it only selects 0 and 2. that does mean the existing[0:10]assertion has to change and the docs need a note, so worth a maintainer confirming before either of us writes the patch.tomschr commentedon Dec 3, 2026
Thank you @Beehtrix and @SonicHedghog for raising this. I agree, we should follow the documented behavior and they should match: if a slice selects any undefined version part, it should raise
IndexError.I'd apply normal Python slice semantics to determine the selected positions.
This means:
v[0:4]→ raises when prerelease is absent.v[0:5]→ raises when prerelease/build is absent.v[0:10]on1.2.3→ also raises, because Python clamps the stop to the tuple length, and the resulting selection still contains the undefined positions.1.2.3-rc.1+build.1[0:10]→ returns all five parts.v[0:4:2]selects indexes 0, 2 and succeeds.v[0:5:2]selects 0, 2, 4 and raises if build is absent.v[4:0:-2]selects existing positions according to normal slice semantics.v[3:4]→ str. This part of the current API is not idiomatic. It's returning a scalar from a one-element slice.I think we are on the same page. 😄
SonicHedghog commentedon Dec 3, 2026
same page then. i'll put together a pr with regression tests.
SonicHedghog commentedon Dec 3, 2026
The PR is ready for review: #535 — it implements the confirmed semantics, including the stepped-slice and clamping cases discussed above. The one-element slice scalar return is preserved as-is and noted in the PR, since changing that would be a separate breaking change.