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:
snowy-yang committed 2026-09-18 21:18:22 +08:00
1 parent c7c8a6308a
commit fcd4cb4649
25 files changed
+1204 -794

No files matched your search

+16 -16
View File
@@ -8,6 +8,7 @@
from __future__ import annotations
import contextlib
import os
import random
import sys
@@ -16,10 +17,10 @@ import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from farm import config # noqa: E402
from farm.engine import FarmEngine, GameError # noqa: E402
from farm.models import GameState, Plot # noqa: E402
from farm.storage import load_state, save_state # noqa: E402
from farm import config
from farm.engine import FarmEngine, GameError
from farm.models import GameState, Plot
from farm.storage import load_state, save_state
class EngineTestCase(unittest.TestCase):
@@ -190,14 +191,14 @@ class EngineTestCase(unittest.TestCase):
self.assertEqual(ctx.exception.code, "plot_limit")
def test_buy_plot_without_gold(self) -> None:
self.engine.state.level = 5 # 先解锁土地上限,才能走到"钱不够"这一步
self.engine.state.level = 5 # 先解锁土地上限,才能走到"钱不够"这一步
self.engine.state.gold = 0
with self.assertRaises(GameError) as ctx:
self.engine.buy_plot()
self.assertEqual(ctx.exception.code, "not_enough_gold")
def test_buy_plot_blocked_at_level_cap(self) -> None:
self.engine.state.gold = 10 ** 6
self.engine.state.gold = 10**6
with self.assertRaises(GameError) as ctx:
self.engine.buy_plot()
self.assertEqual(ctx.exception.code, "plot_limit")
@@ -262,7 +263,7 @@ class EngineTestCase(unittest.TestCase):
self.assertEqual(ctx.exception.code, "unknown_animal")
def test_pasture_limit(self) -> None:
self.engine.state.gold = 10 ** 6
self.engine.state.gold = 10**6
for _ in range(config.MAX_ANIMALS):
self.engine.buy_animal("chicken")
with self.assertRaises(GameError) as ctx:
@@ -280,7 +281,7 @@ class EngineTestCase(unittest.TestCase):
self.assertLess(self.engine.state.gold, gold) # 被扣了高价补货费
def test_buy_feed_validation(self) -> None:
self.engine.state.gold = 10 ** 6
self.engine.state.gold = 10**6
with self.assertRaises(GameError):
self.engine.buy_feed(0)
with self.assertRaises(GameError):
@@ -350,7 +351,7 @@ class EngineTestCase(unittest.TestCase):
def test_save_load_roundtrip(self) -> None:
self.engine.plant(1, "carrot")
self.engine.buy_animal("chicken")
self.engine.state.gold = 500 # 操作完成后再定金币,避免被扣费干扰断言
self.engine.state.gold = 500 # 操作完成后再定金币,避免被扣费干扰断言
self.engine.state.feed = 7
self.engine.state.day = 13
path = os.path.join(self.tmp, "roundtrip.json")
@@ -402,14 +403,14 @@ class EngineTestCase(unittest.TestCase):
self.assertEqual(state.day, 1)
self.assertEqual(state.gold, 0)
self.assertEqual(state.level, 1)
self.assertIsNone(state.plots[0].crop) # 非法作物被清空
self.assertEqual(state.animals, []) # 非法动物被丢弃
self.assertGreater(state.market["wheat"], 0) # 非法价格回落默认值
self.assertIsNone(state.plots[0].crop) # 非法作物被清空
self.assertEqual(state.animals, []) # 非法动物被丢弃
self.assertGreater(state.market["wheat"], 0) # 非法价格回落默认值
self.assertGreaterEqual(len(state.plots), config.BASE_PLOTS)
# ============================================================ 并发
def test_concurrent_operations_are_safe(self) -> None:
self.engine.state.gold = 10 ** 6
self.engine.state.gold = 10**6
errors = []
def worker(index: int) -> None:
@@ -418,10 +419,9 @@ class EngineTestCase(unittest.TestCase):
self.engine.snapshot()
self.engine.available_actions()
if index % 3 == 0:
try:
# 金币可能已经不够了,这里只关心它不崩溃
with contextlib.suppress(GameError):
self.engine.buy_feed(1)
except GameError:
pass
except Exception as exc: # 记录任何非预期异常
errors.append(exc)