检查 Python 中的类型提示覆盖率
Check type hint coverage in Python
我想在我的 Python 包中的所有地方强制执行类型提示,并配置 CI 以在构建失败时失败,以防它不是 100%。
我可以使用 mypy or any other package in Python similarly to how I can check code coverage with e.g. coverage.py 报告类型提示覆盖率吗?
要求严格的 100% 类型提示覆盖率是否也明智?
Is there a way for me to report type hint coverage using mypy or any other package in Python similarly to how I can check code coverage with e.g. coverage.py?
是的,有很多禁止 various uses of Any
, as well as missing type annotations. Or you can just pass the --strict
flag to disallow a large number of questionable constructs with a single flag. The precise definition of --strict
may change over time, so it might be a poor fit for CI (i.e. "Why is the build broken, all I did was install a new mypy
?"). There are also a number of flags for generating coverage reports 的标志。
Is it also sensible to require a strict 100% of type hint coverage?
这在很大程度上取决于您希望通过类型提示完成什么,以及您的代码库的历史和当前状态。例如,如果你和你所有的开发伙伴都来自静态类型背景(即你以前使用静态类型语言编程,例如 C++ 和 Java),并且你没有很多旧的 Python 代码来支持,那么这很可能会很好地工作。另一方面,如果您有大量遗留代码(可能是在最后一分钟从 Python 2 转换过来的),或者如果您的许多开发人员都习惯于动态类型,那么可能会有更多的阻抗失配。如果您不确定这是否适合您,我建议设置单独的 CI 构建,有和没有严格。如果严格的一直是红色而 non-strict 一直是绿色,那么在启用强制类型提示之前您可能需要克服技术或文化问题。
我想在我的 Python 包中的所有地方强制执行类型提示,并配置 CI 以在构建失败时失败,以防它不是 100%。
我可以使用 mypy or any other package in Python similarly to how I can check code coverage with e.g. coverage.py 报告类型提示覆盖率吗?
要求严格的 100% 类型提示覆盖率是否也明智?
Is there a way for me to report type hint coverage using mypy or any other package in Python similarly to how I can check code coverage with e.g. coverage.py?
是的,有很多禁止 various uses of Any
, as well as missing type annotations. Or you can just pass the --strict
flag to disallow a large number of questionable constructs with a single flag. The precise definition of --strict
may change over time, so it might be a poor fit for CI (i.e. "Why is the build broken, all I did was install a new mypy
?"). There are also a number of flags for generating coverage reports 的标志。
Is it also sensible to require a strict 100% of type hint coverage?
这在很大程度上取决于您希望通过类型提示完成什么,以及您的代码库的历史和当前状态。例如,如果你和你所有的开发伙伴都来自静态类型背景(即你以前使用静态类型语言编程,例如 C++ 和 Java),并且你没有很多旧的 Python 代码来支持,那么这很可能会很好地工作。另一方面,如果您有大量遗留代码(可能是在最后一分钟从 Python 2 转换过来的),或者如果您的许多开发人员都习惯于动态类型,那么可能会有更多的阻抗失配。如果您不确定这是否适合您,我建议设置单独的 CI 构建,有和没有严格。如果严格的一直是红色而 non-strict 一直是绿色,那么在启用强制类型提示之前您可能需要克服技术或文化问题。