migrate to field.annotation and fix default var wrapping for undefined
This commit is contained in:
parent
c057f2e3f3
commit
7f8a457d74
@ -162,8 +162,10 @@ def _compile_client_storage_field(
|
|||||||
for field_type in (Cookie, LocalStorage):
|
for field_type in (Cookie, LocalStorage):
|
||||||
if isinstance(field.default, field_type):
|
if isinstance(field.default, field_type):
|
||||||
cs_obj = field.default
|
cs_obj = field.default
|
||||||
elif isinstance(field.type_, type) and issubclass(field.type_, field_type):
|
elif isinstance(field.annotation, type) and issubclass(
|
||||||
cs_obj = field.type_()
|
field.annotation, field_type
|
||||||
|
):
|
||||||
|
cs_obj = field.annotation()
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
return field_type, cs_obj.options()
|
return field_type, cs_obj.options()
|
||||||
|
@ -22,6 +22,7 @@ from typing import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from pydantic.fields import ModelPrivateAttr
|
from pydantic.fields import ModelPrivateAttr
|
||||||
|
from pydantic_core._pydantic_core import PydanticUndefinedType
|
||||||
|
|
||||||
from reflex.base import Base
|
from reflex.base import Base
|
||||||
from reflex.compiler.templates import STATEFUL_COMPONENT
|
from reflex.compiler.templates import STATEFUL_COMPONENT
|
||||||
@ -219,9 +220,11 @@ class Component(BaseComponent, ABC):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# Set default values for any props.
|
# Set default values for any props.
|
||||||
if types._issubclass(field.type_, Var):
|
if types._issubclass(field.annotation, Var):
|
||||||
field.required = False
|
# TODO: pydantic v2 AttributeError: 'FieldInfo' object attribute 'is_required' is read-only
|
||||||
field.default = Var.create(field.default)
|
# 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.
|
# Ensure renamed props from parent classes are applied to the subclass.
|
||||||
if cls._rename_props:
|
if cls._rename_props:
|
||||||
|
@ -1090,8 +1090,8 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|||||||
for prop_name in self.base_vars:
|
for prop_name in self.base_vars:
|
||||||
field = fields[prop_name]
|
field = fields[prop_name]
|
||||||
if isinstance(field.default, ClientStorageBase) or (
|
if isinstance(field.default, ClientStorageBase) or (
|
||||||
isinstance(field.type_, type)
|
isinstance(field.annotation, type)
|
||||||
and issubclass(field.type_, ClientStorageBase)
|
and issubclass(field.annotation, ClientStorageBase)
|
||||||
):
|
):
|
||||||
setattr(self, prop_name, copy.deepcopy(field.default))
|
setattr(self, prop_name, copy.deepcopy(field.default))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user