Skip to content

Commit

Permalink
chore: 使用 Ruff 替代 Black 作为格式化工具
Browse files Browse the repository at this point in the history
  • Loading branch information
st1020 committed Dec 10, 2023
1 parent 5c05641 commit 1a355a9
Show file tree
Hide file tree
Showing 11 changed files with 952 additions and 693 deletions.
20 changes: 13 additions & 7 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ on:
workflow_dispatch:

jobs:
black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: psf/black@stable

ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1

- uses: pdm-project/setup-pdm@v3
name: Setup PDM
with:
python-version: 3.9
cache: true
- name: Install dependencies
run: pdm install -G dev -G lint

- run: |
eval $(pdm venv activate)
ruff check --output-format=github .
ruff format --check .
pylint:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
cancel-in-progress: true
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: false
env:
Expand Down
20 changes: 0 additions & 20 deletions .pre-commit-config.yaml

This file was deleted.

16 changes: 7 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

### 安装依赖

AliceBot 使用 [PDM](https://pdm.fming.dev/) 管理项目的依赖,并使用 [pre-commit](https://pre-commit.com/) 在提交前执行代码审查
AliceBot 使用 [PDM](https://pdm.fming.dev/) 管理项目的依赖。

因此,你需要先根据 [PDM](https://pdm.fming.dev/latest/#installation) 的指引安装 PDM,之后在项目目录中执行以下命令:
因此,你需要先根据 [PDM](https://pdm-project.org/latest/#installation) 的指引安装 PDM,之后在项目目录中执行以下命令:

```shell
pdm install
pre-commit install
```

AliceBot 项目的文档使用 [VitePress](https://vitepress.dev/) 生成,如果你想要贡献 AliceBot 的文档,请额外安装 [pnpm](https://pnpm.io/) 环境,并在项目目录中执行以下命令:
Expand All @@ -27,16 +26,15 @@ pnpm install

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

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

```json
{
"[python]": {
"editor.formatOnPaste": false,
"editor.formatOnSaveMode": "file"
},
"python.analysis.diagnosticMode": "workspace",
"python.formatting.provider": "black"
"editor.formatOnSaveMode": "file",
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
```

Expand All @@ -50,7 +48,7 @@ pnpm install

AliceBot 的代码风格遵循 [PEP 8](https://www.python.org/dev/peps/pep-0008/) 规范。

AliceBot 使用 [Black](https://github.com/psf/black) 作为格式化工具,并使用 [isort](https://github.com/PyCQA/isort) 确保导入顺序,如果你已经按照上文配置了 VSCodo,那么你应该很容易遵循此规范,否则请在提交前手动执行上述格式化工具。
AliceBot 使用 [Ruff](https://docs.astral.sh/ruff/) 作为格式化工具,如果你已经按照上文配置了 VSCode,那么你应该很容易遵循此规范,否则请在提交前手动执行上述格式化工具。

### 类型注解

Expand Down
17 changes: 13 additions & 4 deletions alicebot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
overload,
)

from pydantic import ValidationError, create_model
from pydantic import (
ValidationError,
create_model, # pyright: ignore[reportUnknownVariableType]
)

from alicebot.adapter import Adapter
from alicebot.config import AdapterConfig, ConfigModel, MainConfig, PluginConfig
Expand Down Expand Up @@ -91,8 +94,12 @@ class Bot:
_restart_flag: bool # 重新启动标志
_module_path_finder: ModulePathFinder # 用于查找 plugins 的模块元路径查找器
_raw_config_dict: Dict[str, Any] # 原始配置字典
_adapter_tasks: Set["asyncio.Task[None]"] # 适配器任务集合,用于保持对适配器任务的引用
_handle_event_tasks: Set["asyncio.Task[None]"] # 事件处理任务,用于保持对适配器任务的引用
_adapter_tasks: Set[
"asyncio.Task[None]"
] # 适配器任务集合,用于保持对适配器任务的引用
_handle_event_tasks: Set[
"asyncio.Task[None]"
] # 事件处理任务,用于保持对适配器任务的引用

# 以下属性不会在重启时清除
_config_file: Optional[str] # 配置文件
Expand All @@ -102,7 +109,9 @@ class Bot:
_extend_plugins: List[
Union[Type[Plugin[Any, Any, Any]], str, Path]
] # 使用 load_plugins() 方法程序化加载的插件列表
_extend_plugin_dirs: List[Path] # 使用 load_plugins_from_dirs() 方法程序化加载的插件路径列表
_extend_plugin_dirs: List[
Path
] # 使用 load_plugins_from_dirs() 方法程序化加载的插件路径列表
_extend_adapters: List[
Union[Type[Adapter[Any, Any]], str]
] # 使用 load_adapter() 方法程序化加载的适配器列表
Expand Down
2 changes: 1 addition & 1 deletion alicebot/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def __eq__(self, other: object) -> bool:
是否相等。
"""
return (
type(other) is self.__class__ # pylint: disable=unidiomatic-typecheck
isinstance(other, self.__class__)
and self.type == other.type
and self.data == other.data
)
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"homepage": "https://docs.alicebot.dev/",
"license": "MIT",
"dependencies": {
"vitepress": "1.0.0-rc.24",
"vue": "^3.3.7"
"vitepress": "1.0.0-rc.31",
"vue": "^3.3.11"
},
"devDependencies": {
"@types/node": "^20.8.9",
"@types/node": "^20.10.4",
"conventional-changelog-cli": "^3.0.0",
"markdownlint-cli2": "^0.8.1",
"prettier": "^3.0.3",
"pyright": "^1.1.333",
"prettier": "^3.1.1",
"pyright": "^1.1.339",
"zhlint": "^0.7.1"
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""通知事件。"""
# pyright: reportIncompatibleVariableOverride=false

from typing import Literal, Optional
from typing import Any, Dict, Literal, Optional

from pydantic import create_model
from pydantic import create_model # pyright: ignore[reportUnknownVariableType]

from .base import (
FriendInfo,
Expand Down Expand Up @@ -44,14 +44,15 @@ class FriendInputStatusChangedEvent(FriendEvent):


# 因为 from 是 python 关键字,所以不能直接定义
_temp_dict: Dict[str, Any] = {
"from": (str, ...),
"to": (str, ...),
}
FriendNickChangedEvent = create_model(
"FriendNickChangedEvent",
type=(Literal["FriendNickChangedEvent"], ...),
friend=(FriendInfo, ...),
**{
"from": (str, ...),
"to": (str, ...),
},
**_temp_dict,
__config__=None,
__base__=FriendEvent,
__module__=__name__,
Expand Down
Loading

0 comments on commit 1a355a9

Please sign in to comment.