nonebot2/nonebot/typing.py

137 lines
3.7 KiB
Python
Raw Normal View History

2020-08-19 12:29:37 +00:00
"""
类型
====
下面的文档中类型部分使用 Python Type Hint 语法 `PEP 484`_`PEP 526`_ `typing`_
除了 Python 内置的类型下面还出现了如下 NoneBot 自定类型实际上它们是 Python 内置类型的别名
以下类型均可从 nonebot.typing 模块导入
.. _PEP 484:
https://www.python.org/dev/peps/pep-0484/
.. _PEP 526:
https://www.python.org/dev/peps/pep-0526/
.. _typing:
https://docs.python.org/3/library/typing.html
"""
2018-10-15 17:03:50 +00:00
2020-12-16 17:52:16 +00:00
from functools import singledispatch
from typing import Any, Dict, Union, overload, Optional, Callable, NoReturn, Awaitable, TYPE_CHECKING
2020-08-07 09:05:08 +00:00
if TYPE_CHECKING:
2020-12-06 16:31:14 +00:00
from nonebot.adapters import Bot, Event
from nonebot.matcher import Matcher
2020-06-30 02:13:58 +00:00
2020-08-07 09:51:57 +00:00
2020-08-08 15:08:01 +00:00
def overrides(InterfaceClass: object):
2020-08-07 09:51:57 +00:00
2020-08-08 15:08:01 +00:00
def overrider(func: Callable) -> Callable:
2020-08-07 09:51:57 +00:00
assert func.__name__ in dir(
InterfaceClass), f"Error method: {func.__name__}"
return func
return overrider
2020-12-05 18:30:19 +00:00
State = Dict[Any, Any]
2020-08-19 12:29:37 +00:00
"""
2020-12-05 18:30:19 +00:00
:类型: ``Dict[Any, Any]``
:说明:
事件处理状态 State 类型
"""
2020-12-06 16:31:14 +00:00
EventPreProcessor = Callable[["Bot", "Event", State], Awaitable[None]]
2020-12-05 18:30:19 +00:00
"""
:类型: ``Callable[[Bot, Event, State], Awaitable[None]]``
2020-08-19 12:29:37 +00:00
:说明:
事件预处理函数 EventPreProcessor 类型
"""
2020-12-06 16:31:14 +00:00
EventPostProcessor = Callable[["Bot", "Event", State], Awaitable[None]]
"""
2020-12-05 18:30:19 +00:00
:类型: ``Callable[[Bot, Event, State], Awaitable[None]]``
:说明:
事件预处理函数 EventPostProcessor 类型
"""
2020-12-06 16:31:14 +00:00
RunPreProcessor = Callable[["Matcher", "Bot", "Event", State], Awaitable[None]]
"""
2020-12-05 18:30:19 +00:00
:类型: ``Callable[[Matcher, Bot, Event, State], Awaitable[None]]``
:说明:
事件响应器运行前预处理函数 RunPreProcessor 类型
"""
2020-12-06 16:31:14 +00:00
RunPostProcessor = Callable[
["Matcher", Optional[Exception], "Bot", "Event", State], Awaitable[None]]
"""
2020-12-05 18:30:19 +00:00
:类型: ``Callable[[Matcher, Optional[Exception], Bot, Event, State], Awaitable[None]]``
:说明:
2020-11-16 03:25:42 +00:00
事件响应器运行前预处理函数 RunPostProcessor 类型第二个参数为运行时产生的错误如果存在
2020-08-19 12:29:37 +00:00
"""
2020-08-10 05:06:02 +00:00
2020-12-06 16:31:14 +00:00
RuleChecker = Callable[["Bot", "Event", State], Union[bool, Awaitable[bool]]]
2020-08-19 15:00:31 +00:00
"""
2020-12-05 18:30:19 +00:00
:类型: ``Callable[[Bot, Event, State], Union[bool, Awaitable[bool]]]``
2020-08-19 15:00:31 +00:00
:说明:
RuleChecker 即判断是否响应事件的处理函数
"""
2020-12-06 16:31:14 +00:00
PermissionChecker = Callable[["Bot", "Event"], Union[bool, Awaitable[bool]]]
2020-08-19 15:00:31 +00:00
"""
2020-09-13 05:01:23 +00:00
:类型: ``Callable[[Bot, Event], Union[bool, Awaitable[bool]]]``
2020-08-19 15:00:31 +00:00
:说明:
RuleChecker 即判断是否响应消息的处理函数
"""
2020-12-16 17:52:16 +00:00
# @overload
# async def Handler(bot: "Bot") -> None:
# ...
# @overload
# async def Handler(bot: "Bot", event: "Event") -> None:
# ...
# @overload
# async def Handler(bot: "Bot", state: State) -> None:
# ...
# @overload
# async def Handler(bot: Any, event: Any, state: State) -> None:
# ...
Handler = Union[Callable[["Bot", "Event", State], Union[Awaitable[None],
Awaitable[NoReturn]]],
Callable[["Bot", State], Union[Awaitable[None],
Awaitable[NoReturn]]],
Callable[["Bot", "Event"], Union[Awaitable[None],
Awaitable[NoReturn]]],
Callable[["Bot"], Union[Awaitable[None], Awaitable[NoReturn]]]]
2020-08-19 15:00:31 +00:00
"""
2020-12-06 16:31:14 +00:00
:类型: ``Callable[[Bot, Event, State], Union[Awaitable[None], Awaitable[NoReturn]]]``
2020-08-19 15:00:31 +00:00
:说明:
Handler 即事件的处理函数
"""
2020-12-07 06:09:43 +00:00
ArgsParser = Callable[["Bot", "Event", State], Union[Awaitable[None],
Awaitable[NoReturn]]]
2020-08-19 15:00:31 +00:00
"""
2020-12-07 06:09:43 +00:00
:类型: ``Callable[[Bot, Event, State], Union[Awaitable[None], Awaitable[NoReturn]]]``
2020-08-19 15:00:31 +00:00
:说明:
ArgsParser 即消息参数解析函数 Matcher.got 获取参数时被运行
"""