fix: 工程化收敛并移除 ASYNC230/ASYNC240 忽略#5729
Conversation
There was a problem hiding this comment.
Sorry @zouyonghe, your pull request is larger than the review limit of 150000 diff characters
|
This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on improving the engineering quality of the project by converging on coding standards, enhancing asynchronous operations, and enforcing consistent quality checks. It addresses technical debt by removing rule exceptions and upgrading dependencies, leading to a more stable and maintainable codebase. Highlights
Changelog
Ignored Files
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
这个 PR 做了一次大规模的工程质量改进,目标非常明确且完成度很高。
- Python 3.12 基线对齐:代码库全面升级到了 Python 3.12,并使用了 PEP 695 等新特性,非常棒。
- 异步代码治理:系统性地将异步函数中的阻塞式 I/O 调用(如
open,os.path.*)替换为asyncio.to_thread,彻底解决了ASYNC230/ASYNC240的 linting 问题,显著提升了异步代码的健壮性。 - 代码一致性:在多处 API 中将
timeout参数重命名为timeout_seconds,并细心地增加了向后兼容处理,这体现了很好的工程素养。 - 配置和依赖更新:相关的 CI 和 pre-commit 配置也得到了同步更新,确保了工程化链路的完整性。
整体而言,这是一次非常有价值的重构,代码质量和可维护性都得到了极大的提升。我只发现了一个小的遗漏点,详见具体评论。
Note: Security Review did not run due to the size of the PR.
astrbot/core/utils/io.py
Outdated
| def file_to_base64(file_path: str) -> str: | ||
| with open(file_path, "rb") as f: | ||
| data_bytes = f.read() | ||
| base64_str = base64.b64encode(data_bytes).decode() | ||
| data_bytes = Path(file_path).read_bytes() | ||
| base64_str = base64.b64encode(data_bytes).decode() | ||
| return "base64://" + base64_str |
There was a problem hiding this comment.
这个函数中的 Path(file_path).read_bytes() 是一个阻塞式 I/O 操作。由于此函数是从异步上下文中同步调用的(例如 Image.convert_to_base64),它会阻塞事件循环,这违反了 ASYNC230 规则。
建议将此函数修改为异步函数,并使用 asyncio.to_thread 来异步读取文件。请注意,修改后需要更新所有调用方以使用 await。
| def file_to_base64(file_path: str) -> str: | |
| with open(file_path, "rb") as f: | |
| data_bytes = f.read() | |
| base64_str = base64.b64encode(data_bytes).decode() | |
| data_bytes = Path(file_path).read_bytes() | |
| base64_str = base64.b64encode(data_bytes).decode() | |
| return "base64://" + base64_str | |
| async def file_to_base64(file_path: str) -> str: | |
| data_bytes = await asyncio.to_thread(Path(file_path).read_bytes) | |
| base64_str = base64.b64encode(data_bytes).decode() | |
| return "base64://" + base64_str |
整体描述
本 PR 以“工程化质量收敛”为目标,完成了从基线约束、CI 门禁、到代码级治理的全流程修复。核心结果是:
步骤说明
pyproject.toml中ASYNC230、ASYNC240忽略项。open()/os.path.*/Path.*调用:asyncio.to_thread+Path.read_* / write_*或线程封装函数。uv run ruff format .uv run ruff check .make pr-test-neo(pytest + smoke test)影响与收益