فهرست منبع

update behaviour for wrong state passed as argument (#1447)

Thomas Brandého 1 سال پیش
والد
کامیت
6bc622e67d
2فایلهای تغییر یافته به همراه17 افزوده شده و 6 حذف شده
  1. 8 6
      reflex/app.py
  2. 9 0
      reflex/utils/console.py

+ 8 - 6
reflex/app.py

@@ -108,6 +108,7 @@ class App(Base):
         """
         super().__init__(*args, **kwargs)
         state_subclasses = State.__subclasses__()
+        inferred_state = state_subclasses[-1]
         is_testing_env = constants.PYTEST_CURRENT_TEST in os.environ
 
         # Special case to allow test cases have multiple subclasses of rx.State.
@@ -117,13 +118,14 @@ class App(Base):
                 raise ValueError(
                     "rx.State has been subclassed multiple times. Only one subclass is allowed"
                 )
-            if self.state != DefaultState:
-                console.deprecate(
-                    "Passing the state as keyword argument to `rx.App` is deprecated. "
-                    "The base state will automatically be inferred as the subclass of `rx.State`."
-                )
-            self.state = state_subclasses[-1]
 
+            # verify that provided state is valid
+            if self.state not in [DefaultState, inferred_state]:
+                console.warn(
+                    f"Using substate ({self.state.__name__}) as root state in `rx.App` is currently not supported."
+                    f" Defaulting to root state: ({inferred_state.__name__})"
+                )
+            self.state = inferred_state
         # Get the config
         config = get_config()
 

+ 9 - 0
reflex/utils/console.py

@@ -21,6 +21,15 @@ def deprecate(msg: str) -> None:
     _console.print(f"[yellow]DeprecationWarning: {msg}[/yellow]")
 
 
+def warn(msg: str) -> None:
+    """Print a warning about bad usage in Reflex.
+
+    Args:
+        msg: The warning message.
+    """
+    _console.print(f"[orange1]UsageWarning: {msg}[/orange1]")
+
+
 def log(msg: str) -> None:
     """Takes a string and logs it to the console.