stubpy¶
stubpy generates .pyi stub files for Python modules and packages. It
traces **kwargs and *args back to the parameters they forward to, so
the stub lists the real signature instead of **kwargs. It uses only the
standard library.
In 30 seconds
Given this module:
# shapes.py
class Shape:
def __init__(self, color: str = "black", opacity: float = 1.0): ...
class Rect(Shape):
def __init__(self, width: float, height: float, **kwargs):
super().__init__(**kwargs)
From the command line:
$ pip install stubpy
$ stubpy render shapes.py
From Python:
import stubpy
print(stubpy.render("shapes.py").text)
Both print the stub, with Rect.__init__ expanded:
class Shape:
def __init__(self, color: str = 'black', opacity: float = 1.0) -> None: ...
class Rect(Shape):
def __init__(
self,
width: float,
height: float,
*,
color: str = 'black',
opacity: float = 1.0,
) -> None: ...
Features
Feature |
What it does |
Example |
|---|---|---|
|
Expands |
|
Source and runtime |
Parses the source and imports the module, using whichever says more; an import policy limits what may be imported, down to nothing. |
|
Alias preservation |
Keeps |
|
Special classes |
Dataclass, |
|
Package generation |
Stubs a whole tree, mirrors its layout under a stub root and adds |
|
Check mode |
Fails when the stubs on disk differ from what would be generated; |
|
Hints |
|
|
Incremental merge |
Replaces only the marked region of an existing stub and keeps your edits. |
|
Configuration |
|
|
Stub-only packages |
Writes each package as a PEP 561 |
|
Run cache |
Reuses the stub of every module unchanged since the last run. |
|
Docstring types |
Fills missing types from NumPy, Google and Sphinx docstrings. |
|
Extension points |
Register handlers for your own annotation types, name forwarding targets chosen at runtime, and post-process each stub. |
Why stubpy¶
*args/**kwargsback-tracingOther generators leave
*argsand**kwargsas written. stubpy follows the forwarding call and lists the parameters the callee accepts, so editors can complete them.- Source and runtime together
The source is always parsed; the module is also imported unless the import policy forbids it (
--imports none, or--no-import). The live objects give inherited methods and evaluated types, the source gives aliases, defaults and decorators as written, and the two are merged per symbol. If an import fails, that module is stubbed from the source with a warning.- Zero dependencies, many runtimes
The package depends on nothing but the standard library. Supported runtimes, all tested in CI: CPython 3.10 to 3.15, free-threaded CPython 3.14t, PyPy 3.11, GraalPy 25 and Pyodide (WebAssembly). See Installation.
- Alias preservation
An alias such as
types.Lengthstaystypes.Lengthin the stub, so the stub reads like the source.- CI check mode
stubpy checkwrites nothing and exits1when a stub is missing or stale. A pre-commit hook and a GitHub Actions step are in CI and pre-commit.