From 5beea25b3171256c72b964e9200be542850b5b19 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Tue, 28 Jan 2025 22:56:38 -0800 Subject: [PATCH] improve var create typing (#4701) --- .../components/radix/primitives/accordion.py | 3 +- .../components/radix/primitives/accordion.pyi | 4 +- reflex/vars/base.py | 53 ++++++++++++++++++- reflex/vars/number.py | 2 +- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/reflex/components/radix/primitives/accordion.py b/reflex/components/radix/primitives/accordion.py index 170b3ed9cea..2d9c7ae960e 100644 --- a/reflex/components/radix/primitives/accordion.py +++ b/reflex/components/radix/primitives/accordion.py @@ -196,8 +196,9 @@ class AccordionItem(AccordionComponent): # The header of the accordion item. header: Var[Union[Component, str]] + # The content of the accordion item. - content: Var[Union[Component, str]] = Var.create(None) + content: Var[Union[Component, str, None]] = Var.create(None) _valid_children: List[str] = [ "AccordionHeader", diff --git a/reflex/components/radix/primitives/accordion.pyi b/reflex/components/radix/primitives/accordion.pyi index 03208f49675..447451d11eb 100644 --- a/reflex/components/radix/primitives/accordion.pyi +++ b/reflex/components/radix/primitives/accordion.pyi @@ -308,7 +308,9 @@ class AccordionItem(AccordionComponent): value: Optional[Union[Var[str], str]] = None, disabled: Optional[Union[Var[bool], bool]] = None, header: Optional[Union[Component, Var[Union[Component, str]], str]] = None, - content: Optional[Union[Component, Var[Union[Component, str]], str]] = None, + content: Optional[ + Union[Component, Var[Optional[Union[Component, str]]], str] + ] = None, color_scheme: Optional[ Union[ Literal[ diff --git a/reflex/vars/base.py b/reflex/vars/base.py index 4ca53eeb11a..6f4eddf42c6 100644 --- a/reflex/vars/base.py +++ b/reflex/vars/base.py @@ -80,6 +80,7 @@ VAR_TYPE = TypeVar("VAR_TYPE", covariant=True) OTHER_VAR_TYPE = TypeVar("OTHER_VAR_TYPE") +STRING_T = TypeVar("STRING_T", bound=str) warnings.filterwarnings("ignore", message="fields may not start with an underscore") @@ -550,12 +551,60 @@ def _replace( return value_with_replaced + @overload + @classmethod + def create( # type: ignore[override] + cls, + value: bool, + _var_data: VarData | None = None, + ) -> BooleanVar: ... + + @overload + @classmethod + def create( # type: ignore[override] + cls, + value: int, + _var_data: VarData | None = None, + ) -> NumberVar[int]: ... + + @overload @classmethod def create( cls, - value: Any, + value: float, _var_data: VarData | None = None, - ) -> Var: + ) -> NumberVar[float]: ... + + @overload + @classmethod + def create( + cls, + value: STRING_T, + _var_data: VarData | None = None, + ) -> StringVar[STRING_T]: ... + + @overload + @classmethod + def create( + cls, + value: None, + _var_data: VarData | None = None, + ) -> NoneVar: ... + + @overload + @classmethod + def create( + cls, + value: OTHER_VAR_TYPE, + _var_data: VarData | None = None, + ) -> Var[OTHER_VAR_TYPE]: ... + + @classmethod + def create( + cls, + value: OTHER_VAR_TYPE, + _var_data: VarData | None = None, + ) -> Var[OTHER_VAR_TYPE]: """Create a var from a value. Args: diff --git a/reflex/vars/number.py b/reflex/vars/number.py index 475c08b5538..050dc2329b2 100644 --- a/reflex/vars/number.py +++ b/reflex/vars/number.py @@ -31,7 +31,7 @@ var_operation_return, ) -NUMBER_T = TypeVar("NUMBER_T", int, float, Union[int, float], bool) +NUMBER_T = TypeVar("NUMBER_T", int, float, bool) if TYPE_CHECKING: from .sequence import ArrayVar