Feature: 支持子依赖定义 Pydantic 类型校验 (#2310)

This commit is contained in:
Ju4tCode
2023-08-29 18:45:12 +08:00
committed by GitHub
parent 79f833b946
commit f59271bd47
6 changed files with 244 additions and 25 deletions

View File

@ -5,7 +5,7 @@ FrontMatter:
"""
import inspect
from typing import Any, Dict, TypeVar, Callable, ForwardRef
from typing import Any, Dict, Callable, ForwardRef
from loguru import logger
from pydantic.fields import ModelField
@ -13,8 +13,6 @@ from pydantic.typing import evaluate_forwardref
from nonebot.exception import TypeMisMatch
V = TypeVar("V")
def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
"""获取可调用对象签名"""
@ -49,10 +47,10 @@ def get_typed_annotation(param: inspect.Parameter, globalns: Dict[str, Any]) ->
return annotation
def check_field_type(field: ModelField, value: V) -> V:
def check_field_type(field: ModelField, value: Any) -> Any:
"""检查字段类型是否匹配"""
_, errs_ = field.validate(value, {}, loc=())
v, errs_ = field.validate(value, {}, loc=())
if errs_:
raise TypeMisMatch(field, value)
return value
return v