diff --git a/reflex/config.py b/reflex/config.py index c3c5e4cf5..562a6caf7 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -703,6 +703,9 @@ class Config(Base): # Path to file containing key-values pairs to override in the environment; Dotenv format. env_file: Optional[str] = None + # Whether to only make rx.Field annotated state attributes base vars + state_explicit_vars: bool = False + def __init__(self, *args, **kwargs): """Initialize the config values. diff --git a/reflex/state.py b/reflex/state.py index 40afcbc79..26a30f257 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -559,11 +559,17 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow): **new_backend_vars, } + from reflex.vars.base import Field + # Set the base and computed vars. cls.base_vars = { f.name: get_var_for_field(cls, f) for f in cls.get_fields().values() if f.name not in cls.get_skip_vars() + and ( + not get_config().state_explicit_vars + or get_origin(f.outer_type_) is Field + ) } cls.computed_vars = { v._js_expr: v._replace(merge_var_data=VarData.from_state(cls)) diff --git a/reflex/utils/types.py b/reflex/utils/types.py index ac30342e2..8234dd999 100644 --- a/reflex/utils/types.py +++ b/reflex/utils/types.py @@ -692,6 +692,12 @@ def is_backend_base_variable(name: str, cls: Type) -> bool: if hint == ClassVar: return False + from reflex.config import get_config + from reflex.vars.base import Field + + if get_config().state_explicit_vars and get_origin(hint) is not Field: + return False + if name in cls.inherited_backend_vars: return False