Skip to content

Commit

Permalink
add typing to function vars (#4372)
Browse files Browse the repository at this point in the history
* add typing to function vars

* import ParamSpec from typing_extensions

* remove ellipsis as they are not supported in 3.9

* try importing everything from extensions

* special case 3.9

* don't use Any from extensions

* get typevar from extensions
  • Loading branch information
adhami3310 authored Nov 13, 2024
1 parent 5d88263 commit 27c1a7e
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 47 deletions.
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

0 comments on commit 27c1a7e

Please sign in to comment.