Results and diagnostics¶
- class StubResult(text: str, diagnostics: tuple[Diagnostic, ...] = (), source: Path | None = None, module_name: str = '', output: Path | None = None)[source]¶
Bases:
objectThe stub of one module and what happened while building it.
Returned by
render();generate()andcheck()collect one per module in aReport.- Parameters:
text (str) – The stub text. For
generate()andcheck()this is the full file content, merged into the existing stub whenincrementalis on; empty when generation failed.diagnostics (tuple of Diagnostic, optional) – Everything recorded while building the stub, in order. Defaults to
().source (pathlib.Path or None, optional) – The
.pyfile (or package folder, for a placeholder__init__.pyi) the stub was built from. Defaults toNone.module_name (str, optional) – The module’s dotted name; loose scripts get a synthetic one. Defaults to
"".output (pathlib.Path or None, optional) – Where the stub is (or would be) written;
Noneforrender(). Defaults toNone.
See also
ReportThe outcome of a whole run.
Examples
>>> result = StubResult("x: int\n", module_name="mod") >>> result.ok, result.errors, result.warnings (True, [], [])
- diagnostics: tuple[Diagnostic, ...] = ()¶
- property errors: list[Diagnostic]¶
The ERROR diagnostics.
- property warnings: list[Diagnostic]¶
The WARNING diagnostics.
- property import_failures: list[Diagnostic]¶
The WARNINGs saying the module could not be imported and was stubbed from its source.
- class Report(results: list[StubResult] = <factory>, written: list[Path] = <factory>, stale: list[Path] = <factory>, excluded: list[tuple[~pathlib.Path, str]]=<factory>, action: str = 'generate', strict: bool = False, type_errors: int = 0, typecheck_note: str = '', pruned: list[Path] = <factory>)[source]¶
Bases:
objectThe outcome of
generate()orcheck().Iterating a report yields its
StubResultobjects. A run builds one itself; the fields are listed so a report can be read or constructed.- Parameters:
results (list of StubResult, optional) – One per module, in processing order. Defaults to an empty list.
written (list of pathlib.Path, optional) – Stubs written to disk (always empty for
check). Defaults to an empty list.stale (list of pathlib.Path, optional) – Stubs on disk that differ from what would be generated, or are missing (
checkonly). Defaults to an empty list.excluded (list of tuple of (pathlib.Path, str), optional) – Files a folder target skipped and the
excludepattern that matched; never counted as failures. Defaults to an empty list.action (str, optional) –
"generate"or"check". Defaults to"generate".strict (bool, optional) – Count results with WARNING diagnostics as failed. Defaults to
False.type_errors (int, optional) – Errors the type checker found in the stubs (
typecheck). Defaults to 0.typecheck_note (str, optional) – Why the type check did not run, if it was asked for and did not. Defaults to
"".pruned (list of pathlib.Path, optional) – Orphan stubs (
prune): removed bygenerate, reported (and counted stale) bycheck. Defaults to an empty list.
See also
StubResultThe stub of one module.
Examples
>>> report = Report(results=[StubResult("x: int\n")], action="check") >>> report.ok, len(report) (True, 1) >>> report.summary() 'Checked 1 stub: 0 stale, 0 failed.' >>> from stubpy.state.diagnostics import Diagnostic, Level, Stage >>> broken = Diagnostic(Level.WARNING, Stage.LOAD, "m.py", "Import failed, falling back to source analysis: E") >>> Report(results=[StubResult("x: int", (broken,))], written=[Path("m.pyi")]).summary() 'Generated 1 stub, 0 failed. 1 module stubbed from source only: import failed.'
- results: list[StubResult]¶
- property failed: list[StubResult]¶
Results with ERROR diagnostics (or WARNINGs in strict mode).
- summary() str[source]¶
Return a one-line summary, e.g.
'Generated 3 stubs, 0 failed.'.- Returns:
str – For
generate, the number of stubs written and failed; forcheck, the number checked, stale and failed. Modules stubbed from source only because their import failed, or the import policy blocked it, are counted after.
Examples
>>> Report().summary() 'Generated 0 stubs, 0 failed.' >>> Report(action="check").summary() 'Checked 0 stubs: 0 stale, 0 failed.'
- class Diagnostic(level: Level, stage: Stage, symbol: str, message: str)[source]¶
Bases:
objectAn immutable record of one issue detected during stub generation.
str(diagnostic)gives[LEVEL] (stage) symbol: message.- Parameters:
See also
DiagnosticCollectorAccumulates diagnostics during a run.
Examples
>>> d = Diagnostic(Level.WARNING, Stage.RESOLVE, ... "Child.__init__", "Could not resolve kwargs") >>> str(d) '[WARNING] (resolve) Child.__init__: Could not resolve kwargs'
- class Level(*values)[source]¶
Bases:
IntEnumSeverity of a
Diagnostic.Levels are ordered, so
d.level >= Level.WARNINGselects warnings and errors.- Variables:
INFO – Progress notes, shown with
verbose.WARNING – Something was approximated (for example an unresolvable
**kwargs); the stub is still usable. A failure understrict.ERROR – The module could not be stubbed; its
StubResultis notok.
Examples
>>> Level.ERROR > Level.WARNING True
- INFO = 10¶
- WARNING = 20¶
- ERROR = 30¶
Exit codes and the meaning of each level are in Exit codes and diagnostics.