The Python static type checking library is used to detect potential errors in programs.
Introduction to the
Mypy is Python’s static type checking library. You can add type annotations to Python programs and use Mypy to statically type check them. You can find potential errors in your programs without running code. You can also add git hooks to automate checking before committing code. Mypy has a powerful and easy-to-use type system with modern features such as type inference, generics, callable types, tuple types, union types, and structural subtypes.
Using Python 3 annotation syntax (PEP 484 and PEP 526) or Python 2 annotation-based annotations, you can effectively annotate your code and use mypy to check for common errors in your code.
You can mix dynamic and static typing in your programs, and for legacy code, you can always fall back to dynamic typing if static typing isn’t convenient. Such as:
from typing import Iterator
def fib(n: int) -> Iterator[int]:
a, b = 0, 1
while a < n:
yield a
a, b = b, a + b
Copy the code
For Python 2.7, standard annotations are written as comments:
def is_palindrome(s):
# type: (str) -> bool
return s == s[::-1]
Copy the code
Project Address:
github.com/python/mypy
Quick start
Install using PIP:
$ python3 -m pip install -U mypy
Copy the code
To run the latest version of the code, you can install it from Git:
$ python3 -m pip install -U git+git://github.com/python/mypy.git
Copy the code
Once installed, you can type check the static types of your application using the following command:
$ mypy PROGRAM
Copy the code
Basic way to run tests:
$ pip3 install -r test-requirements.txt
$ python2 -m pip install -U typing
$ ./runtests.py
Copy the code
IDE integration
- Vim
- Use Syntastic: add let g: syntastic_PYTHon_checkers =[‘mypy’] to ~/. Vimrc
- Use ALE: add configuration explicitly enabled in ~/vim/ftplugin/python.vim, let b:ale_linters = [‘mypy’]
- Emas, using Flycheck and flycheck-mypy
- Sublime Text, use Sublimelinter-contrib-mypy
- Autom, using linter-mypy
- PyCharm, using the mypy plugin
- VS Code already provides integration with Mypy
- It can be added to git hook to implement automatic checking before committing code
Common commands
Checks the specified content
$ mypy foo.py bar.py some_directory
Copy the code
This command is recursive and checks all specified files in the directory. Mypy also allows you to check specified code in other ways:
-m MODULE, -- MODULE MODULE: Type check for supplied modules without recursively checking -p PACKAGE, -- PACKAGE PACKAGE: -c PROGRAM_TEXT, --command PROGRAM_TEXT: checks the provided character string as a program --exclude: ignores the specified file name, directory name, and path when discovering the file to be checked recursivelyCopy the code
The configuration file
--config-file CONFIG_FILE: reads configuration from a given file. Override the built-in defaults for mypyCopy the code
No type definition or invocation
- disallow untyped - calls: Disallow-untyped -defs; disallow-incomplete-defs; disallow-untyped-defs; Error reported when encountering partially annotated function definitions --disallow-untyped-decorators, error reported when functions with type annotations are decorated with unannotated decoratorsCopy the code
Configuration error
--show-error-context: messages that explain the error context before all errors --show-column-numbers: show line numbers --show-error-codes: show error codes --pretty: Format error messages, auto-wrap, display source code snippets, display error location markers, etc. --show-absolute-path: display the absolute path of the fileCopy the code
Error message generation
--any-exprs-report DIR: generate a text file report in the specified directory --cobertura-xml-report DIR: Generate a Cobertura XML type check report in the specified directory. The LXML library must be installed to generate this report --html-report / -- XSLt-html-report DIR: --lineprecision-report DIR: Generates a plain text file report in the specified directory containing statistics such as the number of type-checking rows for each moduleCopy the code
Open Source Outpost shares hot, interesting and useful open source projects daily. Participated in the maintenance of 100,000 + Star open source technology resource library, including: Python, Java, C/C++, Go, JS, CSS, Node.js, PHP,.net, etc.