Pre-commit Hooks

KQCircuits uses pre-commit to run a set of automatic checks on staged files before each commit is finalized. This catches formatting issues, whitespace errors and malformed configuration files early, so that CI stays green and reviews can focus on the substance of a change.

The hook configuration lives at the repository root in .pre-commit-config.yaml. It currently runs:

The hooks only run on files under tests/, klayout_package/ and util/.

Installation

Activate the virtual environment you use for developing KQCircuits (see Developer Standalone module Setup) and install pre-commit. Either from pip directly:

pip install pre-commit

Or by using pinned versions for improved security (from repository root):

pip install -r klayout_package/python/requirements/pre-commit-requirements.txt

Then, from the repository root, install the git hooks:

pre-commit install

This creates the .git/hooks/pre-commit script. From now on the hooks run automatically every time you run git commit.

The first time the hooks run, pre-commit will download and cache the hook environments for each repository listed in .pre-commit-config.yaml. This may take a few seconds.

Running hooks manually

You can run the hooks against all files at any time (not just staged ones):

pre-commit run --all-files

To run a single hook by id, e.g. black:

pre-commit run black --all-files

To run the hooks on the currently staged files only:

pre-commit run

Skipping the hooks for a single commit is possible with git commit --no-verify, but please use this sparingly — the CI pipeline will re-run the same checks anyway.

Updating hooks

Hook versions in .pre-commit-config.yaml are pinned and are not updated automatically. Do not use pre-commit autoupdate: it would silently move every hook to the most recently tagged upstream release without any review.

Instead, treat hook updates like any other dependency update: pick the release you want, bump its rev on a branch, run pre-commit run --all-files to see what the new version changes, and get the change reviewed.

Security considerations

pre-commit builds an isolated environment for every hook by cloning the pinned repository revision and installing it, together with any additional_dependencies declared in the hook definition. In other words, it downloads and executes third-party code, so the hook configuration should be treated like a pinned dependency file:

  • rev entries are fixed revisions; they change only through reviewed updates as described above.

  • If a hook ever needs additional_dependencies, pin every entry to an exact version, e.g. types-requests==2.32.0.20240602. Never add unpinned or range-constrained dependencies.

PyCharm configuration

KQCircuits developers using PyCharm can use pre-commit directly from the IDE. The recommended approach is to install the hooks through PyCharm’s built-in terminal — they will then run automatically through PyCharm’s Git integration.

  1. Open the PyCharm terminal (Alt+F12 on Windows/Linux, Option+F12 on Mac).

  2. Install pre-commit in the project’s virtual environment pip install ..., see appropriate command from Installation.

  3. Install the git hooks from the repository root:

    pre-commit install
    
  4. Use PyCharm’s Git integration as usual (commit dialog, Ctrl+K / Cmd+K). The pre-commit hook is invoked automatically before the commit is finalized; if a hook fails, the commit is aborted and the output is shown in the terminal / Version Control tool window.

Note

Pre-commit only runs on files staged via git. PyCharm’s own “Commit” action stages the selected files first, so the hooks behave the same way whether you commit from the terminal or from the PyCharm dialog.