@h1dr0n/skill-pool 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +296 -0
- package/bin/cli.js +157 -0
- package/package.json +41 -0
- package/skills/api/agents/backend-specialist.md +69 -0
- package/skills/api/agents/database-optimizer.md +176 -0
- package/skills/api/manifest.yaml +20 -0
- package/skills/api/rules/auth-security.md +45 -0
- package/skills/api/skills/api-patterns/SKILL.md +81 -0
- package/skills/api/skills/api-patterns/api-style.md +42 -0
- package/skills/api/skills/api-patterns/auth.md +24 -0
- package/skills/api/skills/api-patterns/documentation.md +26 -0
- package/skills/api/skills/api-patterns/graphql.md +41 -0
- package/skills/api/skills/api-patterns/rate-limiting.md +31 -0
- package/skills/api/skills/api-patterns/response.md +37 -0
- package/skills/api/skills/api-patterns/rest.md +40 -0
- package/skills/api/skills/api-patterns/scripts/api_validator.py +211 -0
- package/skills/api/skills/api-patterns/security-testing.md +122 -0
- package/skills/api/skills/api-patterns/trpc.md +41 -0
- package/skills/api/skills/api-patterns/versioning.md +22 -0
- package/skills/api/skills/database-patterns.md +126 -0
- package/skills/api/skills/deployment-patterns.md +105 -0
- package/skills/api/skills/docker-patterns.md +135 -0
- package/skills/common/agents/code-reviewer.md +78 -0
- package/skills/common/agents/planner.md +80 -0
- package/skills/common/agents/security-reviewer.md +82 -0
- package/skills/common/agents/software-architect.md +81 -0
- package/skills/common/manifest.yaml +25 -0
- package/skills/common/rules/coding-style.md +39 -0
- package/skills/common/rules/git-workflow.md +33 -0
- package/skills/common/rules/security.md +25 -0
- package/skills/common/skills/architecture/SKILL.md +55 -0
- package/skills/common/skills/architecture/context-discovery.md +43 -0
- package/skills/common/skills/architecture/examples.md +94 -0
- package/skills/common/skills/architecture/pattern-selection.md +68 -0
- package/skills/common/skills/architecture/patterns-reference.md +50 -0
- package/skills/common/skills/architecture/trade-off-analysis.md +77 -0
- package/skills/common/skills/brainstorming/SKILL.md +163 -0
- package/skills/common/skills/brainstorming/dynamic-questioning.md +350 -0
- package/skills/common/skills/clean-code.md +99 -0
- package/skills/common/skills/code-review-checklist.md +86 -0
- package/skills/common/skills/plan-writing/SKILL.md +152 -0
- package/skills/common/skills/skill-feedback.md +94 -0
- package/skills/common/skills/tdd-workflow.md +130 -0
- package/skills/common/skills/verification-loop.md +112 -0
- package/skills/cpp/agents/cpp-build-resolver.md +90 -0
- package/skills/cpp/agents/cpp-reviewer.md +72 -0
- package/skills/cpp/manifest.yaml +15 -0
- package/skills/cpp/skills/cpp-coding-standards.md +722 -0
- package/skills/cpp/skills/cpp-testing.md +323 -0
- package/skills/devops/agents/devops-automator.md +376 -0
- package/skills/devops/agents/sre.md +90 -0
- package/skills/devops/manifest.yaml +20 -0
- package/skills/devops/skills/deployment-patterns.md +427 -0
- package/skills/devops/skills/deployment-procedures/SKILL.md +241 -0
- package/skills/devops/skills/docker-patterns.md +364 -0
- package/skills/devops/skills/e2e-testing.md +326 -0
- package/skills/devops/skills/github-ops.md +144 -0
- package/skills/django/manifest.yaml +16 -0
- package/skills/django/skills/django-patterns.md +734 -0
- package/skills/django/skills/django-security.md +593 -0
- package/skills/django/skills/django-tdd.md +729 -0
- package/skills/django/skills/django-verification.md +469 -0
- package/skills/dotnet/agents/csharp-reviewer.md +101 -0
- package/skills/dotnet/manifest.yaml +14 -0
- package/skills/dotnet/skills/csharp-testing.md +321 -0
- package/skills/dotnet/skills/dotnet-patterns.md +321 -0
- package/skills/go/agents/code-reviewer.md +76 -0
- package/skills/go/agents/go-build-resolver.md +94 -0
- package/skills/go/agents/go-reviewer.md +76 -0
- package/skills/go/manifest.yaml +17 -0
- package/skills/go/rules/go-style.md +55 -0
- package/skills/go/skills/golang-patterns.md +674 -0
- package/skills/go/skills/golang-testing.md +720 -0
- package/skills/java/agents/java-build-resolver.md +153 -0
- package/skills/java/agents/java-reviewer.md +92 -0
- package/skills/java/manifest.yaml +18 -0
- package/skills/java/skills/java-coding-standards.md +147 -0
- package/skills/java/skills/jpa-patterns.md +151 -0
- package/skills/java/skills/springboot-patterns.md +314 -0
- package/skills/java/skills/springboot-security.md +272 -0
- package/skills/kotlin/agents/kotlin-build-resolver.md +118 -0
- package/skills/kotlin/agents/kotlin-reviewer.md +159 -0
- package/skills/kotlin/manifest.yaml +17 -0
- package/skills/kotlin/skills/kotlin-coroutines-flows.md +284 -0
- package/skills/kotlin/skills/kotlin-patterns.md +711 -0
- package/skills/kotlin/skills/kotlin-testing.md +824 -0
- package/skills/laravel/manifest.yaml +15 -0
- package/skills/laravel/skills/laravel-patterns.md +409 -0
- package/skills/laravel/skills/laravel-security.md +279 -0
- package/skills/laravel/skills/laravel-tdd.md +277 -0
- package/skills/laravel/skills/laravel-verification.md +173 -0
- package/skills/mobile/agents/dart-build-resolver.md +201 -0
- package/skills/mobile/agents/flutter-reviewer.md +243 -0
- package/skills/mobile/manifest.yaml +19 -0
- package/skills/mobile/skills/android-clean-architecture.md +339 -0
- package/skills/mobile/skills/dart-flutter-patterns.md +563 -0
- package/skills/mobile/skills/swiftui-patterns.md +259 -0
- package/skills/nestjs/manifest.yaml +13 -0
- package/skills/nestjs/skills/nestjs-patterns.md +230 -0
- package/skills/perl/manifest.yaml +13 -0
- package/skills/perl/skills/perl-patterns.md +504 -0
- package/skills/perl/skills/perl-security.md +503 -0
- package/skills/perl/skills/perl-testing.md +475 -0
- package/skills/python/agents/python-reviewer.md +98 -0
- package/skills/python/manifest.yaml +18 -0
- package/skills/python/rules/python-style.md +69 -0
- package/skills/python/skills/python-patterns/SKILL.md +441 -0
- package/skills/python/skills/python-patterns.md +90 -0
- package/skills/python/skills/python-testing.md +81 -0
- package/skills/rust/agents/rust-build-resolver.md +148 -0
- package/skills/rust/agents/rust-reviewer.md +94 -0
- package/skills/rust/manifest.yaml +16 -0
- package/skills/rust/rules/rust-style.md +107 -0
- package/skills/rust/skills/rust-patterns.md +499 -0
- package/skills/rust/skills/rust-testing.md +500 -0
- package/skills/security/agents/accessibility-auditor.md +316 -0
- package/skills/security/agents/security-reviewer.md +108 -0
- package/skills/security/manifest.yaml +19 -0
- package/skills/security/skills/red-team-tactics/SKILL.md +199 -0
- package/skills/security/skills/security-bounty-hunter.md +99 -0
- package/skills/security/skills/security-review.md +495 -0
- package/skills/security/skills/security-scan.md +165 -0
- package/skills/security/skills/vulnerability-scanner/SKILL.md +276 -0
- package/skills/security/skills/vulnerability-scanner/checklists.md +121 -0
- package/skills/security/skills/vulnerability-scanner/scripts/security_scan.py +458 -0
- package/skills/swift/manifest.yaml +16 -0
- package/skills/swift/skills/swift-actor-persistence.md +142 -0
- package/skills/swift/skills/swift-concurrency.md +216 -0
- package/skills/swift/skills/swift-protocol-di-testing.md +190 -0
- package/skills/swift/skills/swiftui-patterns.md +259 -0
- package/skills/unity/agents/game-designer.md +167 -0
- package/skills/unity/agents/unity-architect.md +52 -0
- package/skills/unity/agents/unity-editor-tool-developer.md +310 -0
- package/skills/unity/agents/unity-multiplayer-engineer.md +321 -0
- package/skills/unity/agents/unity-shader-graph-artist.md +269 -0
- package/skills/unity/manifest.yaml +21 -0
- package/skills/unity/rules/csharp-patterns.md +48 -0
- package/skills/unity/rules/unity-specific.md +53 -0
- package/skills/unity/skills/systematic-debugging.md +92 -0
- package/skills/unity/skills/unity-architecture.md +173 -0
- package/skills/unreal/agents/level-designer.md +208 -0
- package/skills/unreal/agents/technical-artist.md +229 -0
- package/skills/unreal/agents/unreal-multiplayer-architect.md +313 -0
- package/skills/unreal/agents/unreal-systems-engineer.md +310 -0
- package/skills/unreal/agents/unreal-technical-artist.md +256 -0
- package/skills/unreal/agents/unreal-world-builder.md +273 -0
- package/skills/unreal/manifest.yaml +21 -0
- package/skills/unreal/skills/unreal-patterns.md +183 -0
- package/skills/web/agents/frontend-specialist.md +71 -0
- package/skills/web/agents/ui-designer.md +383 -0
- package/skills/web/agents/ux-architect.md +469 -0
- package/skills/web/manifest.yaml +22 -0
- package/skills/web/rules/accessibility.md +54 -0
- package/skills/web/rules/css-performance.md +52 -0
- package/skills/web/skills/e2e-testing.md +132 -0
- package/skills/web/skills/frontend-design/SKILL.md +452 -0
- package/skills/web/skills/frontend-design/animation-guide.md +331 -0
- package/skills/web/skills/frontend-design/color-system.md +311 -0
- package/skills/web/skills/frontend-design/decision-trees.md +418 -0
- package/skills/web/skills/frontend-design/motion-graphics.md +306 -0
- package/skills/web/skills/frontend-design/scripts/accessibility_checker.py +183 -0
- package/skills/web/skills/frontend-design/scripts/ux_audit.py +722 -0
- package/skills/web/skills/frontend-design/typography-system.md +345 -0
- package/skills/web/skills/frontend-design/ux-psychology.md +1116 -0
- package/skills/web/skills/frontend-design/visual-effects.md +383 -0
- package/skills/web/skills/react-nextjs.md +135 -0
- package/skills/web/skills/tailwind-patterns/SKILL.md +269 -0
- package/src/adapters/antigravity.js +164 -0
- package/src/adapters/claude.js +188 -0
- package/src/adapters/cursor.js +161 -0
- package/src/adapters/index.js +67 -0
- package/src/adapters/windsurf.js +158 -0
- package/src/commands/add.js +266 -0
- package/src/commands/create.js +127 -0
- package/src/commands/diff.js +78 -0
- package/src/commands/info.js +88 -0
- package/src/commands/init.js +224 -0
- package/src/commands/install.js +90 -0
- package/src/commands/list.js +54 -0
- package/src/commands/remove.js +101 -0
- package/src/commands/targets.js +32 -0
- package/src/commands/update.js +57 -0
- package/src/core/manifest.js +57 -0
- package/src/core/plugins.js +86 -0
- package/src/core/resolver.js +84 -0
- package/src/core/tracker.js +49 -0
- package/src/utils/fs.js +80 -0
- package/src/utils/git.js +52 -0
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: python-patterns
|
|
3
|
+
description: Python development principles and decision-making. Framework selection, async patterns, type hints, project structure. Teaches thinking, not copying.
|
|
4
|
+
allowed-tools: Read, Write, Edit, Glob, Grep
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Python Patterns
|
|
8
|
+
|
|
9
|
+
> Python development principles and decision-making for 2025.
|
|
10
|
+
> **Learn to THINK, not memorize patterns.**
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## ⚠️ How to Use This Skill
|
|
15
|
+
|
|
16
|
+
This skill teaches **decision-making principles**, not fixed code to copy.
|
|
17
|
+
|
|
18
|
+
- ASK user for framework preference when unclear
|
|
19
|
+
- Choose async vs sync based on CONTEXT
|
|
20
|
+
- Don't default to same framework every time
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 1. Framework Selection (2025)
|
|
25
|
+
|
|
26
|
+
### Decision Tree
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
What are you building?
|
|
30
|
+
│
|
|
31
|
+
├── API-first / Microservices
|
|
32
|
+
│ └── FastAPI (async, modern, fast)
|
|
33
|
+
│
|
|
34
|
+
├── Full-stack web / CMS / Admin
|
|
35
|
+
│ └── Django (batteries-included)
|
|
36
|
+
│
|
|
37
|
+
├── Simple / Script / Learning
|
|
38
|
+
│ └── Flask (minimal, flexible)
|
|
39
|
+
│
|
|
40
|
+
├── AI/ML API serving
|
|
41
|
+
│ └── FastAPI (Pydantic, async, uvicorn)
|
|
42
|
+
│
|
|
43
|
+
└── Background workers
|
|
44
|
+
└── Celery + any framework
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Comparison Principles
|
|
48
|
+
|
|
49
|
+
| Factor | FastAPI | Django | Flask |
|
|
50
|
+
|--------|---------|--------|-------|
|
|
51
|
+
| **Best for** | APIs, microservices | Full-stack, CMS | Simple, learning |
|
|
52
|
+
| **Async** | Native | Django 5.0+ | Via extensions |
|
|
53
|
+
| **Admin** | Manual | Built-in | Via extensions |
|
|
54
|
+
| **ORM** | Choose your own | Django ORM | Choose your own |
|
|
55
|
+
| **Learning curve** | Low | Medium | Low |
|
|
56
|
+
|
|
57
|
+
### Selection Questions to Ask:
|
|
58
|
+
1. Is this API-only or full-stack?
|
|
59
|
+
2. Need admin interface?
|
|
60
|
+
3. Team familiar with async?
|
|
61
|
+
4. Existing infrastructure?
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## 2. Async vs Sync Decision
|
|
66
|
+
|
|
67
|
+
### When to Use Async
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
async def is better when:
|
|
71
|
+
├── I/O-bound operations (database, HTTP, file)
|
|
72
|
+
├── Many concurrent connections
|
|
73
|
+
├── Real-time features
|
|
74
|
+
├── Microservices communication
|
|
75
|
+
└── FastAPI/Starlette/Django ASGI
|
|
76
|
+
|
|
77
|
+
def (sync) is better when:
|
|
78
|
+
├── CPU-bound operations
|
|
79
|
+
├── Simple scripts
|
|
80
|
+
├── Legacy codebase
|
|
81
|
+
├── Team unfamiliar with async
|
|
82
|
+
└── Blocking libraries (no async version)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### The Golden Rule
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
I/O-bound → async (waiting for external)
|
|
89
|
+
CPU-bound → sync + multiprocessing (computing)
|
|
90
|
+
|
|
91
|
+
Don't:
|
|
92
|
+
├── Mix sync and async carelessly
|
|
93
|
+
├── Use sync libraries in async code
|
|
94
|
+
└── Force async for CPU work
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Async Library Selection
|
|
98
|
+
|
|
99
|
+
| Need | Async Library |
|
|
100
|
+
|------|---------------|
|
|
101
|
+
| HTTP client | httpx |
|
|
102
|
+
| PostgreSQL | asyncpg |
|
|
103
|
+
| Redis | aioredis / redis-py async |
|
|
104
|
+
| File I/O | aiofiles |
|
|
105
|
+
| Database ORM | SQLAlchemy 2.0 async, Tortoise |
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## 3. Type Hints Strategy
|
|
110
|
+
|
|
111
|
+
### When to Type
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
Always type:
|
|
115
|
+
├── Function parameters
|
|
116
|
+
├── Return types
|
|
117
|
+
├── Class attributes
|
|
118
|
+
├── Public APIs
|
|
119
|
+
|
|
120
|
+
Can skip:
|
|
121
|
+
├── Local variables (let inference work)
|
|
122
|
+
├── One-off scripts
|
|
123
|
+
├── Tests (usually)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Common Type Patterns
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
# These are patterns, understand them:
|
|
130
|
+
|
|
131
|
+
# Optional → might be None
|
|
132
|
+
from typing import Optional
|
|
133
|
+
def find_user(id: int) -> Optional[User]: ...
|
|
134
|
+
|
|
135
|
+
# Union → one of multiple types
|
|
136
|
+
def process(data: str | dict) -> None: ...
|
|
137
|
+
|
|
138
|
+
# Generic collections
|
|
139
|
+
def get_items() -> list[Item]: ...
|
|
140
|
+
def get_mapping() -> dict[str, int]: ...
|
|
141
|
+
|
|
142
|
+
# Callable
|
|
143
|
+
from typing import Callable
|
|
144
|
+
def apply(fn: Callable[[int], str]) -> str: ...
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Pydantic for Validation
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
When to use Pydantic:
|
|
151
|
+
├── API request/response models
|
|
152
|
+
├── Configuration/settings
|
|
153
|
+
├── Data validation
|
|
154
|
+
├── Serialization
|
|
155
|
+
|
|
156
|
+
Benefits:
|
|
157
|
+
├── Runtime validation
|
|
158
|
+
├── Auto-generated JSON schema
|
|
159
|
+
├── Works with FastAPI natively
|
|
160
|
+
└── Clear error messages
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## 4. Project Structure Principles
|
|
166
|
+
|
|
167
|
+
### Structure Selection
|
|
168
|
+
|
|
169
|
+
```
|
|
170
|
+
Small project / Script:
|
|
171
|
+
├── main.py
|
|
172
|
+
├── utils.py
|
|
173
|
+
└── requirements.txt
|
|
174
|
+
|
|
175
|
+
Medium API:
|
|
176
|
+
├── app/
|
|
177
|
+
│ ├── __init__.py
|
|
178
|
+
│ ├── main.py
|
|
179
|
+
│ ├── models/
|
|
180
|
+
│ ├── routes/
|
|
181
|
+
│ ├── services/
|
|
182
|
+
│ └── schemas/
|
|
183
|
+
├── tests/
|
|
184
|
+
└── pyproject.toml
|
|
185
|
+
|
|
186
|
+
Large application:
|
|
187
|
+
├── src/
|
|
188
|
+
│ └── myapp/
|
|
189
|
+
│ ├── core/
|
|
190
|
+
│ ├── api/
|
|
191
|
+
│ ├── services/
|
|
192
|
+
│ ├── models/
|
|
193
|
+
│ └── ...
|
|
194
|
+
├── tests/
|
|
195
|
+
└── pyproject.toml
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### FastAPI Structure Principles
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
Organize by feature or layer:
|
|
202
|
+
|
|
203
|
+
By layer:
|
|
204
|
+
├── routes/ (API endpoints)
|
|
205
|
+
├── services/ (business logic)
|
|
206
|
+
├── models/ (database models)
|
|
207
|
+
├── schemas/ (Pydantic models)
|
|
208
|
+
└── dependencies/ (shared deps)
|
|
209
|
+
|
|
210
|
+
By feature:
|
|
211
|
+
├── users/
|
|
212
|
+
│ ├── routes.py
|
|
213
|
+
│ ├── service.py
|
|
214
|
+
│ └── schemas.py
|
|
215
|
+
└── products/
|
|
216
|
+
└── ...
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## 5. Django Principles (2025)
|
|
222
|
+
|
|
223
|
+
### Django Async (Django 5.0+)
|
|
224
|
+
|
|
225
|
+
```
|
|
226
|
+
Django supports async:
|
|
227
|
+
├── Async views
|
|
228
|
+
├── Async middleware
|
|
229
|
+
├── Async ORM (limited)
|
|
230
|
+
└── ASGI deployment
|
|
231
|
+
|
|
232
|
+
When to use async in Django:
|
|
233
|
+
├── External API calls
|
|
234
|
+
├── WebSocket (Channels)
|
|
235
|
+
├── High-concurrency views
|
|
236
|
+
└── Background task triggering
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Django Best Practices
|
|
240
|
+
|
|
241
|
+
```
|
|
242
|
+
Model design:
|
|
243
|
+
├── Fat models, thin views
|
|
244
|
+
├── Use managers for common queries
|
|
245
|
+
├── Abstract base classes for shared fields
|
|
246
|
+
|
|
247
|
+
Views:
|
|
248
|
+
├── Class-based for complex CRUD
|
|
249
|
+
├── Function-based for simple endpoints
|
|
250
|
+
├── Use viewsets with DRF
|
|
251
|
+
|
|
252
|
+
Queries:
|
|
253
|
+
├── select_related() for FKs
|
|
254
|
+
├── prefetch_related() for M2M
|
|
255
|
+
├── Avoid N+1 queries
|
|
256
|
+
└── Use .only() for specific fields
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
## 6. FastAPI Principles
|
|
262
|
+
|
|
263
|
+
### async def vs def in FastAPI
|
|
264
|
+
|
|
265
|
+
```
|
|
266
|
+
Use async def when:
|
|
267
|
+
├── Using async database drivers
|
|
268
|
+
├── Making async HTTP calls
|
|
269
|
+
├── I/O-bound operations
|
|
270
|
+
└── Want to handle concurrency
|
|
271
|
+
|
|
272
|
+
Use def when:
|
|
273
|
+
├── Blocking operations
|
|
274
|
+
├── Sync database drivers
|
|
275
|
+
├── CPU-bound work
|
|
276
|
+
└── FastAPI runs in threadpool automatically
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### Dependency Injection
|
|
280
|
+
|
|
281
|
+
```
|
|
282
|
+
Use dependencies for:
|
|
283
|
+
├── Database sessions
|
|
284
|
+
├── Current user / Auth
|
|
285
|
+
├── Configuration
|
|
286
|
+
├── Shared resources
|
|
287
|
+
|
|
288
|
+
Benefits:
|
|
289
|
+
├── Testability (mock dependencies)
|
|
290
|
+
├── Clean separation
|
|
291
|
+
├── Automatic cleanup (yield)
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Pydantic v2 Integration
|
|
295
|
+
|
|
296
|
+
```python
|
|
297
|
+
# FastAPI + Pydantic are tightly integrated:
|
|
298
|
+
|
|
299
|
+
# Request validation
|
|
300
|
+
@app.post("/users")
|
|
301
|
+
async def create(user: UserCreate) -> UserResponse:
|
|
302
|
+
# user is already validated
|
|
303
|
+
...
|
|
304
|
+
|
|
305
|
+
# Response serialization
|
|
306
|
+
# Return type becomes response schema
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
---
|
|
310
|
+
|
|
311
|
+
## 7. Background Tasks
|
|
312
|
+
|
|
313
|
+
### Selection Guide
|
|
314
|
+
|
|
315
|
+
| Solution | Best For |
|
|
316
|
+
|----------|----------|
|
|
317
|
+
| **BackgroundTasks** | Simple, in-process tasks |
|
|
318
|
+
| **Celery** | Distributed, complex workflows |
|
|
319
|
+
| **ARQ** | Async, Redis-based |
|
|
320
|
+
| **RQ** | Simple Redis queue |
|
|
321
|
+
| **Dramatiq** | Actor-based, simpler than Celery |
|
|
322
|
+
|
|
323
|
+
### When to Use Each
|
|
324
|
+
|
|
325
|
+
```
|
|
326
|
+
FastAPI BackgroundTasks:
|
|
327
|
+
├── Quick operations
|
|
328
|
+
├── No persistence needed
|
|
329
|
+
├── Fire-and-forget
|
|
330
|
+
└── Same process
|
|
331
|
+
|
|
332
|
+
Celery/ARQ:
|
|
333
|
+
├── Long-running tasks
|
|
334
|
+
├── Need retry logic
|
|
335
|
+
├── Distributed workers
|
|
336
|
+
├── Persistent queue
|
|
337
|
+
└── Complex workflows
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
---
|
|
341
|
+
|
|
342
|
+
## 8. Error Handling Principles
|
|
343
|
+
|
|
344
|
+
### Exception Strategy
|
|
345
|
+
|
|
346
|
+
```
|
|
347
|
+
In FastAPI:
|
|
348
|
+
├── Create custom exception classes
|
|
349
|
+
├── Register exception handlers
|
|
350
|
+
├── Return consistent error format
|
|
351
|
+
└── Log without exposing internals
|
|
352
|
+
|
|
353
|
+
Pattern:
|
|
354
|
+
├── Raise domain exceptions in services
|
|
355
|
+
├── Catch and transform in handlers
|
|
356
|
+
└── Client gets clean error response
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
### Error Response Philosophy
|
|
360
|
+
|
|
361
|
+
```
|
|
362
|
+
Include:
|
|
363
|
+
├── Error code (programmatic)
|
|
364
|
+
├── Message (human readable)
|
|
365
|
+
├── Details (field-level when applicable)
|
|
366
|
+
└── NOT stack traces (security)
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
---
|
|
370
|
+
|
|
371
|
+
## 9. Testing Principles
|
|
372
|
+
|
|
373
|
+
### Testing Strategy
|
|
374
|
+
|
|
375
|
+
| Type | Purpose | Tools |
|
|
376
|
+
|------|---------|-------|
|
|
377
|
+
| **Unit** | Business logic | pytest |
|
|
378
|
+
| **Integration** | API endpoints | pytest + httpx/TestClient |
|
|
379
|
+
| **E2E** | Full workflows | pytest + DB |
|
|
380
|
+
|
|
381
|
+
### Async Testing
|
|
382
|
+
|
|
383
|
+
```python
|
|
384
|
+
# Use pytest-asyncio for async tests
|
|
385
|
+
|
|
386
|
+
import pytest
|
|
387
|
+
from httpx import AsyncClient
|
|
388
|
+
|
|
389
|
+
@pytest.mark.asyncio
|
|
390
|
+
async def test_endpoint():
|
|
391
|
+
async with AsyncClient(app=app, base_url="http://test") as client:
|
|
392
|
+
response = await client.get("/users")
|
|
393
|
+
assert response.status_code == 200
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### Fixtures Strategy
|
|
397
|
+
|
|
398
|
+
```
|
|
399
|
+
Common fixtures:
|
|
400
|
+
├── db_session → Database connection
|
|
401
|
+
├── client → Test client
|
|
402
|
+
├── authenticated_user → User with token
|
|
403
|
+
└── sample_data → Test data setup
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
---
|
|
407
|
+
|
|
408
|
+
## 10. Decision Checklist
|
|
409
|
+
|
|
410
|
+
Before implementing:
|
|
411
|
+
|
|
412
|
+
- [ ] **Asked user about framework preference?**
|
|
413
|
+
- [ ] **Chosen framework for THIS context?** (not just default)
|
|
414
|
+
- [ ] **Decided async vs sync?**
|
|
415
|
+
- [ ] **Planned type hint strategy?**
|
|
416
|
+
- [ ] **Defined project structure?**
|
|
417
|
+
- [ ] **Planned error handling?**
|
|
418
|
+
- [ ] **Considered background tasks?**
|
|
419
|
+
|
|
420
|
+
---
|
|
421
|
+
|
|
422
|
+
## 11. Anti-Patterns to Avoid
|
|
423
|
+
|
|
424
|
+
### ❌ DON'T:
|
|
425
|
+
- Default to Django for simple APIs (FastAPI may be better)
|
|
426
|
+
- Use sync libraries in async code
|
|
427
|
+
- Skip type hints for public APIs
|
|
428
|
+
- Put business logic in routes/views
|
|
429
|
+
- Ignore N+1 queries
|
|
430
|
+
- Mix async and sync carelessly
|
|
431
|
+
|
|
432
|
+
### ✅ DO:
|
|
433
|
+
- Choose framework based on context
|
|
434
|
+
- Ask about async requirements
|
|
435
|
+
- Use Pydantic for validation
|
|
436
|
+
- Separate concerns (routes → services → repos)
|
|
437
|
+
- Test critical paths
|
|
438
|
+
|
|
439
|
+
---
|
|
440
|
+
|
|
441
|
+
> **Remember**: Python patterns are about decision-making for YOUR specific context. Don't copy code—think about what serves your application best.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: python-patterns
|
|
3
|
+
description: Python idioms and patterns - context managers, protocols, generators, async, Django/FastAPI. Use when writing Python code.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Python Patterns
|
|
7
|
+
|
|
8
|
+
## When to Activate
|
|
9
|
+
- Writing Python applications
|
|
10
|
+
- Choosing design patterns for Python
|
|
11
|
+
- Working with Django or FastAPI
|
|
12
|
+
- Async programming decisions
|
|
13
|
+
|
|
14
|
+
## Context Managers
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from contextlib import contextmanager
|
|
18
|
+
|
|
19
|
+
@contextmanager
|
|
20
|
+
def managed_connection(url: str):
|
|
21
|
+
conn = create_connection(url)
|
|
22
|
+
try:
|
|
23
|
+
yield conn
|
|
24
|
+
finally:
|
|
25
|
+
conn.close()
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- Use `with` statements for resource management
|
|
29
|
+
- Write custom context managers for setup/teardown pairs
|
|
30
|
+
|
|
31
|
+
## Protocol Types (Structural Typing)
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from typing import Protocol
|
|
35
|
+
|
|
36
|
+
class Repository(Protocol):
|
|
37
|
+
def find_by_id(self, id: str) -> dict | None: ...
|
|
38
|
+
def save(self, entity: dict) -> dict: ...
|
|
39
|
+
|
|
40
|
+
# Any class with these methods satisfies the protocol
|
|
41
|
+
class PostgresRepo:
|
|
42
|
+
def find_by_id(self, id: str) -> dict | None:
|
|
43
|
+
...
|
|
44
|
+
def save(self, entity: dict) -> dict:
|
|
45
|
+
...
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Generators for Large Data
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
def read_large_file(path: str):
|
|
52
|
+
with open(path) as f:
|
|
53
|
+
for line in f:
|
|
54
|
+
yield line.strip()
|
|
55
|
+
|
|
56
|
+
# Process without loading all into memory
|
|
57
|
+
for line in read_large_file("data.csv"):
|
|
58
|
+
process(line)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Async Patterns
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
import asyncio
|
|
65
|
+
import httpx
|
|
66
|
+
|
|
67
|
+
async def fetch_all(urls: list[str]) -> list[dict]:
|
|
68
|
+
async with httpx.AsyncClient() as client:
|
|
69
|
+
tasks = [client.get(url) for url in urls]
|
|
70
|
+
responses = await asyncio.gather(*tasks)
|
|
71
|
+
return [r.json() for r in responses]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
- Use `asyncio.gather` for concurrent I/O
|
|
75
|
+
- Use `async with` for async context managers
|
|
76
|
+
- Never mix sync and async without `asyncio.to_thread`
|
|
77
|
+
|
|
78
|
+
## Django Specifics
|
|
79
|
+
|
|
80
|
+
- Fat models, thin views
|
|
81
|
+
- Use `select_related` / `prefetch_related` to avoid N+1
|
|
82
|
+
- Custom managers for complex querysets
|
|
83
|
+
- Signals only for cross-app concerns
|
|
84
|
+
|
|
85
|
+
## FastAPI Specifics
|
|
86
|
+
|
|
87
|
+
- Use Pydantic models for request/response validation
|
|
88
|
+
- Dependency injection via `Depends()`
|
|
89
|
+
- Background tasks for non-blocking operations
|
|
90
|
+
- Async endpoints for I/O-bound work
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: python-testing
|
|
3
|
+
description: Python testing with pytest - fixtures, parametrize, mocking, coverage. Use when writing or reviewing Python tests.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Python Testing
|
|
7
|
+
|
|
8
|
+
## When to Activate
|
|
9
|
+
- Writing tests for Python code
|
|
10
|
+
- Setting up pytest fixtures
|
|
11
|
+
- Choosing mocking strategy
|
|
12
|
+
- Reviewing test coverage
|
|
13
|
+
|
|
14
|
+
## Framework: pytest
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
def test_create_user_returns_user_with_id():
|
|
18
|
+
# Arrange
|
|
19
|
+
data = {"name": "Alice", "email": "alice@example.com"}
|
|
20
|
+
|
|
21
|
+
# Act
|
|
22
|
+
user = create_user(data)
|
|
23
|
+
|
|
24
|
+
# Assert
|
|
25
|
+
assert user.id is not None
|
|
26
|
+
assert user.name == "Alice"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Fixtures
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import pytest
|
|
33
|
+
|
|
34
|
+
@pytest.fixture
|
|
35
|
+
def db_session():
|
|
36
|
+
session = create_test_session()
|
|
37
|
+
yield session
|
|
38
|
+
session.rollback()
|
|
39
|
+
session.close()
|
|
40
|
+
|
|
41
|
+
@pytest.fixture
|
|
42
|
+
def sample_user(db_session):
|
|
43
|
+
return UserFactory.create(session=db_session)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- Use fixtures for setup/teardown
|
|
47
|
+
- Scope appropriately: `function`, `class`, `module`, `session`
|
|
48
|
+
- Use `conftest.py` for shared fixtures
|
|
49
|
+
|
|
50
|
+
## Parametrize
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
@pytest.mark.parametrize("input,expected", [
|
|
54
|
+
("hello", "HELLO"),
|
|
55
|
+
("world", "WORLD"),
|
|
56
|
+
("", ""),
|
|
57
|
+
])
|
|
58
|
+
def test_uppercase(input, expected):
|
|
59
|
+
assert uppercase(input) == expected
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Mocking
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from unittest.mock import patch, MagicMock
|
|
66
|
+
|
|
67
|
+
def test_send_email_calls_smtp():
|
|
68
|
+
with patch("app.email.smtp_client") as mock_smtp:
|
|
69
|
+
send_welcome_email("user@test.com")
|
|
70
|
+
mock_smtp.send.assert_called_once()
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
- Mock at the boundary (external APIs, databases)
|
|
74
|
+
- Prefer dependency injection over patching
|
|
75
|
+
- Don't mock what you don't own without integration tests
|
|
76
|
+
|
|
77
|
+
## Coverage
|
|
78
|
+
|
|
79
|
+
- Minimum 80% coverage
|
|
80
|
+
- Run: `pytest --cov=src --cov-report=term-missing`
|
|
81
|
+
- Focus on behavior coverage, not line coverage
|