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

This commit is contained in:
Benedikt Bartscher 2024-02-29 01:23:02 +01:00
parent c057f2e3f3
commit 7f8a457d74
No known key found for this signature in database
3 changed files with 12 additions and 7 deletions

View File

@ -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()

View File

@ -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:

View File

@ -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))