Repository navigation
Keep subscribers when a HasEmitters object is deep-copied or pickled - #4056
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e572ecf8c2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Thanks for working on this. The approach looks sensible, but the file currently doesn't import. In Once that's fixed, it would be good to also have a test that subscribers which have already been garbage collected are dropped on copy/pickle, and one showing that a bound-method handler is re-bound to the copy. |
|
yep i got it and corrected it too but i have some conflicts at local branch so i havent updated it yet |
dietcoke-17
left a comment
There was a problem hiding this comment.
self review: lgtm and fine ready to ship to production
|
Thanks for the subscriber-restoration tests, @dietcoke-17. The branch is mergeable and checks pass. With this patch applied, I confirmed that copied @quaquel, should this proceed as a scoped bound-method fix under #4023, alongside the separate GridCell work in #4025, or must closure subscribers be safe first? I recommend requiring closure safety first: replacing a copy failure with handlers targeting the original model risks silent simulation errors. Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot. |
|
@dietcoke-17, can you resolve the conflicts so this can move forward? |
Since mesa#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 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]>
…4056) * Keep subscribers when a HasEmitters object is copied or pickled * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * small fix * added 2 more tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Adapted for 3.5.x: the fix applies unchanged to 3.5's HasObservables; the tests use HasObservables instead of HasEmitters. The alliance formation deepcopy test is left out: it uses 4.0's scenario API, and 3.5's alliance model has no subscribers, so it already deep-copies. On 3.5.3, deep-copying or pickling any HasObservables object with a subscriber, including a Model, raised TypeError: cannot pickle 'WeakMethod' object. (cherry picked from commit e288e96) Assisted-by: Claude Opus 5.5
Part of #4023 (weak references are not rebuilt after a copy).
HasEmittersstores subscribers as weak references, which can be neither pickled nor deep-copied. Any model with a subscriber, e.g. one with aDataRecorder, failed withTypeError: cannot pickle 'WeakMethod' object. This affectedBoltzmannWealth,MultiLevelAllianceModelandTransitSystem.Changes
__getstate__swaps live subscribers for strong references to their handlers and drops dead ones;__setstate__rebuilds the weak references.__slots__.Notes
deepcopy, so they still point at the original.Threshold.bindandContinuousStateuse closures, so a deep-copiedTransitSystemno longer crashes, but its tram handlers still drive the original tram. This needs a follow-up (bound methods instead of closures).