Metadata-Version: 2.5
Name: depbisect
Version: 0.1.0
Summary: Find the dependency update that broke your Python tests: delta debugging over lockfile changes.
Project-URL: Homepage, https://github.com/DevIntense/depbisect
Project-URL: Issues, https://github.com/DevIntense/depbisect/issues
Author: Dennis Kiwitz
License-Expression: MIT
License-File: LICENSE
Keywords: bisect,delta-debugging,dependencies,lockfile,pip,poetry,regression,testing,uv
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Debuggers
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.9
Requires-Dist: packaging>=22
Requires-Dist: tomli>=1.1; python_version < '3.11'
Description-Content-Type: text/markdown

# depbisect

**Find the dependency update that broke your Python tests.**

You ran `uv lock --upgrade`, merged a Renovate PR, or CI rebuilt with fresh wheels overnight.
Nineteen packages changed. The tests fail. *Which* change did it?

`depbisect` answers that automatically. It builds throwaway environments that mix the old and the
new lockfile, runs your test command in each of them, and narrows the difference down with
[delta debugging](https://www.st.cs.uni-saarland.de/papers/tse2002/). You get the culprit, the
exact release that introduced the failure, and the smallest verified fix.

```console
$ depbisect -- pytest -q
depbisect 0.1.0
  good : git:HEAD:uv.lock (19 packages)
  bad  : uv.lock (21 packages)
  diff : 19 changed (2 added, 17 upgraded)
  project: installing . in editable mode (disable with --no-project)
  test : pytest -q
  env  : uv · python 3.13.18 · .depbisect

[ 1] baseline good set as locked ........................ PASS   0.9s
[ 2] baseline bad set as locked ......................... FAIL   0.8s
[ 3] cause    good + 10 of 19 changes ................... PASS   0.7s
[ 4] cause    good + 10 of 19 changes ................... FAIL   0.7s
[ 5] cause    good + 8 of 19 changes .................... PASS   0.6s
[ 6] cause    good + 4 of 19 changes .................... FAIL   0.6s
[ 7] cause    good + typing-extensions + typing-inspection .. PASS   0.5s
[ 8] cause    good + urllib3 + werkzeug ................. FAIL   0.5s
[ 9] cause    good + urllib3 ............................ PASS   0.4s
[10] cause    good + werkzeug ........................... FAIL   0.4s
[11] fix      bad, revert werkzeug ...................... PASS   1.0s
[12] version  good + werkzeug 3.1.0 ..................... FAIL   0.7s
[13] version  good + werkzeug 3.0.3 ..................... FAIL   0.4s
[14] version  good + werkzeug 3.0.1 ..................... FAIL   0.4s
[15] version  good + werkzeug 3.0.0 ..................... FAIL   0.4s

Culprit:
  werkzeug 2.3.8 -> 3.1.9  major upgrade
  Applying this change to the good set is enough to reproduce the failure.

Release bisection:
  werkzeug 3.0.0 is the first release that fails (last good: 2.3.8)
  release page: https://pypi.org/project/werkzeug/3.0.0/

Smallest fix for the bad set:
  werkzeug==2.3.8
  or as a constraint: werkzeug<3.0.0
  (verified: the bad set passes with this revert)

Failure output (last lines)
  |     from demoapp import app
  | src/demoapp/__init__.py:1: in <module>
  |     from flask import Flask, jsonify
  | .depbisect/venv/lib/python3.13/site-packages/flask/__init__.py:5: in <module>
  |     from .app import Flask as Flask
  | .depbisect/venv/lib/python3.13/site-packages/flask/app.py:30: in <module>
  |     from werkzeug.urls import url_quote
  | E   ImportError: cannot import name 'url_quote' from 'werkzeug.urls' (/tmp/demoapp/.depbisect/venv/lib/python3.13/site-packages/werkzeug/urls.py)
  | =========================== short test summary info ============================
  | ERROR tests/test_health.py
  | !!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
  | 1 error in 0.22s

19 changes, 15 test runs, 9s
```

That is a real run: a Flask 2.2 app after `uv lock --upgrade`, with no configuration at all.
Nineteen changed packages, 15 test runs, 9 seconds.

## Why this exists

* `git bisect` searches **commits**. When one commit (`uv lock --upgrade`, a Dependabot group
  PR, a rebuilt Docker image) changes many packages at once, it can only point at that commit.
* Tools such as `where-broke` or `script-bisect` search the history of **one** package you
  already suspect. With twenty changes you do not know which one to suspect.
* For npm there is `npm-bisect`. For Python lockfiles there was nothing that searches the
  *set* of changed packages, so people do it by hand: pin half, reinstall, rerun, repeat.

`depbisect` automates exactly that manual loop, and does it better than by hand:

| | |
|---|---|
| **Interactions** | Some failures need two changes together (new numpy *and* old pandas). Delta debugging finds the minimal combination, not just single packages. |
| **Consistent mixes** | Applying `pydantic 2.10` without its exact `pydantic-core` pin would fail for the wrong reason. depbisect reads the Requires-Dist metadata of both environments and pulls in required partner changes automatically. |
| **Exact release** | Once a single culprit is known, its releases in between are bisected too, so you learn that `werkzeug 3.0.0` broke it, not just "something up to 3.1.9". |
| **Verified fix** | The suggested revert is run against the bad set before it is shown to you. |
| **Fast** | With [uv](https://github.com/astral-sh/uv) every trial is a `uv pip sync` from the local cache, typically under a second. Identical package sets are never tested twice. |

## Install

`depbisect` needs Python 3.9 or newer. [uv](https://docs.astral.sh/uv/) is strongly recommended
(it is used automatically when found); without it depbisect falls back to `venv` + `pip`, which
works but is much slower.

```bash
# run without installing
uvx --from git+https://github.com/DevIntense/depbisect depbisect -- pytest

# or install it
pipx install git+https://github.com/DevIntense/depbisect
pip install git+https://github.com/DevIntense/depbisect
```

## Usage

```text
depbisect [--good SPEC] [--bad SPEC] [options] -- <test command>
```

The test command decides: exit code `0` means pass, `125` means "cannot test this one, skip it"
(as in `git bisect run`), anything else means fail. A single argument is run through the shell,
so `-- "pytest -x && python smoke.py"` works.

### Zero configuration

In a git repository with a lockfile, the defaults are the lockfile as committed at `HEAD`
(**good**) and the lockfile in your working tree (**bad**). This is the
"I just upgraded and now it is broken" case:

```bash
uv lock --upgrade        # or: poetry update, pip-compile -U, ...
pytest                   # fails
depbisect -- pytest -x   # tells you why
```

### Where the package sets come from

| Spec | Meaning |
|---|---|
| `uv.lock` | uv lockfile, with markers, forks, extras and dependency groups resolved for the test interpreter |
| `poetry.lock` | Poetry lockfile |
| `pylock.toml`, `pylock.*.toml` | standard lockfile ([PEP 751](https://peps.python.org/pep-0751/)) |
| `Pipfile.lock` | Pipenv lockfile |
| `requirements.txt` (any other name) | exact pins as written by `pip freeze`, `uv pip compile` or `pip-compile`, hashes and markers allowed |
| `git:REF:PATH` | any of the above as it was at a git revision, e.g. `git:main:uv.lock` or `git:v1.4.0:requirements.txt` |
| `env:PATH` | the packages installed in a virtualenv or interpreter, e.g. `env:.venv` or `env:/usr/bin/python3` |
| a directory | the first lockfile found in it |

Mix them freely: `--good git:main:uv.lock --bad env:.venv` compares what main locks with what
is actually installed in your venv.

### Options

| Option | |
|---|---|
| `-g, --good SPEC` / `-b, --bad SPEC` | the working and the failing package set |
| `--python PY` | interpreter or version for the test environments (`3.12`, `/usr/bin/python3.11`) |
| `-e, --editable PATH` | install your own project in editable mode (repeatable). Detected automatically for `uv.lock` and Poetry projects; `--no-project` turns that off |
| `--with REQ` | extra requirement for every run, e.g. `--with pytest` when your lockfile has no test runner |
| `--fail-pattern REGEX` | only count a failure if the output matches. Other failures count as *unresolved*, so an unrelated breakage in a mixed set cannot mislead the search |
| `--timeout SECONDS` | time limit per test run; a hang counts as a failure |
| `--repeat N` | run the test N times per trial, any failure counts (for flaky regressions) |
| `--phase cause\|fix\|both` | only search what breaks it, only search the smallest repair, or both (default) |
| `--no-versions` | skip the release bisection of the culprit |
| `--allow-inconsistent` | also test mixed sets whose declared requirements cannot be satisfied |
| `--backend auto\|uv\|pip` | how environments are built |
| `--workdir DIR` | scratch directory for the environment and the per trial logs (default `.depbisect`, git ignored automatically) |
| `--json FILE` / `--markdown FILE` | machine readable and Markdown reports. On GitHub Actions the Markdown report is also added to the job summary |
| `--list` | only print the changes between good and bad |
| `-v, --verbose` / `--no-color` | show the reason for every trial / plain output |

Exit codes: `0` culprit found, `1` error, `2` usage error, `3` not reproducible (the good set
does not pass or the bad set does not fail), `4` inconclusive.

## Recipes

**Narrow the failure down to one error.** Mixed environments can fail in ways that have nothing
to do with your problem. Tell depbisect what your problem looks like:

```bash
depbisect --fail-pattern "AttributeError: .*has no attribute 'url_quote'" -- pytest tests/test_api.py
```

**A Dependabot or Renovate PR is red.**

```bash
git fetch origin main
depbisect --good git:origin/main:uv.lock --bad uv.lock -- pytest -x
```

**It works on my machine but not in the fresh CI image.**

```bash
pip freeze > ci-freeze.txt   # in CI, then download it
depbisect --good env:.venv --bad ci-freeze.txt -- pytest -x
```

**Private package index.** Indexes listed in the lockfile or in `--index-url` /
`--extra-index-url` lines of a requirements file are passed on to the installer.

**Your own code must be installed.** `uv.lock` and Poetry projects are detected and installed in
editable mode automatically. For anything else, pass `-e .` (or `-e packages/core -e packages/api`
in a monorepo).

## GitHub Action

Run depbisect when the tests of a dependency update PR fail and get the answer in the job summary:

```yaml
on: pull_request

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - uses: actions/setup-python@v7
        with:
          python-version: "3.12"
      - run: pip install uv && uv sync --locked && uv run pytest
      - name: Which dependency broke it?
        if: failure() && github.event_name == 'pull_request'
        uses: DevIntense/depbisect@v0.1.0
        with:
          command: pytest -x
```

Inputs: `command` (required), `good` (default: the lockfile at the PR's base commit), `bad`
(default: the lockfile in the checkout), `lockfile` (default: detected), `python`, `args`
(any other options). Outputs: `status`, `culprit` (for example `werkzeug 2.3.8 -> 3.1.9`),
`first-bad-version` and `report` (path of the JSON report).

## How it works

1. **Baselines.** The good set must pass and the bad set must fail, otherwise there is nothing
   to search and depbisect stops with exit code 3 and the logs.
2. **Cause.** `ddmin` (Zeller and Hildebrandt, 2002) over the changed packages applied to the good
   set. For a single culprit this behaves like a binary search; for culprits that only fail
   together it returns the minimal combination. Every selection is first *closed* over declared
   requirements so that mixed environments stay installable and consistent.
3. **Fix.** The same search in the other direction: the smallest set of reverts that makes the
   bad set pass. Usually that is the culprit itself, but not always (for an interaction, reverting
   one of the two is enough).
4. **Release.** For a single upgraded culprit, the releases between the good and the bad version
   are listed from the package index (non-yanked, no pre-releases unless an end is one) and
   bisected. Releases that cannot be installed for your interpreter are skipped like
   `git bisect skip`, and reported if they leave the answer ambiguous.

Each trial is one `uv pip sync` of an exact package set into a single scratch virtualenv
(`pip install --no-deps` into a fresh venv with the pip backend), followed by your command with
that environment activated. Every trial has a log in `.depbisect/logs/` with the exact package
list, the installer output and the test output.

## Limitations

* Python only. Package sets must be fully pinned; ranges in a requirements file are treated as
  fixed opaque values.
* The failure must be reproducible on your machine and interpreter. Use `--repeat` for flaky
  failures and `--fail-pattern` to ignore unrelated ones.
* Lockfile entries for other platforms are filtered with the markers of the test interpreter, so
  a Windows only failure has to be bisected on Windows.
* Mixed environments are checked against declared requirements only. A package that is
  incompatible without saying so can still produce a failure in a mix that does not exist in
  either lockfile; `--fail-pattern` is the safeguard.
* Release bisection queries the [JSON Simple API](https://peps.python.org/pep-0691/) of the
  index (PyPI and most modern indexes).

## Development

```bash
uv venv && uv pip install -e . pytest ruff
pytest                                   # unit tests, offline, < 1 s
DEPBISECT_INTEGRATION=1 pytest -m integration   # real environments, needs network and uv
ruff check src tests
```

The example in [`examples/flask-werkzeug3`](examples/flask-werkzeug3) reproduces the real
Flask 2.2 / Werkzeug 3.0 breakage from September 2023.

## License

MIT
