style: 引入 ruff,474 项告警清零
配置见 pyproject.toml:
- line-length=100:贴着现有代码定。若用默认的 88 会重排 122 行,
噪音淹没真正有价值的修复。
- 启用 E/W/F/I/UP/B/C4/SIM/RET/PERF/RUF;**不**启用 PTH。因为本项目的路径
多是"字符串身份"——要塞进 subprocess 命令行、写进 JSON 存档、打印给用户,
全换 pathlib 只是包一层再拆一层,换来 70 处纯风格改动。
- 忽略 RUF001/002/003:中文全角标点是正确写法,不是"歧义字符"。
- 忽略 PERF203:剩下两处都是"逐项容错"语义(逐个牧场存档要立刻上报失败、
收获全部要跳过失败地块)。把 try 提到循环外会毁掉"一项失败不影响其它项"
这个行为,是错的优化。
自动修复 358 项:Dict/List/Optional 换成 PEP 585/604 写法、导入排序、
失效的 noqa、空占位 f-string 等。
手工修复的(这些是真问题,不是风格偏好):
- B904 x9:except 中 raise 补异常链。刻意区分——网络/IO 失败用 from exc 保留
根因;参数解析失败用 from None,因为底层 ValueError 对玩家只是噪音。
- RUF012 x2:StateView 的 plots/animals 原本是**类级共享可变列表**,
改成在 __init__ 里初始化(apply({}) 会提前返回,所以必须显式摆好)。
- F841:TUI 里一个赋值后再没用过的死变量。
- SIM105 x11:try/except/pass 统一改成 contextlib.suppress。
- SIM115:日志句柄改用 with。Popen 已把 fd 复制给子进程,父进程可安全关闭。
- E501 x7:拆超长行;main.py 那处顺手提取变量,避免超长嵌套表达式。
- E741:把 l/r 改成 left_line/right_line。
- 其余:RUF046 / PERF401 / RET504 / RUF005 / UP031 / B007。
ruff format 重排 24 个文件。
验证:97 项测试全绿。
This commit is contained in:
1 parent
c7c8a6308a
commit
fcd4cb4649
25 files changed
+1204
-794
No files matched your search
+6
-5
@@ -18,14 +18,14 @@ from __future__ import annotations
|
||||
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from typing import Any, Optional, Union
|
||||
from typing import Any
|
||||
|
||||
LOOPBACK_HOSTS = {"127.0.0.1", "localhost", "::1", "[::1]", "0.0.0.0", ""}
|
||||
|
||||
_no_proxy_opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
|
||||
|
||||
|
||||
def is_loopback(host: Optional[str]) -> bool:
|
||||
def is_loopback(host: str | None) -> bool:
|
||||
"""判断主机名是否是本机回环(含 127.x.x.x 网段与 IPv6 回环)。"""
|
||||
if host is None:
|
||||
return False
|
||||
@@ -35,7 +35,7 @@ def is_loopback(host: Optional[str]) -> bool:
|
||||
return host.startswith("127.")
|
||||
|
||||
|
||||
def host_of(target: Union[str, urllib.request.Request]) -> Optional[str]:
|
||||
def host_of(target: str | urllib.request.Request) -> str | None:
|
||||
url = target.full_url if isinstance(target, urllib.request.Request) else target
|
||||
try:
|
||||
return urllib.parse.urlsplit(url).hostname
|
||||
@@ -43,8 +43,9 @@ def host_of(target: Union[str, urllib.request.Request]) -> Optional[str]:
|
||||
return None
|
||||
|
||||
|
||||
def open_url(target: Union[str, urllib.request.Request], timeout: float = 6.0,
|
||||
host: Optional[str] = None) -> Any:
|
||||
def open_url(
|
||||
target: str | urllib.request.Request, timeout: float = 6.0, host: str | None = None
|
||||
) -> Any:
|
||||
"""打开一个 URL 或 Request,回环地址自动绕过代理。
|
||||
|
||||
返回值是 ``http.client.HTTPResponse``,用法与 ``urllib.request.urlopen`` 一致。
|
||||
|
||||
Reference in new issue
Block a user