mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 02:50:48 +00:00
♿ reuse type check code for dependent
This commit is contained in:
@ -17,6 +17,7 @@ from pydantic.fields import Required, Undefined, ModelField
|
||||
from nonebot.log import logger
|
||||
from nonebot.exception import TypeMisMatch
|
||||
from nonebot.adapters import Bot, Event, Message
|
||||
from nonebot.dependencies.utils import check_field_type
|
||||
from nonebot.dependencies import Param, Dependent, CustomConfig
|
||||
from nonebot.typing import T_State, T_Handler, T_DependencyCache
|
||||
from nonebot.consts import (
|
||||
@ -174,13 +175,14 @@ class DependParam(Param):
|
||||
class _BotChecker(Param):
|
||||
async def _solve(self, bot: Bot, **kwargs: Any) -> Any:
|
||||
field: ModelField = self.extra["field"]
|
||||
_, errs_ = field.validate(bot, {}, loc=("bot",))
|
||||
if errs_:
|
||||
try:
|
||||
return check_field_type(field, bot)
|
||||
except TypeMisMatch:
|
||||
logger.debug(
|
||||
f"Bot type {type(bot)} not match "
|
||||
f"annotation {field._type_display()}, ignored"
|
||||
)
|
||||
raise TypeMisMatch(field, bot)
|
||||
raise
|
||||
|
||||
|
||||
class BotParam(Param):
|
||||
@ -217,13 +219,14 @@ class BotParam(Param):
|
||||
class _EventChecker(Param):
|
||||
async def _solve(self, event: Event, **kwargs: Any) -> Any:
|
||||
field: ModelField = self.extra["field"]
|
||||
_, errs_ = field.validate(event, {}, loc=("event",))
|
||||
if errs_:
|
||||
try:
|
||||
return check_field_type(field, event)
|
||||
except TypeMisMatch:
|
||||
logger.debug(
|
||||
f"Event type {type(event)} not match "
|
||||
f"annotation {field._type_display()}, ignored"
|
||||
)
|
||||
raise TypeMisMatch(field, event)
|
||||
raise
|
||||
|
||||
|
||||
class EventParam(Param):
|
||||
|
Reference in New Issue
Block a user