mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-28 08:41:29 +00:00
🎨 update typing support
This commit is contained in:
@ -8,12 +8,14 @@
|
||||
import abc
|
||||
from functools import reduce, partial
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, Union, TypeVar, Optional, Callable, Iterable, Awaitable, Generic, TYPE_CHECKING
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from nonebot.config import Config
|
||||
from nonebot.typing import Driver, Message, WebSocket
|
||||
from nonebot.typing import Any, Dict, Union, Optional, Callable, Iterable, Awaitable, TypeVar, Generic
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nonebot.drivers import BaseDriver as Driver, BaseWebSocket as WebSocket
|
||||
|
||||
|
||||
class BaseBot(abc.ABC):
|
||||
@ -23,12 +25,12 @@ class BaseBot(abc.ABC):
|
||||
|
||||
@abc.abstractmethod
|
||||
def __init__(self,
|
||||
driver: Driver,
|
||||
driver: "Driver",
|
||||
connection_type: str,
|
||||
config: Config,
|
||||
self_id: str,
|
||||
*,
|
||||
websocket: Optional[WebSocket] = None):
|
||||
websocket: Optional["WebSocket"] = None):
|
||||
"""
|
||||
:参数:
|
||||
|
||||
@ -60,7 +62,7 @@ class BaseBot(abc.ABC):
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
async def check_permission(cls, driver: Driver, connection_type: str,
|
||||
async def check_permission(cls, driver: "Driver", connection_type: str,
|
||||
headers: dict, body: Optional[dict]) -> str:
|
||||
"""
|
||||
:说明:
|
||||
@ -252,7 +254,7 @@ class BaseEvent(abc.ABC, Generic[T]):
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def message(self) -> Optional[Message]:
|
||||
def message(self) -> Optional["BaseMessage"]:
|
||||
"""消息内容"""
|
||||
raise NotImplementedError
|
||||
|
||||
@ -373,7 +375,9 @@ class BaseMessage(list, abc.ABC):
|
||||
|
||||
@staticmethod
|
||||
@abc.abstractmethod
|
||||
def _construct(msg: Union[str, dict, list]) -> Iterable[BaseMessageSegment]:
|
||||
def _construct(
|
||||
msg: Union[str, dict, list,
|
||||
BaseModel]) -> Iterable[BaseMessageSegment]:
|
||||
raise NotImplementedError
|
||||
|
||||
def __add__(
|
||||
|
@ -3,22 +3,25 @@ import sys
|
||||
import hmac
|
||||
import json
|
||||
import asyncio
|
||||
from typing import Any, Dict, Union, Optional, TYPE_CHECKING
|
||||
|
||||
import httpx
|
||||
|
||||
from nonebot.log import logger
|
||||
from nonebot.config import Config
|
||||
from nonebot.typing import overrides
|
||||
from nonebot.adapters import BaseBot
|
||||
from nonebot.message import handle_event
|
||||
from nonebot.exception import RequestDenied
|
||||
from nonebot.typing import Any, Dict, Union, Optional
|
||||
from nonebot.typing import overrides, Driver, WebSocket
|
||||
|
||||
from .event import Event
|
||||
from .message import Message, MessageSegment
|
||||
from .exception import NetworkError, ApiNotAvailable, ActionFailed
|
||||
from .utils import log
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nonebot.drivers import BaseDriver as Driver, BaseWebSocket as WebSocket
|
||||
|
||||
|
||||
def get_auth_bearer(access_token: Optional[str] = None) -> Optional[str]:
|
||||
if not access_token:
|
||||
@ -212,12 +215,12 @@ class Bot(BaseBot):
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
driver: Driver,
|
||||
driver: "Driver",
|
||||
connection_type: str,
|
||||
config: Config,
|
||||
self_id: str,
|
||||
*,
|
||||
websocket: Optional[WebSocket] = None):
|
||||
websocket: Optional["WebSocket"] = None):
|
||||
|
||||
super().__init__(driver,
|
||||
connection_type,
|
||||
@ -235,7 +238,7 @@ class Bot(BaseBot):
|
||||
|
||||
@classmethod
|
||||
@overrides(BaseBot)
|
||||
async def check_permission(cls, driver: Driver, connection_type: str,
|
||||
async def check_permission(cls, driver: "Driver", connection_type: str,
|
||||
headers: dict, body: Optional[dict]) -> str:
|
||||
"""
|
||||
:说明:
|
||||
|
@ -1,8 +1,9 @@
|
||||
import asyncio
|
||||
from typing import Any, Dict, List, Union, Optional
|
||||
|
||||
from nonebot.config import Config
|
||||
from nonebot.adapters import BaseBot
|
||||
from nonebot.typing import Any, Dict, List, Union, Driver, Optional, WebSocket
|
||||
from nonebot.drivers import BaseDriver as Driver, BaseWebSocket as WebSocket
|
||||
|
||||
from .event import Event
|
||||
from .message import Message, MessageSegment
|
||||
|
@ -1,5 +1,7 @@
|
||||
from typing import Optional
|
||||
|
||||
from nonebot.typing import overrides
|
||||
from nonebot.adapters import BaseEvent
|
||||
from nonebot.typing import Optional, overrides
|
||||
|
||||
from .message import Message
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
from nonebot.typing import Optional
|
||||
from typing import Optional
|
||||
|
||||
from nonebot.exception import (AdapterException, ActionFailed as
|
||||
BaseActionFailed, NetworkError as
|
||||
BaseNetworkError, ApiNotAvailable as
|
||||
|
@ -1,6 +1,7 @@
|
||||
import re
|
||||
from typing import Any, Dict, Union, Tuple, Iterable, Optional
|
||||
|
||||
from nonebot.typing import Any, Dict, Union, Tuple, Iterable, Optional, overrides
|
||||
from nonebot.typing import overrides
|
||||
from nonebot.adapters import BaseMessage, BaseMessageSegment
|
||||
|
||||
from .utils import log, escape, unescape, _b2s
|
||||
|
@ -1,4 +1,5 @@
|
||||
from nonebot.typing import Optional
|
||||
from typing import Optional
|
||||
|
||||
from nonebot.utils import logger_wrapper
|
||||
|
||||
log = logger_wrapper("CQHTTP")
|
||||
|
@ -1,6 +1,7 @@
|
||||
import hmac
|
||||
import base64
|
||||
from datetime import datetime
|
||||
from typing import Any, Union, Optional, TYPE_CHECKING
|
||||
|
||||
import httpx
|
||||
from nonebot.log import logger
|
||||
@ -8,7 +9,6 @@ from nonebot.config import Config
|
||||
from nonebot.adapters import BaseBot
|
||||
from nonebot.message import handle_event
|
||||
from nonebot.exception import RequestDenied
|
||||
from nonebot.typing import Any, Union, Driver, Optional
|
||||
|
||||
from .utils import log
|
||||
from .event import Event
|
||||
@ -16,13 +16,16 @@ from .model import MessageModel
|
||||
from .message import Message, MessageSegment
|
||||
from .exception import NetworkError, ApiNotAvailable, ActionFailed, SessionExpired
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nonebot.drivers import BaseDriver as Driver
|
||||
|
||||
|
||||
class Bot(BaseBot):
|
||||
"""
|
||||
钉钉 协议 Bot 适配。继承属性参考 `BaseBot <./#class-basebot>`_ 。
|
||||
"""
|
||||
|
||||
def __init__(self, driver: Driver, connection_type: str, config: Config,
|
||||
def __init__(self, driver: "Driver", connection_type: str, config: Config,
|
||||
self_id: str, **kwargs):
|
||||
|
||||
super().__init__(driver, connection_type, config, self_id, **kwargs)
|
||||
@ -35,7 +38,7 @@ class Bot(BaseBot):
|
||||
return "ding"
|
||||
|
||||
@classmethod
|
||||
async def check_permission(cls, driver: Driver, connection_type: str,
|
||||
async def check_permission(cls, driver: "Driver", connection_type: str,
|
||||
headers: dict, body: Optional[dict]) -> str:
|
||||
"""
|
||||
:说明:
|
||||
|
@ -1,5 +1,6 @@
|
||||
from typing import Union, Optional
|
||||
|
||||
from nonebot.adapters import BaseEvent
|
||||
from nonebot.typing import Union, Optional
|
||||
|
||||
from .message import Message
|
||||
from .model import MessageModel, ConversationType, TextMessage
|
||||
|
@ -1,4 +1,5 @@
|
||||
from nonebot.typing import Optional
|
||||
from typing import Optional
|
||||
|
||||
from nonebot.exception import (AdapterException, ActionFailed as
|
||||
BaseActionFailed, ApiNotAvailable as
|
||||
BaseApiNotAvailable, NetworkError as
|
||||
|
@ -1,5 +1,7 @@
|
||||
from nonebot.typing import Any, Dict, Union, Iterable
|
||||
from typing import Any, Dict, Union, Iterable
|
||||
|
||||
from nonebot.adapters import BaseMessage, BaseMessageSegment
|
||||
|
||||
from .utils import log
|
||||
from .model import TextMessage
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
from typing import List, Optional
|
||||
from enum import Enum
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user