Skip to content

ObservableList SET signal passes an exhausted generator as new when assigned a generator #3907

Closed

Description

@Shivansh1205

When an ObservableList is assigned a generator (or any other one-shot iterable), the ListSignals.SET signal's new payload is the already-exhausted iterator, so subscribers see no values.

Code

https://github.com/mesa/mesa/blob/2ac2da65/mesa/experimental/mesa_signals/observable_collections.py#L51-L61

setattr(
    instance,
    self.private_name,
    SignalingList(value, instance, self.public_name),   # consumes value
)
instance.notify(
    self.public_name,
    ListSignals.SET,
    old=old_value,
    new=value,                                           # the consumed iterable
)

Why it is wrong

ObservableList.__set__ accepts any Iterable (per its signature), and SignalingList.__init__ materializes it with list(iterable). The signal then forwards the original value. For a generator, that object has already been consumed, so list(message.additional_kwargs["new"]) is [].

For a plain list, new is the caller's own list object. If the caller mutates that list later, the payload a subscriber kept changes with it.

The batched path already does the right thing. _aggregate_list emits SET with new = list(getattr(owner, name)), a snapshot of the stored values. So the same assignment produces different payloads depending on whether it happens inside batch().

Reproduce

from mesa.experimental.mesa_signals import HasEmitters, ListSignals, ObservableList

class A(HasEmitters):
    items = ObservableList()
    def __init__(self):
        super().__init__()
        self.items = []

a = A()
received = []
def handler(msg):
    received.append(msg)

a.observe("items", ListSignals.SET, handler)
a.items = (x * 2 for x in range(3))

print(list(a.items))                                   # [0, 2, 4]
print(list(received[0].additional_kwargs["new"]))      # []  <- subscribers see nothing

Expected behavior

  • new in the SET signal contains the values now stored in the list, e.g. [0, 2, 4].
  • It is a snapshot, consistent with the batched SET signal.

I have a branch with a fix and a regression test and will open a PR.

Activity

  1. added
    bug
    on Oct 1, 2026
  2. added a commit that references this issue on Nov 23, 2026
    8b4584a
  3. added a commit that references this issue on Nov 26, 2026
    e7b48be
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions