From 70f9325c94c059707fcdd7232303383294549868 Mon Sep 17 00:00:00 2001 From: uy/sun Date: Wed, 6 Sep 2023 18:13:19 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E4=BD=BF=E7=94=A8=20ruff=20=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81=20(#181)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/main.yml | 2 +- .pre-commit-config.yaml | 26 +++++++++----------- bot.py | 4 +-- pyproject.toml | 12 ++++++--- src/plugins/publish/__init__.py | 4 +-- src/plugins/publish/render.py | 2 +- src/plugins/publish/utils.py | 7 +++--- src/utils/plugin_test.py | 4 +-- src/utils/store_test/__init__.py | 2 +- src/utils/store_test/models.py | 5 +--- src/utils/validation/models.py | 7 ++---- tests/publish/events/issue-comment-skip.json | 2 +- tests/publish/utils/test_get_type.py | 1 - tests/publish/utils/test_re.py | 3 --- tests/utils/store_test/store/adapters.json | 2 +- tests/utils/store_test/store/bots.json | 2 +- tests/utils/store_test/store/drivers.json | 2 +- 17 files changed, 39 insertions(+), 48 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b8aff3b7..740ce07c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Python uses: he0119/setup-python@main with: - python-version: '3.11' + python-version: "3.11" - name: Install prerequisites run: poetry install diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 98e0c590..2bf5a5f7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,30 +5,28 @@ ci: autoupdate_schedule: weekly autoupdate_commit_msg: "chore: auto update by pre-commit hooks" repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.0.287 + hooks: + - id: ruff + args: [--fix, --exit-non-zero-on-fix] + stages: [commit] + - repo: https://github.com/pycqa/isort rev: 5.12.0 hooks: - id: isort + stages: [commit] - repo: https://github.com/psf/black rev: 23.7.0 hooks: - id: black + stages: [commit] - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.0.2 + rev: v3.0.3 hooks: - id: prettier - types_or: [markdown] - - - repo: https://github.com/asottile/pyupgrade - rev: v3.10.1 - hooks: - - id: pyupgrade - args: [--py310-plus] - - - repo: https://github.com/hadialqattan/pycln - rev: v2.2.2 - hooks: - - id: pycln - args: [--config, pyproject.toml] + types_or: [javascript, jsx, ts, tsx, markdown, yaml, json] + stages: [commit] diff --git a/bot.py b/bot.py index 8af1938f..fc82db84 100644 --- a/bot.py +++ b/bot.py @@ -37,7 +37,7 @@ async def handle_github_action_event(): ): bot = nonebot.get_bot() await handle_event(bot, event) - except: + except Exception: logger.exception("处理 GitHub Action 事件时出现异常") finally: # 处理一次之后就退出 @@ -51,7 +51,7 @@ def _setup(self): async def _startup(self): try: await super()._startup() - except: + except Exception: logger.exception("启动 GitHub 适配器时出现异常") driver = cast(Driver, self.driver) driver.exit(True) diff --git a/pyproject.toml b/pyproject.toml index 642e694b..0ca46440 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,14 +35,18 @@ profile = "black" line_length = 88 skip_gitignore = true +[tool.ruff] +select = ["E", "W", "F", "UP", "C", "T", "PYI", "Q"] +ignore = ["E402", "E501", "C901", "UP037", "T201"] + +[tool.pyright] +typeCheckingMode = "basic" + [tool.pytest.ini_options] asyncio_mode = "auto" [tool.coverage.report] -exclude_also = [ - "if TYPE_CHECKING:", - "raise NotImplementedError", -] +exclude_also = ["if TYPE_CHECKING:", "raise NotImplementedError"] [tool.coverage.run] omit = ["src/utils/plugin_test.py"] diff --git a/src/plugins/publish/__init__.py b/src/plugins/publish/__init__.py index 5521d315..15b78154 100644 --- a/src/plugins/publish/__init__.py +++ b/src/plugins/publish/__init__.py @@ -111,8 +111,8 @@ async def handle_pr_close( event.payload.pull_request.head.ref, ] ) - logger.info(f"已删除对应分支") - except: + logger.info("已删除对应分支") + except Exception: logger.info("对应分支不存在或已删除") if event.payload.pull_request.merged: diff --git a/src/plugins/publish/render.py b/src/plugins/publish/render.py index 1115b683..8a66ba0b 100644 --- a/src/plugins/publish/render.py +++ b/src/plugins/publish/render.py @@ -33,7 +33,7 @@ def _loc_to_name(loc: str) -> str: def loc_to_name(loc: list[str | int]) -> str: """将 loc 转换为可读名称""" - return " > ".join([_loc_to_name(str(l)) for l in loc]) + return " > ".join([_loc_to_name(str(item)) for item in loc]) env = jinja2.Environment( diff --git a/src/plugins/publish/utils.py b/src/plugins/publish/utils.py index b22d4467..a6d8f644 100644 --- a/src/plugins/publish/utils.py +++ b/src/plugins/publish/utils.py @@ -54,7 +54,6 @@ IssuesReopenedPropIssue, ) from githubkit.webhooks.models import Label as WebhookLabel - from githubkit.webhooks.models import PullRequestClosedPropPullRequest def run_shell_command(command: list[str]): @@ -127,7 +126,7 @@ def commit_and_push(result: ValidationDict, branch_name: str, issue_number: int) run_shell_command(["git", "add", "-A"]) try: run_shell_command(["git", "commit", "-m", commit_message]) - except: + except Exception: # 如果提交失败,因为是 pre-commit hooks 格式化代码导致的,所以需要再次提交 run_shell_command(["git", "add", "-A"]) run_shell_command(["git", "commit", "-m", commit_message]) @@ -139,7 +138,7 @@ def commit_and_push(result: ValidationDict, branch_name: str, issue_number: int) raise Exception else: logger.info("检测到本地分支与远程分支一致,跳过推送") - except: + except Exception: logger.info("检测到本地分支与远程分支不一致,尝试强制推送") run_shell_command(["git", "push", "origin", branch_name, "-f"]) @@ -339,7 +338,7 @@ def update_file(result: ValidationDict) -> None: json.dump(data, f, ensure_ascii=False, indent=2) # 结尾加上换行符,不然会被 pre-commit fix f.write("\n") - logger.info(f"文件更新完成") + logger.info("文件更新完成") async def should_skip_plugin_test( diff --git a/src/utils/plugin_test.py b/src/utils/plugin_test.py index 47e8894d..64ab5d24 100644 --- a/src/utils/plugin_test.py +++ b/src/utils/plugin_test.py @@ -1,10 +1,10 @@ -""" 插件加载测试 +""" 插件加载测试 测试代码修改自 ,谢谢 [Lan 佬](https://github.com/Lancercmd)。 在 GitHub Actions 中运行,通过 GitHub Event 文件获取所需信息。并将测试结果保存至 GitHub Action 的输出文件中。 -当前会输出 RESULT, OUTPUT, METADATA 三个数据,分别对应测试结果、测试输出、插件元数据。 +当前会输出 RESULT, OUTPUT, METADATA 三个数据,分别对应测试结果、测试输出、插件元数据。 经测试可以直接在 Python 3.10+ 环境下运行,无需额外依赖。 """ diff --git a/src/utils/store_test/__init__.py b/src/utils/store_test/__init__.py index b9bb9ede..cd79cce6 100644 --- a/src/utils/store_test/__init__.py +++ b/src/utils/store_test/__init__.py @@ -1,4 +1,4 @@ -""" 测试插件商店中的插件 +""" 测试插件商店中的插件 直接通过 `python -m src.utils.store_test` 运行 """ diff --git a/src/utils/store_test/models.py b/src/utils/store_test/models.py index 0cb4e37b..e5000aef 100644 --- a/src/utils/store_test/models.py +++ b/src/utils/store_test/models.py @@ -1,7 +1,4 @@ -from typing import TYPE_CHECKING, Any, Literal, TypedDict - -if TYPE_CHECKING: - from src.utils.validation.models import ErrorDict +from typing import Any, Literal, TypedDict class StorePlugin(TypedDict): diff --git a/src/utils/validation/models.py b/src/utils/validation/models.py index fc157c48..0be6b72f 100644 --- a/src/utils/validation/models.py +++ b/src/utils/validation/models.py @@ -85,11 +85,8 @@ def prevent_duplication(cls, values: dict[str, Any]) -> dict[str, Any]: module_name and project_link and any( - map( - lambda x: x["module_name"] == module_name - and x["project_link"] == project_link, - data, - ) + x["module_name"] == module_name and x["project_link"] == project_link + for x in data ) ): raise DuplicationError(project_link=project_link, module_name=module_name) diff --git a/tests/publish/events/issue-comment-skip.json b/tests/publish/events/issue-comment-skip.json index 3d769b62..bc8b0438 100644 --- a/tests/publish/events/issue-comment-skip.json +++ b/tests/publish/events/issue-comment-skip.json @@ -240,4 +240,4 @@ "type": "User", "url": "https://api.github.com/users/he0119" } -} \ No newline at end of file +} diff --git a/tests/publish/utils/test_get_type.py b/tests/publish/utils/test_get_type.py index 9fb8bc4e..a8de3712 100644 --- a/tests/publish/utils/test_get_type.py +++ b/tests/publish/utils/test_get_type.py @@ -60,7 +60,6 @@ async def test_get_type_by_title(): async def test_get_type_by_title_wrong(): from src.plugins.publish.utils import get_type_by_title - from src.utils.validation.models import PublishType title = "Something: test" publish_type = get_type_by_title(title) diff --git a/tests/publish/utils/test_re.py b/tests/publish/utils/test_re.py index 5b21e79e..deff9c7a 100644 --- a/tests/publish/utils/test_re.py +++ b/tests/publish/utils/test_re.py @@ -1,6 +1,3 @@ -from nonebug import App - - async def test_missing_info(): """测试缺失信息的情况 diff --git a/tests/utils/store_test/store/adapters.json b/tests/utils/store_test/store/adapters.json index 1129dc28..32264ee6 100644 --- a/tests/utils/store_test/store/adapters.json +++ b/tests/utils/store_test/store/adapters.json @@ -9,4 +9,4 @@ "tags": [], "is_official": true } -] \ No newline at end of file +] diff --git a/tests/utils/store_test/store/bots.json b/tests/utils/store_test/store/bots.json index dd872e6e..34b601ce 100644 --- a/tests/utils/store_test/store/bots.json +++ b/tests/utils/store_test/store/bots.json @@ -7,4 +7,4 @@ "tags": [], "is_official": false } -] \ No newline at end of file +] diff --git a/tests/utils/store_test/store/drivers.json b/tests/utils/store_test/store/drivers.json index 1dd12e6a..64337f69 100644 --- a/tests/utils/store_test/store/drivers.json +++ b/tests/utils/store_test/store/drivers.json @@ -19,4 +19,4 @@ "tags": [], "is_official": true } -] \ No newline at end of file +]