Treat double-underscore names as plain attributes, not fields
Type of change
- Bug fix (non-breaking change which fixes an issue)
Description
This PR restores the behavior from v0.9.12 where double-underscore state attributes (name-mangled __private names and dunders) are treated as plain class attributes rather than backend vars. They can still be explicitly declared as fields using rx.field().
Changes:
-
Core logic (
packages/reflex-base/src/reflex_base/vars/base.py):- Added
_private_prefixes()helper to identify which name prefixes should be treated as private (non-field) attributes in a given class - Updated
_unannotated_fields()to skip names starting with private prefixes unless explicitly declared asField - Updated
_annotated_fields()to skip annotated private names unless they areFieldinstances - Modified field collection logic to properly handle private names from base classes
- Added
-
Tests (
tests/units/reflex_base/vars/test_base.pyandtests/units/test_state.py):- Added
test_private_names_are_not_fields()to verify that name-mangled private attributes, hand-mangled names, and dunders remain plain attributes - Added
test_private_names_of_plain_base_are_not_fields()to verify inheritance behavior - Added
test_private_attribute_is_assignable()to verify that plain private attributes can be assigned without marking state dirty
- Added
-
Changelog: Added news fragments documenting the fix
Rationale
Private attributes (those starting with __) should not automatically become backend vars, as this breaks the expected Python semantics where such names are mangled and treated as implementation details. Users who want a private attribute to be a backend var can explicitly declare it with rx.field().
Testing
- Added comprehensive unit tests covering:
- Name-mangled private attributes staying as plain attributes
- Explicitly declared private fields becoming backend vars
- Inheritance of private attributes from plain base classes
- Assignment behavior of private attributes in handlers
- All new tests pass and verify the expected behavior