finish typing change

This commit is contained in:
yanyongyu
2020-12-17 21:09:30 +08:00
parent 6bd69e6708
commit 799144e64d
19 changed files with 309 additions and 315 deletions

View File

@ -19,7 +19,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 State, RuleChecker
from nonebot.typing import T_State, T_RuleChecker
if TYPE_CHECKING:
from nonebot.adapters import Bot, Event
@ -43,12 +43,12 @@ class Rule:
__slots__ = ("checkers",)
def __init__(
self, *checkers: Callable[["Bot", "Event", State],
Awaitable[bool]]) -> None:
self, *checkers: Callable[["Bot", "Event", T_State],
Awaitable[bool]]) -> None:
"""
:参数:
* ``*checkers: Callable[[Bot, Event, State], Awaitable[bool]]``: **异步** RuleChecker
* ``*checkers: Callable[[Bot, Event, T_State], Awaitable[bool]]``: **异步** RuleChecker
"""
self.checkers = set(checkers)
@ -59,10 +59,11 @@ class Rule:
:类型:
* ``Set[Callable[[Bot, Event, State], Awaitable[bool]]]``
* ``Set[Callable[[Bot, Event, T_State], Awaitable[bool]]]``
"""
async def __call__(self, bot: "Bot", event: "Event", state: State) -> bool:
async def __call__(self, bot: "Bot", event: "Event",
state: T_State) -> bool:
"""
:说明:
@ -72,7 +73,7 @@ class Rule:
* ``bot: Bot``: Bot 对象
* ``event: Event``: Event 对象
* ``state: State``: 当前 State
* ``state: T_State``: 当前 State
:返回:
@ -82,7 +83,7 @@ class Rule:
*map(lambda c: c(bot, event, state), self.checkers))
return all(results)
def __and__(self, other: Optional[Union["Rule", RuleChecker]]) -> "Rule":
def __and__(self, other: Optional[Union["Rule", T_RuleChecker]]) -> "Rule":
checkers = self.checkers.copy()
if other is None:
return self
@ -118,7 +119,7 @@ class TrieRule:
@classmethod
def get_value(cls, bot: "Bot", event: "Event",
state: State) -> Tuple[Dict[str, Any], Dict[str, Any]]:
state: T_State) -> Tuple[Dict[str, Any], Dict[str, Any]]:
if event.get_type() != "message":
state["_prefix"] = {"raw_command": None, "command": None}
state["_suffix"] = {"raw_command": None, "command": None}
@ -182,7 +183,7 @@ def startswith(msg: str) -> Rule:
* ``msg: str``: 消息开头字符串
"""
async def _startswith(bot: "Bot", event: "Event", state: State) -> bool:
async def _startswith(bot: "Bot", event: "Event", state: T_State) -> bool:
if event.get_type() != "message":
return False
text = event.get_plaintext()
@ -202,7 +203,7 @@ def endswith(msg: str) -> Rule:
* ``msg: str``: 消息结尾字符串
"""
async def _endswith(bot: "Bot", event: "Event", state: State) -> bool:
async def _endswith(bot: "Bot", event: "Event", state: T_State) -> bool:
if event.get_type() != "message":
return False
return event.get_plaintext().endswith(msg)
@ -221,7 +222,7 @@ def keyword(*keywords: str) -> Rule:
* ``*keywords: str``: 关键词
"""
async def _keyword(bot: "Bot", event: "Event", state: State) -> bool:
async def _keyword(bot: "Bot", event: "Event", state: T_State) -> bool:
if event.get_type() != "message":
return False
text = event.get_plaintext()
@ -269,7 +270,7 @@ def command(*cmds: Union[str, Tuple[str, ...]]) -> Rule:
for start, sep in product(command_start, command_sep):
TrieRule.add_prefix(f"{start}{sep.join(command)}", command)
async def _command(bot: "Bot", event: "Event", state: State) -> bool:
async def _command(bot: "Bot", event: "Event", state: T_State) -> bool:
return state["_prefix"]["command"] in commands
return Rule(_command)
@ -295,7 +296,7 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
pattern = re.compile(regex, flags)
async def _regex(bot: "Bot", event: "Event", state: State) -> bool:
async def _regex(bot: "Bot", event: "Event", state: T_State) -> bool:
if event.get_type() != "message":
return False
matched = pattern.search(str(event.get_message()))
@ -320,7 +321,7 @@ def to_me() -> Rule:
* 无
"""
async def _to_me(bot: "Bot", event: "Event", state: State) -> bool:
async def _to_me(bot: "Bot", event: "Event", state: T_State) -> bool:
return event.is_tome()
return Rule(_to_me)