Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions reflex/istate/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,15 @@ def __getattr__(self, __name: str) -> Any:
)

if (
not isinstance(self.__wrapped__, Base)
or __name not in NEVER_WRAP_BASE_ATTRS
) and hasattr(value, "__func__"):
(
not isinstance(self.__wrapped__, Base)
or __name not in NEVER_WRAP_BASE_ATTRS
)
and (func := getattr(value, "__func__", None)) is not None
and not inspect.isclass(getattr(value, "__self__", None))
):
# Rebind `self` to the proxy on methods to capture nested mutations.
return functools.partial(value.__func__, self) # pyright: ignore [reportFunctionMemberAccess, reportAttributeAccessIssue]
return functools.partial(func, self)

if is_mutable_type(type(value)) and __name not in (
"__wrapped__",
Expand Down
17 changes: 17 additions & 0 deletions tests/units/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,18 @@ def append_to_ls(self, item: dict):
"""
self.ls.append(item)

@classmethod
def from_dict(cls, data: dict) -> ModelDC:
"""Create an instance of ModelDC from a dictionary.

Args:
data: The dictionary to create the instance from.

Returns:
An instance of ModelDC.
"""
return cls(**data)


@pytest.mark.asyncio
async def test_state_proxy(
Expand Down Expand Up @@ -3945,6 +3957,11 @@ def test_mutable_models():
assert state.dirty_vars == set()
state.dc.append_to_ls({"new": "item"})
assert state.dirty_vars == {"dc"}
state.dirty_vars.clear()

dc_from_dict = state.dc.from_dict({"foo": "from_dict", "ls": []})
assert dc_from_dict == ModelDC(foo="from_dict", ls=[])
assert state.dirty_vars == set()


def test_dict_and_get_delta():
Expand Down
Loading