Command line¶
stubpy [generate] [TARGET ...] [-o OUT] [--dry-run] [--diff]
write stubs (the default command)
stubpy check [TARGET ...] [-o OUT] [--diff] exit 1 when a stub is missing or stale; writes nothing
stubpy render TARGET print one stub to stdout
stubpy docstrings {list,sync,check,verify} [STUB ...] [-m MODULE] [--no-config]
keep hand-written stubs in step with a module
stubpy --version
generate is implied when the first argument is not a sub-command, so stubpy mod.py
and stubpy generate mod.py are the same. python -m stubpy is equivalent.
Targets¶
A TARGET is any of:
a
.pyfile,a folder (searched recursively; a package folder uses its
__init__.py),a quoted glob such as
"src/**/*.py"(stubpy expands it even when the shell does not),a dotted module name such as
mypkg.core. It must be importable: installed, or onsys.path.
With no target, stubpy uses targets and modules from the config file.
render takes exactly one target: a file or a dotted module name. A glob is accepted
only when it matches a single file.
Output¶
-o / --output decides where stubs go:
|
Result |
|---|---|
ends in |
that exact file |
any other path |
a stub root: |
not given |
|
A .pyi output with several targets is a usage error.
Options¶
generate, check and render take these options. Each maps to a Configuration key and
overrides the config file.
Option |
Effect |
|---|---|
|
Which modules may be imported. By default ( |
|
Also allow |
|
Never import |
|
Stop with exit code 2 when one module takes longer than |
|
Import each module in a child process first, so one that exceeds |
|
Same as |
|
Stub names starting with |
|
Use docstrings as stub bodies. |
|
Fill missing types from NumPy, Google or Sphinx docstrings (see Inferred types). |
|
Keep nanobind array shape, order and device as |
|
Merge into existing stubs, keeping edits outside the markers. See Incremental updates. |
|
After a folder run, remove ( |
|
Type-check the stubs with mypy (or pyright); each error is a WARNING. See CI and pre-commit. |
|
Reuse the stubs of modules unchanged since the last run ( |
|
Write |
|
Keep |
|
Keep |
|
Stub every public name even when |
|
|
|
|
|
Copy |
|
Pattern of files or folders to skip. Repeatable; replaces the defaults (hidden files, build output, names Python cannot import). See Configuration. |
|
Pattern to skip on top of the defaults. Repeatable. |
|
List every skipped file and the pattern that matched. |
|
Oldest Python the stubs must import on ( |
|
Ignore |
|
Print every diagnostic, INFO included. |
|
Count warnings as failures. |
|
Print errors only. |
docstrings has its own options (-m MODULE, -q); see Compiled modules.
check --diff prints a unified diff per stale stub. generate --diff prints the diff of each
stub it changes and writes them; generate --dry-run writes nothing and lists the stubs that
would change (with --diff, their diffs). generate and check have -o; render
does not.
What is printed¶
generate prints wrote <path> per stub and a summary on stdout. check prints
stale <path> per stale stub (plus the diff with --diff) and a summary.
Diagnostics of failed modules go to stderr; --verbose prints all of them.
-q prints errors only.
$ stubpy render broken.py
FAILED broken.py
[ERROR] (ast_pass) broken.py: Source does not parse on this Python version: SyntaxError: invalid syntax (broken.py, line 3)
$ echo $?
1
A module that cannot be imported is not an error: it is stubbed from the source with a
WARNING (a failure under --strict). A file that does not parse is an ERROR.
A file written with syntax newer than the running Python (a type statement or
def f[T] on 3.11, except* on 3.10, except A, B: before 3.14) is skipped with a
WARNING instead, shown as skipped <path>; it is not written and does not fail the run.
Such a module is usually imported only behind a sys.version_info check. Run stubpy on
that Python or newer to stub it. Stubs of the modules that import it keep the version check, so a
type checker targeting the newer Python looks for the skipped module’s stub: generate stubs with
the newest Python the package supports.
Exit codes are listed in Exit codes and diagnostics.
Docstrings¶
stubpy docstrings keeps the hand-written stub of a compiled or Cython module in step with
it: it copies the module’s docstrings in, and reports names and parameters the two disagree on
(see Compiled modules).
$ stubpy docstrings list # module -> stub, and a count
$ stubpy docstrings sync pkg/_native.pyi # write the docstrings
$ stubpy docstrings check # exit 1 when a stub is out of date
$ stubpy docstrings verify # exit 1 when names or parameters differ
$ stubpy docstrings sync -m pkg._native # by module name
With no STUB and no -m, the stubs come from the [tool.stubpy.docstrings] table of the
config file (see Configuration). --no-config ignores the config file, so STUB or
-m is then required.
Exit codes: 0 up to date (or synced), 1 out of date (check) or differing (verify), 2 usage error or
a module that fails to import or parse. Every such failure is reported, not only the first.
Examples¶
$ stubpy mod.py # write mod.pyi next to mod.py
$ stubpy src/mypkg -o stubs # write stubs/mypkg/**/*.pyi
$ stubpy src --extend-exclude "tests"
$ stubpy mypkg.core -o stubs # by module name
$ stubpy check src/mypkg -o stubs --diff
$ stubpy render mypkg.core --imports none
$ stubpy --incremental # targets from [tool.stubpy]