Examples

Here is an example of a mypy.ini file. To use this config file, place it at the rootof your repo and run mypy.

  1. # Global options:
  2.  
  3. [mypy]
  4. python_version = 2.7
  5. warn_return_any = True
  6. warn_unused_configs = True
  7.  
  8. # Per-module options:
  9.  
  10. [mypy-mycode.foo.*]
  11. disallow_untyped_defs = True
  12.  
  13. [mypy-mycode.bar]
  14. warn_return_any = False
  15.  
  16. [mypy-somelibrary]
  17. ignore_missing_imports = True

This config file specifies three global options in the [mypy] section. These threeoptions will:

  • Type-check your entire project assuming it will be run using Python 2.7.(This is equivalent to using the —python-version 2.7 or -2 flag).
  • Report an error whenever a function returns a value that is inferredto have type Any.
  • Report any config options that are unused by mypy. (This will help us catch typoswhen making changes to our config file). Next, this module specifies three per-module options. The first two options change how mypytype checks code in mycode.foo.* and mycode.bar, which we assume here are two modulesthat you wrote. The final config option changes how mypy type checks somelibrary, which weassume here is some 3rd party library you’ve installed and are importing. These options will:

  • Selectively disallow untyped function definitions only within the mycode.foopackage – that is, only for function definitions defined in themycode/foo directory.

  • Selectively disable the “function is returning any” warnings withinmycode.bar only. This overrides the global default we set earlier.
  • Suppress any error messages generated when your codebase tries importing themodule somelibrary. This is useful if somelibrary is some 3rd party librarymissing type hints.