Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF-647] deepcopy on rx.Model not working #1841

Closed
jpbaltazar opened this issue Sep 20, 2023 · 2 comments · Fixed by #1845
Closed

[REF-647] deepcopy on rx.Model not working #1841

jpbaltazar opened this issue Sep 20, 2023 · 2 comments · Fixed by #1845
Assignees
Labels
bug Something isn't working linear Created by Linear-GitHub Sync

Comments

@jpbaltazar
Copy link

jpbaltazar commented Sep 20, 2023

Hi I've been using deepcopy to transform some complex rx.Model entities onto dictionaries of primitives and I've verified it works up until version 2.7 but after some recent commit it stops working.

A minimal example to reproduce this issue:

from copy import deepcopy

import reflex as rx

class MyModel(rx.Model):
    s: str

class State(rx.State):
    """The app state."""

    my_model : MyModel = MyModel(**{"s":"hello"})

    def print_deepcopy(self):
        print(deepcopy(self.my_model))

def index() -> rx.Component:
    return rx.vstack(
        rx.button("Button", on_click=State.print_deepcopy)
    )

# Add state and page to the app.
app = rx.App()
app.add_page(index)
app.compile()

From SyncLinear.com | REF-647

@Lendemor Lendemor added the bug Something isn't working label Sep 20, 2023
@masenf masenf added the linear Created by Linear-GitHub Sync label Sep 20, 2023
@masenf masenf changed the title deepcopy on rx.Model not working [REF-647] deepcopy on rx.Model not working Sep 20, 2023
@masenf
Copy link
Collaborator

masenf commented Sep 20, 2023

Fairly sure this was introduced via #1748, which wraps list, dict, set, and Base in wrapt.ObjectProxy to enable more efficient change tracking.

When I run the provided code (with slight modifications), I get

Traceback (most recent call last):
  File "/Users/masenf/code/reflex-dev/VENV-repro-6/lib/python3.11/site-packages/reflex/state.py", line 816, in _process_event
    events = fn(**payload)
             ^^^^^^^^^^^^^
  File "/Users/masenf/code/reflex-dev/repro-1841/repro_1841/repro_1841.py", line 14, in print_deepcopy
    print(deepcopy(self.my_model))
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/copy.py", line 153, in deepcopy
    y = copier(memo)
        ^^^^^^^^^^^^
NotImplementedError: object proxy must define __deepcopy__()

Sounds like I need to make the new MutableProxy compatible with __deepcopy__ and add a test case.

Thank you @jpbaltazar for reporting this issue on pre-release code so we can get it fixed! 🙏

@masenf masenf self-assigned this Sep 20, 2023
masenf added a commit that referenced this issue Sep 20, 2023
Allow state vars accessed through MutableProxy to be copied and unconnected
from the proxy. Copied objects that are modified will not mark the associated
field on the state as dirty.

Fix #1841
Fix REF-647
@jpbaltazar
Copy link
Author

Thank you so much for solving this in record time! I'm glad to be able to at least contribute to this project in this way :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working linear Created by Linear-GitHub Sync
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants