python-pre-commit
Create or merge pre-commit configuration for uv-based Python projects while preserving existing hooks.
Files10
View on GitHubSKILL.mdskills/python-pre-commit/SKILL.md
GitHubPurpose
Produce a valid .pre-commit-config.yaml for a uv-based Python project with the canonical hook set (ruff, ruff-format, pre-commit-hooks). Keep slow hooks (pytest, pyright) on manual stage so they never block ordinary commits.
Trigger / When to use
Use this skill when:
- A uv-based Python project has no
.pre-commit-config.yamland needs one created from scratch. - An existing
.pre-commit-config.yamlin a uv project is missing canonical hooks and needs them merged in. - The user asks to "set up pre-commit", "add pre-commit hooks", or "configure commit hooks" for a uv Python project.
Do not use this skill when:
- The project does not use uv (pip, poetry, conda, or bare virtualenv).
- The request is about CI/CD pipeline configuration (GitHub Actions, pre-commit.ci cloud service).
- The request is about secrets scanning hooks (detect-secrets, gitleaks, etc.).
- The request asks for
.git/hooks/file creation rather than.pre-commit-config.yaml.
Inputs
- The target project directory path.
- Whether a
.pre-commit-config.yamlalready exists. - Whether pyright strict mode is used in the project (determines whether to include the optional pyright hook).
- The target ruff rev tag from https://github.com/astral-sh/ruff-pre-commit/releases (independent of the uv-installed ruff version).
Process
Check for existing config — look for
.pre-commit-config.yamlin the project root.- New config (file does not exist): create a fresh file with the full canonical hook set from
references/hooks-catalog.md. - Update existing config (file exists): read the current file, identify which canonical hooks are missing, and merge them in without overwriting hooks the user has already configured.
- New config (file does not exist): create a fresh file with the full canonical hook set from
Determine ruff rev — Use the
--ruff-versiondefault (v0.15.12) fromscripts/apply_precommit.py, or specify a custom tag. The rev comes from ruff-pre-commit releases and is independent of the uv-installed ruff version. Update manually when upgrading.Decide on optional hooks:
- Include the
pytesthook usingstages: [manual]. It must never be on the default stage. - Include the
pyrighthook withstages: [manual]only if the project uses pyright strict mode.
- Include the
Write the config — for a NEW file only, use
scripts/apply_precommit.pyto generate.pre-commit-config.yamlfrom the canonical template:# New config (preview first): uv run scripts/apply_precommit.py --dry-run # Write (default ruff version v0.15.12): uv run scripts/apply_precommit.py # Write with a specific older ruff version: uv run scripts/apply_precommit.py --ruff-version v0.11.9For an existing file, do not run the generator with
--forceto merge. Read the relevant blocks inreferences/hooks-catalog.mdand merge only missing hooks manually, preserving existing repos, revisions, arguments, stages, comments, and custom hooks. The generator supports whole-file replacement, not merging; use--forceonly for an explicitly requested replacement after a recoverable backup.If the script is not available, fall back to manually producing
.pre-commit-config.yamlfollowing the hook structure inreferences/hooks-catalog.md.After writing the config, apply the pyright decision from Step 3:
- Project uses pyright strict mode: append the pyright hook block from
references/hooks-catalog.mdto.pre-commit-config.yaml. - Project does not use pyright: no further action needed; the template already omits the pyright hook.
- Project uses pyright strict mode: append the pyright hook block from
Provide install command — after writing the file, output the commands the user must run manually:
uv run pre-commit install uv run pre-commit run --all-filesDo not run these commands automatically; report them for the user to execute.
Examples
- Positive: A new uv project has no
.pre-commit-config.yaml. The skill creates one with ruff, ruff-format, and pre-commit-hooks at default stage, and a pytest hook atmanualstage. Runninguv run pre-commit run --all-filesexits 0. - Negative: Adding pytest as a default-stage hook so every
git commitruns the full test suite. This blocks ordinary commits and defeats the purpose of fast pre-commit checks. pytest must always usestages: [manual].
Outputs
.pre-commit-config.yamlin the project root.- Install instructions for the user:
uv run pre-commit install uv run pre-commit run --all-files
Boundaries
- Does not cover CI/CD pipeline configuration (GitHub Actions, pre-commit.ci).
- Does not support non-uv toolchains (pip, poetry, conda).
- Does not manage secrets scanning hooks (detect-secrets, gitleaks, etc.).
- Does not write
.git/hooks/files directly; only produces.pre-commit-config.yaml. - Does not run
uv run pre-commit installoruv run pre-commit run --all-filesautonomously; those commands are the user's responsibility.
Validation
Required Checks
- Confirm the target project directory is known and reachable before writing any file.
- Confirm the project uses uv (e.g.,
pyproject.tomlpresent,uv.lockor[tool.uv]section); halt if not. - Check whether
.pre-commit-config.yamlalready exists to choose create vs. merge path. - Verify the ruff rev tag format is a valid semver tag (e.g.,
v0.15.12) before embedding it in the config.
Quality Checks
- After writing
.pre-commit-config.yaml, confirm it is valid YAML (no syntax errors). - Confirm all canonical hooks from
references/hooks-catalog.mdare present in the output file. - Confirm
pytesthook (if included) usesstages: [manual]— neverstages: [commit]. - Confirm
pyrighthook (if included) usesstages: [manual].
On Soft Fail
- Return with a clear explanation of what could not be completed; do not silently produce a partial config.
- If ruff rev cannot be determined, report the issue and use the documented pinned default rev (
v0.15.12) when no different version is requested; record that currency was not verified.
Failure Handling
Missing Context
- BLOCKED — if the target project directory cannot be determined, stop and ask before proceeding.
Ambiguous Requirement
- If the existing
.pre-commit-config.yamlhas conflicting hook entries (e.g., duplicaterepo:blocks for ruff), report the conflict to the user, show both versions, and ask which to keep before merging. - Do not silently overwrite or discard existing hooks; use
--forceonly when the user explicitly requests it.
Execution Limitation
- If
scripts/apply_precommit.pyis not available, fall back to manually producing.pre-commit-config.yamlfollowingreferences/hooks-catalog.md; note the fallback in the output. - If the ruff rev cannot be fetched from ruff-pre-commit releases (network unavailable), use the default rev (
v0.15.12) and explicitly note that the user should verify currency before committing. - If
pre-commitis not installed in the project environment, output the install instructions and note thatuv run pre-commit installwill fail until the dependency is added.
Local references
reference.md: index overview pointing to split topic files inreferences/.references/hooks-catalog.md: canonical YAML blocks for ruff, pre-commit-hooks, pytest, and pyright hooks.references/stage-matrix.md: stage decision table (commit/manual/push) and manual-run commands.references/uv-run-format.md:uv run+language: systementry rationale with correct and incorrect patterns.references/version-pinning.md: ruff rev alignment strategy, upgrade steps, and pre-commit-hooks rev guidance.examples.md: detailed scenarios for new setup, config merging, pyright strict projects, and anti-patterns.templates/pre-commit-config.yaml: canonical pre-commit config template with RUFF_VERSION placeholder; used as the base for new config creation in Process Step 4.scripts/apply_precommit.py: CLI script that readstemplates/pre-commit-config.yaml, substitutesRUFF_VERSION, and writes.pre-commit-config.yamlto the current directory. Supports--ruff-version,--dry-run, and--force.tests/: Unit tests forscripts/apply_precommit.py; verifies version substitution, dry-run (no disk write), force-overwrite, and guard-on-existing-file behaviors.