Type Checking and mypy Cheat Sheet

A Programming › Python cheat sheet in English by amicheletti
7,326 views 2,239 downloads 20 Jul 2017 History
3.7 avg · 0 ratings

MyPy Tool

mypy [files]
Command line to check syntax
-v
Be Verbose
-any-e­xpr­s-r­eport 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

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 1

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.