CI and pre-commit

stubpy check is the CI command. It builds every stub in memory, compares it with the file on disk, writes nothing and sets the exit code.

$ stubpy check --diff
stale stubs\mypkg\core.pyi
--- a/stubs/mypkg/core.pyi
+++ b/stubs/mypkg/core.pyi
@@ -3,3 +3,4 @@
 class Client:
     def __init__(self, host: str, port: int = 80) -> None: ...
     def get(self, path: str) -> bytes: ...
+    def close(self) -> None: ...
Checked 2 stubs: 1 stale, 0 failed.
$ echo $?
1

A stub that does not exist yet counts as stale. With no arguments check acts on the targets and modules of [tool.stubpy], so the same file drives generate and check.

--prune also counts stubs that no module produces any more (left behind when a module is moved or split) as stale, and generate --prune removes them; stubs tracked by git are kept.

--typecheck also runs mypy (or pyright, when mypy is not installed) over the stubs on disk and reports each error as a WARNING on its stub; with --strict an error fails the run:

$ stubpy check --typecheck --strict
FAILED stubs\mypkg\core.pyi
    [WARNING] (typecheck) core.pyi: mypy: line 4: Name "Session" is not defined [name-defined]
Checked 2 stubs: 0 stale, 1 failed. 1 type error in the stubs.

Exit codes

Code

Meaning

0

Every stub is up to date and no module failed.

1

A stub is stale or missing, a module failed to load, or a warning was raised under --strict.

2

Usage error, an invalid config file, or a target that cannot be found.

See Exit codes and diagnostics.

pre-commit

The repository ships two hooks in .pre-commit-hooks.yaml. Both ignore the file names pre-commit passes and act on the configured targets.

Hook id

Runs

Use

stubpy-check

stubpy check --diff

Fail the commit when a stub is stale, and show why.

stubpy-generate

stubpy generate --quiet

Rewrite the stubs; the commit stops when a stub changed, so you can stage the result.

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/wzjoriv/stubpy
    rev: 1.7.1
    hooks:
      - id: stubpy-check

stubpy imports your code, and pre-commit runs hooks in an isolated environment. Either list your dependencies, or pass --imports none so nothing is imported:

hooks:
  - id: stubpy-check
    args: [--imports, none]
    additional_dependencies: [numpy]     # only if you keep the import

GitHub Actions

# .github/workflows/stubs.yml
name: Stubs
on: [push, pull_request]

jobs:
  stubs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install -e . stubpy
      - run: stubpy check --diff

The project is installed so that stubpy can import it. The step fails, and prints the diff, when a committed stub no longer matches the source.

From Python

import stubpy

report = stubpy.check(config=stubpy.load_config())
for path in report.stale:
    print("stale", path)
raise SystemExit(0 if report.ok else 1)

report.ok is True when nothing is stale and nothing failed. With strict a module that raised warnings counts as failed.