Architecture¶
Every stage up to StubResult is a pure function of its inputs. Only the last
step touches the disk. That is why render can return text without writing anything and
check can compare without changing a file.
Data flow¶
flowchart TB
subgraph IN["Targets"]
PY[".py file / package folder"]
MOD["dotted module name"]
EXT["compiled .pyd / .so"]
end
subgraph LOAD["analysis.loader"]
IMP["package-aware import<br/>root on sys.path, restore after"]
SRC["source text when available"]
end
PY --> IMP & SRC
MOD --> IMP
EXT --> IMP
SRC --> HV["analysis.harvest<br/>AST pre-pass: defs, decorators,<br/>forwarding calls, __all__, literals"]
IMP --> LIVE["live module objects"]
LIVE --> CMP["compiled.signatures<br/>text sig / pybind11 / nanobind / Cython"]
HV --> ST["analysis.symbols<br/>SymbolTable"]
LIVE --> ST
CMP --> ST
ST --> RS["resolve<br/>**kwargs / *args back-tracing<br/>MRO, cls(), self.m(), functions"]
RS --> EM["emit<br/>classes, functions, variables,<br/>aliases, overloads, special classes"]
EM --> HD["core.header<br/>typing / cross-module imports"]
HD --> RES[("StubResult<br/>text + diagnostics")]
RES --> OUT{"core.pipeline"}
OUT -->|generate| W["write .pyi"]
OUT -->|incremental| M["merge into existing .pyi"]
OUT -->|check| C["stale-stub report"]
OUT -->|render| P["return text"]
CFG["config<br/>Config / pyproject"] -.-> LOAD & RS & EM & OUT
HINT["hint decorators<br/>stubpy.hint.signature / ignore"] -.-> ST
Compiled modules (EXT) take the compiled.signatures path: signatures come from the
binding tool’s metadata instead of source; see Compiled modules.
How a stub is built¶
Resolve targets. Files, folders, globs and dotted names become a list of modules; folders skip the excluded paths (see Configuration). Modules are grouped by their import root so each group loads against the right
sys.path.Load. Unless the import policy forbids it, the module is imported with its package root on
sys.path(restored afterwards). Asys.meta_pathguard refuses modules the policy (imports,deny_imports) does not allow, and is removed afterwards. If the import fails, that module continues from the source alone, with a WARNING; if the policy blocks it, with an INFO. The source is always read.Harvest. An AST pass records definitions, decorators,
__all__, literal values, hints and the calls that forward**kwargs/*args. In a package run every file is parsed and harvested once, before the first stub, so modules can see what their siblings import; the pipeline then reuses that harvest and keeps only the small facts it needs from each tree (imports, lazy__getattr__names), released once the file is rendered.Symbol table. Live objects and harvested facts are merged per symbol and per parameter into one
SymbolTableof classes, functions, variables, aliases and overload groups.Resolve parameters. For each signature that takes
**kwargsor*args, the forwarding target is found (MRO,cls(...),self.method(...), a function) and its parameters are merged in. See Forwarding *args and **kwargs.Emit. Each symbol is written as stub text: classes (with dedicated forms for dataclasses, enums,
TypedDictandNamedTuple), functions, variables, aliases and overloads.Header. The
typingand cross-module imports the text needs are added at the top.Result. Text and diagnostics are returned as a
StubResult.Write, merge or compare.
generatewrites the file. Withincrementalthe text is first merged into the existing stub.checkcompares it with the file on disk.
Sub-package map¶
Package |
Contents |
|---|---|
|
|
|
The run state every later layer shares: the run context ( |
|
The loader ( |
|
|
|
Stub text for classes, functions, variables, aliases and overloads; signature assembly
( |
|
Results, target resolution ( |
|
The |
|
Compiled-module support: detection, runtime introspection, binding-tool signatures, C API and Cython sources, docstring sync. |
|
Incremental-update markers; |
|
The user-facing decorators and the advanced namespace. |
Constants live next to their users: stubpy._constants holds the ones shared by several
sub-packages (and the ones stubpy.hint needs), and each sub-package keeps its own in a
private _constants module.
Layering rules¶
Dependencies flow one way: config → state → analysis → resolve → emit → core →
cli. compiled sits beside analysis and resolve: the two may import each other,
and neither imports emit, core or cli.
A module or sub-package whose name starts with
_belongs to its parent package only. Anything another package imports has a public name.A package never imports a later one. The run state (
state) sits directly aboveconfigso that every later layer can share one context and one diagnostic collector.configimports no other sub-package.The user API is re-exported from
stubpy/__init__.pywith__all__.
tests/test_package_boundaries.py and tests/test_package_exports.py check these
rules.