Skip to content

Commit

Permalink
feat(event): 为 Event 类添加泛型支持
Browse files Browse the repository at this point in the history
  • Loading branch information
st1020 committed Apr 23, 2022
1 parent fd59e94 commit 8fb3601
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion alicebot/adapter/http_server_test_adapter.py
Expand Up @@ -14,7 +14,7 @@
from alicebot.message import T_Message, T_MessageSegment


class HttpServerTestEvent(Event):
class HttpServerTestEvent(Event['HttpServerTestAdapter']):
"""HTTP 服务端示例适配器事件类。"""
message: Message

Expand Down
8 changes: 5 additions & 3 deletions alicebot/event/__init__.py
Expand Up @@ -3,17 +3,19 @@
事件类的基类。适配器开发者应实现此事件类基类的子类。
"""
from abc import ABC
from typing import Any, Optional
from typing import Any, Generic, Optional, Union

from pydantic import BaseModel

from alicebot.message import Message
from alicebot.typing import T_Adapter
from alicebot.utils import DataclassEncoder

__all__ = ['Event']


class Event(ABC, BaseModel):
# 这里不能用 pydantic.generics.GenericModel,否则容易在编写适配器陷入循环引用,对适配器的编写造成困难
class Event(ABC, BaseModel, Generic[T_Adapter]):
"""事件类的基类。
Attributes:
Expand All @@ -22,7 +24,7 @@ class Event(ABC, BaseModel):
__handled__: 表示事件是否被处理过了,用于适配器处理。
警告:请勿手动更改此属性的值。
"""
adapter: Any
adapter: Union[Any, T_Adapter] # Union[Any, ...] 等效与 Any,防止 pydantic 进行类型检验,但使静态类型检查工具可以识别到
type: Optional[str]
__handled__: bool = False

Expand Down

0 comments on commit 8fb3601

Please sign in to comment.