From 5478b1fc86a00073d3a20d1f72426107dc188610 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Wed, 20 Sep 2023 14:27:55 -0700 Subject: [PATCH] state: implement __copy__ and __deepcopy__ for MutableProxy 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 --- reflex/state.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/reflex/state.py b/reflex/state.py index 1b5eda865a..dfe9803c78 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -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)