Skip to content
Merged
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 @@ -879,6 +879,9 @@ class Config: # pyright: ignore [reportIncompatibleVariableOverride]
# Path to file containing key-values pairs to override in the environment; Dotenv format.
env_file: str | None = None

# Whether to automatically create setters for state base vars
state_auto_setters: bool = True

# Whether to display the sticky "Built with Reflex" badge on all pages.
show_built_with_reflex: bool | None = None

Expand Down
4 changes: 3 additions & 1 deletion reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ def _init_var(cls, prop: Var):
Raises:
VarTypeError: if the variable has an incorrect type
"""
from reflex.config import get_config
from reflex.utils.exceptions import VarTypeError

if not types.is_valid_var_type(prop._var_type):
Expand All @@ -1023,7 +1024,8 @@ def _init_var(cls, prop: Var):
f'Found var "{prop._js_expr}" with type {prop._var_type}.'
)
cls._set_var(prop)
cls._create_setter(prop)
if get_config().state_auto_setters:
cls._create_setter(prop)
cls._set_default_value(prop)

@classmethod
Expand Down
27 changes: 27 additions & 0 deletions tests/units/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -3406,6 +3406,33 @@ def test_redis_state_manager_config_knobs_invalid_lock_warning_threshold(
del sys.modules[constants.Config.MODULE]


def test_auto_setters_off(tmp_path):
proj_root = tmp_path / "project1"
proj_root.mkdir()

config_string = """
import reflex as rx
config = rx.Config(
app_name="project1",
state_auto_setters=False,
)
"""

(proj_root / "rxconfig.py").write_text(dedent(config_string))

with chdir(proj_root):
# reload config for each parameter to avoid stale values
reflex.config.get_config(reload=True)
from reflex.state import State

class TestState(State):
"""A test state."""

num: int = 0

assert list(TestState.event_handlers) == ["setvar"]


class MixinState(State, mixin=True):
"""A mixin state for testing."""

Expand Down