reuse type check code for dependent

This commit is contained in:
yanyongyu
2022-01-28 14:49:04 +08:00
parent 1271a757c9
commit ad712c59b3
3 changed files with 29 additions and 14 deletions

View File

@ -4,11 +4,16 @@ FrontMatter:
description: nonebot.dependencies.utils 模块
"""
import inspect
from typing import Any, Dict, Callable
from typing import Any, Dict, TypeVar, Callable
from loguru import logger
from pydantic.fields import ModelField
from pydantic.typing import ForwardRef, evaluate_forwardref
from nonebot.exception import TypeMisMatch
V = TypeVar("V")
def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
"""获取可调用对象签名"""
@ -40,3 +45,10 @@ def get_typed_annotation(param: inspect.Parameter, globalns: Dict[str, Any]) ->
)
return inspect.Parameter.empty
return annotation
def check_field_type(field: ModelField, value: V) -> V:
_, errs_ = field.validate(value, {}, loc=())
if errs_:
raise TypeMisMatch(field, value)
return value