diff --git a/nonebot/command/__init__.py b/nonebot/command/__init__.py index 6b463cd8..c68e668e 100644 --- a/nonebot/command/__init__.py +++ b/nonebot/command/__init__.py @@ -7,13 +7,14 @@ from typing import ( ) from nonebot import NoneBot, permission as perm -from nonebot.command.argfilter import ArgFilter_T, ValidateError +from nonebot.command.argfilter import ValidateError from nonebot.helpers import context_id, send, render_expression from nonebot.log import logger from nonebot.message import Message from nonebot.session import BaseSession from nonebot.typing import ( - Context_T, CommandName_T, CommandArgs_T, Message_T, State_T + Context_T, CommandName_T, CommandArgs_T, Message_T, State_T, + Filter_T ) # key: one segment of command name @@ -231,7 +232,7 @@ class CommandSession(BaseSession): self.current_key: Optional[str] = None # initialize current argument filters - self.current_arg_filters: Optional[List[ArgFilter_T]] = None + self.current_arg_filters: Optional[List[Filter_T]] = None self._current_send_kwargs: Dict[str, Any] = {} @@ -332,7 +333,7 @@ class CommandSession(BaseSession): def get(self, key: str, *, prompt: Optional[Message_T] = None, - arg_filters: Optional[List[ArgFilter_T]] = None, + arg_filters: Optional[List[Filter_T]] = None, **kwargs) -> Any: """ Get an argument with a given key. diff --git a/nonebot/command/argfilter/__init__.py b/nonebot/command/argfilter/__init__.py index ba78e3e1..ee751bce 100644 --- a/nonebot/command/argfilter/__init__.py +++ b/nonebot/command/argfilter/__init__.py @@ -1,12 +1,11 @@ -from typing import Callable, Any, Awaitable, Union, List, TYPE_CHECKING +from typing import Awaitable, List, TYPE_CHECKING from nonebot.helpers import render_expression +from nonebot.typing import Filter_T if TYPE_CHECKING: from nonebot.command import CommandSession -ArgFilter_T = Callable[[Any], Union[Any, Awaitable[Any]]] - class ValidateError(ValueError): def __init__(self, message=None): @@ -14,7 +13,7 @@ class ValidateError(ValueError): async def run_arg_filters(session: 'CommandSession', - arg_filters: List[ArgFilter_T]) -> None: + arg_filters: List[Filter_T]) -> None: """ Run a specific list of argument filters on a command session. diff --git a/nonebot/command/argfilter/validators.py b/nonebot/command/argfilter/validators.py index 5b6dad4b..edbcd36c 100644 --- a/nonebot/command/argfilter/validators.py +++ b/nonebot/command/argfilter/validators.py @@ -1,7 +1,7 @@ import re from typing import Callable, Any -from nonebot.command.argfilter import ValidateError, ArgFilter_T +from nonebot.command.argfilter import ValidateError, Filter_T class BaseValidator: @@ -16,7 +16,7 @@ def _raise_failure(message): raise ValidateError(message) -def not_empty(message=None) -> ArgFilter_T: +def not_empty(message=None) -> Filter_T: """ Validate any object to ensure it's not empty (is None or has no elements). """ @@ -32,7 +32,7 @@ def not_empty(message=None) -> ArgFilter_T: def fit_size(min_length: int = 0, max_length: int = None, - message=None) -> ArgFilter_T: + message=None) -> Filter_T: """ Validate any sized object to ensure the size/length is in a given range [min_length, max_length]. @@ -49,7 +49,7 @@ def fit_size(min_length: int = 0, max_length: int = None, def match_regex(pattern: str, message=None, *, flags=0, - fullmatch: bool = False) -> ArgFilter_T: + fullmatch: bool = False) -> Filter_T: """ Validate any string object to ensure it matches a given pattern. """ @@ -69,7 +69,7 @@ def match_regex(pattern: str, message=None, *, flags=0, def ensure_true(bool_func: Callable[[Any], bool], - message=None) -> ArgFilter_T: + message=None) -> Filter_T: """ Validate any object to ensure the result of applying a boolean function to it is True. @@ -83,7 +83,7 @@ def ensure_true(bool_func: Callable[[Any], bool], return validate -def between_inclusive(start=None, end=None, message=None) -> ArgFilter_T: +def between_inclusive(start=None, end=None, message=None) -> Filter_T: """ Validate any comparable object to ensure it's between `start` and `end` inclusively. diff --git a/nonebot/typing.py b/nonebot/typing.py index df890f14..121ef6d5 100644 --- a/nonebot/typing.py +++ b/nonebot/typing.py @@ -1,4 +1,4 @@ -from typing import Union, List, Dict, Any, Sequence, Callable, Tuple +from typing import Union, List, Dict, Any, Sequence, Callable, Tuple, Awaitable Context_T = Dict[str, Any] Message_T = Union[str, Dict[str, Any], List[Dict[str, Any]]] @@ -6,3 +6,4 @@ Expression_T = Union[str, Sequence[str], Callable] CommandName_T = Tuple[str, ...] CommandArgs_T = Dict[str, Any] State_T = Dict[str, Any] +Filter_T = Callable[[Any], Union[Any, Awaitable[Any]]]