From 9c3bac61572494b31669f66b319380fb633b7545 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Thu, 5 Dec 2024 00:09:50 +0100 Subject: [PATCH] feat: add state_explicit_vars config option --- reflex/config.py | 3 +++ reflex/state.py | 6 ++++++ reflex/utils/types.py | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/reflex/config.py b/reflex/config.py index 88230cefec3..8262712d56b 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -683,6 +683,9 @@ class Config: # 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 55f29cf45f3..0411aa7a3a6 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -536,11 +536,17 @@ def __init_subclass__(cls, mixin: bool = False, **kwargs): **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 0c39eacc4e2..f9d7e310865 100644 --- a/reflex/utils/types.py +++ b/reflex/utils/types.py @@ -689,6 +689,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