2022-01-19 16:16:56 +08:00
|
|
|
|
"""本模块定义了 NoneBot 模块中共享的一些类型。
|
2020-08-19 20:29:37 +08:00
|
|
|
|
|
2022-01-19 16:16:56 +08:00
|
|
|
|
下面的文档中,「类型」部分使用 Python 的 Type Hint 语法,
|
|
|
|
|
参考 [`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)。
|
2020-08-19 20:29:37 +08:00
|
|
|
|
|
|
|
|
|
除了 Python 内置的类型,下面还出现了如下 NoneBot 自定类型,实际上它们是 Python 内置类型的别名。
|
|
|
|
|
|
2022-01-16 11:30:09 +08:00
|
|
|
|
FrontMatter:
|
|
|
|
|
sidebar_position: 11
|
|
|
|
|
description: nonebot.typing 模块
|
2020-08-19 20:29:37 +08:00
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
from typing import (
|
|
|
|
|
TYPE_CHECKING,
|
|
|
|
|
Any,
|
|
|
|
|
Dict,
|
|
|
|
|
Union,
|
|
|
|
|
TypeVar,
|
|
|
|
|
Callable,
|
|
|
|
|
Optional,
|
|
|
|
|
Awaitable,
|
|
|
|
|
)
|
2020-08-07 17:05:08 +08:00
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-12-21 18:22:14 +08:00
|
|
|
|
from asyncio import Task
|
|
|
|
|
|
2022-01-19 16:16:56 +08:00
|
|
|
|
from nonebot.adapters import Bot
|
2021-03-03 16:06:19 +08:00
|
|
|
|
from nonebot.permission import Permission
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
2021-11-21 16:12:36 +08:00
|
|
|
|
T_Wrapped = TypeVar("T_Wrapped", bound=Callable)
|
2020-12-22 20:11:48 +08:00
|
|
|
|
|
2020-08-07 17:51:57 +08:00
|
|
|
|
|
2022-01-19 16:16:56 +08:00
|
|
|
|
def overrides(InterfaceClass: object) -> Callable[[T_Wrapped], T_Wrapped]:
|
|
|
|
|
"""标记一个方法为父类 interface 的 implement"""
|
|
|
|
|
|
2020-12-22 20:11:48 +08:00
|
|
|
|
def overrider(func: T_Wrapped) -> T_Wrapped:
|
2021-11-22 23:21:26 +08:00
|
|
|
|
assert func.__name__ in dir(InterfaceClass), f"Error method: {func.__name__}"
|
2020-08-07 17:51:57 +08:00
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
return overrider
|
|
|
|
|
|
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
# state
|
2020-12-17 21:09:30 +08:00
|
|
|
|
T_State = Dict[Any, Any]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""事件处理状态 State 类型"""
|
2020-12-17 21:09:30 +08:00
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
_DependentCallable = Union[Callable[..., T], Callable[..., Awaitable[T]]]
|
|
|
|
|
|
|
|
|
|
# driver hooks
|
|
|
|
|
T_BotConnectionHook = _DependentCallable[Any]
|
2022-04-04 10:35:14 +08:00
|
|
|
|
"""Bot 连接建立时钩子函数
|
|
|
|
|
|
|
|
|
|
依赖参数:
|
|
|
|
|
|
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
T_BotDisconnectionHook = _DependentCallable[Any]
|
2022-04-04 10:35:14 +08:00
|
|
|
|
"""Bot 连接断开时钩子函数
|
|
|
|
|
|
|
|
|
|
依赖参数:
|
|
|
|
|
|
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
# api hooks
|
2022-02-04 11:06:38 +08:00
|
|
|
|
T_CallingAPIHook = Callable[["Bot", str, Dict[str, Any]], Awaitable[Any]]
|
2022-01-23 11:48:35 +08:00
|
|
|
|
"""`bot.call_api` 钩子函数"""
|
2021-04-01 20:23:55 +08:00
|
|
|
|
T_CalledAPIHook = Callable[
|
2022-02-04 11:06:38 +08:00
|
|
|
|
["Bot", Optional[Exception], str, Dict[str, Any], Any], Awaitable[Any]
|
2021-11-22 23:21:26 +08:00
|
|
|
|
]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""`bot.call_api` 后执行的函数,参数分别为 bot, exception, api, data, result"""
|
2020-12-28 13:36:00 +08:00
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
# event hooks
|
|
|
|
|
T_EventPreProcessor = _DependentCallable[Any]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""事件预处理函数 EventPreProcessor 类型
|
2021-11-21 12:36:44 +08:00
|
|
|
|
|
2022-01-12 19:15:56 +08:00
|
|
|
|
依赖参数:
|
2020-08-19 20:29:37 +08:00
|
|
|
|
|
2022-01-15 09:35:30 +08:00
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- EventParam: Event 对象
|
|
|
|
|
- StateParam: State 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
2020-11-07 17:35:44 +08:00
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
T_EventPostProcessor = _DependentCallable[Any]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""事件预处理函数 EventPostProcessor 类型
|
2021-11-21 12:36:44 +08:00
|
|
|
|
|
2022-01-12 19:15:56 +08:00
|
|
|
|
依赖参数:
|
2020-11-07 17:35:44 +08:00
|
|
|
|
|
2022-01-15 09:35:30 +08:00
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- EventParam: Event 对象
|
|
|
|
|
- StateParam: State 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
2020-11-07 17:35:44 +08:00
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
# matcher run hooks
|
|
|
|
|
T_RunPreProcessor = _DependentCallable[Any]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""事件响应器运行前预处理函数 RunPreProcessor 类型
|
2021-11-21 12:36:44 +08:00
|
|
|
|
|
2022-01-12 19:15:56 +08:00
|
|
|
|
依赖参数:
|
2020-11-07 17:35:44 +08:00
|
|
|
|
|
2022-01-15 09:35:30 +08:00
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- EventParam: Event 对象
|
|
|
|
|
- StateParam: State 对象
|
|
|
|
|
- MatcherParam: Matcher 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
2020-11-07 17:35:44 +08:00
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
T_RunPostProcessor = _DependentCallable[Any]
|
2022-06-24 10:51:06 +08:00
|
|
|
|
"""事件响应器运行后后处理函数 RunPostProcessor 类型
|
2021-11-21 12:36:44 +08:00
|
|
|
|
|
2022-01-12 19:15:56 +08:00
|
|
|
|
依赖参数:
|
2020-11-07 17:35:44 +08:00
|
|
|
|
|
2022-01-15 09:35:30 +08:00
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- EventParam: Event 对象
|
|
|
|
|
- StateParam: State 对象
|
|
|
|
|
- MatcherParam: Matcher 对象
|
|
|
|
|
- ExceptionParam: 异常对象(可能为 None)
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
2020-08-19 20:29:37 +08:00
|
|
|
|
"""
|
2020-08-10 13:06:02 +08:00
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
# rule, permission
|
|
|
|
|
T_RuleChecker = _DependentCallable[bool]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""RuleChecker 即判断是否响应事件的处理函数。
|
2021-11-21 12:36:44 +08:00
|
|
|
|
|
2022-01-12 19:15:56 +08:00
|
|
|
|
依赖参数:
|
2021-11-21 12:36:44 +08:00
|
|
|
|
|
2022-01-15 09:35:30 +08:00
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- EventParam: Event 对象
|
|
|
|
|
- StateParam: State 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
2020-08-19 23:00:31 +08:00
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
T_PermissionChecker = _DependentCallable[bool]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""PermissionChecker 即判断事件是否满足权限的处理函数。
|
2021-11-21 12:36:44 +08:00
|
|
|
|
|
2022-01-12 19:15:56 +08:00
|
|
|
|
依赖参数:
|
2020-08-19 23:00:31 +08:00
|
|
|
|
|
2022-01-15 09:35:30 +08:00
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- EventParam: Event 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
2020-08-19 23:00:31 +08:00
|
|
|
|
"""
|
2020-12-17 01:52:16 +08:00
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
T_Handler = _DependentCallable[Any]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""Handler 处理函数。"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
T_TypeUpdater = _DependentCallable[str]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""TypeUpdater 在 Matcher.pause, Matcher.reject 时被运行,用于更新响应的事件类型。默认会更新为 `message`。
|
2021-12-12 18:19:08 +08:00
|
|
|
|
|
2022-01-12 19:15:56 +08:00
|
|
|
|
依赖参数:
|
2021-03-03 16:06:19 +08:00
|
|
|
|
|
2022-01-15 09:35:30 +08:00
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- EventParam: Event 对象
|
|
|
|
|
- StateParam: State 对象
|
|
|
|
|
- MatcherParam: Matcher 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
2021-03-03 16:06:19 +08:00
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
T_PermissionUpdater = _DependentCallable["Permission"]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""PermissionUpdater 在 Matcher.pause, Matcher.reject 时被运行,用于更新会话对象权限。默认会更新为当前事件的触发对象。
|
2021-12-12 18:19:08 +08:00
|
|
|
|
|
2022-01-12 19:15:56 +08:00
|
|
|
|
依赖参数:
|
2021-03-03 16:06:19 +08:00
|
|
|
|
|
2022-01-15 09:35:30 +08:00
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- EventParam: Event 对象
|
|
|
|
|
- StateParam: State 对象
|
|
|
|
|
- MatcherParam: Matcher 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
2021-03-03 16:06:19 +08:00
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
T_DependencyCache = Dict[_DependentCallable[Any], "Task[Any]"]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""依赖缓存, 用于存储依赖函数的返回值"""
|