nonebot2/nonebot/typing.py

165 lines
3.9 KiB
Python
Raw Normal View History

2020-06-30 10:13:58 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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
"""
2018-10-16 01:03:50 +08:00
2020-08-07 17:05:08 +08:00
from types import ModuleType
2020-08-10 13:06:02 +08:00
from typing import NoReturn, TYPE_CHECKING
2020-08-07 17:05:08 +08:00
from typing import Any, Set, List, Dict, Type, Tuple, Mapping
2020-08-10 13:06:02 +08:00
from typing import Union, TypeVar, Optional, Iterable, Callable, Awaitable
2020-08-07 17:05:08 +08:00
2020-08-07 17:51:57 +08:00
# import some modules needed when checking types
2020-08-07 17:05:08 +08:00
if TYPE_CHECKING:
2020-08-17 16:09:41 +08:00
from nonebot.rule import Rule as RuleClass
2020-08-10 13:06:02 +08:00
from nonebot.matcher import Matcher as MatcherClass
from nonebot.drivers import BaseDriver, BaseWebSocket
2020-08-17 16:09:41 +08:00
from nonebot.permission import Permission as PermissionClass
2020-08-10 14:50:12 +08:00
from nonebot.adapters import BaseBot, BaseEvent, BaseMessage, BaseMessageSegment
2020-06-30 10:13:58 +08:00
2020-08-07 17:51:57 +08:00
2020-08-08 23:08:01 +08:00
def overrides(InterfaceClass: object):
2020-08-07 17:51:57 +08:00
2020-08-08 23:08:01 +08:00
def overrider(func: Callable) -> Callable:
2020-08-07 17:51:57 +08:00
assert func.__name__ in dir(
InterfaceClass), f"Error method: {func.__name__}"
return func
return overrider
2020-08-10 13:06:02 +08:00
Driver = TypeVar("Driver", bound="BaseDriver")
2020-08-19 20:29:37 +08:00
"""
:类型: `BaseDriver`
:说明:
所有 Driver 的基类
"""
2020-08-10 13:06:02 +08:00
WebSocket = TypeVar("WebSocket", bound="BaseWebSocket")
2020-08-19 20:29:37 +08:00
"""
:类型: `BaseWebSocket`
:说明:
所有 WebSocket 的基类
"""
2020-08-10 13:06:02 +08:00
Bot = TypeVar("Bot", bound="BaseBot")
2020-08-19 20:29:37 +08:00
"""
:类型: `BaseBot`
:说明:
所有 Bot 的基类
"""
2020-08-10 14:50:12 +08:00
Event = TypeVar("Event", bound="BaseEvent")
2020-08-19 20:29:37 +08:00
"""
:类型: `BaseEvent`
:说明:
所有 Event 的基类
"""
2020-08-10 13:06:02 +08:00
Message = TypeVar("Message", bound="BaseMessage")
2020-08-19 20:29:37 +08:00
"""
:类型: `BaseMessage`
:说明:
所有 Message 的基类
"""
2020-08-10 13:06:02 +08:00
MessageSegment = TypeVar("MessageSegment", bound="BaseMessageSegment")
2020-08-19 20:29:37 +08:00
"""
:类型: `BaseMessageSegment`
:说明:
所有 MessageSegment 的基类
"""
2020-08-10 13:06:02 +08:00
2020-08-17 16:09:41 +08:00
PreProcessor = Callable[[Bot, Event, dict], Union[Awaitable[None],
Awaitable[NoReturn]]]
2020-08-19 20:29:37 +08:00
"""
:类型: `Callable[[Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]`
:说明:
消息预处理函数 PreProcessor 类型
"""
2020-08-10 13:06:02 +08:00
Matcher = TypeVar("Matcher", bound="MatcherClass")
2020-08-19 23:00:31 +08:00
"""
:类型: `Matcher`
:说明:
Matcher 即响应事件的处理类通过 Rule 判断是否响应事件运行 Handler
"""
2020-08-17 16:09:41 +08:00
Rule = TypeVar("Rule", bound="RuleClass")
2020-08-19 23:00:31 +08:00
"""
:类型: `Rule`
:说明:
Rule 即判断是否响应事件的处理类内部存储 RuleChecker 返回全为 True 则响应事件
"""
2020-08-17 16:09:41 +08:00
RuleChecker = Callable[[Bot, Event, dict], Awaitable[bool]]
2020-08-19 23:00:31 +08:00
"""
:类型: `Callable[[Bot, Event, dict], Awaitable[bool]]`
:说明:
RuleChecker 即判断是否响应事件的处理函数
"""
2020-08-17 16:09:41 +08:00
Permission = TypeVar("Permission", bound="PermissionClass")
2020-08-19 23:00:31 +08:00
"""
:类型: `Permission`
:说明:
Permission 即判断是否响应消息的处理类内部存储 PermissionChecker 返回只要有一个 True 则响应消息
"""
2020-08-17 16:09:41 +08:00
PermissionChecker = Callable[[Bot, Event], Awaitable[bool]]
2020-08-19 23:00:31 +08:00
"""
:类型: `Callable[[Bot, Event], Awaitable[bool]]`
:说明:
RuleChecker 即判断是否响应消息的处理函数
"""
Handler = Callable[[Bot, Event, dict], Union[Awaitable[None],
Awaitable[NoReturn]]]
"""
:类型: `Callable[[Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]`
:说明:
Handler 即事件的处理函数
"""
ArgsParser = Callable[[Bot, Event, dict], Union[Awaitable[None],
Awaitable[NoReturn]]]
2020-08-19 23:00:31 +08:00
"""
:类型: `Callable[[Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]`
:说明:
ArgsParser 即消息参数解析函数 Matcher.got 获取参数时被运行
"""