Переглянути джерело

migrate to field.annotation and fix default var wrapping for undefined

Benedikt Bartscher 1 рік тому
батько
коміт
7f8a457d74
3 змінених файлів з 12 додано та 7 видалено
  1. 4 2
      reflex/compiler/utils.py
  2. 6 3
      reflex/components/component.py
  3. 2 2
      reflex/state.py

+ 4 - 2
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()

+ 6 - 3
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:

+ 2 - 2
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))