Skip to content

Commit

Permalink
style: 启用 ruff 的 RUF 与 ASYNC 规则 (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 authored Mar 8, 2024
1 parent 07b6b1d commit 737abec
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ci:
autoupdate_commit_msg: "chore: auto update by pre-commit hooks"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.15
rev: v0.3.1
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
13 changes: 8 additions & 5 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,28 @@ async def handle_github_action_event():
await handle_event(bot, event)
except Exception:
logger.exception("处理 GitHub Action 事件时出现异常")
finally:
# 处理一次之后就退出
driver.exit(True)


handle_event_task = None


class Adapter(GITHUBAdapter):
def _setup(self):
self.driver.on_startup(self._startup)

async def _startup(self):
driver = cast(Driver, self.driver)
try:
await super()._startup()
except Exception:
logger.exception("启动 GitHub 适配器时出现异常")
driver = cast(Driver, self.driver)
driver.exit(True)
return

# 完成启动后创建任务处理 GitHub Action 事件
asyncio.create_task(handle_github_action_event())
handle_event_task = asyncio.create_task(handle_github_action_event())
# 处理完成之后就退出
handle_event_task.add_done_callback(lambda _: driver.exit(True))

@classmethod
def payload_to_event(
Expand Down
28 changes: 26 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,32 @@ pytest-xdist = "^3.3.1"
respx = "^0.20.1"

[tool.ruff]
select = ["E", "W", "F", "UP", "C", "T", "PYI", "Q"]
ignore = ["E402", "E501", "C901", "UP037"]
line-length = 88
target-version = "py310"

[tool.ruff.lint]
select = [
"F", # Pyflakes
"W", # pycodestyle warnings
"E", # pycodestyle errors
"UP", # pyupgrade
"ASYNC", # flake8-async
"C4", # flake8-comprehensions
"T10", # flake8-debugger
"T20", # flake8-print
"PYI", # flake8-pyi
"PT", # flake8-pytest-style
"Q", # flake8-quotes
"RUF", # Ruff-specific rules
]
ignore = [
"E402", # module-import-not-at-top-of-file
"E501", # line-too-long
"UP037", # quoted-annotation
"RUF001", # ambiguous-unicode-character-string
"RUF002", # ambiguous-unicode-character-docstring
"RUF003", # ambiguous-unicode-character-comment
]

[tool.pyright]
typeCheckingMode = "basic"
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/publish/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_labels(
| IssueCommentCreated,
):
"""获取议题或拉取请求的标签"""
if isinstance(event, (PullRequestClosed, PullRequestReviewSubmitted)):
if isinstance(event, PullRequestClosed | PullRequestReviewSubmitted):
labels = event.payload.pull_request.labels
else:
labels = event.payload.issue.labels
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/publish/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ async def render_comment(result: "ValidationDict", reuse: bool = False) -> str:
if result["type"] == PublishType.PLUGIN:
# https://github.com/he0119/action-test/actions/runs/4469672520
if plugin_config.plugin_test_result or plugin_config.skip_plugin_test:
result["data"][
"action_url"
] = f"https://github.com/{plugin_config.github_repository}/actions/runs/{plugin_config.github_run_id}"
result["data"]["action_url"] = (
f"https://github.com/{plugin_config.github_repository}/actions/runs/{plugin_config.github_run_id}"
)

template = env.get_template("comment.md.jinja")
return await template.render_async(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/plugin_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" 插件加载测试
"""插件加载测试
测试代码修改自 <https://github.com/Lancercmd/nonebot2-store-test>,谢谢 [Lan 佬](https://github.com/Lancercmd)。
Expand All @@ -8,7 +8,7 @@
经测试可以直接在 Python 3.10+ 环境下运行,无需额外依赖。
"""
# ruff: noqa: T201
# ruff: noqa: T201, ASYNC101

import json
import os
Expand Down
2 changes: 1 addition & 1 deletion src/utils/store_test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" 测试插件商店中的插件
"""测试插件商店中的插件
直接通过 `python -m src.utils.store_test` 运行
"""
3 changes: 2 additions & 1 deletion src/utils/store_test/validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" 测试并验证插件 """
"""测试并验证插件"""

import json
import os
import re
Expand Down
2 changes: 1 addition & 1 deletion src/utils/validation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" 验证数据是否符合规范 """
"""验证数据是否符合规范"""

from typing import Any, cast

Expand Down
2 changes: 1 addition & 1 deletion src/utils/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def supported_adapters_validator(
if v is None:
return None

if not isinstance(v, (list, set)):
if not isinstance(v, list | set):
raise PydanticCustomError("set_type", "值应该是一个集合")

supported_adapters = {resolve_adapter_name(x) for x in v}
Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ async def app(app: App, tmp_path: Path, mocker: MockerFixture):
get_pypi_data.cache_clear()


@pytest.fixture(autouse=True, scope="function")
def clear_cache(app: App):
@pytest.fixture(autouse=True)
def _clear_cache(app: App):
"""每次运行前都清除 cache"""
from src.utils.validation.utils import check_url

check_url.cache_clear()


@pytest.fixture
@pytest.fixture()
def mocked_api(respx_mock: MockRouter):
respx_mock.get("exception", name="exception").mock(side_effect=httpx.ConnectError)
respx_mock.get(
Expand Down Expand Up @@ -203,4 +203,4 @@ def mocked_api(respx_mock: MockRouter):
},
],
)
yield respx_mock
return respx_mock
2 changes: 2 additions & 0 deletions tests/publish/process/test_publish_check.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ruff: noqa: ASYNC101

import json
from pathlib import Path
from typing import Any, cast
Expand Down
1 change: 1 addition & 0 deletions tests/publish/utils/test_resolve_conflict_pull_requests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: ASYNC101
import json
from pathlib import Path
from typing import Any, cast
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/store_test/test_store_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def load_json(name: str) -> dict:
return json.load(f)


@pytest.fixture
@pytest.fixture()
def mocked_store_data(
tmp_path: Path,
mocker: MockerFixture,
Expand Down
8 changes: 2 additions & 6 deletions tests/utils/store_test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ async def test_load_json_failed(mocked_api: MockRouter):

mocked_api.get(STORE_ADAPTERS_URL).respond(404)

with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="下载文件失败:"):
load_json(STORE_ADAPTERS_URL)

assert str(e.value) == "下载文件失败:"


async def test_get_pypi_data_failed(mocked_api: MockRouter):
"""获取 PyPI 数据失败"""
from src.utils.store_test.utils import get_pypi_data

with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="获取 PyPI 数据失败:"):
get_pypi_data("project_link_failed")

assert str(e.value) == "获取 PyPI 数据失败:"

0 comments on commit 737abec

Please sign in to comment.