This page extends Common Rules with Python specific content.Applies to:
**/*.py, **/*.pyiCoding 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
Usepytest.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
Context Managers
Context Managers
Use context managers (
with statement) for resource management:Generators
Generators
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:
black/ruff: Auto-format .py files after edit
black/ruff: Auto-format .py files after edit
Automatically runs black or ruff formatter on Python files after edits.Example hook:
mypy/pyright: Run type checking after editing .py files
mypy/pyright: Run type checking after editing .py files
Runs type checker to catch type errors immediately.
Warnings
Related
Common Rules
Language-agnostic base rules
Hooks Overview
Complete hook system reference