Example CLAUDE.md — Python Project¶
This example shows a complete CLAUDE.md for a Python backend service using FastAPI, pytest for testing, and standard Python tooling. It demonstrates conventions for type hints, virtual environments, and project structure.
The CLAUDE.md File¶
````markdown
Project: Inventory API¶
FastAPI backend service for inventory management. Python 3.12+.
Setup¶
- Virtual environment:
source .venv/bin/activate(always activate before running commands) - Install deps:
pip install -e ".[dev]" - Environment variables: copy
.env.exampleto.envfor local development
Commands¶
pytest— run all testspytest tests/test_items.py::test_create_item -v— run a single testpytest --cov=src --cov-report=term-missing— run tests with coverageruff check .— lintruff format .— format codemypy src/— type checkinguvicorn src.main:app --reload— start dev server
Run ruff check . && mypy src/ before committing.
Project Structure¶
src/— application source codesrc/main.py— FastAPI app entrypoint and router mountingsrc/models/— SQLAlchemy ORM modelssrc/schemas/— Pydantic request/response schemassrc/routers/— API route handlers, one file per resourcesrc/services/— business logic layer (routers call services, services call repositories)src/repositories/— database access layersrc/dependencies.py— FastAPI dependency injection (db sessions, auth)tests/— mirrors src/ structure:tests/test_routers/,tests/test_services/, etc.alembic/— database migrations
Coding Conventions¶
- Type hints on all function signatures — parameters and return types
- Use
from __future__ import annotationsat the top of every module - Pydantic models for all API input/output — never pass raw dicts across boundaries
- Use
pathlib.Pathinstead ofos.path - f-strings for string formatting (no .format() or % formatting)
- Docstrings on public functions using Google style
```python def get_item(item_id: int, db: Session) -> Item: """Fetch a single item by ID.
Args:
item_id: The item's primary key.
db: Database session.
Returns:
The matching Item.
Raises:
NotFoundError: If no item matches the ID.
"""
```
Testing¶
- Use pytest with fixtures defined in
conftest.py - Test database: use an in-memory SQLite or test-specific PostgreSQL database — never touch the dev database
- Use
httpx.AsyncClientwithappfor API integration tests - Factory fixtures for creating test data (see
tests/factories.py) - Aim for >80% coverage on business logic in
src/services/
Database¶
- SQLAlchemy 2.0 style (use
select()notsession.query()) - All schema changes go through Alembic migrations — never modify tables manually
- New migration:
alembic revision --autogenerate -m "description" - Apply migrations:
alembic upgrade head
Error Handling¶
- Use custom exception classes in
src/exceptions.py - Routers should not catch generic exceptions — let the global exception handler deal with unexpected errors
- Service layer raises domain exceptions (NotFoundError, ValidationError, etc.)
- Never return error details from unhandled exceptions in production responses
Git¶
- Conventional commits: feat:, fix:, chore:
- Run the full check before committing:
ruff check . && ruff format --check . && mypy src/ && pytest
Do NOT¶
- Do not use
import * - Do not use
objects.raw()or raw SQL — use the repository layer - Do not use
print()— use the configuredloggingmodule - Do not use
*imports ````
Key Sections Explained¶
Setup — The virtual environment reminder is critical. Claude needs to know to activate the venv before running any Python commands.
Project Structure — The layered architecture (routers -> services -> repositories) tells Claude where new code should go and prevents it from putting business logic in route handlers.
Coding Conventions — Type hints and Pydantic model requirements ensure Claude generates code that passes mypy and follows team standards.
Database — Specifying SQLAlchemy 2.0 style prevents Claude from using the older query API. The Alembic instructions ensure schema changes are done properly.
Do NOT — Prevents common Python anti-patterns. The import * and mutable default argument rules catch issues Claude might otherwise introduce.
See Also¶
- CLAUDE.md Setup Guide — how to structure your own CLAUDE.md
- Minimal Example — a simpler starting point
- React Example — for frontend projects