Skip to content

Commit ad14f38

Browse files
authored
add type hinting to existing types (#3729)
* add type hinting to existing types * dang it darglint * i cannot
1 parent 129adc9 commit ad14f38

File tree

6 files changed

+415
-65
lines changed

6 files changed

+415
-65
lines changed

reflex/experimental/vars/base.py

+51-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010
TYPE_CHECKING,
1111
Any,
1212
Callable,
13+
Dict,
14+
Generic,
15+
List,
1316
Optional,
17+
Set,
18+
Tuple,
1419
Type,
1520
TypeVar,
21+
Union,
1622
overload,
1723
)
1824

@@ -42,13 +48,15 @@
4248
from .object import ObjectVar, ToObjectOperation
4349
from .sequence import ArrayVar, StringVar, ToArrayOperation, ToStringOperation
4450

51+
VAR_TYPE = TypeVar("VAR_TYPE")
52+
4553

4654
@dataclasses.dataclass(
4755
eq=False,
4856
frozen=True,
4957
**{"slots": True} if sys.version_info >= (3, 10) else {},
5058
)
51-
class ImmutableVar(Var):
59+
class ImmutableVar(Var, Generic[VAR_TYPE]):
5260
"""Base class for immutable vars."""
5361

5462
# The name of the var.
@@ -405,6 +413,8 @@ def guess_type(self) -> ImmutableVar:
405413
return self.to(ArrayVar, var_type)
406414
if issubclass(fixed_type, str):
407415
return self.to(StringVar)
416+
if issubclass(fixed_type, Base):
417+
return self.to(ObjectVar, var_type)
408418
return self
409419

410420

@@ -531,3 +541,43 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
531541
return wrapper
532542

533543
return decorator
544+
545+
546+
def unionize(*args: Type) -> Type:
547+
"""Unionize the types.
548+
549+
Args:
550+
args: The types to unionize.
551+
552+
Returns:
553+
The unionized types.
554+
"""
555+
if not args:
556+
return Any
557+
first, *rest = args
558+
if not rest:
559+
return first
560+
return Union[first, unionize(*rest)]
561+
562+
563+
def figure_out_type(value: Any) -> Type:
564+
"""Figure out the type of the value.
565+
566+
Args:
567+
value: The value to figure out the type of.
568+
569+
Returns:
570+
The type of the value.
571+
"""
572+
if isinstance(value, list):
573+
return List[unionize(*(figure_out_type(v) for v in value))]
574+
if isinstance(value, set):
575+
return Set[unionize(*(figure_out_type(v) for v in value))]
576+
if isinstance(value, tuple):
577+
return Tuple[unionize(*(figure_out_type(v) for v in value)), ...]
578+
if isinstance(value, dict):
579+
return Dict[
580+
unionize(*(figure_out_type(k) for k in value)),
581+
unionize(*(figure_out_type(v) for v in value.values())),
582+
]
583+
return type(value)

reflex/experimental/vars/function.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from reflex.vars import ImmutableVarData, Var, VarData
1212

1313

14-
class FunctionVar(ImmutableVar):
14+
class FunctionVar(ImmutableVar[Callable]):
1515
"""Base class for immutable function vars."""
1616

1717
def __call__(self, *args: Var | Any) -> ArgsFunctionOperation:

reflex/experimental/vars/number.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from reflex.vars import ImmutableVarData, Var, VarData
1616

1717

18-
class NumberVar(ImmutableVar):
18+
class NumberVar(ImmutableVar[Union[int, float]]):
1919
"""Base class for immutable number vars."""
2020

2121
def __add__(self, other: number_types | boolean_types) -> NumberAddOperation:
@@ -693,7 +693,7 @@ def _cached_var_name(self) -> str:
693693
return f"Math.trunc({str(value)})"
694694

695695

696-
class BooleanVar(ImmutableVar):
696+
class BooleanVar(ImmutableVar[bool]):
697697
"""Base class for immutable boolean vars."""
698698

699699
def __and__(self, other: bool) -> BooleanAndOperation:

0 commit comments

Comments
 (0)