Skip to content

Emit stored values as new in ObservableList SET signal - #3908

Closed
Shivansh1205 wants to merge 1 commit into
mesa:mainfrom
Shivansh1205:fix/observablelist-set-signal-new
Closed

Shivansh1205 wants to merge 1 commit into
mesa:mainfrom
Shivansh1205:fix/observablelist-set-signal-new

Conversation

@Shivansh1205

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

Assigning a generator to an ObservableList emitted a SET signal whose new was the already-consumed generator, so subscribers received no values.

Bug / Issue

Fixes #3907

ObservableList.__set__ accepts any iterable and SignalingList consumes it with list(iterable), but the signal forwarded the original object. For a plain list, new also aliased the caller's list.

Implementation

The SignalingList is built first, and new=list(new_list.data) is emitted: a snapshot of the stored values. This matches the batched path (_aggregate_list already emits new = list(getattr(owner, name))), so a SET payload no longer depends on whether the assignment happened inside batch(). old is unchanged.

Testing

  • Added test_observable_list_set_signal_new_value in tests/experimental/test_mesa_signals.py. It checks that a generator assignment yields new == [0, 2, 4], and that mutating the source list after assignment does not change the emitted payload.
    • Fails before the change with assert [] == [0, 2, 4], and passes after.
  • tests/experimental: 313 passed, including all batching tests.
  • ruff check / ruff format --check clean.

Additional Notes

For a plain list, subscribers now get an equal copy rather than the caller's own list object. Nothing in Mesa relies on identity here, and the batched signal already behaved this way.

ObservableList.__set__ builds a SignalingList from the assigned iterable
and then emitted the original object as the SET signal's new value. For
a generator or other one-shot iterable, SignalingList has already
consumed it, so subscribers received an exhausted iterator. For a plain
list, the payload aliased the caller's list.

Emit a snapshot of the stored values instead, matching the SET signal
that batching already produces in _aggregate_list.

Fixes mesa#3907
@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 🔵 +0.9% [+0.3%, +1.5%] 🔵 +1.4% [+1.1%, +1.7%]
BoltzmannWealth large 🔵 +0.9% [+0.2%, +1.6%] 🔵 -0.5% [-2.1%, +1.3%]
Schelling small 🔵 -1.1% [-1.6%, -0.7%] 🔵 -0.3% [-1.1%, +0.6%]
Schelling large 🔵 +1.1% [+0.5%, +1.8%] 🔴 +6.0% [+4.6%, +7.4%]
WolfSheep small 🔵 +0.9% [+0.4%, +1.4%] 🔵 +0.7% [-3.2%, +4.8%]
WolfSheep large 🔵 +0.8% [+0.0%, +1.6%] 🔵 +0.3% [-1.3%, +1.8%]
SugarscapeG1mt small 🔵 -0.4% [-1.0%, +0.2%] 🔵 +0.9% [-0.4%, +2.1%]
SugarscapeG1mt large 🔵 +0.6% [-0.7%, +1.8%] 🔵 +2.1% [+0.2%, +4.2%]
BoidFlockers small 🔵 +0.8% [+0.2%, +1.5%] 🔵 -0.7% [-1.2%, -0.2%]
BoidFlockers large 🔵 +1.4% [+0.8%, +1.9%] 🔵 -0.8% [-1.0%, -0.6%]
MultiLevelAllianceModel small 🔴 +4.4% [+3.2%, +5.4%] 🔵 +1.1% [+0.7%, +1.5%]
MultiLevelAllianceModel large 🔵 +0.8% [-0.6%, +2.0%] 🔵 +2.9% [+1.5%, +4.8%]

@dietcoke-17 dietcoke-17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, @Shivansh1205. The change itself looks right to me. Emitting a snapshot (list(new_list.data)) rather than new_list.data avoids exposing the list's internal storage, and it matches _aggregate_list and the slice branch of SignalingList.__setitem__, which already do list(...).

Test suggestions

  1. The snapshot check mutates source, but SignalingList already copies its input, so it would also pass with new=new_list.data. To actually pin the copy, mutate the stored list after assignment:
source = [1, 2]
agent.inventory = source
agent.inventory.append(3)
assert handler.call_args.args[0].additional_kwargs["new"] == [1, 2]

The handler only observes SET, so the append doesn't fire it. Keeping the source.append(3) check as well is fine.

  1. list(message.additional_kwargs["new"]) == [0, 2, 4] passes for any iterable, including a live SignalingList. Comparing directly with == [0, 2, 4] also asserts that new is a list. Adding assert handler.call_count == 2 before the second check would also make it clear that call_args refers to the latest call.

Non-blocking

  • new is now always a list. Previously a tuple or set was forwarded as-is. Probably worth a line in the release notes.
  • The benchmark bot shows Schelling large run +6.0% and MultiLevelAllianceModel small init +4.4%. This only touches ObservableList assignment, so it's likely noise, but a rerun would confirm.
  • Possible follow-up in a separate PR: old is still inconsistent. Unbatched it is the detached SignalingList (or the shared fallback_value on first assignment), while batched it is a list snapshot.

Otherwise LGTM once the test is tightened.

@quaquel quaquel added the bug Release notes label label Oct 20, 2026
@quaquel

quaquel commented Oct 20, 2026

Copy link
Copy Markdown
Member

@Shivansh1205 do you plan to implement the suggestions or not? there are now also conflicts because of another merged PR.

@quaquel

quaquel commented Oct 24, 2026

Copy link
Copy Markdown
Member

closing this as stale

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 SET signal passes an exhausted generator as new when assigned a generator

3 participants