Skip to content

Commit

Permalink
improve var create typing (#4701)
Browse files Browse the repository at this point in the history
  • Loading branch information
adhami3310 authored Jan 29, 2025
1 parent 58f87a6 commit 5beea25
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
3 changes: 2 additions & 1 deletion reflex/components/radix/primitives/accordion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion reflex/components/radix/primitives/accordion.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down
53 changes: 51 additions & 2 deletions reflex/vars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion reflex/vars/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5beea25

Please sign in to comment.