Skip to content

feat: Add function to skip serialization.#6137

Open
abulvenz wants to merge 1 commit intoreflex-dev:mainfrom
abulvenz:add-possibility-to-skip-serialization
Open

feat: Add function to skip serialization.#6137
abulvenz wants to merge 1 commit intoreflex-dev:mainfrom
abulvenz:add-possibility-to-skip-serialization

Conversation

@abulvenz
Copy link
Contributor

@abulvenz abulvenz commented Feb 18, 2026

This is needed for permissions and roles on states.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 18, 2026

Merging this PR will not alter performance

✅ 8 untouched benchmarks


Comparing abulvenz:add-possibility-to-skip-serialization (3bcac1c) with main (88155a7)

Open in CodSpeed

@abulvenz abulvenz marked this pull request as ready for review February 18, 2026 17:51
@abulvenz
Copy link
Contributor Author

Locally the test passes.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 18, 2026

Greptile Summary

This PR adds a __skip_serialization method to BaseState that, when overridden to return True, causes get_delta() and dict() to return empty results — intended as a hook for permissions/roles on states. However, the implementation has a critical bug: the double-underscore prefix triggers Python's name mangling, meaning subclass overrides will not be called. The method always resolves to BaseState.__skip_serialization() (which returns False), making the feature non-functional.

  • Critical: __skip_serialization uses Python name mangling, preventing subclass overrides — should use _skip_serialization instead
  • Design concern: The early return in get_delta() also silently skips all substate deltas, which may or may not be intended
  • Missing: No docstring on the new method, and no tests covering the new behavior

Confidence Score: 1/5

  • This PR introduces a non-functional feature due to Python name mangling and should not be merged as-is.
  • The double-underscore naming makes the override mechanism completely broken — subclasses cannot override the method, so the feature does nothing. Additionally, there are no tests and the substate delta skipping behavior is undocumented.
  • reflex/state.py — the __skip_serialization method and its two call sites need to be fixed to use a single leading underscore for proper subclass override support.

Important Files Changed

Filename Overview
reflex/state.py Adds __skip_serialization hook to BaseState used in get_delta and dict, but the double-underscore prefix triggers Python name mangling, making subclass overrides impossible — defeating the feature's purpose. No tests added.

Flowchart

flowchart TD
    A["get_delta() called"] --> B{"__skip_serialization()?"}
    B -->|True| C["Return empty delta {}"]
    B -->|False| D["_mark_dirty_computed_vars()"]
    D --> E["Compute subdelta for dirty vars"]
    E --> F["Recursively get substate deltas"]
    F --> G["Return full delta"]

    H["dict() called"] --> I{"initial?"}
    I -->|Yes| J["Proceed normally"]
    I -->|No| K{"__skip_serialization()?"}
    K -->|True| L["Return empty dict {}"]
    K -->|False| J

    style C fill:#f99,stroke:#333
    style L fill:#f99,stroke:#333
    style B fill:#ff9,stroke:#333
    style K fill:#ff9,stroke:#333
Loading

Last reviewed commit: 3bcac1c

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +2062 to +2063
def __skip_serialization(self) -> bool:
return False
Copy link
Contributor

Choose a reason for hiding this comment

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

Name mangling prevents subclass override

The double-underscore prefix (__skip_serialization) triggers Python's name mangling. Inside BaseState, the call self.__skip_serialization() is compiled as self._BaseState__skip_serialization(), meaning it will always call BaseState's version (which returns False), regardless of any override in a subclass.

Since the stated purpose is for subclasses to override this method (e.g., for permissions/roles on states), this makes the feature unusable. Use a single leading underscore instead:

Suggested change
def __skip_serialization(self) -> bool:
return False
def _skip_serialization(self) -> bool:
return False

And update the two call sites accordingly (self._skip_serialization()).

Comment on lines +2073 to +2074
if self.__skip_serialization():
return delta
Copy link
Contributor

Choose a reason for hiding this comment

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

Early return also silently skips all substate deltas

When __skip_serialization() returns True, this early return prevents the recursive substate delta collection at line 2098-2099. This means if a parent state skips serialization, all of its substates are also silently skipped, even if those substates do not skip serialization themselves.

This may be intentional, but it's a significant behavioral side-effect that should be documented. If substates should still be serialized independently, the skip should only guard the subdelta block (lines 2083-2094), not the entire method.

Comment on lines +2062 to +2063
def __skip_serialization(self) -> bool:
return False
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing docstring on new method

This method has no docstring, which is inconsistent with the rest of BaseState where all methods have docstrings. Adding one would clarify the intended purpose and how subclasses should use it.

Suggested change
def __skip_serialization(self) -> bool:
return False
def __skip_serialization(self) -> bool:
"""Whether to skip serialization for this state.
Override in a subclass to skip sending state updates to the frontend.
Returns:
True if serialization should be skipped, False otherwise.
"""
return False

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

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

to bring this feature, we need some unit test cases added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments