From 4d9f427b192c6397730c7a9e9b6707c9e072def3 Mon Sep 17 00:00:00 2001 From: Elijah Ahianyo Date: Thu, 29 Aug 2024 20:23:21 +0000 Subject: [PATCH] [REF-3597] Type check Radio items (#3856) --- .../radix/themes/components/radio_group.py | 14 ++++++++++++++ .../radix/themes/components/radio_group.pyi | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/reflex/components/radix/themes/components/radio_group.py b/reflex/components/radix/themes/components/radio_group.py index 049e47b29..174abbac6 100644 --- a/reflex/components/radix/themes/components/radio_group.py +++ b/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 diff --git a/reflex/components/radix/themes/components/radio_group.pyi b/reflex/components/radix/themes/components/radio_group.pyi index 8fb19ae67..559607ae5 100644 --- a/reflex/components/radix/themes/components/radio_group.pyi +++ b/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. """ ...