Help us improve
Share bugs, ideas, or general feedback.
From scaffolding
Provides Python backend patterns for layered architecture, async I/O, dependency injection, and separation of HTTP, business logic, and data access layers. Examples use FastAPI, SQLAlchemy, and Pydantic.
How this skill is triggered — by the user, by Claude, or both
Slash command
/scaffolding:python-patternsThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Best practices for Python backend development: layered architecture, async I/O, dependency injection, and clear separation between HTTP handling, business logic, and data access. The concrete examples below use FastAPI, SQLAlchemy, and Pydantic, but the patterns apply to any Python web framework, ORM, and validation library.
Share bugs, ideas, or general feedback.
Best practices for Python backend development: layered architecture, async I/O, dependency injection, and clear separation between HTTP handling, business logic, and data access. The concrete examples below use FastAPI, SQLAlchemy, and Pydantic, but the patterns apply to any Python web framework, ORM, and validation library.
| Layer | Responsibility |
|---|---|
| Endpoints | HTTP handling, request/response |
| Services | Business logic, orchestration |
| Repositories | Data access, queries |
| Models | Database schema |
| Schemas | Data validation, serialization |
These layers are framework-agnostic — keep HTTP concerns, business rules, and data access in separate modules regardless of which framework/ORM you use.
Illustrative — this is one concrete stack shown as an example. Substitute your framework's equivalents (any ASGI/WSGI framework, ORM, and validation library). The layering and separation-of-concerns patterns above are the reusable part.
app/
└── backend/
├── app/
│ ├── main.py # FastAPI app initialization
│ ├── config.py # Settings (pydantic-settings)
│ ├── api/v1/endpoints/ # Route handlers
│ ├── core/ # Security, exceptions
│ ├── models/ # SQLAlchemy models
│ ├── schemas/ # Pydantic schemas
│ ├── services/ # Business logic
│ ├── repositories/ # Data access
│ └── db/session.py # Database session
├── tests/
├── alembic.ini
└── requirements.txt
async_sessionmaker for async sessionsexpire_on_commit=False for response dataselect() statements (SQLAlchemy 2.0 style)scalar_one_or_none() for single itemsscalars().all() for listsselectin or joinedload| Type | Purpose | Example |
|---|---|---|
| Base | Shared fields | UserBase(email, username) |
| Create | POST request | UserCreate(Base + password) |
| Update | PATCH request | UserUpdate(all optional) |
| Response | API response | UserResponse(Base + id, created_at) |
| InDB | Internal with secrets | UserInDB(Response + hashed_password) |
model_config = ConfigDict(from_attributes=True) for ORMField() for validation constraintsfield_validator for custom validationget_by_id(id) - Single item by primary keyget_all(skip, limit) - Paginated listcreate(**kwargs) - Insert new recordupdate(id, **kwargs) - Update existingdelete(id) - Remove recordget_by_email(), get_active_users()get_db - Database sessionget_current_user - Authenticated userget_current_superuser - Admin userget_user_service(session)# Endpoint receives dependencies, passes to service
async def create_user(
data: UserCreate,
session: AsyncSession = Depends(get_db)
):
service = UserService(session)
return await service.create(data)
pydantic-settings for env loading@lru_cache for singleton@property for computed valuesDATABASE_URL - Database connectionSECRET_KEY - JWT signingDEBUG - Development modeCORS_ORIGINS - Allowed originsdb_session - In-memory SQLite sessionclient - AsyncClient with appget_db dependency for testspytest.mark.asyncio for async tests| Tool | Purpose |
|---|---|
| black | Code formatting |
| ruff | Linting |
| mypy | Type checking |
| pytest | Testing |
| pytest-cov | Coverage |
npx claudepluginhub komluk/scaffolding --plugin scaffoldingProvides Python backend patterns for FastAPI, Django, SQLAlchemy, async code, and Pydantic v2. Covers principles like async usage, lifespan managers, and type annotations; includes references and stack detection script.
Provides expert Python backend patterns for Django, FastAPI, Flask: Pydantic models, dependency injection, background tasks, exception handling, lifespan events, async programming.
Provides Python/FastAPI best practices: layered architecture, Pydantic v2 schemas, async patterns, SQLAlchemy 2.0, soft deletes, pagination, config. Use for writing code or reviewing FastAPI projects.