Following imports

Mypy is designed to doggedly follow all imports,even if the imported module is not a file you explicitly wanted mypy to check.

For example, suppose we have two modules mycode.foo and mycode.bar:the former has type hints and the latter does not. We runmypy -m mycode.foo and mypy discovers that mycode.foo importsmycode.bar.

How do we want mypy to type check mycode.bar? We can configure thedesired behavior by using the —follow-imports flag. This flagaccepts one of four string values:

  • normal (the default) follows all imports normally andtype checks all top level code (as well as the bodies of allfunctions and methods with at least one type annotation inthe signature).

  • silent behaves in the same way as normal but willadditionally suppress any error messages.

  • skip will not follow imports and instead will silentlyreplace the module (and anything imported from it) with anobject of type Any.

  • error behaves in the same way as skip but is not quite assilent – it will flag the import as an error, like this:

  1. main.py:1: note: Import of 'mycode.bar' ignored
  2. main.py:1: note: (Using --follow-imports=error, module not passed on command line)

If you are starting a new codebase and plan on using type hints fromthe start, we recommend you use either —follow-imports=normal(the default) or —follow-imports=error. Either option will helpmake sure you are not skipping checking any part of your codebase byaccident.

If you are planning on adding type hints to a large, existing code base,we recommend you start by trying to make your entire codebase (includingfiles that do not use type hints) pass under —follow-imports=normal.This is usually not too difficult to do: mypy is designed to report asfew error messages as possible when it is looking at unannotated code.

If doing this is intractable, we recommend passing mypy just the filesyou want to type check and use —follow-imports=silent. Even ifmypy is unable to perfectly type check a file, it can still glean someuseful information by parsing it (for example, understanding what methodsa given object has). See Using mypy with an existing codebase for more recommendations.

We do not recommend using skip unless you know what you are doing:while this option can be quite powerful, it can also cause manyhard-to-debug errors.