make tag immutable#5641
Conversation
There was a problem hiding this comment.
Greptile Summary
This PR implements a significant architectural change by making the Tag class and all its subclasses immutable throughout the Reflex codebase. The core change converts the Tag dataclass from mutable to immutable by adding frozen=True to the dataclass decorator and refactoring all mutation operations to return new instances instead of modifying existing ones.
Key changes include:
-
Tag class immutability: The base Tag class is now frozen, with field types changed from concrete collections (
dict,list) to abstract interfaces (Mapping,Sequence). The__post_init__method that modified props in-place was removed. -
Method refactoring: All Tag methods (
set(),add_props(),remove_props()) now usedataclasses.replace()to return new instances rather than mutating existing ones. -
Subclass consistency: All Tag subclasses (CondTag, MatchTag, IterTag) are now also frozen to maintain consistency in the inheritance hierarchy.
-
Component rendering updates: Throughout the component system, code that previously mutated tags in-place has been updated to use method chaining and proper assignment patterns that work with immutable objects.
-
Test updates: Test expectations have been updated to match the new Tag.iter behavior where props are returned as formatted strings rather than raw objects.
This change aligns with functional programming principles commonly used in React-like systems, where components should be treated as immutable data structures. The immutable design prevents accidental mutations that could lead to shared state bugs and improves thread safety during component rendering.
Confidence score: 4/5
• This is a well-structured refactoring that improves code safety and follows React best practices, but involves significant architectural changes across many files.
• The score reflects the comprehensive nature of the changes and potential for edge cases, though the implementation appears sound and all methods properly handle immutability.
• Files requiring extra attention: reflex/components/tags/tag.py (core Tag class changes), reflex/components/component.py (central rendering logic), and tests/units/components/test_tag.py (test assertion logic changes).
12 files reviewed, no comments
CodSpeed Performance ReportMerging #5641 will not alter performanceComparing Summary
|
No description provided.