From 4bd2156929a457b60276bd6d2054895651bdd7b0 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Wed, 29 Dec 2021 21:52:46 +0800 Subject: [PATCH] :bug: fix matcher.skip missing --- nonebot/dependencies/__init__.py | 4 ++-- nonebot/exception.py | 10 +++++++++- nonebot/matcher.py | 23 +++++++++++++++-------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/nonebot/dependencies/__init__.py b/nonebot/dependencies/__init__.py index 147dcb1d..6040401a 100644 --- a/nonebot/dependencies/__init__.py +++ b/nonebot/dependencies/__init__.py @@ -15,7 +15,7 @@ from pydantic.fields import Required, FieldInfo, Undefined, ModelField from nonebot.log import logger from .utils import get_typed_signature -from nonebot.exception import SkippedException +from nonebot.exception import TypeMisMatch from nonebot.utils import run_sync, is_coroutine_callable T = TypeVar("T", bound="Dependent") @@ -182,7 +182,7 @@ class Dependent(Generic[R]): f"type {type(value)} not match depends {self.call} " f"annotation {field._type_display()}, ignored" ) - raise SkippedException(field, value) + raise TypeMisMatch(field, value) else: values[field.name] = value diff --git a/nonebot/exception.py b/nonebot/exception.py index ce2fb9e6..62eb23b7 100644 --- a/nonebot/exception.py +++ b/nonebot/exception.py @@ -126,12 +126,20 @@ class SkippedException(MatcherException): 可以在 ``Handler`` 中通过 ``Matcher.skip()`` 抛出。 """ + +class TypeMisMatch(SkippedException): + """ + :说明: + + 当前 ``Handler`` 的参数类型不匹配。 + """ + def __init__(self, param: ModelField, value: Any): self.param: ModelField = param self.value: Any = value def __repr__(self): - return f"" + return f"" def __str__(self): self.__repr__() diff --git a/nonebot/matcher.py b/nonebot/matcher.py index b55287f7..af7cd8d2 100644 --- a/nonebot/matcher.py +++ b/nonebot/matcher.py @@ -42,13 +42,6 @@ from nonebot.consts import ( LAST_RECEIVE_KEY, REJECT_CACHE_TARGET, ) -from nonebot.exception import ( - PausedException, - StopPropagation, - SkippedException, - FinishedException, - RejectedException, -) from nonebot.typing import ( Any, T_State, @@ -58,6 +51,14 @@ from nonebot.typing import ( T_DependencyCache, T_PermissionUpdater, ) +from nonebot.exception import ( + TypeMisMatch, + PausedException, + StopPropagation, + SkippedException, + FinishedException, + RejectedException, +) if TYPE_CHECKING: from nonebot.plugin import Plugin @@ -646,6 +647,10 @@ class Matcher(metaclass=MatcherMeta): await cls.send(prompt, **kwargs) raise RejectedException + @classmethod + def skip(cls) -> NoReturn: + raise SkippedException + def get_receive(self, id: str, default: T = None) -> Union[Event, T]: return self.state.get(RECEIVE_KEY.format(id=id), default) @@ -729,11 +734,13 @@ class Matcher(metaclass=MatcherMeta): stack=stack, dependency_cache=dependency_cache, ) - except SkippedException as e: + except TypeMisMatch as e: logger.debug( f"Handler {handler} param {e.param.name} value {e.value} " f"mismatch type {e.param._type_display()}, skipped" ) + except SkippedException as e: + logger.debug(f"Handler {handler} skipped") except StopPropagation: self.block = True finally: