refactor(types): type customization matching contracts

This commit is contained in:
jxxghp
2026-10-16 17:11:52 +08:00
parent 572e2e921e
commit 6ed327128f
6 changed files with 27 additions and 31 deletions

View File

@@ -1,19 +1,25 @@
from typing import Callable
from collections.abc import Callable, Iterable
import regex as re
from app.foundation.singleton import Singleton
_customization_provider: Callable[[], object] = lambda: ()
def _empty_customization() -> tuple[str, ...]:
"""启动装配前不提供自定义占位符。"""
return ()
def configure_customization_provider(provider: Callable[[], object]) -> None:
_customization_provider: Callable[[], Iterable[str] | None] = _empty_customization
def configure_customization_provider(provider: Callable[[], Iterable[str] | None]) -> None:
"""注入当前自定义占位符来源,避免领域匹配器读取持久化配置。"""
global _customization_provider
_customization_provider = provider
def get_customization() -> object:
def get_customization() -> Iterable[str] | None:
"""返回当前自定义占位符原始配置。"""
return _customization_provider()
@@ -24,7 +30,7 @@ def set_custom_separator(separator: str | None) -> None:
separator = None
if separator is not None and not isinstance(separator, str):
raise TypeError("custom separator must be a string or None")
CustomizationMatcher().custom_separator = separator # type: ignore[no-untyped-call]
CustomizationMatcher().custom_separator = separator
class CustomizationMatcher(metaclass=Singleton):
@@ -32,14 +38,14 @@ class CustomizationMatcher(metaclass=Singleton):
识别自定义占位符
"""
def __init__(self):
def __init__(self) -> None:
"""初始化自定义占位符正则缓存。"""
self.customization = None
self.custom_separator = None
self._customization_re_cache = {}
self.customization: str | None = None
self.custom_separator: str | None = None
self._customization_re_cache: dict[str, re.Pattern[str]] = {}
@staticmethod
def normalize_customization(customization):
def normalize_customization(customization: Iterable[object] | None) -> list[object]:
"""
规范化自定义占位符配置,兼容历史字符串与列表两种保存格式。
"""
@@ -50,13 +56,13 @@ class CustomizationMatcher(metaclass=Singleton):
return list(filter(None, customization))
@staticmethod
def _normalize_customization(customization):
def _normalize_customization(customization: Iterable[object] | None) -> list[object]:
"""
兼容旧调用,统一转到公开的自定义占位符规范化入口。
"""
return CustomizationMatcher.normalize_customization(customization)
def match(self, title=None):
def match(self, title: str | None = None) -> str:
"""
:param title: 资源标题或文件名
:return: 匹配结果
@@ -78,13 +84,13 @@ class CustomizationMatcher(metaclass=Singleton):
customization_re = re.compile(r"%s" % self.customization)
self._customization_re_cache[self.customization] = customization_re
# 处理重复多次的情况,保留先后顺序(按添加自定义占位符的顺序)
unique_customization = {}
unique_customization: dict[str, int] = {}
for item in customization_re.findall(title):
if not isinstance(item, tuple):
item = (item,)
for i in range(len(item)):
if item[i] and unique_customization.get(item[i]) is None:
unique_customization[item[i]] = i
unique_customization = list(dict(sorted(unique_customization.items(), key=lambda x: x[1])).keys())
ordered_customization = list(dict(sorted(unique_customization.items(), key=lambda x: x[1])).keys())
separator = self.custom_separator or "@"
return separator.join(unique_customization)
return separator.join(ordered_customization)

View File

@@ -75,7 +75,7 @@ uv run --locked --no-sync python scripts/architecture/complexity.py
回调和返回值类型;全仓错误计数为零并不保证单独纳入 strict frontier 后也为零。
2026-10-16 开始逐文件清理类型债务,`app/domain/title.py` 通过局部可空类型声明保留
既有搜索解析行为,并纳入严格清单;当前清单共 89 个文件(88 个生产文件和 1 个脚本)。
既有搜索解析行为,并纳入严格清单;当前清单共 90 个文件(89 个生产文件和 1 个脚本)。
每个治理提交聚焦一个生产文件及其必要测试、门禁配置、真实下降基线和指标文档。
验收同时检查完整严格清单与全宿主 Linux 分析,避免 `follow_imports = skip` 隐藏依赖
签名冲突;不通过忽略诊断或放宽类型合同接受回退。最新文件清单以 `mypy.ini` 为准,

View File

@@ -106,10 +106,10 @@ ARCH-201 至 ARCH-204 均达到实现、验证、提交、推送和远端门禁
| Model/Oper 事务债务 | 0 | 自建 Session、自动事务装饰器、直接 commit/rollback 等基线均为零 |
| Module Contract | 215 specs / 211 methods / 263 calls | 动态方法名为 0;分类事实富化通过显式合同进入插件调度,旧 YAML 分类方法已移除;download 新增 custom_words 可选参数,旧 provider 签名通过显式合同兼容 |
| Event Contract | 53 | 均已有 payload model,但当前全部是 diagnostic enforcement |
| Python 源码量 | 372,263 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
| Python 源码量 | 372,269 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
| 长方法 | 309 个超过 80 行 | 本次 AST 扫描排除 `app/plugins/**`;61 个超过 150 行,17 个超过 250 行;新增函数仍受 C901/嵌套深度门禁约束 |
| 全量 mypy 历史债务 | 8,874 / 465 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
| Ruff 历史诊断 | 99 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` |
| 全量 mypy 历史债务 | 8,861 / 464 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
| Ruff 历史诊断 | 98 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` |
| 覆盖率固定基线 | Application 80.00%,Domain 80.00% | Chain、Runtime、Agent、Adapter、Startup 未进入包级覆盖率门禁 |
### 3.3 热点文件

View File

@@ -13,6 +13,7 @@ files =
app/domain/title.py,
app/domain/tokens.py,
app/domain/meta/infopath.py,
app/domain/meta/customization.py,
app/domain/meta/releasegroup.py,
app/domain/meta/streamingplatform.py,
app/foundation/text.py,

View File

@@ -1176,16 +1176,10 @@
"operator": 1,
"type-arg": 36
},
"app/domain/meta/customization.py": {
"assignment": 1,
"no-untyped-call": 2,
"no-untyped-def": 4,
"var-annotated": 1
},
"app/domain/meta/metaanime.py": {
"arg-type": 1,
"assignment": 1,
"no-untyped-call": 4,
"no-untyped-call": 2,
"no-untyped-def": 3,
"type-arg": 1
},
@@ -1208,7 +1202,6 @@
"arg-type": 2,
"assignment": 2,
"attr-defined": 1,
"no-untyped-call": 2,
"no-untyped-def": 1,
"type-arg": 2
},
@@ -1224,7 +1217,6 @@
"app/domain/metainfo.py": {
"arg-type": 5,
"assignment": 4,
"no-untyped-call": 1,
"no-untyped-def": 1,
"type-arg": 10
},

View File

@@ -2,9 +2,6 @@
"app/domain/media.py": {
"E731": 1
},
"app/domain/meta/customization.py": {
"E731": 1
},
"app/domain/meta/runtime.py": {
"E731": 2
},