Quellcode durchsuchen

prevent shadowing of computed vars (#3221)

Thomas Brandého vor 1 Jahr
Ursprung
Commit
a7355962fd
1 geänderte Dateien mit 15 neuen und 0 gelöschten Zeilen
  1. 15 0
      reflex/state.py

+ 15 - 0
reflex/state.py

@@ -450,6 +450,8 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
         super().__init_subclass__(**kwargs)
         super().__init_subclass__(**kwargs)
         # Event handlers should not shadow builtin state methods.
         # Event handlers should not shadow builtin state methods.
         cls._check_overridden_methods()
         cls._check_overridden_methods()
+        # Computed vars should not shadow builtin state props.
+        cls._check_overriden_basevars()
 
 
         # Reset subclass tracking for this class.
         # Reset subclass tracking for this class.
         cls.class_subclasses = set()
         cls.class_subclasses = set()
@@ -696,6 +698,19 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
                 f"The event handler name `{method_name}` shadows a builtin State method; use a different name instead"
                 f"The event handler name `{method_name}` shadows a builtin State method; use a different name instead"
             )
             )
 
 
+    @classmethod
+    def _check_overriden_basevars(cls):
+        """Check for shadow base vars and raise error if any.
+
+        Raises:
+            NameError: When a computed var shadows a base var.
+        """
+        for computed_var_ in cls._get_computed_vars():
+            if computed_var_._var_name in cls.__annotations__:
+                raise NameError(
+                    f"The computed var name `{computed_var_._var_name}` shadows a base var in {cls.__module__}.{cls.__name__}; use a different name instead"
+                )
+
     @classmethod
     @classmethod
     def get_skip_vars(cls) -> set[str]:
     def get_skip_vars(cls) -> set[str]:
         """Get the vars to skip when serializing.
         """Get the vars to skip when serializing.