Skip to content

Commit 94ee006

Browse files
committed
build: configure ty and fix type annotations
1 parent 69975c2 commit 94ee006

69 files changed

Lines changed: 303 additions & 252 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,13 @@ repos:
2727
hooks:
2828
- id: codespell
2929
additional_dependencies: [".[toml]"]
30+
- repo: local
31+
hooks:
32+
- id: ty-check
33+
name: ty type check
34+
description: Run Astral's ty type checker.
35+
entry: uvx ty check
36+
language: system
37+
pass_filenames: false
38+
require_serial: true
39+
types: [python]

AGENTS.md

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Agent Guidelines
22

3-
## 开发环境
3+
## 安装
44

55
```bash
66
git clone git@github.com:OpenWSGR/AutoWSGR.git
@@ -14,70 +14,25 @@ pre-commit install
1414
```bash
1515
source .venv/bin/activate # Linux/macOS
1616
# .venv\Scripts\activate # Windows
17-
pytest
18-
pre-commit run --all-files
1917
```
2018

21-
## 代码风格
22-
23-
- Python 版本:3.12+
24-
- 格式化与 lint:**Ruff**(已覆盖 isort / black 功能),配置见 `pyproject.toml`
25-
- 目标行宽 100,单引号字符串
26-
- 禁止相对导入(`ban-relative-imports = all`
27-
- 英语拼写检查:**codespell**,忽略词表见 `docs/spelling_wordlist.txt`
28-
29-
提交前务必运行:
19+
## pytest
3020

3121
```bash
32-
pre-commit run --all-files
22+
pytest -n auto
3323
```
3424

35-
## 测试
25+
## pre-commit 检查
3626

37-
- 单元测试:`pytest`(测试目录 `testing/`
38-
- 功能测试:运行 `examples/` 目录中的脚本进行端到端验证
27+
提交前务必运行:
3928

4029
```bash
41-
pytest
42-
```
43-
44-
## 约定式提交(Conventional Commits)
45-
46-
提交信息格式:
47-
48-
```
49-
<type>(<scope>): <简短描述>
50-
51-
<正文>
52-
```
53-
54-
常用类型:
55-
56-
- `feat`:新功能
57-
- `fix`:修复
58-
- `build`:构建系统或依赖变更
59-
- `docs`:文档
60-
- `style`:不影响代码逻辑的格式调整
61-
- `refactor`:重构
62-
- `test`:测试
63-
64-
示例:
65-
66-
```
67-
build: migrate from setuptools to hatchling
68-
69-
- Replace setuptools with hatchling as build backend
70-
- Remove obsolete MANIFEST.in
30+
pre-commit run --all-files
7131
```
7232

73-
## 构建与打包
74-
75-
- Build backend:**hatchling**
76-
- 包数据(图片、YAML、JAR 等)位于 `autowsgr/data/`,由 hatchling 自动包含,无需 `MANIFEST.in`
33+
包含 **Ruff**(格式化与 lint)和 **ty**(类型检查)。
7734

78-
```bash
79-
uv build
80-
```
35+
## 约定式提交
8136

8237
## 文档
8338

autowsgr/combat/engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(
6262

6363
# 运行时状态 (由 fight() 重置)
6464
self._plan: CombatPlan = CombatPlan(name='', mode=CombatMode.BATTLE)
65-
self._recognizer: CombatRecognizer = None # type: ignore[assignment] # set in fight()
65+
self._recognizer: CombatRecognizer | None = None # set in fight()
6666
self._phase = CombatPhase.PROCEED
6767
self._last_action = 'yes'
6868
self._node = '0'

autowsgr/combat/handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ def _handle_spot_enemy(self) -> ConditionFlag: # noqa: PLR0912
163163
# ── 信息采集 ──
164164
mode = 'exercise' if self._plan.mode == CombatMode.EXERCISE else 'fight'
165165
enemies = get_enemy_info(self._device, mode=mode)
166-
enemy_formation = get_enemy_formation(self._device, self._ocr)
166+
enemy_formation = (
167+
get_enemy_formation(self._device, self._ocr) if self._ocr is not None else ''
168+
)
167169
_log.info('[Combat] 敌方编成: {} 阵型: {}', enemies, enemy_formation)
168170

169171
decision = self._get_current_decision()

autowsgr/combat/history.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class CombatEvent:
9494
extra: dict[str, Any] = field(default_factory=dict)
9595

9696
def __str__(self) -> str:
97-
parts = [f'[{self.event_type.name}]']
97+
parts: list[str] = [f'[{self.event_type.name}]']
9898
if self.node:
9999
parts.append(f'节点={self.node}')
100100
if self.action:

autowsgr/combat/rules.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@
3030
import re
3131
from dataclasses import dataclass, field
3232
from enum import Enum, auto
33-
from typing import Any
33+
from typing import TYPE_CHECKING, Any
3434

3535
from autowsgr.infra.logger import get_logger
3636
from autowsgr.types import Formation
3737

3838

39+
if TYPE_CHECKING:
40+
from collections.abc import Mapping
41+
42+
3943
# 允许在规则中出现的舰种标识符
4044
_log = get_logger('combat.recognition')
4145

@@ -134,7 +138,7 @@ def __post_init__(self) -> None:
134138
if self.op not in _OPERATORS:
135139
raise ValueError(f"不支持的操作符: '{self.op}',支持: {list(_OPERATORS)}")
136140

137-
def evaluate(self, context: dict[str, int | float]) -> bool:
141+
def evaluate(self, context: Mapping[str, int | float]) -> bool:
138142
"""在给定上下文中评估此条件。"""
139143
if '+' in self.field:
140144
parts = [p.strip() for p in self.field.split('+')]
@@ -159,7 +163,7 @@ class Rule:
159163
conditions: list[Condition]
160164
action: RuleAction
161165

162-
def evaluate(self, context: dict[str, int | float]) -> bool:
166+
def evaluate(self, context: Mapping[str, int | float]) -> bool:
163167
"""所有条件是否均满足。"""
164168
return all(c.evaluate(context) for c in self.conditions)
165169

@@ -184,7 +188,7 @@ class RuleEngine:
184188
rules: list[Rule] = field(default_factory=list)
185189
default: RuleAction = field(default_factory=RuleAction.no_action)
186190

187-
def evaluate(self, context: dict[str, int | float]) -> RuleAction:
191+
def evaluate(self, context: Mapping[str, int | float]) -> RuleAction:
188192
"""对敌方编成上下文评估所有规则。
189193
190194
Parameters

autowsgr/context/game_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class GameContext:
6666

6767
# ── 基础设施引用 (可选) ──
6868

69-
ocr: OCREngine
69+
ocr: OCREngine | None = None
7070
"""OCR 引擎实例 (章节/阵型识别等)。"""
7171

7272
# ── 游戏运行时状态 ──

autowsgr/emulator/os_control/windows.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def stop(self) -> None:
9393
_log.info('云手机无需关闭')
9494
return
9595
case _:
96+
assert self._process_name is not None
9697
subprocess.run( # noqa: S603
9798
['taskkill', '-f', '-im', self._process_name], # noqa: S607
9899
check=True,

autowsgr/infra/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def load(path: str | Path | None = None) -> UserConfig:
486486
return UserConfig()
487487
except ValidationError:
488488
# WSL/Linux 下默认配置缺少 serial/path 无法通过验证,提供占位值
489-
return UserConfig(emulator={'serial': '', 'path': ''})
489+
return UserConfig(emulator=EmulatorConfig(serial='', path=''))
490490
config = UserConfig.from_yaml(path)
491491
_log.info('已加载配置: {}', path)
492492
return config

autowsgr/infra/logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
from collections.abc import Callable
6767

6868
import numpy as np
69-
from loguru import Logger
69+
from loguru import Logger, Record
7070

7171

7272
# ═══════════════════════════════════════════════════════════════════════════════
@@ -103,7 +103,7 @@
103103
# ═══════════════════════════════════════════════════════════════════════════════
104104

105105

106-
def _src_patcher(record: dict) -> None:
106+
def _src_patcher(record: Record) -> None:
107107
"""将 record["file"].path 转为以项目根目录为基准的相对路径,并存入 extra["src"]。
108108
109109
格式示例:``emulator/controller.py:346``

0 commit comments

Comments
 (0)