Skip to content

Usage

Command line

Run it with uvx:

$ uvx unref
src/project/models.py:12:7: unused-class 'Account'
src/project/utils.py:40:5: unused-function 'parse_date'

unref checks the project that the current directory is in, or only the files and directories it is given:

uvx unref src scripts

unref exits with a non-zero status if anything is found, unless --exit-zero is given. See the rules for what it reports and how to ignore a finding, and the command line for its options.

pre-commit and prek

As a pre-commit or prek hook, alongside Ruff, which covers the unused imports and local variables that unref leaves out:

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.17.5
    hooks:
      - id: ruff-check
      - id: ruff-format
  - repo: https://github.com/Apakottur/unref-pre-commit
    rev: v0.5.1
    hooks:
      - id: unref

prek also reads .pre-commit-config.yaml, or the same hooks from its own prek.toml:

[[repos]]
repo = "https://github.com/astral-sh/ruff-pre-commit"
rev = "v0.17.5"
hooks = [{ id = "ruff-check" }, { id = "ruff-format" }]

[[repos]]
repo = "https://github.com/Apakottur/unref-pre-commit"
rev = "v0.5.1"
hooks = [{ id = "unref" }]

Set rev to the latest release, or let pre-commit autoupdate or prek autoupdate fill it in. The hook lives in unref-pre-commit, which installs the prebuilt wheel of unref from PyPI, rather than building it from source. Each release of unref is mirrored there as a tag of the same version.

The hook checks the whole project, not only the changed files: a definition is unused only if no file references it.

GitHub Actions

With --output-format github, the findings show as annotations on the files of a pull request:

jobs:
  unref:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: astral-sh/setup-uv@v10
      - run: uv sync # Optional: lets unref follow base classes from the dependencies, see `python`.
      - run: uvx unref --output-format github