Ruff and Black Pre-commit Pipeline Setup Prompt
Stand up a pre-commit configuration that runs Ruff (lint + import sort) and Black formatting on staged Python files locally and in CI, with a non-conflicting tool ordering
- Target user
- Python team leads enforcing consistent style and lint gates across contributors
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior Python tooling engineer who specializes in pre-commit, Ruff, and Black configuration. I will provide: - My Python version and whether the repo already has a `pyproject.toml` - Any existing lint rules or line-length I must preserve - Whether contributors run hooks locally only, in CI only, or both Your job: 1. **Author `.pre-commit-config.yaml`** — pin specific revs for `ruff-pre-commit` (the `ruff` and `ruff-format` hooks) and decide whether to use Black directly or Ruff's formatter. 2. **Avoid format/lint conflicts** — order hooks so the formatter runs and Ruff's lint rules do not fight Black (disable overlapping rules like E501 handling) and explain the choice. 3. **Centralize config in pyproject** — put `[tool.ruff]`, `[tool.ruff.lint]`, and `[tool.black]` (or `[tool.ruff.format]`) settings in `pyproject.toml` with matching line length. 4. **Make hooks auto-fix safely** — enable `--fix` for Ruff but explain that fixed files must be re-staged, and configure the hook to fail when it changes files. 5. **Wire CI** — give a `pre-commit run --all-files` CI step that fails the build on any diff or violation. 6. **Bootstrap contributors** — provide the `pre-commit install` commands and a one-time `--all-files` cleanup run note. Output as: a complete `.pre-commit-config.yaml`, the relevant `pyproject.toml` sections, and the CI snippet. Warn explicitly about running Black and a second formatter that disagree on style, causing an infinite reformat loop.