generate_package¶
- generate_package(package_dir: str | Path, output_dir: str | Path | None = None, ctx_factory: Callable[[Path, Path], StubContext] | Callable[[], StubContext] | None = None, config: StubConfig | None = None) PackageResult[source]¶
Generate
.pyistubs for every.pyfile in package_dir.Walks package_dir recursively and calls
generate_stub()for each.pysource file. For every sub-directory that contains an__init__.py, the corresponding__init__.pyiis created under output_dir (if it was not already produced bygenerate_stub).Files matching any pattern in
config.excludeare skipped. Files that fail with an exception or ERROR-level diagnostics are recorded inPackageResult.failedand processing continues.- Parameters:
package_dir (str or Path) – Root of the package to process.
output_dir (str or Path or None) – Directory where stubs are written. The subdirectory structure of package_dir is reproduced under output_dir. When
None(default), stubs are written alongside the source files.ctx_factory (callable or None) –
Called to produce a fresh
StubContextfor each file. Two signatures are accepted:ctx_factory()— called with no arguments (backward compatible).ctx_factory(source_path, output_path)— called with the absolutePathof the source.pyfile and the destination.pyipath, allowing per-file customisation (e.g. differentexecution_modefor slow modules, extra exclude patterns for generated files, custom annotation handlers).
When
None, a context derived from config (or a default config) is used.config (StubConfig or None) – Configuration applied when ctx_factory is
None. When both areNone,StubConfigdefaults are used.
- Returns:
PackageResult – Contains
stubs_written(success paths) andfailed((path, diagnostics)pairs for errored files).- Raises:
FileNotFoundError – If package_dir does not exist on disk.
Examples
>>> from stubpy import generate_package >>> result = generate_package("mypackage/", "stubs/") >>> print(result.summary()) Generated 8 stubs, 0 failed.
See also
Public API reference — overview of all public names.