Skip to content

Commit

Permalink
style: 使用 ruff 格式化代码 (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 authored Sep 6, 2023
1 parent 943b9e9 commit 70f9325
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 12 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
4 changes: 2 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
# 处理一次之后就退出
Expand All @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/publish/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions src/plugins/publish/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down Expand Up @@ -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])
Expand All @@ -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"])

Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/plugin_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
""" 插件加载测试
""" 插件加载测试
测试代码修改自 <https://github.com/Lancercmd/nonebot2-store-test>,谢谢 [Lan 佬](https://github.com/Lancercmd)。
在 GitHub Actions 中运行,通过 GitHub Event 文件获取所需信息。并将测试结果保存至 GitHub Action 的输出文件中。
当前会输出 RESULT, OUTPUT, METADATA 三个数据,分别对应测试结果、测试输出、插件元数据。
当前会输出 RESULT, OUTPUT, METADATA 三个数据,分别对应测试结果、测试输出、插件元数据。
经测试可以直接在 Python 3.10+ 环境下运行,无需额外依赖。
"""
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` 运行
"""
5 changes: 1 addition & 4 deletions src/utils/store_test/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
7 changes: 2 additions & 5 deletions src/utils/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/publish/events/issue-comment-skip.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,4 @@
"type": "User",
"url": "https://api.github.com/users/he0119"
}
}
}
1 change: 0 additions & 1 deletion tests/publish/utils/test_get_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions tests/publish/utils/test_re.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from nonebug import App


async def test_missing_info():
"""测试缺失信息的情况
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/store_test/store/adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"tags": [],
"is_official": true
}
]
]
2 changes: 1 addition & 1 deletion tests/utils/store_test/store/bots.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"tags": [],
"is_official": false
}
]
]
2 changes: 1 addition & 1 deletion tests/utils/store_test/store/drivers.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"tags": [],
"is_official": true
}
]
]

0 comments on commit 70f9325

Please sign in to comment.