stubpy
Generate accurate .pyi stubs for your Python package, with **kwargs and *args traced back to the parameters they forward to. Standard library only.

Features
| Feature |
What you get |
**kwargs / *args back-tracing |
Forwarded parameters written into the stub signature. |
| Source and runtime |
Parses the source and imports the module, merged per symbol; an import policy (--imports none) executes nothing. |
| Alias preservation |
Aliases like Length stay as written, with imports added. |
| Special classes |
Dedicated stubs for dataclasses, enums, TypedDict, NamedTuple, Generic, overloads. |
| Compiled extensions |
C API, Cython, pybind11 and nanobind modules, including docstrings. |
| Package generation |
One command stubs a whole tree, with __init__.pyi. |
| Check mode |
stubpy check exits 1 on missing or stale stubs. |
| Orphan pruning |
--prune removes stubs whose module no longer exists; stubs tracked in git are kept. |
| Hints |
@hint.signature, @hint.returns, @hint.ignore for what introspection misses. |
| Incremental merge |
Rewrites only the marked region and keeps hand edits. |
| Stub-only packages |
--stub-package writes a PEP 561 <pkg>-stubs distribution, ready to build. |
| Run cache |
--cache reuses the stubs of modules unchanged since the last run. |
| Type-checked stubs |
--typecheck runs mypy (or pyright) over the stubs; errors are reported per stub. |
| Docstring types |
--infer-types fills missing types from NumPy, Google and Sphinx docstrings. |
| Extension points |
Annotation handlers, forwarding resolvers and stub hooks. |
| Configuration |
[tool.stubpy] in pyproject.toml; flags override it. |
| Version branches |
sys.version_info / sys.platform blocks kept, as typeshed does. |
| Lazy attributes |
Names behind a module __getattr__ become explicit. |
| Pre-commit hooks |
stubpy-check and stubpy-generate, ready to use. |
CPython 3.10 to 3.15 (free-threaded 3.14t included), PyPy, GraalPy and Pyodide. No dependencies.
Install
pip install stubpy
Use
stubpy src/mypkg -o stubs # write stubs/mypkg/**/*.pyi
stubpy check --diff # exit 1 when a stub is stale
stubpy render mypkg/core.py # print one stub
import stubpy
report = stubpy.generate("src/mypkg", "stubs") # write stubs
print(stubpy.render("mypkg/core.py").text) # one module, nothing written
stubpy.check("src/mypkg", "stubs").ok # False when a stub is stale
Example
# 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)
stubpy render shapes.py:
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: ...
Configuration
# pyproject.toml
[tool.stubpy]
targets = ["src/mypkg"]
output_dir = "stubs"
With this, stubpy and stubpy check need no arguments.
CI and pre-commit
# .pre-commit-config.yaml
repos:
- repo: https://github.com/wzjoriv/stubpy
rev: 1.0.4
hooks:
- id: stubpy-check
# GitHub Actions step
- run: stubpy check --diff --typecheck
Documentation
Contributing
Setup, the scripts/dev.py commands and the project's conventions are in the
contributing guide.
License
MIT. See LICENSE.