STUBGEN(1) | Mypy | STUBGEN(1) |
stubgen - Generate draft type hint stubs for Python modules
stubgen [-h] [-py2] [-m MODULE] [-p PACKAGE] [OPTIONS...] [FILES...]
Mypy is a static type checker for Python 3 and Python 2.7. Mypy includes the stubgen tool that can automatically generate stub files (.pyi files) for Python modules and C extension modules.
A stub file (see PEP 484) contains only type hints for the public interface of a module, with empty function bodies. Mypy can use a stub file instead of the real implementation to provide type information for the module. They are useful for third-party modules whose authors have not yet added type hints (and when no stubs are available in typeshed) and C extension modules (which mypy can’t directly process).
Stubgen generates draft stubs. The auto-generated stub files often require some manual updates, and most types will default to Any. The stubs will be much more useful if you add more precise type annotations, at least for the most commonly used functionality.
NOTE:
You can give stubgen paths of the source files for which you want to generate stubs:
$ stubgen foo.py bar.py
This generates stubs out/foo.pyi and out/bar.pyi. The default output directory out can be overridden with -o DIR.
You can also pass directories, and stubgen will recursively search them for any .py files and generate stubs for all of them:
$ stubgen my_pkg_dir
Alternatively, you can give module or package names using the -m or -p options:
$ stubgen -m foo -m bar -p my_pkg_dir
Details of the options:
Stubgen will not recursively generate stubs for any submodules of the provided module.
Stubgen will recursively generate stubs for all submodules of the provided package. This flag is identical to --module apart from this behavior.
NOTE:
Stubgen applies heuristics to avoid generating stubs for submodules that include tests or vendored third-party packages.
By default stubgen will try to import the target modules and packages. This allows stubgen to use runtime introspection to generate stubs for C extension modules and to improve the quality of the generated stubs. By default, stubgen will also use mypy to perform light-weight semantic analysis of any Python modules. Use the following flags to alter the default behavior:
Full documentation is available online at: https://mypy.readthedocs.io/en/latest/stubgen.html or locally at: /usr/share/doc/mypy/html (requires mypy-doc package).
Jukka Lehtosalo and contributors
February 24, 2023 |