STUBGEN(1)
β¨ STUBGEN(1) β¨
okay bestie, gather round, because today we're talking about stubgen from the Mypy manual (section 1, if you're keeping track at home) π
πΈ NAME πΈ
stubgen β generate draft type hint stubs for Python modules πβ¨
π¦ SYNOPSIS π¦
stubgen [-h] [-py2] [-m MODULE] [-p PACKAGE] [OPTIONS...] [FILES...]
π DESCRIPTION π
So like. Mypy is a static type checker for Python 3 and Python 2.7 (we contain multitudes), and it comes with this little tool called stubgen that will just... automatically generate stub files (.pyi files) for Python modules and C extension modules. No cap. π₯
A stub file (as per PEP 484, iykyk) contains only the type hints for the public interface of a module, with the function bodies left empty. Mypy can then use the stub file instead of the real implementation to get type info for that module. These are lowkey iconic for:
- third-party modules whose authors haven't added type hints yet (and where no stubs exist in typeshed)
- C extension modules (which mypy can't directly process)
Now, important: stubgen generates draft stubs. The auto-generated files usually need some manual love, and most types will just default to Any π. Your stubs are gonna be so much more useful if you go in and add more precise type annotations β at least for the stuff you actually use all the time. Do it for her (her = future you).
β¨ OPTIONS β¨
heads up bestie: the command-line flags may change between releases. so don't get too attached okay π
π Specifying what to stub
You can hand stubgen the paths of the source files you want stubs for:
$ 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 (more on that below, scroll down bestie π).
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 (your other option, your alternate route, your plan B π), 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, because you know I'm gonna give you the details:
-m MODULE,--module MODULEβ Generate a stub file for the given module. This flag may be repeated multiple times.
Stubgen will not recursively generate stubs for any submodules of the provided module.
-p PACKAGE,--package PACKAGEβ Generate stubs for the given package. This flag maybe repeated multiple times. (yes, it really says "maybe" in the original, we love a typo moment π«Ά)
Stubgen will recursively generate stubs for all submodules of the provided package. This flag is identical to --module apart from this behavior.
Note: You can't mix paths and-m/-poptions in the same stubgen invocation. Pick a lane, babe π«
Stubgen applies heuristics to avoid generating stubs for submodules that include tests or vendored third-party packages (she's self-aware, she knows what's noise).
π Specifying how to generate stubs
By default, stubgen will try to import the target modules and packages. This lets stubgen use runtime introspection to generate stubs for C extension modules and to level up the quality of the generated stubs β¨. By default, stubgen will also use mypy to do light-weight semantic analysis of any Python modules. Use the following flags to alter the default behavior:
--no-importβ Don't try to import modules. Instead only use mypy's normal search mechanism to find sources. This does not support C extension modules. This flag also disables runtime introspection functionality, which mypy uses to find the value of__all__. As a result, the set of exported imported names in stubs may be incomplete. This flag is generally only useful when importing a module causes unwanted side effects, such as the running of tests. Stubgen tries to skip test modules even without this option, but this does not always work (she's trying her best π).--no-analysisβ Don't perform semantic analysis of source files. This may generate worse stubs β in particular, some module, class, and function aliases may be represented as variables with theAnytype. This is generally only useful if semantic analysis causes a critical mypy error. Does not apply to C extension modules. Incompatible with--inspect-mode.--inspect-modeβ Import and inspect modules instead of parsing source code. This is the default behavior for C modules and pyc-only packages. The flag is useful to force inspection for pure Python modules that make use of dynamically generated members that would otherwise be omitted when using the default behavior of code parsing. Implies--no-analysisas analysis requires source code.--doc-dir PATHβ Try to infer better signatures by parsing.rstdocumentation in PATH. This may result in better stubs, but currently it only works for C extension modules.
β Additional flags
-h,--helpβ Show help message and exit. (the classic π)--ignore-errorsβ If an exception was raised during stub generation, continue to process any remaining modules instead of immediately failing with an error.--include-privateβ Include definitions that are considered private in stubs (with names such as_foowith single leading underscore and no trailing underscores).--export-lessβ Don't export all names imported from other modules within the same package. Instead, only export imported names that are not referenced in the module that contains the import.--include-docstringsβ Include docstrings in stubs. This will add docstrings to Python function and classes stubs and to C extension function stubs.--search-path PATHβ Specify module search directories, separated by colons (only used if--no-importis given).-o PATH,--output PATHβ Change the output directory. By default the stubs are written in the./outdirectory. The output directory will be created if it doesn't exist. Existing stubs in the output directory will be overwritten without warning. π back up your stuff, bestie, she is NOT asking permission.-v,--verboseβ Produce more verbose output. (she's yapping, lovingly π)-q,--quietβ Produce less verbose output. (she's giving nothing, on purpose π)
π§ ENVIRONMENT π§
MYPYPATHβ Additional module search path entries. The format is the same as the shell's$PATH: one or more directory pathnames separated by colons.
π SEE ALSO π
Full documentation is available online at: <https://mypy.readthedocs.io/en/latest/stubgen.html> or locally at: /usr/share/doc/mypy/html <file:///usr/share/doc/mypy/html> (requires mypy-doc package).
π«Ά Author
Jukka Lehtosalo and contributors πΈ
and that's the whole page, bestie β this one came from the Mypy source, version dated Aug 16, 2026, and it's officially a wrap. go forth and generate some stubs, you absolute icon π β¨periodt
π¦ where this page comes from
- ubuntu stonking: mypy 2.2.0-2 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms