Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve var create typing #4701

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -542,12 +543,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
Loading