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

add typing to function vars #4372

Merged
merged 7 commits into from
Nov 13, 2024
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
9 changes: 5 additions & 4 deletions reflex/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
from reflex.vars.base import LiteralVar, Var
from reflex.vars.function import (
ArgsFunctionOperation,
ArgsFunctionOperationBuilder,
BuilderFunctionVar,
FunctionArgs,
FunctionStringVar,
FunctionVar,
Expand Down Expand Up @@ -797,8 +799,7 @@ def scroll_to(elem_id: str, align_to_top: bool | Var[bool] = True) -> EventSpec:
get_element_by_id = FunctionStringVar.create("document.getElementById")

return run_script(
get_element_by_id(elem_id)
.call(elem_id)
get_element_by_id.call(elem_id)
.to(ObjectVar)
.scrollIntoView.to(FunctionVar)
.call(align_to_top),
Expand Down Expand Up @@ -1580,7 +1581,7 @@ def create(
)


class EventChainVar(FunctionVar, python_types=EventChain):
class EventChainVar(BuilderFunctionVar, python_types=EventChain):
"""Base class for event chain vars."""


Expand All @@ -1592,7 +1593,7 @@ class EventChainVar(FunctionVar, python_types=EventChain):
# Note: LiteralVar is second in the inheritance list allowing it act like a
# CachedVarOperation (ArgsFunctionOperation) and get the _js_expr from the
# _cached_var_name property.
class LiteralEventChainVar(ArgsFunctionOperation, LiteralVar, EventChainVar):
class LiteralEventChainVar(ArgsFunctionOperationBuilder, LiteralVar, EventChainVar):
"""A literal event chain var."""

_var_value: EventChain = dataclasses.field(default=None) # type: ignore
Expand Down
3 changes: 2 additions & 1 deletion reflex/utils/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def get_python_version() -> str:
Returns:
The Python version.
"""
return platform.python_version()
# Remove the "+" from the version string in case user is using a pre-release version.
return platform.python_version().rstrip("+")


def get_reflex_version() -> str:
Expand Down
22 changes: 18 additions & 4 deletions reflex/vars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,29 @@ def _var_is_string(self) -> bool:
return False

def __init_subclass__(
cls, python_types: Tuple[GenericType, ...] | GenericType = types.Unset, **kwargs
cls,
python_types: Tuple[GenericType, ...] | GenericType = types.Unset(),
default_type: GenericType = types.Unset(),
**kwargs,
):
"""Initialize the subclass.
Args:
python_types: The python types that the var represents.
default_type: The default type of the var. Defaults to the first python type.
**kwargs: Additional keyword arguments.
"""
super().__init_subclass__(**kwargs)

if python_types is not types.Unset:
if python_types or default_type:
python_types = (
python_types if isinstance(python_types, tuple) else (python_types,)
(python_types if isinstance(python_types, tuple) else (python_types,))
if python_types
else ()
)

default_type = default_type or (python_types[0] if python_types else Any)

@dataclasses.dataclass(
eq=False,
frozen=True,
Expand All @@ -388,7 +396,7 @@ class ToVarOperation(ToOperation, cls):
default=Var(_js_expr="null", _var_type=None),
)

_default_var_type: ClassVar[GenericType] = python_types[0]
_default_var_type: ClassVar[GenericType] = default_type

ToVarOperation.__name__ = f'To{cls.__name__.removesuffix("Var")}Operation'

Expand Down Expand Up @@ -588,6 +596,12 @@ def to(
output: type[list] | type[tuple] | type[set],
) -> ArrayVar: ...

@overload
def to(
self,
output: type[dict],
) -> ObjectVar[dict]: ...

@overload
def to(
self, output: Type[ObjectVar], var_type: Type[VAR_INSIDE]
Expand Down
Loading
Loading