From 3fb8450fb5d928f07c6d7440193bc53863993218 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Fri, 13 Dec 2024 23:22:47 +0300 Subject: [PATCH] check against optional in annotation str --- reflex/utils/types.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/reflex/utils/types.py b/reflex/utils/types.py index 184eebf7c..4e54aa70f 100644 --- a/reflex/utils/types.py +++ b/reflex/utils/types.py @@ -281,7 +281,19 @@ def true_type_for_pydantic_field(f: ModelField): """ if not isinstance(f.annotation, (str, ForwardRef)): return f.annotation - return f.outer_type_ + + type_ = f.outer_type_ + + if ( + (isinstance(f.annotation, str) and f.annotation.startswith("Optional")) + or ( + isinstance(f.annotation, ForwardRef) + and f.annotation.__forward_arg__.startswith("Optional") + ) + ) and not is_optional(type_): + return Optional[type_] + + return type_ def value_inside_optional(cls: GenericType) -> GenericType: