Mapping file paths to modules
One of the main ways you can tell mypy what files to type checkis by providing mypy the paths to those files. For example:
- $ mypy file_1.py foo/file_2.py file_3.pyi some/directory
This section describes how exactly mypy maps the provided pathsto modules to type check.
- Files ending in
.py
(and stub files ending in.pyi
) arechecked as Python modules. - Files not ending in
.py
or.pyi
are assumed to be Pythonscripts and checked as such. - Directories representing Python packages (i.e. containing a
init.py[i]
file) are checked as Python packages; allsubmodules and subpackages will be checked (subpackages mustthemselves have ainit.py[i]
file). - Directories that don’t represent Python packages (i.e. not directlycontaining an
init.py[i]
file) are checked as follows:- All
*.py[i]
files contained directly therein are checked astoplevel Python modules; - All packages contained directly therein (i.e. immediatesubdirectories with an
init.py[i]
file) are checked astoplevel Python packages.
- All
One more thing about checking modules and packages: if the directorycontaining a module or package specified on the command line has aninit.py[i]
file, mypy assigns these an absolute module name bycrawling up the path until no init.py[i]
file is found.
For example, suppose we run the command mypy foo/bar/baz.py
wherefoo/bar/init.py
exists but foo/init.py
does not. Thenthe module name assumed is bar.baz
and the directory foo
isadded to mypy’s module search path.
On the other hand, if foo/bar/init.py
did not exist, foo/bar
would be added to the module search path instead, and the module nameassumed is just baz
.
If a script (a file not ending in .py[i]
) is processed, the modulename assumed is main
(matching the behavior of thePython interpreter), unless —scripts-are-modules
is passed.