Skip to main content

Python hooks for Claude Code

Project description

python-claude

Python hooks for Claude Code for projects using:

  • poetry
  • ruff
  • mypy
  • pytest

The hooks ensure that the quality tools are automatically used:

Each time a file is edited:

  • Record the file for later processing

When Claude is ready to stop:

  • Reformat edited files with ruff format
  • Repair lints of edited files with ruff check --fix
  • Type-check the project with mypy (only if files were edited)
  • Run the tests with pytest (only if files were edited)

Note: We defer all quality checks until Claude stops to avoid changing files while Claude is working. Changing files during editing would spoil Claude's edits and force it to reread files. Quality checks only run when at least one Python file has been edited during the session.

Installation

poetry add python-claude

Usage

This package provides hooks that can be used with Claude Code's hook system.

Available Commands

  • edited - Tracks edited Python files for deferred processing (used in PostToolUse hook)
  • git status - Shows git status
  • mypy - Runs mypy type checking on edited files (used in Stop hook)
  • pytest - Runs pytest
  • ruff check - Runs ruff check on collected files with auto-fix (used in Stop hook)
  • ruff format - Runs ruff format on collected files (used in Stop hook)
  • session start - Prints introductory message about automatic hooks
  • toggle <check> - Enable/disable a quality check (pytest, mypy, or ruff)

Claude Code Settings

Add hooks to your Claude Code settings.json:

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "poetry run python-claude session start"
          },
          {
            "type": "command",
            "command": "poetry run python-claude git status"
          }
        ]
      }
    ],
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "poetry run python-claude ruff format"
          },
          {
            "type": "command",
            "command": "poetry run python-claude ruff check"
          },
          {
            "type": "command",
            "command": "poetry run python-claude mypy"
          },
          {
            "type": "command",
            "command": "poetry run python-claude pytest"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [
          {
            "type": "command",
            "command": "poetry run python-claude edited"
          }
        ]
      }
    ]
  }
}

Toggling Quality Checks

You can temporarily disable quality checks when needed. This is useful when:

  • Tests are failing and you want to focus on type errors first
  • You want to make multiple changes before running checks
  • You're working on a feature that temporarily breaks tests

Using Slash Commands (Recommended)

The easiest way is to use the provided slash commands in Claude Code:

  • /pytest - Toggle pytest on/off
  • /mypy - Toggle mypy on/off
  • /ruff - Toggle ruff (format + check) on/off

Each command will show whether the check is now enabled or disabled.

Using CLI Directly

You can also use the CLI directly:

poetry run python-claude toggle pytest
poetry run python-claude toggle mypy
poetry run python-claude toggle ruff

The toggle state persists across Claude Code sessions, stored in .claude/quality-checks.json (which is gitignored). All checks are enabled by default.

Development

poetry install
poetry run pytest
poetry run ruff check
poetry run mypy src

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_claude-0.4.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_claude-0.4.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file python_claude-0.4.1.tar.gz.

File metadata

  • Download URL: python_claude-0.4.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.3 Darwin/25.1.0

File hashes

Hashes for python_claude-0.4.1.tar.gz
Algorithm Hash digest
SHA256 ff5919437a24c1f44c2ce48600b1de49cd48714d5050d25f1b03fdf9fd4c36a1
MD5 5363c4057c459b28b500c50cf8ee15e7
BLAKE2b-256 a5c20a8dfaaaa16333a67e5f1d3038c57e3ab8c91d7b949c9c0923ca02c4e326

See more details on using hashes here.

File details

Details for the file python_claude-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: python_claude-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.3 Darwin/25.1.0

File hashes

Hashes for python_claude-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 59b09a46001485044bbe6bc2f5d4144f70001daf4f0078754b2f57240b6a2506
MD5 b0213caf0dfb7071049a243bca62f22b
BLAKE2b-256 d1b5279ea5fd31dd7db1c0fc48280b203cd8808e82d09fb31c566d81677972d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page