From 2b6d6123094a3897dd9108967c2421b228ebfe8f Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Sat, 3 Oct 2026 15:21:40 +0200 Subject: [PATCH] fix(vars): type LiteralObjectVar.create to take any value that serializes to a mapping --- .../news/+literal-object-var-create.bugfix.md | 1 + packages/reflex-base/src/reflex_base/vars/object.py | 5 +++-- tests/type_checking/vars.py | 12 ++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 packages/reflex-base/news/+literal-object-var-create.bugfix.md 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 41123ac887d..b9e385b9f62 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 from typing_extensions import assert_type @@ -11,6 +12,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 @@ -38,3 +40,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))