Feature: 添加插件 Pydantic 相关使用方法 (#2563)

This commit is contained in:
Ju4tCode
2024-02-05 14:00:49 +08:00
committed by GitHub
parent 2ebf956599
commit dace63d9d2
10 changed files with 93 additions and 19 deletions

View File

@ -12,6 +12,7 @@ from typing_extensions import Self, Annotated, is_typeddict
from typing import (
TYPE_CHECKING,
Any,
Set,
Dict,
List,
Type,
@ -50,6 +51,7 @@ __all__ = (
"model_field_validate",
"model_fields",
"model_config",
"model_dump",
"type_validate_python",
"custom_validation",
)
@ -183,6 +185,13 @@ if PYDANTIC_V2: # pragma: pydantic-v2
"""Get config of a model."""
return model.model_config
def model_dump(
model: BaseModel,
include: Optional[Set[str]] = None,
exclude: Optional[Set[str]] = None,
) -> Dict[str, Any]:
return model.model_dump(include=include, exclude=exclude)
def type_validate_python(type_: Type[T], data: Any) -> T:
"""Validate data with given type."""
return TypeAdapter(type_).validate_python(data)
@ -321,6 +330,13 @@ else: # pragma: pydantic-v1
"""Get config of a model."""
return model.__config__
def model_dump(
model: BaseModel,
include: Optional[Set[str]] = None,
exclude: Optional[Set[str]] = None,
) -> Dict[str, Any]:
return model.dict(include=include, exclude=exclude)
def type_validate_python(type_: Type[T], data: Any) -> T:
"""Validate data with given type."""
return parse_obj_as(type_, data)