Show Menu
Cheatography

Type Checking and mypy Cheat Sheet by

MyPy Tool

mypy [files]
Command line to check syntax
-v
Be Verbose
-any-­exp­rs-­report PATH
Provide a path to generate a coverage report
This tool will show you Type Errors found in the code. If verbose is off and the command returns nothing, the code is OK.

Type Definition

from typing import Dict, List, Tuple
Import types definition
var: str = "­"
Define str a variable
var: Dict[str, int] = {}
Define Dict a variable
var: Tuple[str, float, int] = ()
Define Tuple a variable
MyType = Dict[str, int]
Define a custom 'Type`
var: MyType = {}
Define MyType a variable
def functi­on(arg: str) -> int:
Define a function that takes an strand returns an int
var: Any
The Any type accepts all types
var: Union[str, int]
This Union type accepts either a str or an int
pip install mypy
pip install typing
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter

          More Cheat Sheets by amicheletti