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

Minor pickle improvement 2 #4500

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
20 changes: 18 additions & 2 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2105,14 +2105,30 @@ def __getstate__(self):
state["__dict__"].pop("router", None)
state["__dict__"].pop("router_data", None)
# Never serialize parent_state or substates.
state["__dict__"]["parent_state"] = None
state["__dict__"]["substates"] = {}
state["__dict__"].pop("parent_state", None)
state["__dict__"].pop("substates", None)
state["__dict__"].pop("dirty_vars", None)
state["__dict__"].pop("dirty_substates", None)
state["__dict__"].pop("_was_touched", None)
# Remove all inherited vars.
for inherited_var_name in self.inherited_vars:
state["__dict__"].pop(inherited_var_name, None)
return state

def __setstate__(self, state: dict[str, Any]):
"""Set the state from redis deserialization.

This method is called by pickle to deserialize the object.

Args:
state: The state dict for deserialization.
"""
state["__dict__"]["parent_state"] = None
state["__dict__"]["substates"] = {}
state["__dict__"]["dirty_vars"] = set()
state["__dict__"]["dirty_substates"] = set()
super().__setstate__(state)

def _check_state_size(
self,
pickle_state_size: int,
Expand Down
5 changes: 1 addition & 4 deletions tests/units/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2513,10 +2513,7 @@ def test_mutable_copy(mutable_state: MutableTestState, copy_func: Callable):
assert getattr(ms_copy, attr) is not getattr(mutable_state, attr)
ms_copy.custom.array.append(42)
assert "custom" in ms_copy.dirty_vars
if copy_func is copy.copy:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i can implement __copy__ for BaseState if you want to keep this behavior

Copy link
Collaborator

Choose a reason for hiding this comment

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

i dont think anything is depending on this behavior, so we're fine to simplify the test logic -- famous last words 🫣

assert "custom" in mutable_state.dirty_vars
else:
assert not mutable_state.dirty_vars
assert not mutable_state.dirty_vars


@pytest.mark.parametrize(
Expand Down
Loading