current position:Home>11.5K Star! An open source Python static type checking Library

11.5K Star! An open source Python static type checking Library

2022-01-30 06:07:12 Open source outpost

【 Introduction 】:Python Static type checking library , You can find potential errors in the program .

brief introduction

Mypy yes Python Static type checking Library , Can be directed to Python Add type annotation to the program , And use mypy Do static type checking on them , You don't need to run code to find potential errors in your program . You can also join in git hook in , Implement automatic checking before submitting code .Mypy Has a powerful and easy-to-use type system , With modern functions , Such as type inference 、 Generic 、 Callable type 、 A tuple type 、 Union type and structure subtype .

Use Python 3 Annotation syntax (PEP 484 and PEP 526) or Python 2 Annotation based annotation , Be able to effectively annotate code and use mypy Check for common errors in your code .

You can mix dynamic and static types in programs , For legacy code , If it is inconvenient to use static types , You can fallback to a dynamic type at any time . 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 code 

about Python 2.7, Standard notes are written as notes :

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

Project address :

github.com/python/mypy

Quick start

Use pip Installation :

$ python3 -m pip install -U mypy
 Copy code 

If you want to run the latest version of code , It can be downloaded from git Installation :

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

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

$ mypy PROGRAM
 Copy code 

The basic method of running tests :

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

IDE Integrate

  • Vim
  1. Use Syntastic: stay ~/.vimrc add to let g:syntastic_python_checkers=['mypy']
  2. Use ALE: stay ~/vim/ftplugin/python.vim Add configuration to explicitly enable ,let b:ale_linters = ['mypy']
  • Emas, Use Flycheck and Flycheck-mypy
  • Sublime Text, Use SublimeLinter-contrib-mypy
  • Autom, Use linter-mypy
  • PyCharm, Use mypy plug-in unit
  • VS Code, Provided for mypy Integration of
  • Can be added to git hook in , Implement automatic checking before submitting code

Common commands

Check the specified content

$ mypy foo.py bar.py some_directory
 Copy code 

This command is recursively checked , All specified files corresponding to the directory will be checked .Mypy It also allows you to check the specified code in other ways :

-m MODULE, --module MODULE:  Type check the modules provided , Does not recursively check 
-p PACKAGE, --package PACKAGE:  Type check the package provided , Will recursively check 
-c PROGRAM_TEXT, --command PROGRAM_TEXT: Check the supplied string as a program 
--exclude: The specified file name is ignored when recursively finding the file to be checked 、 Directory name and path 
 Copy code 

The configuration file

--config-file CONFIG_FILE: Read configuration from the given file . Cover  mypy  Built in defaults for 
 Copy code 

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 is reported when a function definition without a type annotation 
--disallow-incomplete-defs, An error is reported when a partially annotated function definition is encountered 
--disallow-untyped-decorators, Functions with type annotations report errors when decorated with non annotated decorators 
 Copy code 

Configuration error message

--show-error-context: All errors are preceded by a message that explains the error context 
--show-column-numbers: According to the line Numbers 
--show-error-codes: Display error code 
--pretty: Format error message , Word wrap 、 Display source code snippets and error location markers, etc 
--show-absolute-path: Show the absolute path of the file 
 Copy code 

Error message generation

--any-exprs-report DIR: Generate a text file report in the specified directory 
--cobertura-xml-report DIR: Generate... In the specified directory  Cobertura XML  Type check report , Must install  lxml  Library to generate this report 
--html-report / --xslt-html-report DIR: Generate... In the specified directory  HTML  Type check report , Must install  lxml  Library to generate this report 
--lineprecision-report DIR: Generate a plain text file report in the specified directory , It contains statistics such as the type of each module and the number of check lines 
 Copy code 

Open source outpost Everyday sharing is hot 、 Interesting and practical open source projects . Participate in maintenance 10 ten thousand + Star Open source technology repository for , Include :Python、Java、C/C++、Go、JS、CSS、Node.js、PHP、.NET etc. .

copyright notice
author[Open source outpost],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300607109818.html

Random recommended