Specifying code to be checked
Mypy lets you specify what files it should type check in severaldifferent ways.
- First, you can pass in paths to Python files and directories youwant to type check. For example:
- $ mypy file_1.py foo/file_2.py file_3.pyi some/directory
The above command tells mypy it should type check all of the providedfiles together. In addition, mypy will recursively type check theentire contents of any provided directories.
For more details about how exactly this is done, seeMapping file paths to modules.
- Second, you can use the
-m
flag (long form:—module
) tospecify a module name to be type checked. The name of a moduleis identical to the name you would use to import that modulewithin a Python program. For example, running:
- $ mypy -m html.parser
…will type check the module html.parser
(this happens to bea library stub).
Mypy will use an algorithm very similar to the one Python uses tofind where modules and imports are located on the file system.For more details, see How imports are found.
- Third, you can use the
-p
(long form:—package
) flag tospecify a package to be (recursively) type checked. This flagis almost identical to the-m
flag except that if you give ita package name, mypy will recursively type check all submodulesand subpackages of that package. For example, running:
- $ mypy -p html
…will type check the entire html
package (of library stubs).In contrast, if we had used the -m
flag, mypy would have typechecked just html
’s init.py
file and anything importedfrom there.
Note that we can specify multiple packages and modules on thecommand line. For example:
- $ mypy --package p.a --package p.b --module c
- Fourth, you can also instruct mypy to directly type check smallstrings as programs by using the
-c
(long form:—command
)flag. For example:
- $ mypy -c 'x = [1, 2]; print(x())'
…will type check the above string as a mini-program (and in this case,will report that List[int]
is not callable).