@sylix/coworker 2.0.10 → 2.0.12

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.
Files changed (178) hide show
  1. package/dist/commands/slash/config.d.ts.map +1 -1
  2. package/dist/commands/slash/config.js +23 -5
  3. package/dist/commands/slash/config.js.map +1 -1
  4. package/dist/commands/slash/todo.js +1 -1
  5. package/dist/commands/slash/todo.js.map +1 -1
  6. package/dist/core/CoWorkerAgent.d.ts.map +1 -1
  7. package/dist/core/CoWorkerAgent.js +6 -3
  8. package/dist/core/CoWorkerAgent.js.map +1 -1
  9. package/dist/permissions/PermissionInterceptor.js +1 -1
  10. package/dist/permissions/PermissionInterceptor.js.map +1 -1
  11. package/dist/skills/defaults/accessibility/screen-reader-testing.md +545 -0
  12. package/dist/skills/defaults/accessibility/wcag-audit-patterns.md +555 -0
  13. package/dist/skills/defaults/ai-ml/rag.md +276 -0
  14. package/dist/skills/defaults/backend-development/api-design-principles.md +528 -0
  15. package/dist/skills/defaults/backend-development/api-design.md +285 -0
  16. package/dist/skills/defaults/backend-development/architecture-patterns.md +494 -0
  17. package/dist/skills/defaults/backend-development/async-python.md +237 -0
  18. package/dist/skills/defaults/backend-development/auth-implementation-patterns.md +638 -0
  19. package/dist/skills/defaults/backend-development/bazel-build-optimization.md +387 -0
  20. package/dist/skills/defaults/backend-development/billing-automation/SKILL.md +566 -0
  21. package/dist/skills/defaults/backend-development/code-review-excellence.md +538 -0
  22. package/dist/skills/defaults/backend-development/cqrs-implementation.md +554 -0
  23. package/dist/skills/defaults/backend-development/database-design.md +305 -0
  24. package/dist/skills/defaults/backend-development/debugging-strategies.md +536 -0
  25. package/dist/skills/defaults/backend-development/e2e-testing-patterns.md +544 -0
  26. package/dist/skills/defaults/backend-development/error-handling-patterns.md +641 -0
  27. package/dist/skills/defaults/backend-development/fastapi-templates.md +559 -0
  28. package/dist/skills/defaults/backend-development/fastapi.md +309 -0
  29. package/dist/skills/defaults/backend-development/git-advanced-workflows.md +405 -0
  30. package/dist/skills/defaults/backend-development/microservices-patterns.md +595 -0
  31. package/dist/skills/defaults/backend-development/microservices.md +284 -0
  32. package/dist/skills/defaults/backend-development/monorepo-management.md +623 -0
  33. package/dist/skills/defaults/backend-development/nodejs-backend-patterns.md +1048 -0
  34. package/dist/skills/defaults/backend-development/nx-workspace-patterns.md +457 -0
  35. package/dist/skills/defaults/backend-development/paypal-integration/SKILL.md +478 -0
  36. package/dist/skills/defaults/backend-development/pci-compliance/SKILL.md +480 -0
  37. package/dist/skills/defaults/backend-development/python-anti-patterns.md +349 -0
  38. package/dist/skills/defaults/backend-development/python-background-jobs.md +364 -0
  39. package/dist/skills/defaults/backend-development/python-code-style.md +360 -0
  40. package/dist/skills/defaults/backend-development/python-configuration.md +368 -0
  41. package/dist/skills/defaults/backend-development/python-design-patterns.md +296 -0
  42. package/dist/skills/defaults/backend-development/python-error-handling.md +323 -0
  43. package/dist/skills/defaults/backend-development/python-packaging.md +887 -0
  44. package/dist/skills/defaults/backend-development/python-performance-optimization.md +874 -0
  45. package/dist/skills/defaults/backend-development/python-project-structure.md +252 -0
  46. package/dist/skills/defaults/backend-development/python-resilience.md +376 -0
  47. package/dist/skills/defaults/backend-development/python-resource-management.md +421 -0
  48. package/dist/skills/defaults/backend-development/python-type-safety.md +428 -0
  49. package/dist/skills/defaults/backend-development/sql-optimization-patterns.md +509 -0
  50. package/dist/skills/defaults/backend-development/stripe-integration/SKILL.md +522 -0
  51. package/dist/skills/defaults/backend-development/turborepo-caching.md +376 -0
  52. package/dist/skills/defaults/blockchain/defi-protocol-templates.md +430 -0
  53. package/dist/skills/defaults/blockchain/nft-standards.md +364 -0
  54. package/dist/skills/defaults/blockchain/solidity-security.md +514 -0
  55. package/dist/skills/defaults/blockchain/web3-testing.md +360 -0
  56. package/dist/skills/defaults/business/competitive-landscape/SKILL.md +527 -0
  57. package/dist/skills/defaults/business/market-sizing-analysis/SKILL.md +451 -0
  58. package/dist/skills/defaults/business/startup-financial-modeling/SKILL.md +494 -0
  59. package/dist/skills/defaults/business/startup-metrics-framework/SKILL.md +564 -0
  60. package/dist/skills/defaults/business/team-composition-analysis.md +437 -0
  61. package/dist/skills/defaults/compliance/employment-contract-templates/SKILL.md +527 -0
  62. package/dist/skills/defaults/compliance/gdpr-data-handling/SKILL.md +630 -0
  63. package/dist/skills/defaults/data-engineering/airflow-dag-patterns.md +436 -0
  64. package/dist/skills/defaults/data-engineering/airflow.md +519 -0
  65. package/dist/skills/defaults/data-engineering/data-quality.md +583 -0
  66. package/dist/skills/defaults/data-engineering/dbt-transformation-patterns.md +482 -0
  67. package/dist/skills/defaults/data-engineering/dbt.md +556 -0
  68. package/dist/skills/defaults/data-engineering/ml-pipeline-workflow/SKILL.md +247 -0
  69. package/dist/skills/defaults/data-engineering/spark-optimization.md +348 -0
  70. package/dist/skills/defaults/data-engineering/spark.md +411 -0
  71. package/dist/skills/defaults/database/postgresql.md +202 -0
  72. package/dist/skills/defaults/debugging/systematic-debugging.md +249 -0
  73. package/dist/skills/defaults/devops/architecture-decision-records.md +448 -0
  74. package/dist/skills/defaults/devops/changelog-automation.md +580 -0
  75. package/dist/skills/defaults/devops/cicd.md +314 -0
  76. package/dist/skills/defaults/devops/cloud.md +263 -0
  77. package/dist/skills/defaults/devops/code-review-excellence.md +299 -0
  78. package/dist/skills/defaults/devops/cost-optimization.md +295 -0
  79. package/dist/skills/defaults/devops/deployment-pipeline-design.md +356 -0
  80. package/dist/skills/defaults/devops/docker.md +281 -0
  81. package/dist/skills/defaults/devops/git-workflows.md +205 -0
  82. package/dist/skills/defaults/devops/github-actions.md +311 -0
  83. package/dist/skills/defaults/devops/gitlab-ci-patterns.md +266 -0
  84. package/dist/skills/defaults/devops/hybrid-cloud-networking.md +241 -0
  85. package/dist/skills/defaults/devops/istio-traffic-management.md +327 -0
  86. package/dist/skills/defaults/devops/kubernetes.md +339 -0
  87. package/dist/skills/defaults/devops/linkerd-patterns.md +311 -0
  88. package/dist/skills/defaults/devops/multi-cloud-architecture.md +181 -0
  89. package/dist/skills/defaults/devops/observability.md +243 -0
  90. package/dist/skills/defaults/devops/openapi-spec-generation.md +1024 -0
  91. package/dist/skills/defaults/devops/postmortem-writing.md +396 -0
  92. package/dist/skills/defaults/devops/prometheus-configuration.md +265 -0
  93. package/dist/skills/defaults/devops/secrets-management.md +341 -0
  94. package/dist/skills/defaults/devops/service-mesh-observability.md +385 -0
  95. package/dist/skills/defaults/devops/terraform-module-library.md +244 -0
  96. package/dist/skills/defaults/finance/backtesting-frameworks/SKILL.md +663 -0
  97. package/dist/skills/defaults/finance/risk-metrics-calculation/SKILL.md +557 -0
  98. package/dist/skills/defaults/frontend/accessibility-compliance.md +420 -0
  99. package/dist/skills/defaults/frontend/design-system-patterns.md +337 -0
  100. package/dist/skills/defaults/frontend/interaction-design.md +327 -0
  101. package/dist/skills/defaults/frontend/javascript.md +311 -0
  102. package/dist/skills/defaults/frontend/modern-javascript-patterns.md +927 -0
  103. package/dist/skills/defaults/frontend/react-native-design.md +440 -0
  104. package/dist/skills/defaults/frontend/react.md +345 -0
  105. package/dist/skills/defaults/frontend/responsive-design.md +472 -0
  106. package/dist/skills/defaults/frontend/tailwind-design-system.md +337 -0
  107. package/dist/skills/defaults/frontend/typescript-advanced-types.md +724 -0
  108. package/dist/skills/defaults/frontend/typescript.md +334 -0
  109. package/dist/skills/defaults/frontend/visual-design-foundations.md +326 -0
  110. package/dist/skills/defaults/frontend/web-component-design.md +279 -0
  111. package/dist/skills/defaults/game-development/godot-gdscript-patterns.md +188 -0
  112. package/dist/skills/defaults/game-development/unity-ecs-patterns.md +594 -0
  113. package/dist/skills/defaults/kubernetes/gitops-workflow.md +285 -0
  114. package/dist/skills/defaults/kubernetes/gitops.md +280 -0
  115. package/dist/skills/defaults/kubernetes/helm-chart-scaffolding.md +553 -0
  116. package/dist/skills/defaults/kubernetes/helm.md +343 -0
  117. package/dist/skills/defaults/kubernetes/k8s-manifest-generator.md +501 -0
  118. package/dist/skills/defaults/kubernetes/k8s-security-policies.md +342 -0
  119. package/dist/skills/defaults/kubernetes/manifests.md +330 -0
  120. package/dist/skills/defaults/kubernetes/security.md +337 -0
  121. package/dist/skills/defaults/llm-application/embedding-strategies.md +608 -0
  122. package/dist/skills/defaults/llm-application/hybrid-search-implementation.md +570 -0
  123. package/dist/skills/defaults/llm-application/hybrid-search.md +570 -0
  124. package/dist/skills/defaults/llm-application/langchain-architecture.md +666 -0
  125. package/dist/skills/defaults/llm-application/langchain.md +259 -0
  126. package/dist/skills/defaults/llm-application/llm-evaluation.md +695 -0
  127. package/dist/skills/defaults/llm-application/prompt-engineering-patterns.md +449 -0
  128. package/dist/skills/defaults/llm-application/prompt-engineering.md +219 -0
  129. package/dist/skills/defaults/llm-application/rag-implementation.md +434 -0
  130. package/dist/skills/defaults/llm-application/similarity-search-patterns.md +560 -0
  131. package/dist/skills/defaults/llm-application/similarity-search.md +560 -0
  132. package/dist/skills/defaults/llm-application/vector-index-tuning.md +523 -0
  133. package/dist/skills/defaults/mobile/mobile-android-design.md +440 -0
  134. package/dist/skills/defaults/mobile/mobile-ios-design.md +266 -0
  135. package/dist/skills/defaults/monitoring/distributed-tracing.md +436 -0
  136. package/dist/skills/defaults/monitoring/grafana-dashboards.md +370 -0
  137. package/dist/skills/defaults/monitoring/prometheus-configuration.md +379 -0
  138. package/dist/skills/defaults/monitoring/slo-implementation.md +323 -0
  139. package/dist/skills/defaults/refactoring/code-refactoring.md +349 -0
  140. package/dist/skills/defaults/security/anti-reversing-techniques/SKILL.md +559 -0
  141. package/dist/skills/defaults/security/auditor.md +168 -0
  142. package/dist/skills/defaults/security/binary-analysis-patterns/SKILL.md +438 -0
  143. package/dist/skills/defaults/security/memory-forensics/SKILL.md +483 -0
  144. package/dist/skills/defaults/security/mtls-configuration.md +349 -0
  145. package/dist/skills/defaults/security/protocol-reverse-engineering/SKILL.md +520 -0
  146. package/dist/skills/defaults/security/sast-configuration.md +182 -0
  147. package/dist/skills/defaults/security/security.md +313 -0
  148. package/dist/skills/defaults/security/stride-analysis.md +273 -0
  149. package/dist/skills/defaults/security/threat-mitigation-mapping.md +290 -0
  150. package/dist/skills/defaults/systems/bash-defensive-patterns/SKILL.md +539 -0
  151. package/dist/skills/defaults/systems/bats-testing-patterns/SKILL.md +631 -0
  152. package/dist/skills/defaults/systems/go-concurrency-patterns.md +657 -0
  153. package/dist/skills/defaults/systems/memory-safety-patterns.md +605 -0
  154. package/dist/skills/defaults/systems/rust-async-patterns.md +519 -0
  155. package/dist/skills/defaults/systems/shellcheck-configuration/SKILL.md +456 -0
  156. package/dist/skills/defaults/team-collaboration/multi-reviewer-patterns.md +126 -0
  157. package/dist/skills/defaults/team-collaboration/parallel-feature-development.md +151 -0
  158. package/dist/skills/defaults/testing/javascript-testing-patterns.md +1021 -0
  159. package/dist/skills/defaults/testing/python-testing-patterns.md +351 -0
  160. package/dist/skills/defaults/testing/testing.md +332 -0
  161. package/dist/skills/defaults/workflows/context-driven-development.md +384 -0
  162. package/dist/skills/defaults/workflows/track-management.md +592 -0
  163. package/dist/skills/defaults/workflows/workflow-patterns.md +622 -0
  164. package/dist/skills/index.d.ts +11 -0
  165. package/dist/skills/index.d.ts.map +1 -0
  166. package/dist/skills/index.js +129 -0
  167. package/dist/skills/index.js.map +1 -0
  168. package/dist/utils/character.js +6 -9
  169. package/dist/utils/character.js.map +1 -1
  170. package/dist/utils/contextManager.js +3 -7
  171. package/dist/utils/contextManager.js.map +1 -1
  172. package/dist/utils/inputbar.d.ts.map +1 -1
  173. package/dist/utils/inputbar.js +8 -1
  174. package/dist/utils/inputbar.js.map +1 -1
  175. package/dist/utils/output.d.ts.map +1 -1
  176. package/dist/utils/output.js +3 -35
  177. package/dist/utils/output.js.map +1 -1
  178. package/package.json +1 -1
@@ -0,0 +1,296 @@
1
+ ---
2
+ name: python-design-patterns
3
+ description: Write maintainable Python code using fundamental design principles and patterns.
4
+ ---
5
+
6
+ # Python Design Patterns — CoWorker Edition
7
+
8
+ Build Python code that's easy to understand, test, and modify.
9
+
10
+ ## When to Use This Skill
11
+
12
+ - Designing new components or services
13
+ - Refactoring complex code
14
+ - Deciding between abstractions
15
+ - Choosing inheritance vs composition
16
+
17
+ ## Core Principles
18
+
19
+ ### 1. KISS (Keep It Simple)
20
+
21
+ Choose the simplest solution that works:
22
+
23
+ ```python
24
+ # Over-engineered: Factory with registration
25
+ class OutputFormatterFactory:
26
+ _formatters: dict[str, type] = {}
27
+
28
+ @classmethod
29
+ def register(cls, name: str):
30
+ def decorator(formatter_cls):
31
+ cls._formatters[name] = formatter_cls
32
+ return formatter_cls
33
+ return decorator
34
+
35
+ @classmethod
36
+ def create(cls, name: str):
37
+ return cls._formatters[name]()
38
+
39
+ @OutputFormatterFactory.register("json")
40
+ class JsonFormatter:
41
+ ...
42
+
43
+ # Simple: Just use a dictionary
44
+ FORMATTERS: dict[str, type] = {
45
+ "json": JsonFormatter,
46
+ "csv": CsvFormatter,
47
+ }
48
+
49
+ def get_formatter(name: str):
50
+ if name not in FORMATTERS:
51
+ raise ValueError(f"Unknown format: {name}")
52
+ return FORMATTERS[name]()
53
+ ```
54
+
55
+ ### 2. Single Responsibility
56
+
57
+ Each class does one thing:
58
+
59
+ ```python
60
+ # BAD: Handler does everything
61
+ class UserHandler:
62
+ async def create_user(self, request: Request):
63
+ # HTTP parsing
64
+ data = await request.json()
65
+
66
+ # Validation
67
+ if not data.get("email"):
68
+ return Response({"error": "email required"}, status=400)
69
+
70
+ # Database access
71
+ user = await db.execute(
72
+ "INSERT INTO users VALUES ($1, $2)",
73
+ data["email"], data["name"]
74
+ )
75
+
76
+ return Response({"id": user.id}, status=201)
77
+
78
+ # GOOD: Separated concerns
79
+ class UserService:
80
+ """Business logic only."""
81
+
82
+ def __init__(self, repo: UserRepository):
83
+ self._repo = repo
84
+
85
+ async def create_user(self, data: CreateUserInput):
86
+ user = User(email=data.email, name=data.name)
87
+ return await self._repo.save(user)
88
+
89
+ class UserHandler:
90
+ """HTTP concerns only."""
91
+
92
+ def __init__(self, service: UserService):
93
+ self._service = service
94
+
95
+ async def create_user(self, request: Request):
96
+ data = CreateUserInput(**await request.json())
97
+ user = await self._service.create_user(data)
98
+ return Response(user.to_dict(), status=201)
99
+ ```
100
+
101
+ ### 3. Separation of Layers
102
+
103
+ ```
104
+ ┌─────────────────────────────────────────┐
105
+ │ API Layer (handlers) │
106
+ │ - Parse requests │
107
+ │ - Call services │
108
+ │ - Format responses │
109
+ └─────────────────────────────────────────┘
110
+
111
+
112
+ ┌─────────────────────────────────────────┐
113
+ │ Service Layer (business logic) │
114
+ │ - Domain rules │
115
+ │ - Orchestration │
116
+ └─────────────────────────────────────────┘
117
+
118
+
119
+ ┌─────────────────────────────────────────┐
120
+ │ Repository Layer (data access) │
121
+ │ - SQL queries │
122
+ │ - External APIs │
123
+ └─────────────────────────────────────────┘
124
+ ```
125
+
126
+ ```python
127
+ # Repository
128
+ class UserRepository:
129
+ async def get_by_id(self, user_id: str) -> User | None:
130
+ row = await self.db.fetchrow(
131
+ "SELECT * FROM users WHERE id = $1", user_id
132
+ )
133
+ return User(**row) if row else None
134
+
135
+ # Service
136
+ class UserService:
137
+ def __init__(self, repo: UserRepository):
138
+ self._repo = repo
139
+
140
+ async def get_user(self, user_id: str) -> User:
141
+ user = await self._repo.get_by_id(user_id)
142
+ if user is None:
143
+ raise UserNotFoundError(user_id)
144
+ return user
145
+
146
+ # Handler
147
+ @app.get("/users/{user_id}")
148
+ async def get_user(user_id: str):
149
+ user = await user_service.get_user(user_id)
150
+ return UserResponse.from_user(user)
151
+ ```
152
+
153
+ ### 4. Composition Over Inheritance
154
+
155
+ ```python
156
+ # Inheritance: Rigid and hard to test
157
+ class EmailNotificationService(NotificationService):
158
+ def __init__(self):
159
+ super().__init__()
160
+ self._smtp = SmtpClient()
161
+
162
+ def notify(self, user, message):
163
+ self._smtp.send(user.email, message)
164
+
165
+ # Composition: Flexible and testable
166
+ class NotificationService:
167
+ def __init__(
168
+ self,
169
+ email_sender: EmailSender,
170
+ sms_sender: SmsSender | None = None,
171
+ ):
172
+ self._email = email_sender
173
+ self._sms = sms_sender
174
+
175
+ async def notify(self, user, message, channels=None):
176
+ channels = channels or {"email"}
177
+
178
+ if "email" in channels:
179
+ await self._email.send(user.email, message)
180
+
181
+ if "sms" in channels and self._sms and user.phone:
182
+ await self._sms.send(user.phone, message)
183
+
184
+ # Easy to test
185
+ service = NotificationService(
186
+ email_sender=FakeEmailSender(),
187
+ sms_sender=FakeSmsSender(),
188
+ )
189
+ ```
190
+
191
+ ### 5. Dependency Injection
192
+
193
+ ```python
194
+ from typing import Protocol
195
+
196
+ class Logger(Protocol):
197
+ def info(self, msg: str): ...
198
+ def error(self, msg: str): ...
199
+
200
+ class Cache(Protocol):
201
+ async def get(self, key: str): str | None: ...
202
+ async def set(self, key: str, value: str, ttl: int): ...
203
+
204
+ class UserService:
205
+ def __init__(
206
+ self,
207
+ repository: UserRepository,
208
+ cache: Cache,
209
+ logger: Logger,
210
+ ):
211
+ self._repo = repository
212
+ self._cache = cache
213
+ self._logger = logger
214
+
215
+ async def get_user(self, user_id: str):
216
+ cached = await self._cache.get(f"user:{user_id}")
217
+ if cached:
218
+ self._logger.info("Cache hit", user_id=user_id)
219
+ return User.from_json(cached)
220
+
221
+ user = await self._repo.get_by_id(user_id)
222
+ if user:
223
+ await self._cache.set(f"user:{user_id}", user.to_json(), ttl=300)
224
+
225
+ return user
226
+
227
+ # Production
228
+ service = UserService(
229
+ repository=PostgresUserRepository(db),
230
+ cache=RedisCache(redis),
231
+ logger=StructlogLogger(),
232
+ )
233
+
234
+ # Testing
235
+ service = UserService(
236
+ repository=InMemoryUserRepository(),
237
+ cache=FakeCache(),
238
+ logger=NullLogger(),
239
+ )
240
+ ```
241
+
242
+ ### 6. Rule of Three
243
+
244
+ Wait until you have three similar cases before abstracting:
245
+
246
+ ```python
247
+ # Two similar functions? Don't abstract yet
248
+ def process_orders(orders: list[Order]) -> list[Result]:
249
+ results = []
250
+ for order in orders:
251
+ validated = validate_order(order)
252
+ result = process_validated_order(validated)
253
+ results.append(result)
254
+ return results
255
+
256
+ def process_returns(returns: list[Return]) -> list[Result]:
257
+ results = []
258
+ for ret in returns:
259
+ validated = validate_return(ret)
260
+ result = process_validated_return(validated)
261
+ results.append(result)
262
+ return results
263
+
264
+ # These look similar, but different validation/processing
265
+ # Duplication is often better than wrong abstraction
266
+ ```
267
+
268
+ ### 7. Function Size Guidelines
269
+
270
+ ```python
271
+ # Too long, multiple concerns
272
+ def process_order(order):
273
+ # 50 lines of validation...
274
+ # 30 lines of inventory...
275
+ # 40 lines of payment...
276
+ pass
277
+
278
+ # Better: Composed from focused functions
279
+ def process_order(order: Order) -> Result:
280
+ validate_order(order)
281
+ reserve_inventory(order)
282
+ payment_result = charge_payment(order)
283
+ send_confirmation(order, payment_result)
284
+ return Result(success=True, order_id=order.id)
285
+ ```
286
+
287
+ ## Best Practices
288
+
289
+ 1. **Keep it simple** - Simpler is better
290
+ 2. **Single responsibility** - One reason to change
291
+ 3. **Separate concerns** - Distinct layers
292
+ 4. **Compose, don't inherit** - Flexibility
293
+ 5. **Rule of three** - Wait before abstracting
294
+ 6. **Small functions** - 20-50 lines max
295
+ 7. **Inject dependencies** - Testability
296
+ 8. **Explicit over clever** - Readable wins
@@ -0,0 +1,323 @@
1
+ ---
2
+ name: python-error-handling
3
+ description: Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.
4
+ ---
5
+
6
+ # Python Error Handling
7
+
8
+ Build robust Python applications with proper input validation, meaningful exceptions, and graceful failure handling. Good error handling makes debugging easier and systems more reliable.
9
+
10
+ ## When to Use This Skill
11
+
12
+ - Validating user input and API parameters
13
+ - Designing exception hierarchies for applications
14
+ - Handling partial failures in batch operations
15
+ - Converting external data to domain types
16
+ - Building user-friendly error messages
17
+ - Implementing fail-fast validation patterns
18
+
19
+ ## Core Concepts
20
+
21
+ ### 1. Fail Fast
22
+
23
+ Validate inputs early, before expensive operations. Report all validation errors at once when possible.
24
+
25
+ ### 2. Meaningful Exceptions
26
+
27
+ Use appropriate exception types with context. Messages should explain what failed, why, and how to fix it.
28
+
29
+ ### 3. Partial Failures
30
+
31
+ In batch operations, don't let one failure abort everything. Track successes and failures separately.
32
+
33
+ ### 4. Preserve Context
34
+
35
+ Chain exceptions to maintain the full error trail for debugging.
36
+
37
+ ## Quick Start
38
+
39
+ ```python
40
+ def fetch_page(url: str, page_size: int) -> Page:
41
+ if not url:
42
+ raise ValueError("'url' is required")
43
+ if not 1 <= page_size <= 100:
44
+ raise ValueError(f"'page_size' must be 1-100, got {page_size}")
45
+ # Now safe to proceed...
46
+ ```
47
+
48
+ ## Fundamental Patterns
49
+
50
+ ### Pattern 1: Early Input Validation
51
+
52
+ Validate all inputs at API boundaries before any processing begins.
53
+
54
+ ```python
55
+ def process_order(
56
+ order_id: str,
57
+ quantity: int,
58
+ discount_percent: float,
59
+ ) -> OrderResult:
60
+ """Process an order with validation."""
61
+ # Validate required fields
62
+ if not order_id:
63
+ raise ValueError("'order_id' is required")
64
+
65
+ # Validate ranges
66
+ if quantity <= 0:
67
+ raise ValueError(f"'quantity' must be positive, got {quantity}")
68
+
69
+ if not 0 <= discount_percent <= 100:
70
+ raise ValueError(
71
+ f"'discount_percent' must be 0-100, got {discount_percent}"
72
+ )
73
+
74
+ # Validation passed, proceed with processing
75
+ return _process_validated_order(order_id, quantity, discount_percent)
76
+ ```
77
+
78
+ ### Pattern 2: Convert to Domain Types Early
79
+
80
+ Parse strings and external data into typed domain objects at system boundaries.
81
+
82
+ ```python
83
+ from enum import Enum
84
+
85
+ class OutputFormat(Enum):
86
+ JSON = "json"
87
+ CSV = "csv"
88
+ PARQUET = "parquet"
89
+
90
+ def parse_output_format(value: str) -> OutputFormat:
91
+ """Parse string to OutputFormat enum."""
92
+ try:
93
+ return OutputFormat(value.lower())
94
+ except ValueError:
95
+ valid_formats = [f.value for f in OutputFormat]
96
+ raise ValueError(
97
+ f"Invalid format '{value}'. "
98
+ f"Valid options: {', '.join(valid_formats)}"
99
+ )
100
+
101
+ def export_data(data: list[dict], format_str: str) -> bytes:
102
+ output_format = parse_output_format(format_str)
103
+ ...
104
+ ```
105
+
106
+ ### Pattern 3: Pydantic for Complex Validation
107
+
108
+ Use Pydantic models for structured input validation with automatic error messages.
109
+
110
+ ```python
111
+ from pydantic import BaseModel, Field, field_validator
112
+
113
+ class CreateUserInput(BaseModel):
114
+ email: str = Field(..., min_length=5, max_length=255)
115
+ name: str = Field(..., min_length=1, max_length=100)
116
+ age: int = Field(ge=0, le=150)
117
+
118
+ @field_validator("email")
119
+ @classmethod
120
+ def validate_email_format(cls, v: str) -> str:
121
+ if "@" not in v or "." not in v.split("@")[-1]:
122
+ raise ValueError("Invalid email format")
123
+ return v.lower()
124
+
125
+ @field_validator("name")
126
+ @classmethod
127
+ def normalize_name(cls, v: str) -> str:
128
+ return v.strip().title()
129
+
130
+ try:
131
+ user_input = CreateUserInput(
132
+ email="user@example.com",
133
+ name="john doe",
134
+ age=25,
135
+ )
136
+ except ValidationError as e:
137
+ print(e.errors())
138
+ ```
139
+
140
+ ### Pattern 4: Map Errors to Standard Exceptions
141
+
142
+ | Failure Type | Exception | Example |
143
+ |--------------|-----------|---------|
144
+ | Invalid input | `ValueError` | Bad parameter values |
145
+ | Wrong type | `TypeError` | Expected string, got int |
146
+ | Missing item | `KeyError` | Dict key not found |
147
+ | Operational failure | `RuntimeError` | Service unavailable |
148
+ | Timeout | `TimeoutError` | Operation took too long |
149
+ | File not found | `FileNotFoundError` | Path doesn't exist |
150
+ | Permission denied | `PermissionError` | Access forbidden |
151
+
152
+ ```python
153
+ # Good: Specific exception with context
154
+ raise ValueError(f"'page_size' must be 1-100, got {page_size}")
155
+
156
+ # Avoid: Generic exception, no context
157
+ raise Exception("Invalid parameter")
158
+ ```
159
+
160
+ ## Advanced Patterns
161
+
162
+ ### Pattern 5: Custom Exceptions with Context
163
+
164
+ Create domain-specific exceptions that carry structured information.
165
+
166
+ ```python
167
+ class ApiError(Exception):
168
+ """Base exception for API errors."""
169
+
170
+ def __init__(
171
+ self,
172
+ message: str,
173
+ status_code: int,
174
+ response_body: str | None = None,
175
+ ) -> None:
176
+ self.status_code = status_code
177
+ self.response_body = response_body
178
+ super().__init__(message)
179
+
180
+ class RateLimitError(ApiError):
181
+ """Raised when rate limit is exceeded."""
182
+
183
+ def __init__(self, retry_after: int) -> None:
184
+ self.retry_after = retry_after
185
+ super().__init__(
186
+ f"Rate limit exceeded. Retry after {retry_after}s",
187
+ status_code=429,
188
+ )
189
+
190
+ def handle_response(response: Response) -> dict:
191
+ match response.status_code:
192
+ case 200:
193
+ return response.json()
194
+ case 401:
195
+ raise ApiError("Invalid credentials", 401)
196
+ case 404:
197
+ raise ApiError(f"Resource not found: {response.url}", 404)
198
+ case 429:
199
+ retry_after = int(response.headers.get("Retry-After", 60))
200
+ raise RateLimitError(retry_after)
201
+ case code if 400 <= code < 500:
202
+ raise ApiError(f"Client error: {response.text}", code)
203
+ case code if code >= 500:
204
+ raise ApiError(f"Server error: {response.text}", code)
205
+ ```
206
+
207
+ ### Pattern 6: Exception Chaining
208
+
209
+ Preserve the original exception when re-raising to maintain the debug trail.
210
+
211
+ ```python
212
+ import httpx
213
+
214
+ class ServiceError(Exception):
215
+ """High-level service operation failed."""
216
+ pass
217
+
218
+ def upload_file(path: str) -> str:
219
+ """Upload file and return URL."""
220
+ try:
221
+ with open(path, "rb") as f:
222
+ response = httpx.post("https://upload.example.com", files={"file": f})
223
+ response.raise_for_status()
224
+ return response.json()["url"]
225
+ except FileNotFoundError as e:
226
+ raise ServiceError(f"Upload failed: file not found at '{path}'") from e
227
+ except httpx.HTTPStatusError as e:
228
+ raise ServiceError(
229
+ f"Upload failed: server returned {e.response.status_code}"
230
+ ) from e
231
+ except httpx.RequestError as e:
232
+ raise ServiceError(f"Upload failed: network error") from e
233
+ ```
234
+
235
+ ### Pattern 7: Batch Processing with Partial Failures
236
+
237
+ Never let one bad item abort an entire batch. Track results per item.
238
+
239
+ ```python
240
+ from dataclasses import dataclass
241
+
242
+ @dataclass
243
+ class BatchResult[T]:
244
+ succeeded: dict[int, T]
245
+ failed: dict[int, Exception]
246
+
247
+ @property
248
+ def success_count(self) -> int:
249
+ return len(self.succeeded)
250
+
251
+ @property
252
+ def failure_count(self) -> int:
253
+ return len(self.failed)
254
+
255
+ @property
256
+ def all_succeeded(self) -> bool:
257
+ return len(self.failed) == 0
258
+
259
+ def process_batch(items: list[Item]) -> BatchResult[ProcessedItem]:
260
+ succeeded: dict[int, ProcessedItem] = {}
261
+ failed: dict[int, Exception] = {}
262
+
263
+ for idx, item in enumerate(items):
264
+ try:
265
+ result = process_single_item(item)
266
+ succeeded[idx] = result
267
+ except Exception as e:
268
+ failed[idx] = e
269
+
270
+ return BatchResult(succeeded=succeeded, failed=failed)
271
+
272
+ result = process_batch(items)
273
+ if not result.all_succeeded:
274
+ logger.warning(
275
+ f"Batch completed with {result.failure_count} failures",
276
+ failed_indices=list(result.failed.keys()),
277
+ )
278
+ ```
279
+
280
+ ### Pattern 8: Progress Reporting for Long Operations
281
+
282
+ Provide visibility into batch progress without coupling business logic to UI.
283
+
284
+ ```python
285
+ from collections.abc import Callable
286
+
287
+ ProgressCallback = Callable[[int, int, str], None]
288
+
289
+ def process_large_batch(
290
+ items: list[Item],
291
+ on_progress: ProgressCallback | None = None,
292
+ ) -> BatchResult:
293
+ total = len(items)
294
+ succeeded = {}
295
+ failed = {}
296
+
297
+ for idx, item in enumerate(items):
298
+ if on_progress:
299
+ on_progress(idx, total, f"Processing {item.id}")
300
+
301
+ try:
302
+ succeeded[idx] = process_single_item(item)
303
+ except Exception as e:
304
+ failed[idx] = e
305
+
306
+ if on_progress:
307
+ on_progress(total, total, "Complete")
308
+
309
+ return BatchResult(succeeded=succeeded, failed=failed)
310
+ ```
311
+
312
+ ## Best Practices Summary
313
+
314
+ 1. **Validate early** - Check inputs before expensive operations
315
+ 2. **Use specific exceptions** - `ValueError`, `TypeError`, not generic `Exception`
316
+ 3. **Include context** - Messages should explain what, why, and how to fix
317
+ 4. **Convert types at boundaries** - Parse strings to enums/domain types early
318
+ 5. **Chain exceptions** - Use `raise ... from e` to preserve debug info
319
+ 6. **Handle partial failures** - Don't abort batches on single item errors
320
+ 7. **Use Pydantic** - For complex input validation with structured errors
321
+ 8. **Document failure modes** - Docstrings should list possible exceptions
322
+ 9. **Log with context** - Include IDs, counts, and other debugging info
323
+ 10. **Test error paths** - Verify exceptions are raised correctly