Library stubs and typeshed
Mypy uses library stubs to type check code interacting with librarymodules, including the Python standard library. A library stub definesa skeleton of the public interface of the library, including classes,variables and functions, and their types. Mypy ships with stubs fromthe typeshed project, whichcontains library stubs for the Python builtins, the standard library,and selected third-party packages.
For example, consider this code:
- x = chr(4)
Without a library stub, mypy would have no way of inferring the type of x
and checking that the argument to chr()
has a valid type.
Mypy complains if it can’t find a stub (or a real module) for alibrary module that you import. Some modules ship with stubs that mypycan automatically find, or you can install a 3rd party module withadditional stubs (see Using installed packages for details). You canalso create stubs easily. We discuss ways ofsilencing complaints about missing stubs in Missing imports.