Configuration¶
stubpy reads its options from, in order of precedence:
command-line flags,
the nearest
stubpy.toml, or a[tool.stubpy]table in the nearestpyproject.toml,the defaults.
The search starts at the first existing target (or the working folder) and walks upward.
In one folder stubpy.toml wins over pyproject.toml. --no-config skips the search.
# pyproject.toml
[tool.stubpy]
targets = ["src/mypkg"] # what `stubpy` and `stubpy check` act on with no paths
modules = ["mypkg._native"] # importable modules to stub by name
output_dir = "stubs" # stub root
imports = ["mypkg"] # the standard library and mypkg may be imported
extend_exclude = ["**/_vendor/*"]
In stubpy.toml the keys are top-level; there is no [tool.stubpy] header.
# stubpy.toml
targets = ["src/mypkg"]
output_dir = "stubs"
Keys are the field names of Config; dashes are accepted
(include-private). An unknown key is an error with the closest valid name:
stubpy.toml: Unknown stubpy option 'incldue_private'; did you mean 'include_private'?
Relative targets and output_dir resolve against the folder that holds the file, not
the working directory. On Python 3.10 without tomli a small built-in parser reads the
subset stubpy needs (sections, strings, booleans, numbers, string arrays, comments).
Keys¶
Key |
Type |
Default |
Description |
CLI |
|---|---|---|---|---|
|
|
|
Which modules may be imported, so that the live objects can be merged with the source. |
|
|
list of |
|
Module names that are never imported; wins over |
|
|
number |
|
Seconds one module may take to import; a longer import stops the run with exit code 2 and the stack of every thread. |
|
|
bool |
|
Import each module in a child process first; a module that does not finish within |
|
|
|
|
Stub names that start with |
|
|
|
|
When a module defines |
|
|
|
|
Use each symbol’s docstring as its stub body instead of |
|
|
|
|
Fill in missing parameter and return types from NumPy, Google or Sphinx docstrings. Safe return types and forwarded keywords are inferred without it; see Inferred types. |
|
|
|
|
Keep the shape, order and device of nanobind arrays as |
|
|
|
|
Wrap the stub in |
|
|
|
|
After a folder run, remove ( |
|
|
|
|
Type-check the written stubs with mypy (or pyright) and report each error as a WARNING on its stub; see CI and pre-commit. |
|
|
|
|
Reuse the stub of each module whose source and package imports are unchanged since the last run. The cache is reset by a different config, stubpy or Python. Installed packages are not tracked: clear the cache folder after upgrading a dependency the stubs import. |
|
|
|
|
Folder of the run cache, relative to the working folder. |
|
|
|
|
Write each top-level package as a stub-only distribution |
|
|
|
|
Expand |
|
|
|
|
Expand |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
list of |
Patterns of files and folders a folder target skips. Setting it replaces the defaults; |
|
|
|
list of |
|
Patterns added to |
|
|
|
|
Stub root used when no output is given. Without it each stub is written next to its source. |
|
|
list of |
|
Files, folders or globs to act on when none are given. |
positional |
|
list of |
|
Dotted module names to act on when none are given. |
positional |
|
|
|
Report INFO diagnostics on the command line. |
|
|
|
|
Count WARNING diagnostics as failures (exit code 1). |
|
|
|
|
Oldest Python the stubs must import on. |
|
Booleans must be real booleans (true), not strings. A single string is accepted where a
list is expected (targets = "src/mypkg").
What gets skipped¶
A folder target skips these by default:
Pattern |
Skips |
|---|---|
|
Hidden files and folders. |
|
Caches, build output, environments and tool folders. |
|
Packaging metadata folders. |
|
Built-in rule: folders and module files whose name Python cannot import
( |
A pattern that contains / matches the path relative to the folder target; one without
matches any single folder or file name on the path. exclude replaces the defaults, and
extend_exclude adds to them:
[tool.stubpy]
extend_exclude = ["tests", "mypkg/_vendor/*"] # on top of the defaults
# or replace the defaults; this one keeps hidden folders in:
# exclude = ["__pycache__", "build", "dist", ".venv", "venv", "*.egg-info",
# ".git", ".tox", ".nox", "<non-identifier>"]
On the command line --exclude replaces the defaults too and --extend-exclude adds to
them. A file named directly as a target is always processed, so a hidden or oddly named file
passed explicitly works as a loose script. Skipped files never count as failures in
generate or check.
--show-excluded lists each skipped path with the pattern that matched, and --verbose
prints one INFO line listing them. Report.excluded holds the
(path, pattern) pairs.
$ stubpy generate . --show-excluded
excluded .hidden\x.py (.*)
excluded build\y.py (build)
excluded mypkg\core copy.py (<non-identifier>)
excluded mypkg\foo-bar.py (<non-identifier>)
wrote mypkg\__init__.pyi
wrote mypkg\core.pyi
Generated 2 stubs, 0 failed.
Docstring targets¶
stubpy docstrings (see Command line and Compiled modules) reads the stubs it acts on from
the [tool.stubpy.docstrings] table when it gets no STUB and no -m. Paths are relative
to the config file.
[tool.stubpy.docstrings]
packages = ["mypkg"] # folders searched for *.pyx / compiled modules with a same-stem .pyi beside them
import_root = "src" # module names are relative to it; default: found by walking up __init__.py folders
overrides = {"mypkg.core" = "stubs/core.pyi", "mypkg._old" = ""}
overrides maps a module to a stub path; an empty string drops a discovered module. In
Python the table is Config(docstrings={...}), coerced to
DocstringTargets.
From Python¶
Config is a frozen dataclass; every field has a default.
import stubpy
from stubpy import Config
cfg = Config(imports="none", include_private=True) # strings coerce to the enums
cfg2 = cfg.replace(union_style="legacy") # a validated copy
cfg3 = Config.from_dict({"include-private": True}) # a [tool.stubpy] table
stubpy.generate("src/mypkg", config=cfg)
stubpy.generate(config=stubpy.load_config()) # targets from the file
A bad value raises ValueError (unknown enum value or key) or TypeError (wrong type),
naming the allowed values.
How union_style, alias_style, target_python and version_branches shape the
stub is shown in Output style.