Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add state_explicit_vars config option #4483

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions reflex/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
6 changes: 6 additions & 0 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions reflex/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading