Repository navigation
Raise IndexError for out-of-range negative indices in ObservableList - #3902
Merged
quaquel merged 3 commits intoOct 20, 2026
Merged
Conversation
SignalingList.__setitem__ and __delitem__ normalize a negative integer index by adding len(self.data) once, but never check the result. An index below -len therefore stays negative after normalization and Python's own wrap-around kicks in, so lst[-4] = x on a three-element list overwrites the last element and del lst[-4] deletes it, instead of raising IndexError like a plain list. A REPLACED/REMOVED signal is emitted for the wrong element as well. Bounds-check the normalized index and raise IndexError, matching list. Fixes mesa#3901
|
Performance benchmarks:
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…List" The built-in list raises "list assignment index out of range" for del as well, so the original message already matched. Co-Authored-By: Claude Opus 5.5 <[email protected]>
quaquel
approved these changes
Oct 20, 2026
EwoutH
pushed a commit
that referenced
this pull request
Oct 28, 2026
…3902) * Raise IndexError for out-of-range negative indices in SignalingList SignalingList.__setitem__ and __delitem__ normalize a negative integer index by adding len(self.data) once, but never check the result. An index below -len therefore stays negative after normalization and Python's own wrap-around kicks in, so lst[-4] = x on a three-element list overwrites the last element and del lst[-4] deletes it, instead of raising IndexError like a plain list. A REPLACED/REMOVED signal is emitted for the wrong element as well. Bounds-check the normalized index and raise IndexError, matching list. Fixes #3901 * Match built-in list message for out-of-range del in SignalingList Co-Authored-By: Claude Opus 5.5 <[email protected]> * Revert "Match built-in list message for out-of-range del in SignalingList" The built-in list raises "list assignment index out of range" for del as well, so the original message already matched. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit 9175791)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Pre-PR Checklist
Summary
SignalingList.__setitem__/__delitem__modified the wrong element for an out-of-range negative index instead of raisingIndexError.Bug / Issue
Fixes #3901
A negative index was normalized by adding
len(self.data)once without a bounds check. Forindex < -len, Python's own wrap-around kicked in, solst[-4]on a 3-element list targetedlst[-1]. It also emitted aREPLACED/REMOVEDsignal for that element.Implementation
After normalizing an integer index, the code now checks
0 <= index < len(self.data)and raisesIndexError("list assignment index out of range")(the same messagelistuses) before touching the data or notifying. Slice handling is unchanged.Testing
test_observable_list_out_of_range_negative_indexintests/experimental/test_mesa_signals.py. It checks the following:items[-4] = ...anddel items[-4]on a 3-element list raiseIndexError.DID NOT RAISE IndexError, and passes after.tests/experimental: 313 passed.ruff checkclean.ruff format --checkflags onlymesa/examples/experimental/tram_model/Readme.md, which is pre-existing onmainand untouched here.