5K Star, an open source Python static type checking Library

Mypy is Python's static type checking library. You can add type annotations to Python programs and use mypy to check the...

Mypy is Python's static type checking library. You can add type annotations to Python programs and use mypy to check their static types. You can find potential errors in the program without running code. It can also be added to git hook to realize automatic check before submitting code. Mypy has a powerful and easy-to-use type system with modern functions, 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 can effectively annotate code and use mypy to check common errors in code.

Dynamic types and static types can be mixed in programs. For legacy code, if it is inconvenient to use static types, you can fall back to dynamic types at any time. For example:

from typing import Iterator def fib(n: int) -> Iterator[int]: a, b = 0, 1 while a < n: yield a a, b = b, a + b

For Python 2.7, the standard annotation is written as a comment:

def is_palindrome(s): # type: (str) -> bool return s == s[::-1]

Project address:

https://github.com/python/mypy

Quick start

Install using pip:

$ python3 -m pip install -U mypy

If you want to run the latest version of code, you can install it from git:

$ python3 -m pip install -U git+git://github.com/python/mypy.git

After installation, you can use the following command to type check the static type of the application:

$ mypy PROGRAM

Basic method of running test:

$ pip3 install -r test-requirements.txt $ python2 -m pip install -U typing $ ./runtests.py

IDE integration

  • Vim
  1. Using synaptic: add Let G: synaptic in ~ /. vimrc_ python_ checkers=[‘mypy’]

  2. Use ALE: add configuration in ~ / vim/ftplugin/python.vim and enable let B: ALE explicitly_ linters = [‘mypy’]

  • Emas, using Flycheck and Flycheck mypy

  • Sublime Text, using sublimelinter contrib mypy

  • Autom, using linter mypy

  • PyCharm, using mypy plug-in

  • VS Code, which provides integration with mypy

  • It can be added to git hook to realize automatic check before submitting code

Common commands

Check the specified content
$ mypy foo.py bar.py some_directory

This command is recursive and will check all the specified files under the directory. Mypy also allows you to check the specified code in other ways:

-m MODULE, --module MODULE: Type check is performed on the provided module, not recursively -p PACKAGE, --package PACKAGE: Type check is performed on the provided package, and recursive check is performed -c PROGRAM_TEXT, --command PROGRAM_TEXT: Check the supplied string as a program --exclude: The specified file name, directory name, and path are ignored when recursively discovering the file to be checked
configuration file
--config-file CONFIG_FILE: Reads the configuration from the given file. cover mypy Built in defaults for
No type definitions and calls
--disallow-untyped-calls: An error is reported when a function with type annotation calls a function without annotation definition --disallow-untyped-defs,An error was reported while defining a function without a type annotation --disallow-incomplete-defs,An error is reported when a partially annotated function definition is encountered --disallow-untyped-decorators,A function with a type annotation reports an error when decorated with an uncommented decorator
Configuration error message
--show-error-context: All errors are preceded by a message that explains the error context --show-column-numbers: set number --show-error-codes: Display error code --pretty: Format error messages, wrap lines, display source code snippets, display error location tags, etc --show-absolute-path: Displays the absolute path of the file
Error message generation
--any-exprs-report DIR: Generates a text file report in the specified directory --cobertura-xml-report DIR: Generate in the specified directory Cobertura XML Type check report, must be installed lxml Library to generate this report --html-report / --xslt-html-report DIR: Generate in the specified directory HTML Type check report, must be installed lxml Library to generate this report --lineprecision-report DIR: Generate a plain text file report in the specified directory, which contains statistics such as the number of type check lines of each module
Technical exchange

Welcome to reprint, collect, gain, praise and support!

At present, a technical exchange group has been opened, with more than 2000 group friends. The best way to add notes is: source + Interest direction, which is convenient to find like-minded friends

  • Method ① send the following pictures to wechat, long press identification, and the background replies: add group;
  • Mode ②. Add micro signal: dkl88191, remarks: from CSDN
  • WeChat search official account: Python learning and data mining, background reply: add group

28 October 2021, 21:24 | Views: 8982

Add new comment

For adding a comment, please log in
or create account

0 comments