Logo: Py Git Hooks

Py Git Hooks

Communautéit
l-mb
py-git-hooks

Set up git pre-commit hooks to run ruff, mypy, and basedpyright before commits. Use when configuring automated quality checks in git workflow.

Add to TypingMind

Iwwersiicht

Editeurl-mb
Repositorypython-refactoring-skills
Skill-Nummpy-git-hooks
Stären
57
Forks
1
Matgeliwwert Dateien
1
LizenzMIT
Linken
  • Instruktiounen am Markdown-Format

    Eng SKILL.md-Datei, déi de Modell bei Bedarf luet, soudatt nëmmen Token ufalen, wann eng Ufro tatsächlech dozou passt.

  • Funktionéiert mat all LLM

    KI-Skills si reng Markdown, keen ubidderspezifesche Code. Dofir funktionéiert dëse Skill mat GPT, Claude, Gemini, Grok oder engem lokale Modell.

  • 1 matgeliwwert Dateien

    Skripten, Virlagen a Referenzen, déi de Modell beim Schaffe liese kann. D'Dateie kënnen nëmme gelies ginn a ginn ni ausgeféiert.

  • Open Source

    l-mb huet de Skill op GitHub verëffentlecht. Liest de Quelltext, ier Dir de Skill installéiert.

Installatioun

Installéiert de KI-Skill Py Git Hooks an TypingMind, fir e mat all LLM ze benotzen, oder setzt en an en anere KI-Agent an, deen SKILL.md liest.

1

An TypingMind installéieren

TypingMind installéiert e Skill direkt aus sengem GitHub-Dossier — dobäi gëtt SKILL.md gelies, d'Ressourcedateie gi gebëndelt an d'Resultat gëtt lokal gespäichert.

  1. Maacht d'App op a gitt op Plugins → Skills.
  2. Wielt „Vu GitHub installéieren“.
  3. Pecht d'URL vum Skill-Dossier, déi hei drënner steet, an a bestätegt d'Installatioun.
    https://github.com/l-mb/python-refactoring-skills/tree/main/skills/py-git-hooks
  4. Aktivéiert de Skill an all Chat, an deem Dir e benotze wëllt.
Plugins → Skills → Skill derbäisetzen → Iwwer eng GitHub-URL. Pecht dann d'URL vum Dossier an a klickt op Weider.
2

An engem anere KI-Agent installéieren

All KI-Agent, deen d'Format Agent Skills liest, kann dëse Skill benotzen — kopéiert den Dossier an d'Skill-Verzeechnes vun deem KI-Agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/l-mb/python-refactoring-skills.git /tmp/python-refactoring-skills
mkdir -p .claude/skills
cp -r /tmp/python-refactoring-skills/skills/py-git-hooks .claude/skills/py-git-hooks
Start Claude Code nom Kopéieren nei, fir datt den neie Skill erkannt gëtt.

An TypingMind benotzen

Aktivéiert Py Git Hooks an engem Chat vun TypingMind, an de Modell iwwerhëlt de Rescht. Den Numm an d'Beschreiwung stinn am System-Prompt. Soubal eng Ufro dozou passt, luet de Modell déi komplett Instruktioune selwer — Dir musst de Skill ni manuell opruffen, an et fale keng Token un, bis en tatsächlech benotzt gëtt.

De Modell luet Py Git Hooks automatesch, soubal eng Ufro dozou passt.

Funktionéiert mat all KI-Modell

KI-Skills si reng Markdown-Instruktiounen amplaz vun ubidderspezifeschem Code. Dofir ass Py Git Hooks net un de Modell gebonnen, fir deen de Skill geschriwwe gouf. Installéiert en eemol an TypingMind a benotzt e mat GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder engem lokale Modell, deen Dir selwer bedreift — alles mat Ären eegenen API-Schlësselen.

  • Gëtt nëmme gelueden, wann et néideg ass

    De System-Prompt enthält nëmmen den Numm an d'Beschreiwung. D'Instruktioune gi bei der éischter passender Ufro gelueden, soudatt en onbenotzte Skill näischt kascht.

  • De Modell am lafende Chat wiesselen

    Well de Skill aus Instruktiounen amplaz vu Code besteet, funktionéiert en och no engem Modellwiessel — den nächste Modell liest déi selwecht SKILL.md-Datei.

Skill-Instruktiounen

Dëst ass den Inhalt vu SKILL.md, deen de Modell luet. Liest e virun der Installatioun — e Skill besteet aus Instruktiounen, déi Äre Modell befollegt.

Python Git Hooks Setup

Configure git pre-commit hooks using the pre-commit framework to enforce code quality before commits.

Objectives

  1. Install pre-commit framework with Python quality hooks
  2. Migrate any existing manual hooks to pre-commit framework
  3. Configure Claude Code Stop hook lint gate (runs full lint suite before returning to user)
  4. Ensure hooks run incrementally on changed files only
  5. Auto-fix issues where possible, block on critical errors

Required Tools

Add to [dependency-groups] dev: "pre-commit", "ruff", "mypy", "basedpyright"

  • pre-commit: Hook management framework (required)
  • ruff: Fast linter with auto-fix capability
  • mypy: Standard Python type checker
  • basedpyright: Enhanced type analysis

Permissions: Run py-quality-setup first to configure .claude/settings.local.json with all needed tool permissions.

Setup Workflow

Step 1: Check for Existing Hooks

bash
# Check if manual hooks exist
ls -la .git/hooks/pre-commit 2>/dev/null

# If exists and not a pre-commit managed hook, migrate it (see Migration section)

Step 2: Create Pre-commit Configuration

Create .pre-commit-config.yaml:

yaml
repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.8.0
    hooks:
      - id: ruff
        args: [--fix]
      - id: ruff-format

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.13.0
    hooks:
      - id: mypy
        additional_dependencies: []  # Add type stubs as needed

  - repo: local
    hooks:
      - id: basedpyright
        name: basedpyright
        entry: basedpyright
        language: system
        types: [python]
        pass_filenames: true

Step 3: Install Hooks

bash
# Install pre-commit to project venv
uv pip install pre-commit

# Install git hooks
pre-commit install

# Verify installation
ls -la .git/hooks/pre-commit

Step 4: Test Hooks

bash
# Run on all files (initial validation)
pre-commit run --all-files

# Or test on staged files only
git add some_file.py
pre-commit run

Step 5: Configure Claude Code Stop Hook Lint Gate

Strongly recommended: Configure a Claude Code Stop hook that runs the full lint suite (ruff, mypy, basedpyright) on modified Python files before Claude returns control to the user. If any linter reports errors, Claude is blocked from stopping and must fix them first.

This replaces the older PostToolUse approach (which ran ruff after every individual edit, generating noise on intermediate states).

Install the lint gate script (symlink so updates propagate automatically):

bash
mkdir -p ~/.claude/hooks
ln -sf ~/.claude/skills/py-git-hooks/lint-gate.py ~/.claude/hooks/lint-gate.py

Configure the Stop hook in ~/.claude/settings.json:

json
{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "python3 ~/.claude/hooks/lint-gate.py"
          }
        ]
      }
    ]
  }
}

How it works:

  • Fires once when Claude finishes responding, before the user sees the result
  • Finds modified .py files via git diff (staged, unstaged, and untracked)
  • Auto-fix pass: runs ruff check --fix and ruff format to silently fix trivial issues (import sorting, whitespace, style)
  • Check pass: runs ruff check, mypy, and basedpyright to find remaining unfixable errors
  • If errors remain: returns {"decision": "block", "reason": "..."} — Claude sees the lint output and continues working to fix the issues
  • If clean (after auto-fix): returns {"decision": "stop"} — Claude returns to the user
  • Loop prevention: if stop_hook_active is true in the input (meaning Claude is already retrying after a previous block), the hook allows the stop to prevent infinite loops
  • Gracefully skips tools that aren't installed in the project

Benefits over PostToolUse hooks:

  • Runs once per turn instead of after every edit — no noise from intermediate states
  • Auto-fixes trivial issues so Claude only blocks on real errors
  • Runs the full suite (ruff + mypy + basedpyright), not just ruff
  • User always sees a clean codebase when Claude returns
  • Claude learns from the feedback and avoids repeating the same mistakes

Migration from Manual Hooks

If the project has an existing manual .git/hooks/pre-commit script:

Step 1: Backup Existing Hook

bash
cp .git/hooks/pre-commit .git/hooks/pre-commit.backup

Step 2: Analyze Existing Hook

Read the existing hook to understand what checks it runs:

  • Linting (ruff, flake8, pylint)?
  • Type checking (mypy, pyright)?
  • Formatting (black, isort)?
  • Custom checks?

Step 3: Map to Pre-commit Config

For each check in the manual hook, add equivalent to .pre-commit-config.yaml:

Manual Hook CheckPre-commit Equivalent
ruff checkruff-pre-commit repo
blackruff-format (ruff replaces black)
isortruff with isort rules (I)
flake8ruff (replaces flake8)
mypymirrors-mypy repo
basedpyrightlocal hook
Custom scriptlocal hook with entry: ./script.sh

Step 4: Install Pre-commit and Remove Manual Hook

bash
pre-commit install  # This overwrites .git/hooks/pre-commit
pre-commit run --all-files  # Verify all checks pass

Step 5: Verify and Clean Up

bash
# Test that hooks work
git add .
git commit -m "test" --dry-run

# If successful, remove backup
rm .git/hooks/pre-commit.backup

Hook Behavior

Auto-fixable issues (handled by both pre-commit and the Stop hook lint gate):

  • Import sorting
  • Trailing whitespace
  • Simple style violations
  • Many code quality issues

Blocking issues (require Claude or manual intervention):

  • Type errors (mypy/basedpyright)
  • Syntax errors
  • Complex linting violations that can't be auto-fixed

Bypass (use sparingly):

bash
git commit --no-verify

Performance Optimization

For large codebases:

  • Pre-commit runs on staged files only by default
  • Use mypy incremental mode (cache in .mypy_cache)
  • Consider running mypy/basedpyright in CI only for speed

For monorepos:

  • Use files: pattern in hook config to limit scope
  • Configure separate hooks per package
yaml
- id: mypy
  files: ^src/mypackage/

Troubleshooting

Hook not running:

bash
pre-commit install --force  # Reinstall hooks

Tools not found:

bash
# Ensure tools installed in venv
uv pip install ruff mypy basedpyright

Hook too slow:

  • Profile: pre-commit run --verbose
  • Consider removing mypy/basedpyright from pre-commit, run in CI instead
  • Use stages: [manual] for slow hooks, run explicitly

Update hook versions:

bash
pre-commit autoupdate

Migration from PostToolUse Hooks

If the project was previously set up with PostToolUse hooks for ruff (the older approach), migrate to the Stop hook lint gate:

Step 1: Check for Existing PostToolUse Hooks

bash
grep -A 10 '"PostToolUse"' ~/.claude/settings.json

Step 2: Remove PostToolUse Ruff Hooks

Edit ~/.claude/settings.json and remove any PostToolUse entries that match Edit|Write|MultiEdit and run ruff check. Keep any non-ruff PostToolUse hooks.

Step 3: Install Stop Hook

Follow Step 5 in the Setup Workflow above to install lint-gate.py and configure the Stop hook.

Step 4: Verify

Make a Python edit that introduces a ruff error. Confirm that:

  • No feedback appears after the edit itself (PostToolUse hook removed)
  • When Claude finishes responding, the Stop hook fires and blocks with the error
  • Claude fixes the error and returns clean output

Lint Learning

Claude should learn from lint gate feedback to avoid repeating the same mistakes.

After being blocked by the lint gate: note the error pattern. If the same linter rule triggers repeatedly across edits, record it in auto-memory under the lint-patterns topic. Format:

- **[rule-code]** (tool): What triggers it and how to avoid it.

Before editing Python files: consult auto-memory for known lint pitfalls in this project. Apply those lessons proactively so the lint gate passes on the first try.

This creates a cross-session feedback loop: each lint gate block teaches Claude to write cleaner code next time.

Verification Checklist

  • .pre-commit-config.yaml exists with ruff, mypy, basedpyright hooks
  • pre-commit install has been run
  • pre-commit run --all-files passes
  • Any existing manual hooks have been migrated
  • ~/.claude/hooks/lint-gate.py installed and executable
  • Claude Code Stop hook configured in ~/.claude/settings.json
  • Any old PostToolUse ruff hooks removed from settings
  • Bypass with --no-verify works for emergencies (git pre-commit only)

Examples

Example: New project setup

1. Create venv: uv venv && source .venv/bin/activate
2. Install tools: uv pip install pre-commit ruff mypy basedpyright
3. Create .pre-commit-config.yaml with ruff, mypy, basedpyright
4. Install hooks: pre-commit install
5. Test: pre-commit run --all-files
6. Install lint-gate.py and configure Stop hook in settings.json

Example: Migrate from PostToolUse to Stop hook

1. Remove PostToolUse ruff hooks from ~/.claude/settings.json
2. Symlink lint-gate.py to ~/.claude/hooks/
3. Add Stop hook to ~/.claude/settings.json
4. Verify: make an edit with a ruff error, confirm block-on-stop behavior

Example: Migrate existing manual hook

1. Backup: cp .git/hooks/pre-commit .git/hooks/pre-commit.backup
2. Read backup to identify checks (ruff, mypy, custom scripts)
3. Create .pre-commit-config.yaml mapping each check
4. Install: pre-commit install (overwrites manual hook)
5. Verify: pre-commit run --all-files
6. Clean up: rm .git/hooks/pre-commit.backup

Example: Add to existing pre-commit config

1. Edit .pre-commit-config.yaml
2. Add basedpyright local hook
3. Run: pre-commit run --all-files
4. Commit updated config

Related Skills

  • Prerequisites: py-quality-setup (tools must be configured before adding hooks)
  • Complements: py-security (add bandit to pre-commit for security scanning)
  • See also: All skills benefit from automated enforcement via hooks

Matgeliwwert Dateien

De Modell liest dës Dateie bei Bedarf, wärend de Skill gelueden ass. Si ginn als liesbar Dateie bereetgestallt a ginn ni ausgeféiert.

Heefeg gestallte Froen

Wat mécht de KI-Skill Py Git Hooks?

Set up git pre-commit hooks to run ruff, mypy, and basedpyright before commits. Use when configuring automated quality checks in git workflow.

Firwat Py Git Hooks op TypingMind benotzen?

Well Dir en eemol installéiert a mat all Modell benotze kënnt. Py Git Hooks besteet aus einfachem Markdown amplaz vun ubidderspezifeschem Code. Dee selwechte Skill funktionéiert also mat GPT-5, Claude, Gemini, Grok oder engem lokale Modell – an Dir kënnt de Modell matzen am Chat wiesselen, ouni datt de Skill ophält ze funktionéieren. TypingMind benotzt Är eegen API-Schlësselen, sou datt Dir d'Ubidder direkt bezuelt, amplaz en Abonnement pro Benotzerplaz ze bezuelen. Är Skills a Chats bleiwen an Ärem eegene Späicher.

Wéi installéieren ech Py Git Hooks an TypingMind?

Maacht an TypingMind Plugins → Skills → Vu GitHub installéieren op a setzt https://github.com/l-mb/python-refactoring-skills/tree/main/skills/py-git-hooks an. TypingMind liest d'Datei SKILL.md, bëndelt déi zougehéiereg Dateien an installéiert dat Ganzt als Skill, deen Dir pro Chat aktivéiere kënnt.

Wéi eng KI-Modeller kënnen Py Git Hooks benotzen?

All Modell, deen Dir an TypingMind verbënnt. KI-Skills sinn einfach Markdown-Instruktiounen an net ubidderspezifesche Code. Dofir kënne GPT, Claude, Gemini, Grok a lokal Modeller dëse Skill lueden, wann eng Ufro dozou passt.

Wéi vill KI-Modeller kann ech mat Py Git Hooks benotzen?

Esou vill wéi Dir wëllt. Soulaang e Modell Skills ënnerstëtzt, kënnt Dir Py Git Hooks domat benotzen – zum Beispill mat GPT, Claude, Gemini, Grok, DeepSeek, Mistral oder Llama – alles op TypingMind mat Ären eegenen API-Schlësselen.

Ass de KI-Skill Py Git Hooks gratis?

Jo. l-mb huet de Skill ënner der Lizenz MIT op GitHub publizéiert. Dir bezuelt nëmmen Ären eegene KI-Ubidder fir d'Token, déi Dir benotzt.

Wat si KI-Skills?

E KI-Skill ass e weiderverwendbare Pak vun Instruktiounen, deen engem KI-Modell bäibréngt, wéi en eng bestëmmt Aufgab erfëllt. E follegt dem oppenen Agent Skills-Format: eng Datei SKILL.md mat engem Numm an enger Beschreiwung, plus all Scripten, Virlagen oder Referenzdateien, déi de Modell eventuell brauch. De Modell liest d'Instruktiounen eréischt, wann Är Ufro zum Skill passt. En installéierte Skill kascht also näischt, bis e benotzt gëtt.

Wéi ënnerscheede sech KI-Skills vu Plugins oder MCP-Serveren?

E Plugin oder MCP-Server gëtt engem Modell nei Tools, déi en opruffe kann – Code, deen iergendwou leeft an e Resultat zeréckgëtt. E KI-Skill gëtt dem Modell dogéint Wëssen an eng Virgoensweis: wéi en eng Aufgab ugeet, wéi eng Schrëtt e maache soll a wéi eng gutt Ausgab ausgesäit. Skills bestinn aus einfachem Markdown, brauche kee Server, keen API-Schlëssel a kee Lafzäitëmfeld a funktionéiere mat all Modell.

Alleguer weisen

Elo Ären eegene KI-Aarbechtsberäich ariichten

Abonnéiert Iech op eisen Newsletter, fir iwwer nei Funktiounen an zukünfteg Gewënnspiller informéiert ze ginn 👇