Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`LiteralObjectVar.create` is typed to take any value that serializes to a mapping, such as a dataclass, as it does at runtime, not only a mapping.
5 changes: 3 additions & 2 deletions packages/reflex-base/src/reflex_base/vars/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,15 @@ def _cached_get_all_var_data(self) -> VarData | None:
@classmethod
def create(
cls,
_var_value: Mapping,
_var_value: object,
_var_type: type[OBJECT_TYPE] | None = None,
_var_data: VarData | None = None,
) -> LiteralObjectVar[OBJECT_TYPE]:
"""Create the literal object var.

Args:
_var_value: The value of the var.
_var_value: The value of the var: a mapping, or a value that serializes
to one, such as a dataclass.
_var_type: The type of the var.
_var_data: Additional hooks and imports associated with the Var.

Expand Down
12 changes: 12 additions & 0 deletions tests/type_checking/vars.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Inferred types for `reflex.vars` that are part of the public contract."""

import dataclasses
from typing import Any, Literal, assert_type

from reflex.vars.base import Var
Expand All @@ -9,6 +10,7 @@
LiteralNumberVar,
NumberVar,
)
from reflex.vars.object import LiteralObjectVar
from reflex.vars.sequence import LiteralArrayVar, LiteralStringVar, StringVar

# `Var.create` dispatches on the value type through a long overload set. Each of
Expand Down Expand Up @@ -36,3 +38,13 @@
_str_var: Var[str] = Var("expr")
assert_type(_bool_var.guess_type(), BooleanVar)
assert_type(_str_var.guess_type(), StringVar[str])


# `LiteralObjectVar.create` takes any value that serializes to a mapping, such
# as a dataclass, not only a mapping.
@dataclasses.dataclass
class _Point:
x: int


LiteralObjectVar.create(_Point(1))
Loading