From 7f8a457d74b4b5f40f6e083c0dba1453d0a5dc26 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Thu, 29 Feb 2024 01:23:02 +0100 Subject: [PATCH] migrate to field.annotation and fix default var wrapping for undefined --- reflex/compiler/utils.py | 6 ++++-- reflex/components/component.py | 9 ++++++--- reflex/state.py | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/reflex/compiler/utils.py b/reflex/compiler/utils.py index 497ee12a2..f13e648ea 100644 --- a/reflex/compiler/utils.py +++ b/reflex/compiler/utils.py @@ -162,8 +162,10 @@ def _compile_client_storage_field( for field_type in (Cookie, LocalStorage): if isinstance(field.default, field_type): cs_obj = field.default - elif isinstance(field.type_, type) and issubclass(field.type_, field_type): - cs_obj = field.type_() + elif isinstance(field.annotation, type) and issubclass( + field.annotation, field_type + ): + cs_obj = field.annotation() else: continue return field_type, cs_obj.options() diff --git a/reflex/components/component.py b/reflex/components/component.py index ac4cdee0d..f32b476ba 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -22,6 +22,7 @@ from typing import ( ) from pydantic.fields import ModelPrivateAttr +from pydantic_core._pydantic_core import PydanticUndefinedType from reflex.base import Base from reflex.compiler.templates import STATEFUL_COMPONENT @@ -219,9 +220,11 @@ class Component(BaseComponent, ABC): continue # Set default values for any props. - if types._issubclass(field.type_, Var): - field.required = False - field.default = Var.create(field.default) + if types._issubclass(field.annotation, Var): + # TODO: pydantic v2 AttributeError: 'FieldInfo' object attribute 'is_required' is read-only + # field.is_required = False + if not isinstance(field.default, PydanticUndefinedType): + field.default = Var.create(field.default) # Ensure renamed props from parent classes are applied to the subclass. if cls._rename_props: diff --git a/reflex/state.py b/reflex/state.py index cb726f03f..5ae045341 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -1090,8 +1090,8 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow): for prop_name in self.base_vars: field = fields[prop_name] if isinstance(field.default, ClientStorageBase) or ( - isinstance(field.type_, type) - and issubclass(field.type_, ClientStorageBase) + isinstance(field.annotation, type) + and issubclass(field.annotation, ClientStorageBase) ): setattr(self, prop_name, copy.deepcopy(field.default))