Skip to main content
This page extends Common Rules with Python specific content.Applies to: **/*.py, **/*.pyi

Coding Style

Standards

  • Follow PEP 8 conventions
  • Use type annotations on all function signatures

Immutability

Prefer immutable data structures:

Formatting

black

Code formatting

isort

Import sorting

ruff

Linting

Reference

python-patterns

See skill for comprehensive Python idioms and patterns

Testing

Framework

Use pytest as the testing framework.

Test Organization

Use pytest.mark for test categorization:

Reference

python-testing

See skill for detailed pytest patterns and fixtures

Patterns

Protocol (Duck Typing)

Dataclasses as DTOs

Context Managers & Generators

Use context managers (with statement) for resource management:
Use generators for lazy evaluation and memory-efficient iteration:

Reference

python-patterns

See skill for comprehensive patterns including decorators, concurrency, and package organization

Security

Secret Management

Security Scanning

Use bandit for static security analysis:

Reference

django-security

See skill for Django-specific security guidelines (if applicable)

Hooks

PostToolUse Hooks

Configure in ~/.claude/settings.json:
Automatically runs black or ruff formatter on Python files after edits.
Example hook:
Runs type checker to catch type errors immediately.

Warnings

Warn about print() statements in edited files (use logging module instead)

Common Rules

Language-agnostic base rules

Hooks Overview

Complete hook system reference