nonebot2/nonebot/typing.py

258 lines
6.2 KiB
Python
Raw Normal View History

2020-08-19 20:29:37 +08: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
"""
2021-12-16 23:22:25 +08:00
from asyncio import Task
from typing import (
TYPE_CHECKING,
Any,
Dict,
Union,
TypeVar,
Callable,
NoReturn,
Optional,
Awaitable,
ForwardRef,
)
2020-08-07 17:05:08 +08:00
if TYPE_CHECKING:
from nonebot.adapters import Bot, Event
from nonebot.permission import Permission
2020-06-30 10:13:58 +08:00
T_Wrapped = TypeVar("T_Wrapped", bound=Callable)
2020-08-07 17:51:57 +08:00
2020-08-08 23:08:01 +08:00
def overrides(InterfaceClass: object):
def overrider(func: T_Wrapped) -> T_Wrapped:
assert func.__name__ in dir(InterfaceClass), f"Error method: {func.__name__}"
2020-08-07 17:51:57 +08:00
return func
return overrider
2020-12-17 21:09:30 +08:00
T_State = Dict[Any, Any]
2020-08-19 20:29:37 +08:00
"""
2020-12-06 02:30:19 +08:00
:类型: ``Dict[Any, Any]``
:说明:
事件处理状态 State 类型
"""
2020-12-17 21:09:30 +08:00
2021-05-21 17:06:20 +08:00
T_BotConnectionHook = Callable[["Bot"], Awaitable[None]]
2020-12-28 13:36:00 +08:00
"""
:类型: ``Callable[[Bot], Awaitable[None]]``
:说明:
2021-05-21 17:06:20 +08:00
Bot 连接建立时执行的函数
2020-12-28 13:36:00 +08:00
"""
2021-05-21 17:06:20 +08:00
T_BotDisconnectionHook = Callable[["Bot"], Awaitable[None]]
2020-12-28 13:36:00 +08:00
"""
:类型: ``Callable[[Bot], Awaitable[None]]``
:说明:
2021-05-21 17:06:20 +08:00
Bot 连接断开时执行的函数
2020-12-28 13:36:00 +08:00
"""
2021-03-31 21:20:07 +08:00
T_CallingAPIHook = Callable[["Bot", str, Dict[str, Any]], Awaitable[None]]
2021-04-01 20:23:55 +08:00
"""
:类型: ``Callable[[Bot, str, Dict[str, Any]], Awaitable[None]]``
:说明:
``bot.call_api`` 时执行的函数
"""
T_CalledAPIHook = Callable[
["Bot", Optional[Exception], str, Dict[str, Any], Any], Awaitable[None]
]
2021-04-01 20:23:55 +08:00
"""
:类型: ``Callable[[Bot, Optional[Exception], str, Dict[str, Any], Any], Awaitable[None]]``
:说明:
``bot.call_api`` 后执行的函数参数分别为 bot, exception, api, data, result
"""
2020-12-28 13:36:00 +08:00
T_EventPreProcessor = Callable[..., Union[None, Awaitable[None]]]
2020-12-06 02:30:19 +08:00
"""
:类型: ``Callable[..., Union[None, Awaitable[None]]]``
:依赖参数:
* ``DependParam``: 子依赖参数
* ``BotParam``: Bot 对象
* ``EventParam``: Event 对象
* ``StateParam``: State 对象
* ``DefaultParam``: 带有默认值的参数
2020-08-19 20:29:37 +08:00
:说明:
事件预处理函数 EventPreProcessor 类型
"""
T_EventPostProcessor = Callable[..., Union[None, Awaitable[None]]]
"""
:类型: ``Callable[..., Union[None, Awaitable[None]]]``
:依赖参数:
* ``DependParam``: 子依赖参数
* ``BotParam``: Bot 对象
* ``EventParam``: Event 对象
* ``StateParam``: State 对象
* ``DefaultParam``: 带有默认值的参数
:说明:
事件预处理函数 EventPostProcessor 类型
"""
T_RunPreProcessor = Callable[..., Union[None, Awaitable[None]]]
"""
:类型: ``Callable[..., Union[None, Awaitable[None]]]``
:依赖参数:
* ``DependParam``: 子依赖参数
* ``BotParam``: Bot 对象
* ``EventParam``: Event 对象
* ``StateParam``: State 对象
* ``MatcherParam``: Matcher 对象
* ``DefaultParam``: 带有默认值的参数
:说明:
事件响应器运行前预处理函数 RunPreProcessor 类型
"""
T_RunPostProcessor = Callable[..., Union[None, Awaitable[None]]]
"""
:类型: ``Callable[..., Union[None, Awaitable[None]]]``
:依赖参数:
* ``DependParam``: 子依赖参数
* ``BotParam``: Bot 对象
* ``EventParam``: Event 对象
* ``StateParam``: State 对象
* ``MatcherParam``: Matcher 对象
* ``ExceptionParam``: 异常对象可能为 None
* ``DefaultParam``: 带有默认值的参数
:说明:
2020-11-16 11:25:42 +08:00
事件响应器运行前预处理函数 RunPostProcessor 类型第二个参数为运行时产生的错误如果存在
2020-08-19 20:29:37 +08:00
"""
2020-08-10 13:06:02 +08:00
2021-11-19 18:18:53 +08:00
T_RuleChecker = Callable[..., Union[bool, Awaitable[bool]]]
2020-08-19 23:00:31 +08:00
"""
2021-11-19 18:18:53 +08:00
:类型: ``Callable[..., Union[bool, Awaitable[bool]]]``
2020-08-19 23:00:31 +08:00
:依赖参数:
* ``DependParam``: 子依赖参数
* ``BotParam``: Bot 对象
* ``EventParam``: Event 对象
* ``StateParam``: State 对象
* ``DefaultParam``: 带有默认值的参数
2020-08-19 23:00:31 +08:00
:说明:
RuleChecker 即判断是否响应事件的处理函数
"""
T_PermissionChecker = Callable[..., Union[bool, Awaitable[bool]]]
2020-08-19 23:00:31 +08:00
"""
:类型: ``Callable[..., Union[bool, Awaitable[bool]]]``
:依赖参数:
* ``DependParam``: 子依赖参数
* ``BotParam``: Bot 对象
* ``EventParam``: Event 对象
* ``DefaultParam``: 带有默认值的参数
2020-08-19 23:00:31 +08:00
:说明:
RuleChecker 即判断是否响应消息的处理函数
"""
2020-12-17 01:52:16 +08:00
T_Handler = Callable[..., Any]
2020-08-19 23:00:31 +08:00
"""
:类型: ``Callable[..., Any]``
2020-12-17 21:09:30 +08:00
:说明:
Handler 处理函数
"""
T_ArgsParser = Callable[..., Union[None, Awaitable[None]]]
"""
:类型: ``Callable[..., Union[None, Awaitable[None]]]``
2020-08-19 23:00:31 +08:00
:依赖参数:
2020-08-19 23:00:31 +08:00
* ``DependParam``: 子依赖参数
* ``BotParam``: Bot 对象
* ``EventParam``: Event 对象
* ``StateParam``: State 对象
* ``MatcherParam``: Matcher 对象
* ``DefaultParam``: 带有默认值的参数
2020-08-19 23:00:31 +08:00
:说明:
ArgsParser 即消息参数解析函数 Matcher.got 获取参数时被运行
"""
T_TypeUpdater = Callable[..., Union[str, Awaitable[str]]]
"""
:类型: ``Callable[..., Union[None, Awaitable[None]]]``
:依赖参数:
* ``DependParam``: 子依赖参数
* ``BotParam``: Bot 对象
* ``EventParam``: Event 对象
* ``StateParam``: State 对象
* ``MatcherParam``: Matcher 对象
* ``DefaultParam``: 带有默认值的参数
:说明:
TypeUpdater Matcher.pause, Matcher.reject 时被运行用于更新响应的事件类型默认会更新为 ``message``
"""
T_PermissionUpdater = Callable[..., Union["Permission", Awaitable["Permission"]]]
"""
:类型: ``Callable[..., Union[Permission, Awaitable[Permission]]]``
:依赖参数:
* ``DependParam``: 子依赖参数
* ``BotParam``: Bot 对象
* ``EventParam``: Event 对象
* ``StateParam``: State 对象
* ``MatcherParam``: Matcher 对象
* ``DefaultParam``: 带有默认值的参数
:说明:
PermissionUpdater Matcher.pause, Matcher.reject 时被运行用于更新会话对象权限默认会更新为当前事件的触发对象
"""
2021-12-16 23:22:25 +08:00
T_DependencyCache = Dict[Callable[..., Any], Task[Any]]
"""
:类型: ``Dict[Callable[..., Any], Task[Any]]``
:说明:
依赖缓存, 用于存储依赖函数的返回值
"""