fix: cloning nested UI elements with on_change#8727
Open
Conversation
When UI elements were nested in containers (e.g., checkbox in batch in array), bound method on_change handlers like m.set_state would silently mutate a deep-copied object instead of the original. This happened because deep copying _args created a new bound method targeting a cloned object, and subsequent clones used this stale reference. With this change, we now update the cloned element's args to point to the newly constructed args so subsequent clones use the correct args. Fixes #6435
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a UI cloning bug where on_change handlers (notably bound methods like m.set_state) could end up bound to deep-copied objects when UI elements are nested in containers (e.g., checkbox inside batch inside array), causing state mutations to affect the wrong instance.
Changes:
- Update
UIElement.from_argsto ensure the cloned element’s_argsis replaced with the newly reconstructedInitializationArgs(avoids stale/deepcopied bound-method references). - Preserve
batch’son_changecallback when cloning via_clone(). - Add regression tests covering both single-container and nested-container cloning cases (issue #6435).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
tests/_plugins/ui/_impl/test_batch.py |
Adds regression tests ensuring on_change bound-method handlers still mutate the original model when elements are nested/cloned. |
marimo/_plugins/ui/_impl/batch.py |
Ensures batch._clone() passes through on_change so cloning doesn’t drop the callback. |
marimo/_plugins/ui/_core/ui_element.py |
Fixes deepcopy-based cloning to store the reconstructed InitializationArgs on the cloned element, preventing stale bound-method references. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
mscolnick
approved these changes
Mar 17, 2026
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.
When UI elements were nested in containers (e.g., checkbox in batch in array), bound method on_change handlers like m.set_state would silently mutate a deep-copied object instead of the original. This happened because deep copying _args created a new bound method targeting a cloned object, and subsequent clones used this stale reference.
With this change, we now update the cloned element's args to point to the newly constructed args so subsequent clones use the correct args.
Fixes #6435