generate_stub¶
- generate_stub(filepath: str, output_path: str | None = None, ctx: StubContext | None = None) str[source]¶
Generate a
.pyistub file for the Python source at filepath.A fresh
StubContextis created when ctx isNone, making this function fully re-entrant. Pass an explicit ctx to supply customStubConfigoptions (e.g.include_private=True,execution_mode=ExecutionMode.AST_ONLY) or to inspect diagnostics after the call.Execution modes¶
RUNTIME(default)Execute the module; full introspection available.
AST_ONLYParse the AST only — no module execution. Safer for modules with import-time side effects; live types will be
None.AUTOExecute when possible; fall back to AST-only on load failure.
- param filepath:
Path to the
.pysource file.- type filepath:
str
- param output_path:
Where to write the
.pyifile. Defaults to the same directory and stem as filepath with a.pyiextension.- type output_path:
str, optional
- param ctx:
Pre-configured context. Created fresh when
None.- type ctx:
StubContext, optional
- returns:
str – Full stub content as a string, identical to what is written to disk.
- raises FileNotFoundError:
If filepath does not exist.
- raises ImportError:
If the source file cannot be loaded as a module (
RUNTIMEmode only;AUTOmode falls back to AST-only instead of raising).
See also
generate_packageBatch generation for a whole package directory.
stubpy.loader.load_moduleStage 1 — module loading.
stubpy.ast_pass.ast_harvestStage 2 — AST pre-pass.
stubpy.symbols.build_symbol_tableStage 5 — symbol table.
stubpy.emitter.generate_class_stubClass stub emission.
stubpy.emitter.generate_function_stubFunction stub emission.
stubpy.emitter.generate_variable_stubVariable stub emission.
stubpy.emitter.generate_alias_stubAlias/TypeVar stub emission.
stubpy.emitter.generate_overload_group_stubOverload group stub emission.
See also
Public API reference — overview of all public names.