Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ jobs:
- uses: actions/checkout@v4
- name: Setup Python
uses: ./.github/actions/setup-python
- name: Setup Node
uses: ./.github/actions/setup-node
- run: uv sync --all-extras --all-packages --all-groups
- run: |
source .venv/bin/activate
pnpm run pyright
- run: uv run basedpyright

mypy:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion alicebot/adapter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, bot: "Bot") -> None:
"""
if not hasattr(self, "name"):
self.name = self.__class__.__name__
self.bot: Bot = bot
self.bot = bot
self.handle_event = self.bot.handle_event

@property
Expand Down
6 changes: 3 additions & 3 deletions alicebot/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
事件类的基类。适配器开发者应实现此事件类基类的子类。
"""

from abc import ABC, abstractmethod
from abc import ABCMeta, abstractmethod
from typing import TYPE_CHECKING, Any, Generic, NamedTuple, Optional, Union
from typing_extensions import Self, override

Expand All @@ -14,7 +14,7 @@
__all__ = ["Event", "EventHandleOption", "MessageEvent"]


class Event(ABC, BaseModel, Generic[AdapterT]):
class Event(BaseModel, Generic[AdapterT], metaclass=ABCMeta):
"""事件类的基类。

Attributes:
Expand Down Expand Up @@ -51,7 +51,7 @@ class EventHandleOption(NamedTuple):
handle_get: bool


class MessageEvent(Event[AdapterT], Generic[AdapterT]):
class MessageEvent(Event[AdapterT], Generic[AdapterT], metaclass=ABCMeta):
"""通用的消息事件类的基类。"""

@abstractmethod
Expand Down
17 changes: 5 additions & 12 deletions alicebot/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@
适配器开发者可以根据需要实现此模块中消息类的子类或定义与此不同的消息类型,但建议若可行的话应尽量使用此模块中消息类的子类。
"""

import builtins
from abc import ABC, abstractmethod
from collections.abc import ItemsView, Iterator, KeysView, Mapping, ValuesView
from typing import ( # noqa: UP035
Any,
Generic,
Optional,
SupportsIndex,
Type,
TypeVar,
Union,
overload,
)
from typing import Any, Generic, Optional, SupportsIndex, TypeVar, Union, overload
from typing_extensions import Self, override

from pydantic import BaseModel, Field, GetCoreSchemaHandler
Expand Down Expand Up @@ -51,6 +43,7 @@ def __init__(self, *messages: BuildMessageType[MessageSegmentT]) -> None:
Args:
*messages: 可以被转化为消息的数据。
"""
super().__init__()
segment_class = self.get_segment_class()
for message in messages:
if isinstance(message, list):
Expand Down Expand Up @@ -315,7 +308,7 @@ def _replace_str(self, old: str, new: str, count: int = -1) -> Self:
return temp_msg


class MessageSegment(ABC, BaseModel, Mapping[str, Any], Generic[MessageT]):
class MessageSegment(ABC, BaseModel, Mapping[str, Any], Generic[MessageT]): # pyright: ignore[reportUnsafeMultipleInheritance]
"""消息字段。

本类实现了所有 `Mapping` 类型的方法,这些方法全部是对 `data` 属性的操作。
Expand All @@ -332,7 +325,7 @@ class MessageSegment(ABC, BaseModel, Mapping[str, Any], Generic[MessageT]):

@classmethod
@abstractmethod
def get_message_class(cls) -> Type[MessageT]: # noqa: UP006
def get_message_class(cls) -> builtins.type[MessageT]:
"""获取消息类。

Returns:
Expand Down
6 changes: 3 additions & 3 deletions docs/develop/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pnpm install

### 编辑器配置

虽然并非强制,但建议使用 VSCode 作为编辑器对 AliceBot 项目的代码进行编辑,因为 AliceBot 具有完全的类型注解,VSCode 的 Pylance 插件具有相对较好的静态类型检查效果。
虽然并非强制,但建议使用 VSCode 作为编辑器对 AliceBot 项目的代码进行编辑,因为 AliceBot 具有完全的类型注解,VSCode 的 BasedPyright 插件具有相对较好的静态类型检查效果。

如果你使用 VSCode 作为编辑器,需要安装 **Python**、**Pylance** 和 **Ruff** 插件,并进行以下配置:
如果你使用 VSCode 作为编辑器,需要安装 **Python**、**BasedPyright** 和 **Ruff** 插件,并进行以下配置:

```json
{
Expand Down Expand Up @@ -58,7 +58,7 @@ AliceBot 使用 [Ruff](https://docs.astral.sh/ruff/) 作为格式化工具,如

AliceBot 具有完全的类型注解。

在 [pyproject.toml](https://github.com/AliceBotProject/alicebot/blob/main/pyproject.toml) 文件中已经提供了针对 Pyright (Pylance 背后的类型检查器) 和 MyPy 的配置,请确保你的代码能够通过这种严格程度的类型检查。
在 [pyproject.toml](https://github.com/AliceBotProject/alicebot/blob/main/pyproject.toml) 文件中已经提供了针对 BasedPyright 和 MyPy 的配置,请确保你的代码能够通过这种严格程度的类型检查。

如果必要,你可以在代码中使用 `# type: ignore` 注释来抑制类型检查,但请注意,请将此作为最后手段,不要轻易使用。

Expand Down
18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,29 @@
"docs:serve": "vitepress serve docs",
"changelog": "conventional-changelog -p angular -i docs/changelog.md -s -r 0",
"markdownlint": "markdownlint-cli2 '{*.md,docs/guide/**/*.md,packages/**/*.md}'",
"pyright": "pyright",
"prettier": "prettier -c .",
"prettier:fix": "prettier -w .",
"eslint": "eslint .",
"eslint:fix": "eslint . --fix"
},
"dependencies": {
"@iconify-json/mdi": "^1.2.3",
"unocss": "^66.1.2",
"unocss": "^66.1.3",
"vitepress": "^1.6.3",
"vue": "^3.5.14"
"vue": "^3.5.16"
},
"devDependencies": {
"@eslint/js": "^9.27.0",
"@types/node": "^22.15.21",
"@unocss/eslint-config": "^66.1.2",
"@eslint/js": "^9.28.0",
"@types/node": "^22.15.29",
"@unocss/eslint-config": "^66.1.3",
"conventional-changelog-cli": "^5.0.0",
"eslint": "^9.27.0",
"eslint": "^9.28.0",
"eslint-config-prettier": "^10.1.5",
"eslint-plugin-vue": "^10.1.0",
"globals": "^16.1.0",
"globals": "^16.2.0",
"markdownlint-cli2": "^0.18.1",
"prettier": "^3.5.3",
"pyright": "1.1.400",
"typescript-eslint": "^8.32.1",
"typescript-eslint": "^8.33.0",
"vue-eslint-parser": "^10.1.3"
},
"pnpm": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""APScheduler 适配器事件。"""

# pyright: reportMissingTypeStubs = false
from typing import TYPE_CHECKING, Any, Optional, Type, Union # noqa: UP035
from typing import TYPE_CHECKING, Any, Optional, Union

from apscheduler.job import Job
from apscheduler.triggers.base import BaseTrigger

from alicebot.event import Event

if TYPE_CHECKING:
import builtins

from alicebot.plugin import Plugin

from . import APSchedulerAdapter
Expand All @@ -22,7 +24,7 @@ class APSchedulerEvent(Event["APSchedulerAdapter"]):

type: Optional[str] = "apscheduler"
if TYPE_CHECKING:
plugin_class: Type[Plugin[Any, Any, Any]] # noqa: UP006
plugin_class: "builtins.type[Plugin[Any, Any, Any]]"
else:
plugin_class: Any

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, resp: dict[str, Any]) -> None:
Args:
resp: 返回的响应。
"""
super().__init__()
self.resp = resp


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""消息事件。"""
# pyright: reportIncompatibleVariableOverride=false

from abc import ABC
from typing import TYPE_CHECKING, Any, Literal, Union
from typing_extensions import override

Expand All @@ -14,7 +15,7 @@
from .. import MiraiAdapter # noqa: TID252


class MiraiBaseMessageEvent(MiraiEvent, BaseMessageEvent["MiraiAdapter"]):
class MiraiBaseMessageEvent(ABC, MiraiEvent, BaseMessageEvent["MiraiAdapter"]):
"""Mirai 消息事件基类"""

messageChain: MiraiMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, code: int, resp: dict[str, Any]) -> None:
code: 错误代码。
resp: 返回的响应。
"""
super().__init__()
self.code = code
self.resp = resp

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, resp: Any) -> None:
Args:
resp: 返回的响应。
"""
super().__init__()
self.resp = resp


Expand Down
Loading
Loading