2022-01-19 16:16:56 +08:00
|
|
|
|
"""本模块定义了 NoneBot 模块中共享的一些类型。
|
2020-08-19 20:29:37 +08:00
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
使用 Python 的 Type Hint 语法,
|
2022-01-19 16:16:56 +08:00
|
|
|
|
参考 [`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
|
|
|
|
|
2022-01-16 11:30:09 +08:00
|
|
|
|
FrontMatter:
|
2024-10-22 10:33:48 +08:00
|
|
|
|
mdx:
|
|
|
|
|
format: md
|
2022-01-16 11:30:09 +08:00
|
|
|
|
sidebar_position: 11
|
|
|
|
|
description: nonebot.typing 模块
|
2020-08-19 20:29:37 +08:00
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
import sys
|
|
|
|
|
import types
|
2023-07-17 15:56:27 +08:00
|
|
|
|
import warnings
|
2024-01-26 11:12:57 +08:00
|
|
|
|
import typing as t
|
|
|
|
|
import typing_extensions as t_ext
|
|
|
|
|
from typing import TYPE_CHECKING, TypeVar
|
|
|
|
|
from typing_extensions import ParamSpec, TypeAlias, get_args, override, get_origin
|
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")
|
2023-06-24 14:47:35 +08:00
|
|
|
|
P = ParamSpec("P")
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_Wrapped: TypeAlias = t.Callable[P, T]
|
2020-12-22 20:11:48 +08:00
|
|
|
|
|
2020-08-07 17:51:57 +08:00
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
|
def overrides(InterfaceClass: object):
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""标记一个方法为父类 interface 的 implement"""
|
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
|
warnings.warn(
|
|
|
|
|
"overrides is deprecated and will be removed in a future version, "
|
|
|
|
|
"use @typing_extensions.override instead. "
|
|
|
|
|
"See [PEP 698](https://peps.python.org/pep-0698/) for more details.",
|
|
|
|
|
DeprecationWarning,
|
|
|
|
|
)
|
|
|
|
|
return override
|
2020-08-07 17:51:57 +08:00
|
|
|
|
|
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
if sys.version_info < (3, 10):
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def type_has_args(type_: type[t.Any]) -> bool:
|
|
|
|
|
"""判断类型是否有参数"""
|
|
|
|
|
return isinstance(type_, (t._GenericAlias, types.GenericAlias)) # type: ignore
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
def type_has_args(type_: type[t.Any]) -> bool:
|
|
|
|
|
return isinstance(type_, (t._GenericAlias, types.GenericAlias, types.UnionType)) # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if sys.version_info < (3, 10):
|
|
|
|
|
|
|
|
|
|
def origin_is_union(origin: t.Optional[type[t.Any]]) -> bool:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
"""判断是否是 Union 类型"""
|
|
|
|
|
return origin is t.Union
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def origin_is_union(origin: t.Optional[type[t.Any]]) -> bool:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return origin is t.Union or origin is types.UnionType
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def origin_is_literal(origin: t.Optional[type[t.Any]]) -> bool:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
"""判断是否是 Literal 类型"""
|
|
|
|
|
return origin is t.Literal or origin is t_ext.Literal
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def _literal_values(type_: type[t.Any]) -> tuple[t.Any, ...]:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
return get_args(type_)
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def all_literal_values(type_: type[t.Any]) -> list[t.Any]:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
"""获取 Literal 类型包含的所有值"""
|
|
|
|
|
if not origin_is_literal(get_origin(type_)):
|
|
|
|
|
return [type_]
|
|
|
|
|
|
|
|
|
|
return [x for value in _literal_values(type_) for x in all_literal_values(value)]
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def origin_is_annotated(origin: t.Optional[type[t.Any]]) -> bool:
|
2024-02-06 12:48:23 +08:00
|
|
|
|
"""判断是否是 Annotated 类型"""
|
2024-07-20 14:03:32 +08:00
|
|
|
|
return origin is t_ext.Annotated
|
2024-02-06 12:48:23 +08:00
|
|
|
|
|
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
NONE_TYPES = {None, type(None), t.Literal[None], t_ext.Literal[None]}
|
|
|
|
|
if sys.version_info >= (3, 10):
|
|
|
|
|
NONE_TYPES.add(types.NoneType)
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def is_none_type(type_: type[t.Any]) -> bool:
|
2024-01-26 11:12:57 +08:00
|
|
|
|
"""判断是否是 None 类型"""
|
|
|
|
|
return type_ in NONE_TYPES
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def evaluate_forwardref(
|
|
|
|
|
ref: t.ForwardRef, globalns: dict[str, t.Any], localns: dict[str, t.Any]
|
|
|
|
|
) -> t.Any:
|
2024-06-20 20:49:17 +08:00
|
|
|
|
# Python 3.13/3.12.4+ made `recursive_guard` a kwarg,
|
|
|
|
|
# so name it explicitly to avoid:
|
|
|
|
|
# TypeError: ForwardRef._evaluate()
|
|
|
|
|
# missing 1 required keyword-only argument: 'recursive_guard'
|
|
|
|
|
return ref._evaluate(globalns, localns, recursive_guard=frozenset())
|
2024-01-26 11:12:57 +08:00
|
|
|
|
|
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
# state
|
2024-05-09 15:08:49 +08:00
|
|
|
|
# use annotated flag to avoid ForwardRef recreate generic type (py >= 3.11)
|
|
|
|
|
class StateFlag:
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return "StateFlag()"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_STATE_FLAG = StateFlag()
|
|
|
|
|
|
|
|
|
|
T_State: TypeAlias = t.Annotated[dict[t.Any, t.Any], _STATE_FLAG]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""事件处理状态 State 类型"""
|
2020-12-17 21:09:30 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
_DependentCallable: TypeAlias = t.Union[
|
|
|
|
|
t.Callable[..., T], t.Callable[..., t.Awaitable[T]]
|
|
|
|
|
]
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
# driver hooks
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_BotConnectionHook: TypeAlias = _DependentCallable[t.Any]
|
2022-04-04 10:35:14 +08:00
|
|
|
|
"""Bot 连接建立时钩子函数
|
|
|
|
|
|
|
|
|
|
依赖参数:
|
|
|
|
|
|
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
|
|
|
|
"""
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_BotDisconnectionHook: TypeAlias = _DependentCallable[t.Any]
|
2022-04-04 10:35:14 +08:00
|
|
|
|
"""Bot 连接断开时钩子函数
|
|
|
|
|
|
|
|
|
|
依赖参数:
|
|
|
|
|
|
|
|
|
|
- DependParam: 子依赖参数
|
|
|
|
|
- BotParam: Bot 对象
|
|
|
|
|
- DefaultParam: 带有默认值的参数
|
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
# api hooks
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_CallingAPIHook: TypeAlias = t.Callable[
|
2024-04-16 00:33:48 +08:00
|
|
|
|
["Bot", str, dict[str, t.Any]], t.Awaitable[t.Any]
|
2024-01-26 11:12:57 +08:00
|
|
|
|
]
|
2022-01-23 11:48:35 +08:00
|
|
|
|
"""`bot.call_api` 钩子函数"""
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_CalledAPIHook: TypeAlias = t.Callable[
|
2024-04-16 00:33:48 +08:00
|
|
|
|
["Bot", t.Optional[Exception], str, dict[str, t.Any], t.Any], t.Awaitable[t.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
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_EventPreProcessor: TypeAlias = _DependentCallable[t.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
|
|
|
|
"""
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_EventPostProcessor: TypeAlias = _DependentCallable[t.Any]
|
2023-11-20 10:21:10 +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
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_RunPreProcessor: TypeAlias = _DependentCallable[t.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
|
|
|
|
"""
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_RunPostProcessor: TypeAlias = _DependentCallable[t.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
|
2023-06-24 14:47:35 +08:00
|
|
|
|
T_RuleChecker: TypeAlias = _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
|
|
|
|
"""
|
2023-06-24 14:47:35 +08:00
|
|
|
|
T_PermissionChecker: TypeAlias = _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
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
T_Handler: TypeAlias = _DependentCallable[t.Any]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""Handler 处理函数。"""
|
2023-06-24 14:47:35 +08:00
|
|
|
|
T_TypeUpdater: TypeAlias = _DependentCallable[str]
|
|
|
|
|
"""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
|
|
|
|
"""
|
2023-06-24 14:47:35 +08:00
|
|
|
|
T_PermissionUpdater: TypeAlias = _DependentCallable["Permission"]
|
|
|
|
|
"""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
|
|
|
|
"""
|
2024-04-16 00:33:48 +08:00
|
|
|
|
T_DependencyCache: TypeAlias = dict[_DependentCallable[t.Any], "Task[t.Any]"]
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""依赖缓存, 用于存储依赖函数的返回值"""
|