Prhub

#49570 [MyPy][1/N] Fix mypy errors in some tests/ directories and enforce follow-imports=silent

原始 PR 作者 hickeyma 合并时间 2026-07-30 20:02 文件变更 30 提交数 6 评论 20 代码增减 +166 / -101

执行摘要

增强 mypy 对测试目录的类型检查,引入 SILENT_GROUPS 机制并修复约 25 个类型错误。

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.

此 PR 值得所有参与代码库维护的开发者精读,尤其是 tools/pre_commit/mypy.pySILENT_GROUPS 的设计模式,展示了如何逐步提升类型检查严格度而不阻塞整体流程。review 讨论中的类型层级设计权衡也值得关注。

讨论亮点

Reviewer hmellor 在多个测试文件中建议使用 mocking 替代 SimpleNamespace 以提升类型安全(例如 "Would it be better to use mocking here?"),作者接受建议并更新了 test_vit_fp8_scaling.pytest_precopy_mamba_align.py 等文件。

关于 test_multimodal_config.py 中是否应使用 AttentionBackendEnum 而非字符串的讨论:作者解释字符串输入是 CLI 公共接口,测试目的是验证转换逻辑,hmellor 同意保留原状。

test_gdn_forward_core_split.pyMambaSpec 类型提示与基类 AttentionSpec 不匹配的问题:双方同意作为后续 PR #50148 处理,当前添加 type: ignore 注释以避免阻断。

实现拆解

实现分为以下步骤:

  1. 新增 SILENT_GROUPS 常量:在 tools/pre_commit/mypy.py 中定义 SILENT_GROUPS 列表,其匹配优先级高于 SEPARATE_GROUPS。当文件路径匹配 SILENT_GROUPS 中的目录时,该文件会进入 mypy 的默认组,从而使用 pyproject.toml 中配置的 follow-imports=silent 严格模式。

  2. 迁移已验证目录:将 21 个已确认无类型错误的测试目录从 SEPARATE_GROUPS 移至 SILENT_GROUPS,同时调整 SEPARATE_GROUPS 的排序为最长前缀优先,避免 tests 父组覆盖子目录组。

  3. 修改 group_files() 函数:在文件分组循环中先检查 SILENT_GROUPS,若匹配则直接加入默认组;否则继续检查 SEPARATE_GROUPS。同时调整遍历顺序,确保子目录组不会被父组遮蔽。

  4. 修复 21 个目录中的约 25 个 mypy 类型错误:涉及导入变更(如引入 CallableAnyMagicMock)、类型注解修正(如将 int 改为 CompilationMode)、移除 types.SimpleNamespace 改用真正的类型构造(如 GDNAttentionMetadata)。

  5. 测试配套调整:修改多个测试文件中的类型注解和 mock 使用,确保新类型检查通过。例如在 test_cpu_gdn_ops.py 中用 GDNAttentionMetadata 替换 types.SimpleNamespace,在 test_vit_fp8_scaling.py 中用 MagicMock 替代 SimpleNamespace

文件 模块 状态 重要度
tools/pre_commit/mypy.py 预提交工具 modified 6.82
tests/kernels/mamba/test_precopy_mamba_align.py Mamba 对齐测试 modified 5.93
tests/kernels/mamba/cpu/test_cpu_gdn_ops.py GDN 操作测试 modified 5.23
tests/kernels/core/test_vit_fp8_scaling.py FP8 缩放测试 modified 5.08
tests/plugins/vllm_add_dummy_platform/vllm_add_dummy_platform/dummy_platform.py 虚拟平台测试 modified 5.05
tests/compile/fullgraph/test_full_graph.py 全图编译测试 modified 4.99

关键符号

group_files _parametrize _no_parametrize run_model

关键源码片段

tools/pre_commit/mypy.py core-logic

核心更改:引入 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",
]

评论区精华

建议在测试中使用 Mock 替代 SimpleNamespace 设计

hmellor 在多个测试文件中建议使用 mock 替代 SimpleNamespace 以提升类型安全,例如 'Would it be better to use mocking here?'(test_vit_fp8_attn.py)。

结论:作者接受建议,在 test_vit_fp8_attn.py、test_vit_fp8_scaling.py、test_precopy_mamba_align.py 中改用 MagicMock 和真实类型。 · 已解决

test_multimodal_config.py 中字符串与枚举的选择 设计

hmellor 建议在 test_multimodal_config.py 中使用 AttentionBackendEnum 而非字符串。作者解释字符串输入是 CLI 公共接口,测试目的是验证转换逻辑。

结论:hmellor 同意保留字符串测试。 · 已解决

GDNAttentionMetadataBuilder 类型提示不匹配 正确性

hmellor 指出 MambaSpec 和 AttentionSpec 无继承关系,导致 GDNAttentionMetadataBuilder 的类型提示不正确。作者分析后表示需要更深入修复。

结论:双方同意作为后续 PR #50148 处理,当前添加 type: ignore 注释以避免阻断。 · follow-up

风险与影响

主要风险在于 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 回归风险

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论