Skip to content

Commit dbb1fb6

Browse files
Merge remote-tracking branch 'upstream/main' into explicit-event-id-minification
2 parents 5f57707 + 88155a7 commit dbb1fb6

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

reflex/state.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ def _get_computed_vars(cls) -> list[tuple[str, ComputedVar]]:
492492
"""
493493
return [
494494
(name, v)
495-
for mixin in [*cls._mixins(), cls]
495+
for mixin in (*cls._mixins(), cls)
496496
for name, v in mixin.__dict__.items()
497497
if is_computed_var(v) and name not in cls.inherited_vars
498498
]
@@ -575,14 +575,14 @@ def __init_subclass__(cls, mixin: bool = False, **kwargs):
575575

576576
new_backend_vars = {
577577
name: value if not isinstance(value, Field) else value.default_value()
578-
for mixin_cls in [*cls._mixins(), cls]
578+
for mixin_cls in (*cls._mixins(), cls)
579579
for name, value in list(mixin_cls.__dict__.items())
580580
if types.is_backend_base_variable(name, mixin_cls)
581581
}
582582
# Add annotated backend vars that may not have a default value.
583583
new_backend_vars.update({
584584
name: cls._get_var_default(name, annotation_value)
585-
for mixin_cls in [*cls._mixins(), cls]
585+
for mixin_cls in (*cls._mixins(), cls)
586586
for name, annotation_value in mixin_cls._get_type_hints().items()
587587
if name not in new_backend_vars
588588
and types.is_backend_base_variable(name, mixin_cls)
@@ -805,21 +805,21 @@ def computed_var_func(state: Self):
805805
return getattr(cls, unique_var_name)
806806

807807
@classmethod
808-
def _mixins(cls) -> list[type]:
808+
def _mixins(cls) -> tuple[type[BaseState], ...]:
809809
"""Get the mixin classes of the state.
810810
811811
Returns:
812812
The mixin classes of the state.
813813
"""
814-
return [
814+
return tuple(
815815
mixin
816816
for mixin in cls.__mro__
817817
if (
818818
mixin is not cls
819819
and issubclass(mixin, BaseState)
820820
and mixin._mixin is True
821821
)
822-
]
822+
)
823823

824824
@classmethod
825825
def _handle_local_def(cls):
@@ -837,6 +837,7 @@ def _handle_local_def(cls):
837837
cls.__module__ = reflex.istate.dynamic.__name__
838838

839839
@classmethod
840+
@functools.cache
840841
def _get_type_hints(cls) -> dict[str, Any]:
841842
"""Get the type hints for this class.
842843
@@ -939,8 +940,9 @@ def _check_overridden_basevars(cls):
939940
Raises:
940941
ComputedVarShadowsBaseVarsError: When a computed var shadows a base var.
941942
"""
943+
hints = cls._get_type_hints()
942944
for name, computed_var_ in cls._get_computed_vars():
943-
if name in get_type_hints(cls):
945+
if name in hints:
944946
msg = f"The computed var name `{computed_var_._js_expr}` shadows a base var in {cls.__module__}.{cls.__name__}; use a different name instead"
945947
raise ComputedVarShadowsBaseVarsError(msg)
946948

reflex/utils/types.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
StateVarTypes = (*PrimitiveTypes, Base, type(None))
5858

5959
if TYPE_CHECKING:
60+
from reflex.state import BaseState
6061
from reflex.vars.base import Var
6162

6263
VAR1 = TypeVar("VAR1", bound="Var")
@@ -902,12 +903,12 @@ def is_valid_var_type(type_: type) -> bool:
902903
)
903904

904905

905-
def is_backend_base_variable(name: str, cls: type) -> bool:
906+
def is_backend_base_variable(name: str, cls: type[BaseState]) -> bool:
906907
"""Check if this variable name correspond to a backend variable.
907908
908909
Args:
909910
name: The name of the variable to check
910-
cls: The class of the variable to check
911+
cls: The class of the variable to check (must be a BaseState subclass)
911912
912913
Returns:
913914
bool: The result of the check
@@ -924,11 +925,7 @@ def is_backend_base_variable(name: str, cls: type) -> bool:
924925
if name.startswith(f"_{cls.__name__}__"):
925926
return False
926927

927-
# Extract the namespace of the original module if defined (dynamic substates).
928-
if callable(getattr(cls, "_get_type_hints", None)):
929-
hints = cls._get_type_hints()
930-
else:
931-
hints = get_type_hints(cls)
928+
hints = cls._get_type_hints()
932929
if name in hints:
933930
hint = get_origin(hints[name])
934931
if hint == ClassVar:

0 commit comments

Comments
 (0)