From 2042097f83f3180195a35846bf432bd8c825a78a Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 27 Sep 2020 18:05:13 +0800 Subject: [PATCH] :art: change args into optional --- nonebot/permission.py | 11 +++++++---- nonebot/plugin.py | 34 +++++++++++++--------------------- nonebot/rule.py | 8 +++++--- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/nonebot/permission.py b/nonebot/permission.py index 1f43cf38..e194f453 100644 --- a/nonebot/permission.py +++ b/nonebot/permission.py @@ -14,7 +14,7 @@ import asyncio from nonebot.utils import run_sync -from nonebot.typing import Bot, Event, Union, NoReturn, Callable, Awaitable, PermissionChecker +from nonebot.typing import Bot, Event, Union, NoReturn, Optional, Callable, Awaitable, PermissionChecker class Permission: @@ -53,10 +53,13 @@ class Permission: def __and__(self, other) -> NoReturn: raise RuntimeError("And operation between Permissions is not allowed.") - def __or__(self, other: Union["Permission", - PermissionChecker]) -> "Permission": + def __or__( + self, other: Optional[Union["Permission", + PermissionChecker]]) -> "Permission": checkers = self.checkers.copy() - if isinstance(other, Permission): + if other is None: + return self + elif isinstance(other, Permission): checkers |= other.checkers elif asyncio.iscoroutinefunction(other): checkers.add(other) # type: ignore diff --git a/nonebot/plugin.py b/nonebot/plugin.py index 7ca59bee..4936dc8e 100644 --- a/nonebot/plugin.py +++ b/nonebot/plugin.py @@ -28,7 +28,7 @@ class Plugin(object): def on(rule: Optional[Union[Rule, RuleChecker]] = None, - permission: Permission = Permission(), + permission: Optional[Permission] = None, *, handlers: Optional[List[Handler]] = None, temp: bool = False, @@ -37,7 +37,7 @@ def on(rule: Optional[Union[Rule, RuleChecker]] = None, state: Optional[dict] = None) -> Type[Matcher]: matcher = Matcher.new("", Rule() & rule, - permission, + permission or Permission(), temp=temp, priority=priority, block=block, @@ -67,7 +67,7 @@ def on_metaevent(rule: Optional[Union[Rule, RuleChecker]] = None, def on_message(rule: Optional[Union[Rule, RuleChecker]] = None, - permission: Permission = Permission(), + permission: Optional[Permission] = None, *, handlers: Optional[List[Handler]] = None, temp: bool = False, @@ -76,7 +76,7 @@ def on_message(rule: Optional[Union[Rule, RuleChecker]] = None, state: Optional[dict] = None) -> Type[Matcher]: matcher = Matcher.new("message", Rule() & rule, - permission, + permission or Permission(), temp=temp, priority=priority, block=block, @@ -126,26 +126,21 @@ def on_request(rule: Optional[Union[Rule, RuleChecker]] = None, def on_startswith(msg: str, rule: Optional[Optional[Union[Rule, RuleChecker]]] = None, - permission: Permission = Permission(), **kwargs) -> Type[Matcher]: - return on_message(startswith(msg) & - rule, permission, **kwargs) if rule else on_message( - startswith(msg), permission, **kwargs) + return on_message(startswith(msg) & rule, **kwargs) if rule else on_message( + startswith(msg), **kwargs) def on_endswith(msg: str, rule: Optional[Optional[Union[Rule, RuleChecker]]] = None, - permission: Permission = Permission(), **kwargs) -> Type[Matcher]: - return on_message(endswith(msg) & - rule, permission, **kwargs) if rule else on_message( - startswith(msg), permission, **kwargs) + return on_message(endswith(msg) & rule, **kwargs) if rule else on_message( + startswith(msg), **kwargs) def on_command(cmd: Union[str, Tuple[str, ...]], alias: Set[Union[str, Tuple[str, ...]]] = None, rule: Optional[Union[Rule, RuleChecker]] = None, - permission: Permission = Permission(), **kwargs) -> Union[Type[Matcher], MatcherGroup]: if isinstance(cmd, str): cmd = (cmd,) @@ -162,27 +157,24 @@ def on_command(cmd: Union[str, Tuple[str, ...]], alias = set(map(lambda x: (x,) if isinstance(x, str) else x, alias)) group = MatcherGroup("message", Rule() & rule, - permission, handlers=handlers, **kwargs) for cmd_ in [cmd, *alias]: group.new(rule=command(cmd_)) return group else: - return on_message( - command(cmd) & rule, permission, handlers=handlers, ** - kwargs) if rule else on_message( - command(cmd), permission, handlers=handlers, **kwargs) + return on_message(command(cmd) & rule, handlers=handlers, ** + kwargs) if rule else on_message( + command(cmd), handlers=handlers, **kwargs) def on_regex(pattern: str, flags: Union[int, re.RegexFlag] = 0, rule: Optional[Rule] = None, - permission: Permission = Permission(), **kwargs) -> Type[Matcher]: return on_message(regex(pattern, flags) & - rule, permission, **kwargs) if rule else on_message( - regex(pattern, flags), permission, **kwargs) + rule, **kwargs) if rule else on_message( + regex(pattern, flags), **kwargs) def load_plugin(module_path: str) -> Optional[Plugin]: diff --git a/nonebot/rule.py b/nonebot/rule.py index 0b1b5cc0..7d57b33e 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -20,7 +20,7 @@ from pygtrie import CharTrie from nonebot import get_driver from nonebot.log import logger from nonebot.utils import run_sync -from nonebot.typing import Bot, Any, Dict, Event, Union, Tuple, NoReturn, Callable, Awaitable, RuleChecker +from nonebot.typing import Bot, Any, Dict, Event, Union, Tuple, NoReturn, Optional, Callable, Awaitable, RuleChecker class Rule: @@ -68,9 +68,11 @@ class Rule: *map(lambda c: c(bot, event, state), self.checkers)) return all(results) - def __and__(self, other: Union["Rule", RuleChecker]) -> "Rule": + def __and__(self, other: Optional[Union["Rule", RuleChecker]]) -> "Rule": checkers = self.checkers.copy() - if isinstance(other, Rule): + if other is None: + return self + elif isinstance(other, Rule): checkers |= other.checkers elif asyncio.iscoroutinefunction(other): checkers.add(other) # type: ignore