prepare base
This commit is contained in:
parent
0b4b7c0d12
commit
25d05856f9
@ -2,15 +2,16 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from typing import Any, List, Type
|
from typing import Any, List, Type, Dict, Optional
|
||||||
|
|
||||||
import pydantic
|
import pydantic
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from pydantic.fields import ModelField
|
from pydantic.fields import FieldInfo
|
||||||
|
|
||||||
from reflex import constants
|
from reflex import constants
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: migrate to pydantic v2
|
||||||
def validate_field_name(bases: List[Type["BaseModel"]], field_name: str) -> None:
|
def validate_field_name(bases: List[Type["BaseModel"]], field_name: str) -> None:
|
||||||
"""Ensure that the field's name does not shadow an existing attribute of the model.
|
"""Ensure that the field's name does not shadow an existing attribute of the model.
|
||||||
|
|
||||||
@ -35,7 +36,8 @@ def validate_field_name(bases: List[Type["BaseModel"]], field_name: str) -> None
|
|||||||
|
|
||||||
# monkeypatch pydantic validate_field_name method to skip validating
|
# monkeypatch pydantic validate_field_name method to skip validating
|
||||||
# shadowed state vars when reloading app via utils.prerequisites.get_app(reload=True)
|
# shadowed state vars when reloading app via utils.prerequisites.get_app(reload=True)
|
||||||
pydantic.main.validate_field_name = validate_field_name # type: ignore
|
# TODO
|
||||||
|
# pydantic.main.validate_field_name = validate_field_name # type: ignore
|
||||||
|
|
||||||
|
|
||||||
class Base(pydantic.BaseModel):
|
class Base(pydantic.BaseModel):
|
||||||
@ -61,9 +63,10 @@ class Base(pydantic.BaseModel):
|
|||||||
Returns:
|
Returns:
|
||||||
The object as a json string.
|
The object as a json string.
|
||||||
"""
|
"""
|
||||||
from reflex.utils.serializers import serialize
|
# from reflex.utils.serializers import serialize
|
||||||
|
|
||||||
return self.__config__.json_dumps(self.dict(), default=serialize)
|
return self.model_dump_json()
|
||||||
|
# return self.__config__.json_dumps(self.dict(), default=serialize)
|
||||||
|
|
||||||
def set(self, **kwargs):
|
def set(self, **kwargs):
|
||||||
"""Set multiple fields and return the object.
|
"""Set multiple fields and return the object.
|
||||||
@ -85,7 +88,8 @@ class Base(pydantic.BaseModel):
|
|||||||
Returns:
|
Returns:
|
||||||
The fields of the object.
|
The fields of the object.
|
||||||
"""
|
"""
|
||||||
return cls.__fields__
|
return cls.model_fields
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def add_field(cls, var: Any, default_value: Any):
|
def add_field(cls, var: Any, default_value: Any):
|
||||||
@ -97,14 +101,10 @@ class Base(pydantic.BaseModel):
|
|||||||
var: The variable to add a pydantic field for.
|
var: The variable to add a pydantic field for.
|
||||||
default_value: The default value of the field
|
default_value: The default value of the field
|
||||||
"""
|
"""
|
||||||
new_field = ModelField.infer(
|
field_info = FieldInfo(default=default_value, annotation=var._var_type)
|
||||||
name=var._var_name,
|
cls.model_fields.update({var._var_name: field_info})
|
||||||
value=default_value,
|
cls.model_rebuild(force=True)
|
||||||
annotation=var._var_type,
|
|
||||||
class_validators=None,
|
|
||||||
config=cls.__config__,
|
|
||||||
)
|
|
||||||
cls.__fields__.update({var._var_name: new_field})
|
|
||||||
|
|
||||||
def get_value(self, key: str) -> Any:
|
def get_value(self, key: str) -> Any:
|
||||||
"""Get the value of a field.
|
"""Get the value of a field.
|
||||||
@ -115,7 +115,7 @@ class Base(pydantic.BaseModel):
|
|||||||
Returns:
|
Returns:
|
||||||
The value of the field.
|
The value of the field.
|
||||||
"""
|
"""
|
||||||
if isinstance(key, str) and key in self.__fields__:
|
if isinstance(key, str) and key in self.get_fields():
|
||||||
# Seems like this function signature was wrong all along?
|
# Seems like this function signature was wrong all along?
|
||||||
# If the user wants a field that we know of, get it and pass it off to _get_value
|
# If the user wants a field that we know of, get it and pass it off to _get_value
|
||||||
key = getattr(self, key)
|
key = getattr(self, key)
|
||||||
|
Loading…
Reference in New Issue
Block a user