Skip to main content

Python hooks for Claude Code

Project description

python-claude

Python hooks for Claude Code for projects using:

  • uv
  • 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

uv 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": "uv run python-claude session start"
          },
          {
            "type": "command",
            "command": "uv run python-claude git status"
          }
        ]
      }
    ],
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "uv run python-claude ruff format"
          },
          {
            "type": "command",
            "command": "uv run python-claude ruff check"
          },
          {
            "type": "command",
            "command": "uv run python-claude mypy"
          },
          {
            "type": "command",
            "command": "uv run python-claude pytest"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [
          {
            "type": "command",
            "command": "uv 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:

uv run python-claude toggle pytest
uv run python-claude toggle mypy
uv 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

uv sync
uv run pytest
uv run ruff check
uv 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.5.0.tar.gz (33.6 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.5.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_claude-0.5.0.tar.gz
  • Upload date:
  • Size: 33.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.4 {"installer":{"name":"uv","version":"0.11.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for python_claude-0.5.0.tar.gz
Algorithm Hash digest
SHA256 7e1dad8d4463cd512cda756d72dfa4e0f4635f7a111f845afe44280d3564ca6a
MD5 c8eea3fbe886a929a09e61b0ea845002
BLAKE2b-256 aef62b20946fafcf9384195293dddd13be73ed78d757f41f9ce6df57b169068b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_claude-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.4 {"installer":{"name":"uv","version":"0.11.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for python_claude-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29a5f7af9c104e0329446c31c448b6d79015289f63ce91c9b6e617dbeb44ddd0
MD5 b5e37378d7ec14342e1ba22fb2892f0a
BLAKE2b-256 c44d9add4532ca4c9b1a121870d188b7a8220364f167f510e4cbece079f0b8f0

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