用途:在 pytest 测试代码中强制类型安全,让测试函数、fixture 与 mock 都带类型注解,并通过 mypy 等工具在 CI/CD 中检查。
主要能力:为测试函数和 fixture 添加类型注解(如 def test_x() -> None、fixture 返回 Callable[..., T] 工厂);使用 unittest.mock.create_autospec 生成遵循接口的类型安全 mock,访问不存在的方法会抛 AttributeError;配置 mypy 对 tests.* 模块启用严格检查;编写类型安全断言(如 isinstance 检查)。
使用方式:在测试文件顶部加 from future import annotations,为每个测试函数和 fixture 标注返回类型;用 create_autospec(ShopifyPort, instance=True) 替代普通 Mock;在 pyproject.toml 的 [tool.mypy] 中设置 python_version、strict、disallow_untyped_defs,并用 [[tool.mypy.overrides]] 针对 tests.* 调整(如允许 fixture 的 Any)。
适用场景:编写类型安全测试、为测试文件配置 mypy、用协议/autospec 做 mock、在 CI/CD 中强制类型检查、排查类型相关测试问题。
限制与要求:需 Python 3.11+、pytest >= 7.0、mypy >= 1.7.0,pyright >= 1.1.0 为可选。资料未提供安装命令、价格、平台支持等更多信息。