Results and diagnostics

class StubResult(text: str, diagnostics: tuple[Diagnostic, ...] = (), source: Path | None = None, module_name: str = '', output: Path | None = None)[source]

Bases: object

The stub of one module and what happened while building it.

Returned by render(); generate() and check() collect one per module in a Report.

Parameters:
  • text (str) – The stub text. For generate() and check() this is the full file content, merged into the existing stub when incremental is 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 .py file (or package folder, for a placeholder __init__.pyi) the stub was built from. Defaults to None.

  • 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; None for render(). Defaults to None.

See also

Report

The outcome of a whole run.

Examples

>>> result = StubResult("x: int\n", module_name="mod")
>>> result.ok, result.errors, result.warnings
(True, [], [])
text: str
diagnostics: tuple[Diagnostic, ...] = ()
source: Path | None = None
module_name: str = ''
output: Path | None = None
property ok: bool

True when no ERROR diagnostic was recorded.

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.

property import_blocked: bool

True when the import policy kept the module from being imported.

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: object

The outcome of generate() or check().

Iterating a report yields its StubResult objects. 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 (check only). Defaults to an empty list.

  • excluded (list of tuple of (pathlib.Path, str), optional) – Files a folder target skipped and the exclude pattern 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 by generate, reported (and counted stale) by check. Defaults to an empty list.

See also

StubResult

The 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]
written: list[Path]
stale: list[Path]
excluded: list[tuple[Path, str]]
action: str = 'generate'
strict: bool = False
type_errors: int = 0
typecheck_note: str = ''
pruned: list[Path]
property failed: list[StubResult]

Results with ERROR diagnostics (or WARNINGs in strict mode).

property ok: bool

True when nothing failed and nothing is stale.

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; for check, 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: object

An immutable record of one issue detected during stub generation.

str(diagnostic) gives [LEVEL] (stage) symbol: message.

Parameters:
  • level (Level) – Severity.

  • stage (Stage) – Pipeline stage that raised it.

  • symbol (str) – Name of the symbol being processed, e.g. "MyClass.__init__".

  • message (str) – Human-readable description.

See also

DiagnosticCollector

Accumulates 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'
level: Level
stage: Stage
symbol: str
message: str
class Level(*values)[source]

Bases: IntEnum

Severity of a Diagnostic.

Levels are ordered, so d.level >= Level.WARNING selects 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 under strict.

  • ERROR – The module could not be stubbed; its StubResult is not ok.

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.