# python-best-practices

Defines architectural and coding standards for FastAPI and Pydantic v2. Keeps async endpoints free from blocking calls and aligns layer boundaries.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · ★ 109
- Author: c0x12c
- License: not stated · policy=link-only · description only; the file stays with the author
- Source: https://github.com/c0x12c/ai-toolkit/tree/HEAD/toolkit/skills/python-best-practices/SKILL.md
- Updated: 2026-06-18
- HTML: https://affpapa.org/en/nevibecode/skills/c0x12c-python-best-practices

## What it does

The skill enforces a strict downward flow where routers call services, and services query repositories. Each layer talks only to the one directly beneath it. Outbound network requests go through `httpx.AsyncClient`. Application startup and teardown move to the lifespan context manager instead of the legacy `on_event` handler.

Using the outdated `orm_mode` silently breaks serialization without explicit errors, so the guide mandates `from_attributes=True` for Pydantic v2. Complex nested defaults require `Field(default_factory=list)`. Database definitions swap out legacy `Column` declarations for `Mapped` annotations with `mapped_column()` to retain editor autocomplete.

Async execution gets strict boundaries. Calling blocking code inside `async def` stalls the entire event loop. Because of that, CPU-heavy tasks must use plain `def` functions so FastAPI can run them inside a threadpool.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add c0x12c/ai-toolkit --skill python-best-practices -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/python-best-practices/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/python-best-practices/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add c0x12c/ai-toolkit --skill python-best-practices -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/python-best-practices/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add c0x12c/ai-toolkit --skill python-best-practices -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/python-best-practices/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install c0x12c/ai-toolkit python-best-practices
```

2. **Manually: put the files in place**
- File: `.github/skills/python-best-practices/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add c0x12c/ai-toolkit --skill python-best-practices -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/c0x12c/ai-toolkit --path toolkit/skills/python-best-practices
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/python-best-practices/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add c0x12c/ai-toolkit --skill python-best-practices -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/c0x12c/ai-toolkit/tree/HEAD/toolkit/skills/python-best-practices/SKILL.md

## When to use it
- Enforce strict Router-Service-Repository layer boundaries
- Migrate legacy schemas to Pydantic v2 with from_attributes=True
- Prevent event loop freezes from blocking calls in async routes
- Standardize model definitions using SQLAlchemy 2.0 mapped_column
- Add soft delete filtering through a reusable SoftDeleteMixin

## Example prompts
- Check the routes in `app/api/v1` for blocking operations inside async def
- Convert the models in `app/models/user.py` to SQLAlchemy 2.0 Mapped syntax
- Implement generic pagination for the customer list endpoint using PaginatedResponse

## Similar
- [mcp-builder](https://affpapa.org/en/nevibecode/skills/mcp-builder.md) — skill · 130949 installs
- [claude-api](https://affpapa.org/en/nevibecode/skills/claude-api.md) — skill · 74198 installs
- [lark-im](https://affpapa.org/en/nevibecode/skills/lark-im.md) — skill · 472900 installs
- [lark-event](https://affpapa.org/en/nevibecode/skills/lark-event.md) — skill · 466800 installs
- [caveman-discover](https://affpapa.org/en/nevibecode/skills/caveman-discover.md) — skill · 104400 installs
- [migration](https://affpapa.org/en/nevibecode/skills/migration.md) — skill · 103900 installs

## Other versions with the same name
- [python-best-practices · nathan-gage](https://affpapa.org/en/nevibecode/skills/python-best-practices.md) — 855 installs

From the same set: [c0x12c/ai-toolkit](https://affpapa.org/en/nevibecode/by/c0x12c/ai-toolkit.md) — 33 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
