Skip to content

Commit

Permalink
Revert "refactor: 使用新的 annotations 特性 (#45)" (#47)
Browse files Browse the repository at this point in the history
This reverts commit eb855e2.
  • Loading branch information
st1020 committed Jul 19, 2023
1 parent dba281f commit 1f2d793
Show file tree
Hide file tree
Showing 33 changed files with 412 additions and 530 deletions.
37 changes: 19 additions & 18 deletions alicebot/adapter/__init__.py
Expand Up @@ -2,8 +2,6 @@
所有协议适配器都必须继承自 `Adapter` 基类。
"""
from __future__ import annotations

from abc import ABC, abstractmethod
import os
from typing import (
Expand All @@ -12,7 +10,10 @@
Awaitable,
Callable,
Generic,
Optional,
Type,
TypeVar,
Union,
final,
overload,
)
Expand Down Expand Up @@ -44,18 +45,18 @@ class Adapter(Generic[T_Event, T_Config], ABC):
"""

name: str
bot: Bot
Config: type[T_Config]
bot: "Bot"
Config: Type[T_Config]

def __init__(self, bot: Bot):
def __init__(self, bot: "Bot"):
"""初始化。
Args:
bot: 当前机器人对象。
"""
if not hasattr(self, "name"):
self.name = self.__class__.__name__
self.bot: Bot = bot
self.bot: "Bot" = bot
self.handle_event = self.bot.handle_event

@property
Expand Down Expand Up @@ -110,34 +111,34 @@ async def send(self, *args: Any, **kwargs: Any) -> Any:
@overload
async def get(
self,
func: Callable[[T_Event], bool | Awaitable[bool]] | None = None,
func: Optional[Callable[[T_Event], Union[bool, Awaitable[bool]]]] = None,
*,
event_type: None = None,
max_try_times: int | None = None,
timeout: int | float | None = None,
max_try_times: Optional[int] = None,
timeout: Optional[Union[int, float]] = None,
) -> T_Event:
...

@overload
async def get(
self,
func: Callable[[_T_Event], bool | Awaitable[bool]] | None = None,
func: Optional[Callable[[_T_Event], Union[bool, Awaitable[bool]]]] = None,
*,
event_type: type[_T_Event],
max_try_times: int | None = None,
timeout: int | float | None = None,
event_type: Type[_T_Event],
max_try_times: Optional[int] = None,
timeout: Optional[Union[int, float]] = None,
) -> _T_Event:
...

@final
async def get(
self,
func: Callable[[T_Event], bool | Awaitable[bool]] | None = None,
func: Optional[Callable[[T_Event], Union[bool, Awaitable[bool]]]] = None,
*,
event_type: type[T_Event] | type[_T_Event] | None = None,
max_try_times: int | None = None,
timeout: int | float | None = None,
) -> T_Event | _T_Event:
event_type: Optional[Union[Type[T_Event], Type[_T_Event]]] = None,
max_try_times: Optional[int] = None,
timeout: Optional[Union[int, float]] = None,
) -> Union[T_Event, _T_Event]:
"""获取满足指定条件的的事件,协程会等待直到适配器接收到满足条件的事件、超过最大事件数或超时。
类似 `Bot` 类的 `get()` 方法,但是隐含了判断产生事件的适配器是本适配器。
Expand Down
18 changes: 9 additions & 9 deletions alicebot/adapter/utils.py
Expand Up @@ -2,11 +2,9 @@
这里定义了一些在编写适配器时常用的基类,适配器开发者可以直接继承自这里的类或者用作参考。
"""
from __future__ import annotations

from abc import ABCMeta, abstractmethod
import asyncio
from typing import Literal
from typing import Literal, Optional, Union

import aiohttp
from aiohttp import web
Expand All @@ -30,7 +28,7 @@ class PollingAdapter(Adapter[T_Event, T_Config], metaclass=ABCMeta):

delay: float = 0.1
create_task: bool = False
_on_tick_task: asyncio.Task[None] | None = None
_on_tick_task: Optional["asyncio.Task[None]"] = None

async def run(self):
"""运行适配器。"""
Expand Down Expand Up @@ -179,15 +177,17 @@ class WebSocketAdapter(Adapter[T_Event, T_Config], metaclass=ABCMeta):
同时支持 WebSocket 客户端和服务端。
"""

websocket: web.WebSocketResponse | aiohttp.ClientWebSocketResponse | None = None
websocket: Union[
web.WebSocketResponse, aiohttp.ClientWebSocketResponse, None
] = None

# ws
session: aiohttp.ClientSession | None
session: Optional[aiohttp.ClientSession]

# reverse-ws
app: web.Application | None
runner: web.AppRunner | None
site: web.TCPSite | None
app: Optional[web.Application]
runner: Optional[web.AppRunner]
site: Optional[web.TCPSite]

# config
adapter_type: Literal["ws", "reverse-ws"]
Expand Down

0 comments on commit 1f2d793

Please sign in to comment.