执行摘要
- 一句话:增强mypy对测试目录的类型检查,引入SILENT_GROUPS机制并修复约25个类型错误。
- 推荐动作:此 PR 值得所有参与代码库维护的开发者精读,尤其是
tools/pre_commit/mypy.py 中 SILENT_GROUPS 的设计模式,展示了如何逐步提升类型检查严格度而不阻塞整体流程。review 讨论中的类型层级设计权衡也值得关注。
功能与动机
Part 1 of enabling mypy for tests directory. Implements PR0 and PR1 of the plan defined in feature #49569. MyPy checks tests/** with --follow-imports skip which hides real type errors. group_files() also claims every tests/** file for the "tests" entry in SEPARATE_GROUPS regardless of more specific entries also being present. Therefore removing a directory from SEPARATE_GROUPS alone does not enforce anything until "tests" itself is removed last.
实现拆解
实现分为以下步骤:
-
新增 SILENT_GROUPS 常量:在 tools/pre_commit/mypy.py 中定义 SILENT_GROUPS 列表,其匹配优先级高于 SEPARATE_GROUPS。当文件路径匹配 SILENT_GROUPS 中的目录时,该文件会进入 mypy 的默认组,从而使用 pyproject.toml 中配置的 follow-imports=silent 严格模式。
-
迁移已验证目录:将 21 个已确认无类型错误的测试目录从 SEPARATE_GROUPS 移至 SILENT_GROUPS,同时调整 SEPARATE_GROUPS 的排序为最长前缀优先,避免 tests 父组覆盖子目录组。
-
修改 group_files() 函数:在文件分组循环中先检查 SILENT_GROUPS,若匹配则直接加入默认组;否则继续检查 SEPARATE_GROUPS。同时调整遍历顺序,确保子目录组不会被父组遮蔽。
-
修复 21 个目录中的约 25 个 mypy 类型错误:涉及导入变更(如引入 Callable、Any、MagicMock)、类型注解修正(如将 int 改为 CompilationMode)、移除 types.SimpleNamespace 改用真正的类型构造(如 GDNAttentionMetadata)。
-
测试配套调整:修改多个测试文件中的类型注解和 mock 使用,确保新类型检查通过。例如在 test_cpu_gdn_ops.py 中用 GDNAttentionMetadata 替换 types.SimpleNamespace,在 test_vit_fp8_scaling.py 中用 MagicMock 替代 SimpleNamespace。
关键文件:
tools/pre_commit/mypy.py(模块 预提交工具;类别 source;类型 core-logic;符号 group_files, SILENT_GROUPS): 核心更改:引入 SILENT_GROUPS 机制,修改 group_files() 分组逻辑,启用更严格的 mypy 检查设置。
tests/kernels/mamba/test_precopy_mamba_align.py(模块 Mamba对齐测试;类别 test;类型 test-coverage;符号 _parametrize, _no_parametrize): 类型注解修复:添加 Callable 和 Any 导入,重构 _parametrize 回退函数为 _no_parametrize,增加变量类型标注。
tests/kernels/mamba/cpu/test_cpu_gdn_ops.py(模块 GDN操作测试;类别 test;类型 test-coverage): 测试改进:用 GDNAttentionMetadata 替换 types.SimpleNamespace,更新参数匹配新的构造函数签名。
tests/kernels/core/test_vit_fp8_scaling.py(模块 FP8缩放测试;类别 test;类型 test-coverage): 测试改进:改用 MagicMock 创建 ModelConfig mock,提升类型安全。
tests/plugins/vllm_add_dummy_platform/vllm_add_dummy_platform/dummy_platform.py(模块 虚拟平台测试;类别 test;类型 test-coverage): 类型修复:更新 get_attn_backend_cls 签名以匹配新接口,添加类型注解。
tests/compile/fullgraph/test_full_graph.py(模块 全图编译测试;类别 test;类型 test-coverage;符号 run_model): 类型注解修正:将 compilation_mode 参数类型从 int 改为 CompilationMode,添加 CompilationMode 导入。
关键符号:group_files, _parametrize, _no_parametrize, run_model
关键源码片段
tools/pre_commit/mypy.py
核心更改:引入 SILENT_GROUPS 机制,修改 group_files() 分组逻辑,启用更严格的 mypy 检查设置。
# Paths verified clean under follow_imports="silent". Matched before
# SEPARATE_GROUPS, so these files join the default group and are checked at the
# stricter setting even while a parent directory remains in SEPARATE_GROUPS.
#
# Fixing a directory means moving it from SEPARATE_GROUPS to here. Without that
# move the fixes are not enforced because "tests" claims every file below it.
SILENT_GROUPS = [
"tests/compile/correctness_e2e",
"tests/compile/fullgraph",
"tests/compile/fusions_e2e",
"tests/config",
"tests/entrypoints/generate",
"tests/entrypoints/tool_parsers",
"tests/entrypoints/weight_transfer",
"tests/kernels/core",
"tests/kernels/mamba",
"tests/models/language",
"tests/models/quantization",
"tests/plugins/bge_m3_sparse_plugin",
"tests/plugins/prithvi_io_processor_plugin",
"tests/plugins/vllm_add_dummy_platform",
"tests/plugins/vllm_add_dummy_stat_logger",
"tests/plugins_tests/gguf",
"tests/plugins_tests/lora_resolvers",
"tests/spec_decode",
"tests/transformers_utils",
"tests/v1/distributed",
"tests/v1/shutdown",
]
评论区精华
Reviewer hmellor 在多个测试文件中建议使用 mocking 替代 SimpleNamespace 以提升类型安全(例如 "Would it be better to use mocking here?"),作者接受建议并更新了 test_vit_fp8_scaling.py、test_precopy_mamba_align.py 等文件。
关于 test_multimodal_config.py 中是否应使用 AttentionBackendEnum 而非字符串的讨论:作者解释字符串输入是 CLI 公共接口,测试目的是验证转换逻辑,hmellor 同意保留原状。
test_gdn_forward_core_split.py 中 MambaSpec 类型提示与基类 AttentionSpec 不匹配的问题:双方同意作为后续 PR #50148 处理,当前添加 type: ignore 注释以避免阻断。
- 建议在测试中使用 Mock 替代 SimpleNamespace (design): 作者接受建议,在 test_vit_fp8_attn.py、test_vit_fp8_scaling.py、test_precopy_mamba_align.py 中改用 MagicMock 和真实类型。
- test_multimodal_config.py 中字符串与枚举的选择 (design): hmellor 同意保留字符串测试。
- GDNAttentionMetadataBuilder 类型提示不匹配 (correctness): 双方同意作为后续 PR #50148 处理,当前添加 type: ignore 注释以避免阻断。
风险与影响
- 风险:主要风险在于
SILENT_GROUPS 的引入可能改变 mypy 检查的范围,导致之前被 --follow-imports skip 掩盖的新类型错误在 pre-commit 中出现。不过此 PR 仅将已验证无错误的目录移入 SILENT_GROUPS,且 SEPARATE_GROUPS 仍然保留父组,整体风险可控。部分测试文件从 SimpleNamespace 切换到真正的 mock 对象时可能引入运行时差异,但测试逻辑未改变,回归风险低。
- 影响:影响范围包括:1)pre-commit 的 mypy 检查将更严格地检查 21 个测试目录;2)后续开发者在这些目录中引入新的类型错误会被 mypy 捕获;3)
group_files() 的排序逻辑变更,但功能等效。对用户无直接影响,主要影响库贡献者的开发体验。
- 风险标记:类型检查严格化, mypy 回归风险
关联脉络
- PR #50148 Fix GDNAttentionMetadataBuilder type hint: 作为本 PR review 中发现的 GDNAttentionMetadataBuilder 类型提示问题的后续修复。
参与讨论