Repository navigation
Emit list snapshots in unbatched ObservableList SET signals - #3988
Merged
Merged
Conversation
Assigning to an ObservableList outside of batch() sent the raw assigned object as `new` and the replaced SignalingList (or the descriptor's shared fallback list on first assignment) as `old`. A generator arrived already consumed, mutating `old` still emitted signals on the owner, and mutating the first `old` changed the fallback seen by every other instance. Both payloads are now plain list snapshots, matching what the batched SET aggregation already emits. The snapshots are only built when the assignment is batched or has SET subscribers. Fixes mesa#3907
|
Thanks @ska2704 for picking up #3908 and covering both payloads. I reproduced the exhausted generator and shared Before review:
Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot. |
quaquel
approved these changes
Nov 23, 2026
quaquel
added a commit
to quaquel/mesa
that referenced
this pull request
Nov 23, 2026
ObservableList no longer uses fallback_value as the old value of its first assignment (mesa#3988), so the BaseObservable docstring now uses ContinuousState, which uses it as its initial value, as the example. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW
quaquel
added a commit
that referenced
this pull request
Nov 25, 2026
* Document mesa_signals and add it to the API reference - Add a Signals section to the experimental API reference, listing the public API in reading order with a short introduction. - Observable: document that it signals on assignment only (changes made in place to a mutable value are not observed), that changes are detected with !=, how to set an initial value, and what subscribers receive; add an example. - HasEmitters, observe, observe_class: document that handlers are held by weak reference, so a lambda or local function is silently dropped unless kept alive elsewhere; document the accepted names and signal types, including ALL. - Message: document its fields. - computed_property: document lazy recomputation, the CHANGED signal on invalidation (with only old), and the cyclical dependency error. - BaseObservable: describe what an Observable is and fallback_value. - Run the doctests in core.py as part of the test suite. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW * Update fallback_value example after #3988 ObservableList no longer uses fallback_value as the old value of its first assignment (#3988), so the BaseObservable docstring now uses ContinuousState, which uses it as its initial value, as the example. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW * Document what happens to subscriptions on copy and pickle Since #4056, subscriptions survive deepcopy and pickle as far as their handlers come along. Document this in the HasEmitters docstring, which the API reference renders, including that a bound method stays subscribed only if its object is copied along with the emitter. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW * Document Observable fallback_value --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
EwoutH
pushed a commit
that referenced
this pull request
Nov 26, 2026
Assigning to an ObservableList outside of batch() sent the raw assigned object as `new` and the replaced SignalingList (or the descriptor's shared fallback list on first assignment) as `old`. A generator arrived already consumed, mutating `old` still emitted signals on the owner, and mutating the first `old` changed the fallback seen by every other instance. Both payloads are now plain list snapshots, matching what the batched SET aggregation already emits. The snapshots are only built when the assignment is batched or has SET subscribers. Fixes #3907 Co-authored-by: Jan Kwakkel <[email protected]> Adapted for 3.5.x: the fix applies unchanged. The tests use 3.5's HasObservables instead of HasEmitters and are placed before test_observable_list_mutation, since the batched reverse-slice tests they follow on main are not in 3.5. (cherry picked from commit 8b4584a) Assisted-by: Claude Opus 5.5
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
Assigning to an
ObservableListoutside ofbatch()emits aSETsignal whose payload can be wrong or unsafe to keep. This makes botholdandnewplain list snapshots, which is what the batchedSETalready sends.Bug / Issue
Fixes #3907
This picks up #3908, which was closed as stale, and includes the
oldfollow-up mentioned in its review.Outside a batch,
ObservableList.__set__passes the raw assigned object asnewand whatever was stored before asold. That causes three problems:newfor a generator has already been consumed bySignalingList, so handlers see an empty iterator.oldis the replacedSignalingList. It still has the owner and name, so a handler that keeps and mutates it emitsAPPENDED/REPLACED/etc. on the owner even though the attribute now holds a different list.oldis the descriptor'sfallback_value, one[]shared by every instance of the class. If a handler mutates it, later first assignments on other instances report that content asold.Implementation
In
ObservableList.__set__(mesa/experimental/mesa_signals/observable_collections.py):SignalingListis built and stored first, thennew=list(new_list.data)is emitted.oldislist(old_list.data), or[]when nothing was assigned before. The fallback list is no longer read here.oldsnapshot is what gets captured into the batch context, so the batched and unbatched paths now agree on both values..datadirectly.list(signaling_list)goes through theMutableSequenceiterator, which calls__getitem__once per item in Python and was roughly 20x slower for a 1000 item list.SET, the method returns right after storing the list, so assignments without listeners do no extra copying.BaseObservable.__set__already does the same check.Testing
test_observable_list_set_payload_is_snapshot, parametrized over batched and unbatched assignment: assigning a generator emitsold=[1, 2], new=[0, 2, 4]in both cases, and appending to the stored list afterwards does not change the deliverednew. On main the unbatched case fails becausenewis the generator.test_observable_list_set_old_payload_is_detached: the firstoldis an empty list that is not shared between instances, and mutating a lateroldemits nothing on the owner. On main this fails withassert ['stale'] == [].pytest tests/ -Werror -Wdefault::PendingDeprecationWarning: 936 passed (Python 3.12, macOS).pytest --doctest-modules mesa/experimental/mesa_signals: passed.ruff check mesa testsandruff format --checkon the changed files: clean.Rough timing for
a.items = list(range(1000))with timeit: about 1.8 µs with no subscriber both before and after. With aSETsubscriber it is 2.9 µs on main and 6.3 µs here, which is the cost of the two copies.Additional Notes
newis now always alist. Before, a tuple or set assigned outside a batch was forwarded as is. Handlers that only read the values are not affected, and the batched signal already worked this way.