diff --git a/packages/reflex-base/news/+literal-object-var-create.bugfix.md b/packages/reflex-base/news/+literal-object-var-create.bugfix.md new file mode 100644 index 00000000000..7bf0aee5b70 --- /dev/null +++ b/packages/reflex-base/news/+literal-object-var-create.bugfix.md @@ -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. diff --git a/packages/reflex-base/src/reflex_base/vars/object.py b/packages/reflex-base/src/reflex_base/vars/object.py index 3b4cfc46a3a..8b8504944df 100644 --- a/packages/reflex-base/src/reflex_base/vars/object.py +++ b/packages/reflex-base/src/reflex_base/vars/object.py @@ -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. diff --git a/tests/type_checking/vars.py b/tests/type_checking/vars.py index 772820f74e5..ca0c88259f5 100644 --- a/tests/type_checking/vars.py +++ b/tests/type_checking/vars.py @@ -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 @@ -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 @@ -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))