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

Version slicing silently drops undefined parts instead of raising IndexError #529

Backlog

Description

@SonicHedghog

Which version of Python is the problem with?

3.10

What semver version are you using?

3.2.0

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

from semver import Version

v = Version.parse("1.2.3")
print(v[0:4])   # includes undefined prerelease index 3
print(v[0:5])   # includes undefined prerelease and build indexes

pre = 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.

Activity

  1. Beehtrix commented on Dec 3, 2026

    @Beehtrix

    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.

  2. SonicHedghog commented on Dec 3, 2026

    @SonicHedghog
    ContributorAuthor

    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.

  3. tomschr commented on Dec 3, 2026

    @tomschr
    Member

    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.

    I think we are on the same page. 😄

  4. SonicHedghog commented on Dec 3, 2026

    @SonicHedghog
    ContributorAuthor

    same page then. i'll put together a pr with regression tests.

  5. SonicHedghog commented on Dec 3, 2026

    @SonicHedghog
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    SIze

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions