Skip to content

fix: cloning nested UI elements with on_change#8727

Open
akshayka wants to merge 1 commit intomainfrom
aka/fix-ui-clone-on-change
Open

fix: cloning nested UI elements with on_change#8727
akshayka wants to merge 1 commit intomainfrom
aka/fix-ui-clone-on-change

Conversation

@akshayka
Copy link
Contributor

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

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
@akshayka akshayka requested a review from dmadisetti March 17, 2026 02:32
@vercel
Copy link

vercel bot commented Mar 17, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Mar 17, 2026 2:32am

Request Review

@akshayka akshayka marked this pull request as ready for review March 17, 2026 02:32
Copilot AI review requested due to automatic review settings March 17, 2026 02:32
@akshayka akshayka added the bug Something isn't working label Mar 17, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_args to ensure the cloned element’s _args is replaced with the newly reconstructed InitializationArgs (avoids stale/deepcopied bound-method references).
  • Preserve batch’s on_change callback 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UI elements embedded in Html objects don't update other objects through on_change

3 participants