浏览代码

[REF-3597] Type check Radio items (#3856)

Elijah Ahianyo 9 月之前
父节点
当前提交
4d9f427b19

+ 14 - 0
reflex/components/radix/themes/components/radio_group.py

@@ -12,6 +12,7 @@ from reflex.components.radix.themes.typography.text import Text
 from reflex.event import EventHandler
 from reflex.ivars.base import ImmutableVar, LiteralVar
 from reflex.ivars.sequence import StringVar
+from reflex.utils import types
 from reflex.vars import Var
 
 from ..base import (
@@ -133,6 +134,9 @@ class HighLevelRadioGroup(RadixThemesComponent):
 
         Returns:
             The created radio group component.
+
+        Raises:
+            TypeError: If the type of items is invalid.
         """
         direction = props.pop("direction", "column")
         spacing = props.pop("spacing", "2")
@@ -141,6 +145,16 @@ class HighLevelRadioGroup(RadixThemesComponent):
         color_scheme = props.pop("color_scheme", None)
         default_value = props.pop("default_value", "")
 
+        if (
+            not isinstance(items, (list, Var))
+            or isinstance(items, Var)
+            and not types._issubclass(items._var_type, list)
+        ):
+            items_type = type(items) if not isinstance(items, Var) else items._var_type
+            raise TypeError(
+                f"The radio group component takes in a list, got {items_type} instead"
+            )
+
         default_value = LiteralVar.create(default_value)
 
         # convert only non-strings to json(JSON.stringify) so quotes are not rendered

+ 6 - 0
reflex/components/radix/themes/components/radio_group.pyi

@@ -419,6 +419,9 @@ class HighLevelRadioGroup(RadixThemesComponent):
 
         Returns:
             The created radio group component.
+
+        Raises:
+            TypeError: If the type of items is invalid.
         """
         ...
 
@@ -588,6 +591,9 @@ class RadioGroup(ComponentNamespace):
 
         Returns:
             The created radio group component.
+
+        Raises:
+            TypeError: If the type of items is invalid.
         """
         ...