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
OrderedSet.__getitem__ raised ValueError (from itertools.islice) when called with a negative index, instead of the IndexError callers expect — or, better, returning the element counting from the end, as all Python sequences do.
s=OrderedSet([1, 2, 3])
s[-1] # ValueError: Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize.
Why
itertools.islice rejects negative start/stop values with ValueError. Since __getitem__ is documented and exported as a sequence-style accessor (positive indices already work, positive out-of-range already raises IndexError), negative indices should follow the same contract as Python's built-in sequences.
Fix
Normalise the index before delegating to islice:
if index < 0, add len(self) to convert it to the equivalent positive offset
if still negative after normalisation, raise IndexError with the original index in the message (not the normalised value)
s[-1] # 3 ✓s[-3] # 1 ✓s[-4] # IndexError: index -4 out of range ✓
Two new unit tests cover the happy path and the out-of-range case. Doctests in orderedset.py are updated with examples of s[-1] and s[-3].
This pull request was prepared with the assistance of AI, under my direction and review.
Negative index access (e.g. ``s[-1]``) previously raised ``ValueError``
from ``itertools.islice``, which forbids negative start values. This is
surprising because Python sequences uniformly accept negative indices
(``s[-1]`` returns the last element) and the custom ``__getitem__`` on
``OrderedSet`` already raises ``IndexError`` for positive out-of-range
indices.
Normalise the index before passing it to ``islice``: add ``len(self)``
when ``index < 0``, and raise ``IndexError`` (not ``ValueError``) when
the normalised index is still negative. The original index is preserved
in the error message so the reported value matches what the caller passed.
The assertion `assert 'stuck' in OrderedSet(['loading'])` failed because the 50ms timeout didn't fire within the CI window. The test passes consistently locally. I've pushed a minor fixup commit (removing extra blank lines in the docstring) to re-trigger CI. Could you approve the workflow run so CI can run again?
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
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.
What
OrderedSet.__getitem__raisedValueError(fromitertools.islice) when called with a negative index, instead of theIndexErrorcallers expect — or, better, returning the element counting from the end, as all Python sequences do.Why
itertools.islicerejects negative start/stop values withValueError. Since__getitem__is documented and exported as a sequence-style accessor (positive indices already work, positive out-of-range already raisesIndexError), negative indices should follow the same contract as Python's built-in sequences.Fix
Normalise the index before delegating to
islice:index < 0, addlen(self)to convert it to the equivalent positive offsetIndexErrorwith the original index in the message (not the normalised value)Two new unit tests cover the happy path and the out-of-range case. Doctests in
orderedset.pyare updated with examples ofs[-1]ands[-3].This pull request was prepared with the assistance of AI, under my direction and review.