From da95d0f5198e438b694347e889e5f1185b0c3f1a Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Mon, 5 Aug 2024 16:01:56 -0700 Subject: [PATCH] remove all instances of Var.create --- reflex/components/chakra/forms/switch.py | 3 ++- reflex/components/core/cond.py | 2 +- .../radix/themes/components/radio_group.py | 17 ++++++++--------- .../radix/themes/components/text_field.py | 3 ++- reflex/ivars/base.py | 15 +++++++++++++++ 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/reflex/components/chakra/forms/switch.py b/reflex/components/chakra/forms/switch.py index a52c7c9a5..5b4c02da3 100644 --- a/reflex/components/chakra/forms/switch.py +++ b/reflex/components/chakra/forms/switch.py @@ -4,6 +4,7 @@ from __future__ import annotations from reflex.components.chakra import ChakraComponent, LiteralColorScheme from reflex.event import EventHandler +from reflex.ivars.base import LiteralVar from reflex.vars import Var @@ -34,7 +35,7 @@ class Switch(ChakraComponent): name: Var[str] # The value of the input field when checked (use is_checked prop for a bool) - value: Var[str] = Var.create(True) # type: ignore + value: Var[str] = LiteralVar.create("true") # The spacing between the switch and its label text (0.5rem) spacing: Var[str] diff --git a/reflex/components/core/cond.py b/reflex/components/core/cond.py index 0f1376d3c..f00f7a150 100644 --- a/reflex/components/core/cond.py +++ b/reflex/components/core/cond.py @@ -154,7 +154,7 @@ def cond(condition: Any, c1: Any, c2: Any = None) -> Component | ImmutableVar: raise ValueError("For conditional vars, the second argument must be set.") def create_var(cond_part): - return LiteralVar.create_safe(cond_part) + return LiteralVar.create(cond_part) # convert the truth and false cond parts into vars so the _var_data can be obtained. c1 = create_var(c1) diff --git a/reflex/components/radix/themes/components/radio_group.py b/reflex/components/radix/themes/components/radio_group.py index c159742d7..b16a73a33 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.function import JSON_STRINGIFY +from reflex.ivars.sequence import StringVar from reflex.vars import Var from ..base import ( @@ -160,19 +161,18 @@ class HighLevelRadioGroup(RadixThemesComponent): else: default_value = JSON_STRINGIFY.call(ImmutableVar.create(default_value)) - def radio_group_item(value: str | Var) -> Component: - item_value = Var.create(value, _var_is_string=False) # type: ignore + def radio_group_item(value: Var) -> Component: item_value = rx.cond( - item_value._type() == str, # type: ignore - item_value, - JSON_STRINGIFY.call(item_value), # type: ignore - ) + value._type() == "string", + value, + JSON_STRINGIFY.call(value), + ).to(StringVar) return Text.create( Flex.create( RadioGroupItem.create( value=item_value, - disabled=props.get("disabled", Var.create(False)), + disabled=props.get("disabled", LiteralVar.create(False)), ), item_value, spacing="2", @@ -181,8 +181,7 @@ class HighLevelRadioGroup(RadixThemesComponent): as_="label", ) - items = Var.create(items) # type: ignore - children = [rx.foreach(items, radio_group_item)] + children = [rx.foreach(LiteralVar.create(items), radio_group_item)] return RadioGroupRoot.create( Flex.create( diff --git a/reflex/components/radix/themes/components/text_field.py b/reflex/components/radix/themes/components/text_field.py index c6d7ddf48..aca422840 100644 --- a/reflex/components/radix/themes/components/text_field.py +++ b/reflex/components/radix/themes/components/text_field.py @@ -10,6 +10,7 @@ from reflex.components.core.breakpoints import Responsive from reflex.components.core.debounce import DebounceInput from reflex.components.el import elements from reflex.event import EventHandler +from reflex.ivars.base import LiteralVar from reflex.style import Style, format_as_emotion from reflex.utils import console from reflex.vars import Var @@ -154,7 +155,7 @@ class TextFieldRoot(elements.Div, RadixThemesComponent): style = Style(template.style) style.update(child.style) child._get_style = lambda style=style: { - "css": Var.create(format_as_emotion(style)) + "css": LiteralVar.create(format_as_emotion(style)) } for trigger in template.event_triggers: if trigger not in child.event_triggers: diff --git a/reflex/ivars/base.py b/reflex/ivars/base.py index e2dfdab1f..d7d75a35d 100644 --- a/reflex/ivars/base.py +++ b/reflex/ivars/base.py @@ -684,6 +684,21 @@ class ImmutableVar(Var, Generic[VAR_TYPE]): ).to(ObjectVar) return refs[self] + def _type(self) -> StringVar: + """Returns the type of the object. + + This method uses the `typeof` function from the `FunctionStringVar` class + to determine the type of the object. + + Returns: + StringVar: A string variable representing the type of the object. + """ + from .function import FunctionStringVar + from .sequence import StringVar + + type_of = FunctionStringVar("typeof") + return type_of.call(self).to(StringVar) + OUTPUT = TypeVar("OUTPUT", bound=ImmutableVar)