Skip to content

Typing in Python

In Python typing can be optionally used. To check typing the standard tool is MyPy.

Usage

Function annotations

def func(arg: arg_type, optarg: arg_type = default) -> return_type: 
...

For arguments the syntax is argument: annotation, while the return type is annotated using -> annotation. Note that the annotation must be a valid Python expression.

Variable annotations

Sometimes the type checker needs help in figuring out the types of variables as well. The syntax is similar:

pi: float = 3.142

def circumference(radius: float) -> float:
    return 2 * pi * radius`