Skip to content

Commit

Permalink
state: implement __copy__ and __deepcopy__ for MutableProxy
Browse files Browse the repository at this point in the history
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
  • Loading branch information
masenf committed Sep 20, 2023
1 parent 8befd7d commit 5478b1f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,3 +1313,22 @@ def __setattr__(self, name, value):
super().__setattr__(name, value)
return
self._mark_dirty(super().__setattr__, args=(name, value))

def __copy__(self) -> Any:
"""Return a copy of the proxy.
Returns:
A copy of the wrapped object, unconnected to the proxy.
"""
return copy.copy(self.__wrapped__)

def __deepcopy__(self, memo=None) -> Any:
"""Return a deepcopy of the proxy.
Args:
memo: The memo dict to use for the deepcopy.
Returns:
A deepcopy of the wrapped object, unconnected to the proxy.
"""
return copy.deepcopy(self.__wrapped__, memo=memo)

0 comments on commit 5478b1f

Please sign in to comment.