Skip to content

Raise IndexError for out-of-range negative indices in ObservableList - #3902

Merged
quaquel merged 3 commits into
mesa:mainfrom
Shivansh1205:fix/signalinglist-negative-index
Oct 20, 2026
Merged

quaquel merged 3 commits into
mesa:mainfrom
Shivansh1205:fix/signalinglist-negative-index

Conversation

@Shivansh1205

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

SignalingList.__setitem__ / __delitem__ modified the wrong element for an out-of-range negative index instead of raising IndexError.

Bug / Issue

Fixes #3901

A negative index was normalized by adding len(self.data) once without a bounds check. For index < -len, Python's own wrap-around kicked in, so lst[-4] on a 3-element list targeted lst[-1]. It also emitted a REPLACED / REMOVED signal for that element.

Implementation

After normalizing an integer index, the code now checks 0 <= index < len(self.data) and raises IndexError("list assignment index out of range") (the same message list uses) before touching the data or notifying. Slice handling is unchanged.

Testing

  • Added test_observable_list_out_of_range_negative_index in tests/experimental/test_mesa_signals.py. It checks the following:
    • items[-4] = ... and del items[-4] on a 3-element list raise IndexError.
    • The list is unchanged and no signal is emitted.
    • In-range negative indices still work.
    • Fails before the change with DID NOT RAISE IndexError, and passes after.
  • tests/experimental: 313 passed.
  • ruff check clean. ruff format --check flags only mesa/examples/experimental/tram_model/Readme.md, which is pre-existing on main and untouched here.

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
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Oct 1, 2026
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +2.3% [+1.7%, +2.8%] 🔵 +1.2% [+0.9%, +1.4%]
BoltzmannWealth large 🔴 +3.9% [+3.3%, +4.5%] 🔵 -3.3% [-4.2%, -2.6%]
Schelling small 🔵 +1.1% [+0.7%, +1.5%] 🔵 -2.2% [-2.9%, -1.5%]
Schelling large 🔵 -0.5% [-1.1%, -0.0%] 🟢 -8.7% [-9.9%, -7.4%]
WolfSheep small 🔵 -1.6% [-2.1%, -1.1%] 🔵 +1.3% [-3.3%, +6.0%]
WolfSheep large 🔵 -0.5% [-1.2%, +0.6%] 🟢 -5.2% [-7.7%, -3.1%]
SugarscapeG1mt small 🔴 +5.4% [+4.8%, +5.9%] 🔵 -3.9% [-5.1%, -2.7%]
SugarscapeG1mt large 🔴 +5.2% [+4.1%, +6.4%] 🔵 -2.2% [-4.1%, -0.1%]
BoidFlockers small 🔵 +0.2% [-0.3%, +0.7%] 🔵 -0.3% [-0.7%, +0.1%]
BoidFlockers large 🔵 +0.8% [+0.4%, +1.2%] 🔵 +1.0% [+0.8%, +1.2%]
MultiLevelAllianceModel small 🔵 +2.6% [+1.0%, +4.1%] 🔵 -0.9% [-1.2%, -0.6%]
MultiLevelAllianceModel large 🔵 +1.7% [+1.0%, +2.2%] 🔴 +6.0% [+4.2%, +7.9%]

Shivansh1205 and others added 2 commits October 6, 2026 12:50
…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 quaquel added the bug Release notes label label Oct 20, 2026
@quaquel
quaquel merged commit 9175791 into mesa:main Oct 20, 2026
15 of 16 checks passed
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ObservableList silently modifies the last element for out-of-range negative indices

2 participants