SuperAPI AI 工具集

python-mypy

编程与开发

查看项目

更新于 10-16 08:11

技能介绍

用途:在 Python 项目中引入 mypy 做静态类型检查,在开发期发现类型错误,无需运行时开销,支持从动态类型逐步迁移到静态类型。

主要能力:函数与类签名注解;Optional/Union(含 Python 3.10+ 的 X | None 语法);TypedDict 描述结构化字典;Protocol 实现鸭子类型接口;类型收窄(isinstance、assert);reveal_type 调试推断类型。提供 strict 等多个严格度选项,如 disallow_untyped_defs、check_untyped_defs、warn_return_any、warn_unused_ignores、ignore_missing_imports。支持按模块覆盖的 overrides 配置,可逐步收紧;支持 Pydantic/FastAPI 与 SQLAlchemy mypy 插件。

安装与使用:pip install mypy;为第三方库安装类型桩,如 pip install types-requests types-PyYAML types-boto3;运行 mypy src/ 或 mypy --strict src/;可用 mypy --install-types 检查并安装桩。配置写在 pyproject.toml 的 [tool.mypy],从基础警告逐步升级到 strict = true。第三方缺桩时优先按模块 override 设置 ignore_missing_imports,避免全局忽略;行内抑制使用 # type: ignore[错误码] 并注明原因。CI 可接入 pre-commit(mirrors-mypy)与 GitHub Actions 运行 mypy src/ --config-file pyproject.toml。

适用场景:新项目基线、存量代码渐进迁移、公共 API 优先注解、FastAPI/Pydantic 与 SQLAlchemy 项目。

限制与缺失:资料未提供具体价格、收费方式、性能数据与完整错误码清单;配置与高级模式细节需参考其 references 与 examples 文件。