stubgen tool automatically generates stub files (.pyi files) for Python modules and C extension modules. Stub files contain type hints for the public interface of a module, with empty function bodies.
Why use stubs?
Stub files are useful for:- Third-party modules without type annotations
- C extension modules that mypy can’t directly process
- Providing type information without modifying source code
Stubgen generates draft stubs. The auto-generated files often require manual updates, and most types default to
Any.Basic usage
Specifying what to stub
Paths to source files for which to generate stubs. Can be files or directories.Output:
out/<filename>.pyiGenerate a stub file for the given module. May be repeated.Stubgen will not recursively generate stubs for submodules.
Generate stubs for the given package. May be repeated.Stubgen will recursively generate stubs for all submodules.
Generation mode options
Don’t try to import modules. Instead only use mypy’s normal search mechanism.
- Does not support C extension modules
- Disables runtime introspection (affects
__all__detection) - Useful when importing causes unwanted side effects
falseDon’t perform semantic analysis of source files.
- May generate worse stubs (aliases become
Anyvariables) - Useful if semantic analysis causes critical mypy errors
- Does not apply to C extension modules
- Incompatible with
--inspect-mode
falseImport and inspect modules instead of parsing source code.
- Default behavior for C modules and pyc-only packages
- Useful for pure Python modules with dynamically generated members
- Implies
--no-analysis
false (except for C modules)Try to infer better signatures by parsing .rst documentation in PATH.
- Only works for C extension modules
- Can result in better quality stubs
Output options
Change the output directory.Default:
./outThe output directory will be created if it doesn’t exist. Existing stubs will be overwritten without warning.Include definitions that are considered private in stubs.Private names:
_foo (single leading underscore, no trailing underscores)Default: falseDon’t export all names imported from other modules within the same package.Only export imported names that are referenced in the module.Default:
falseInclude docstrings in stubs.Adds docstrings to:
- Python function stubs
- Python class stubs
- C extension function stubs
falseAdditional options
Show help message and exit.
If an exception is raised during stub generation, continue processing remaining modules.Default:
false (fail immediately on error)Specify module search directories, separated by colons.Only used if
--no-import is given.Produce more verbose output.Default:
falseProduce less verbose output.Default:
falseExamples
Example output
Given this source file:mymodule.py
out/mymodule.pyi
Notice that:
- Type annotations from source are preserved
- Unannotated types default to
Any - Instance attributes are detected from
__init__ - Function bodies are replaced with
...
Heuristics and filtering
Stubgen applies heuristics to avoid generating stubs for:- Test modules and packages
- Vendored third-party packages (common vendor directory names)
- Modules in directories named:
site-packages,node_modules,__pycache__ - Directories starting with
. - Files without
.pyor.pyiextensions