Static type checking in Python with mypy

A tutorial in bullet points

David Piper
1 min readJul 28, 2019
  • using types can prevent mistakes and errors
  • type hints were added to Python in version 3.5
First example: two parameter with str as type hint and str as return type
  • primitive data types like str, int and bool are available without additional imports
  • to use lists, optionals or other types, you need to import them
More than just primitive data types: Also Lists, Optionals and other types are available.
  • to check the types, you can use mypy
  • to install mypy use: pip install mypy
  • to check a single file use: mypy path/to/file.py
  • if there is no output, everything was ok
  • otherwise all errors are listed
  • instead of a single file, all files of a project can be checked as well
  • to find all methods without annotations use:
    mypy path/to/project --disallow-untyped-defs
  • More information on mypy:
    Type hints cheat sheet and The mypy command line

--

--

David Piper
David Piper

Written by David Piper

Working as an iOS and Android dev.

No responses yet