静态类型检查工具mypy¶
static type checker
Mypy 是一个用于 Python 的静态类型检查器。它可以帮助您在编写代码时发现错误,并使您的代码更易于阅读和维护。
主要功能¶
类型检查: Mypy 可以检查您的代码是否符合您指定的类型注解。这可以帮助您发现潜在的错误,例如将字符串分配给数字变量。
类型推断: Mypy 可以推断许多变量的类型,即使您没有为它们提供显式类型注解。这可以使您的代码更简洁易读。
文档生成: Mypy 可以生成包含类型信息的文档。这可以帮助其他开发人员了解您的代码。
Type checkers help ensure that you’re using variables and functions in your code correctly. With mypy, add type hints (PEP 484) to your Python programs, and mypy will warn you when you use those types incorrectly.
Python is a dynamic language, so usually you’ll only see errors in your code when you attempt to run it. Mypy is a static checker, so it finds bugs in your programs without even running them!
Adding type hints for mypy does not interfere with the way your program would otherwise run. Think of type hints as similar to comments! You can always use the Python interpreter to run your code, even if mypy reports errors.
官网: https://mypy.readthedocs.io/en/stable/getting_started.html
GitHub: https://github.com/python/mypy