ndomo 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/.bun-version +1 -0
- package/.dockerignore +79 -0
- package/.editorconfig +18 -0
- package/.env.example +19 -0
- package/.github/CODEOWNERS +8 -0
- package/.github/ISSUE_TEMPLATE/bug_report.yml +62 -0
- package/.github/ISSUE_TEMPLATE/config.yml +2 -0
- package/.github/ISSUE_TEMPLATE/feature_request.yml +34 -0
- package/.github/dependabot.yml +36 -0
- package/.github/pull_request_template.md +24 -0
- package/.github/release.yml +30 -0
- package/.github/workflows/gitleaks.yml +28 -0
- package/.github/workflows/release-please.yml +27 -0
- package/.github/workflows/smoke.yml +29 -0
- package/.husky/commit-msg +1 -0
- package/CHANGELOG.md +114 -0
- package/Dockerfile +32 -0
- package/README.es.md +174 -0
- package/README.md +187 -0
- package/agents/chronicler.md +98 -0
- package/agents/ci-smith.md +136 -0
- package/agents/craftsman.md +341 -0
- package/agents/deploy-smith.md +138 -0
- package/agents/foreman.md +377 -0
- package/agents/go-smith.md +164 -0
- package/agents/guild.md +188 -0
- package/agents/inspector.md +83 -0
- package/agents/js-smith.md +127 -0
- package/agents/ops-scout.md +173 -0
- package/agents/painter.md +200 -0
- package/agents/python-smith.md +120 -0
- package/agents/ranger.md +307 -0
- package/agents/release-smith.md +165 -0
- package/agents/rust-smith.md +159 -0
- package/agents/sage.md +178 -0
- package/agents/scout.md +144 -0
- package/agents/scribe.md +156 -0
- package/agents/smith.md +201 -0
- package/agents/vue-smith.md +155 -0
- package/agents/warden.md +216 -0
- package/agents/zig-smith.md +156 -0
- package/bin/ndomo-analyses.ts +4 -0
- package/bin/ndomo-status.ts +4 -0
- package/biome.json +57 -0
- package/bun.lock +514 -0
- package/commitlint.config.js +3 -0
- package/config/ndomo.config.json +258 -0
- package/config/ndomo.schema.json +166 -0
- package/docs/agents.md +375 -0
- package/docs/bugs/plan-create-orphan-fk.md +131 -0
- package/docs/bugs/task_create_batch-order-index-collision.md +158 -0
- package/docs/configuration.md +276 -0
- package/docs/database.md +364 -0
- package/docs/features/feature-flexible-builder-v1.md +724 -0
- package/docs/features/feature-flexible-builder-v2.md +882 -0
- package/docs/features/feature-flexible-builder.md +974 -0
- package/docs/http-server.md +244 -0
- package/docs/installation.md +259 -0
- package/docs/integrations.md +129 -0
- package/docs/operations/anti-pattern-sub-agent-verify-2026-06-21.md +32 -0
- package/docs/operations/audit-v1.md +417 -0
- package/docs/operations/audit-v2.md +197 -0
- package/docs/operations/audit-v3.md +306 -0
- package/docs/operations/db-optimize-foundations.md +123 -0
- package/docs/operations/verify-gate-architecture.md +82 -0
- package/docs/workflows.md +448 -0
- package/opencode.json +5 -0
- package/package.json +65 -0
- package/release-please-config.json +11 -0
- package/scripts/dev-bust-cache.sh +164 -0
- package/scripts/install.sh +688 -0
- package/scripts/smoke-e2e.ts +704 -0
- package/scripts/smoke-hot.ts +417 -0
- package/scripts/smoke-http.sh +228 -0
- package/scripts/smoke-v4.ts +256 -0
- package/scripts/smoke-v5.ts +397 -0
- package/scripts/smoke.sh +9 -0
- package/scripts/uninstall.sh +224 -0
- package/skills/api-security-best-practices/SKILL.md +915 -0
- package/skills/bash-scripting/SKILL.md +201 -0
- package/skills/bun/SKILL.md +313 -0
- package/skills/cavecrew/SKILL.md +82 -0
- package/skills/caveman/SKILL.md +74 -0
- package/skills/caveman-review/README.md +33 -0
- package/skills/caveman-review/SKILL.md +55 -0
- package/skills/find-skills/SKILL.md +142 -0
- package/skills/frontend-design/LICENSE.txt +177 -0
- package/skills/frontend-design/SKILL.md +55 -0
- package/skills/golang-patterns/SKILL.md +674 -0
- package/skills/golang-security/SKILL.md +185 -0
- package/skills/golang-security/evals/evals.json +595 -0
- package/skills/golang-security/references/architecture.md +268 -0
- package/skills/golang-security/references/checklist.md +80 -0
- package/skills/golang-security/references/cookies.md +200 -0
- package/skills/golang-security/references/cryptography.md +424 -0
- package/skills/golang-security/references/filesystem.md +285 -0
- package/skills/golang-security/references/injection.md +315 -0
- package/skills/golang-security/references/logging.md +163 -0
- package/skills/golang-security/references/memory-safety.md +241 -0
- package/skills/golang-security/references/network.md +253 -0
- package/skills/golang-security/references/secrets.md +189 -0
- package/skills/golang-security/references/third-party.md +159 -0
- package/skills/golang-security/references/threat-modeling.md +189 -0
- package/skills/golang-testing/SKILL.md +720 -0
- package/skills/grill-me/SKILL.md +7 -0
- package/skills/javascript-testing-patterns/SKILL.md +537 -0
- package/skills/javascript-testing-patterns/references/advanced-testing-patterns.md +513 -0
- package/skills/modern-javascript-patterns/SKILL.md +43 -0
- package/skills/modern-javascript-patterns/references/advanced-patterns.md +487 -0
- package/skills/modern-javascript-patterns/references/details.md +457 -0
- package/skills/python-anti-patterns/SKILL.md +349 -0
- package/skills/python-design-patterns/SKILL.md +85 -0
- package/skills/python-design-patterns/references/details.md +353 -0
- package/skills/python-error-handling/SKILL.md +193 -0
- package/skills/python-error-handling/references/details.md +171 -0
- package/skills/python-testing-patterns/SKILL.md +278 -0
- package/skills/python-testing-patterns/references/advanced-patterns.md +411 -0
- package/skills/python-testing-patterns/references/details.md +349 -0
- package/skills/rust-patterns/SKILL.md +500 -0
- package/skills/rust-testing/SKILL.md +501 -0
- package/skills/security-review/SKILL.md +504 -0
- package/skills/security-review/cloud-infrastructure-security.md +361 -0
- package/skills/vue-best-practices/SKILL.md +154 -0
- package/skills/vue-best-practices/references/animation-class-based-technique.md +254 -0
- package/skills/vue-best-practices/references/animation-state-driven-technique.md +291 -0
- package/skills/vue-best-practices/references/component-async.md +97 -0
- package/skills/vue-best-practices/references/component-data-flow.md +307 -0
- package/skills/vue-best-practices/references/component-fallthrough-attrs.md +174 -0
- package/skills/vue-best-practices/references/component-keep-alive.md +137 -0
- package/skills/vue-best-practices/references/component-slots.md +216 -0
- package/skills/vue-best-practices/references/component-suspense.md +228 -0
- package/skills/vue-best-practices/references/component-teleport.md +108 -0
- package/skills/vue-best-practices/references/component-transition-group.md +128 -0
- package/skills/vue-best-practices/references/component-transition.md +125 -0
- package/skills/vue-best-practices/references/composables.md +290 -0
- package/skills/vue-best-practices/references/directives.md +162 -0
- package/skills/vue-best-practices/references/perf-avoid-component-abstraction-in-lists.md +159 -0
- package/skills/vue-best-practices/references/perf-v-once-v-memo-directives.md +182 -0
- package/skills/vue-best-practices/references/perf-virtualize-large-lists.md +187 -0
- package/skills/vue-best-practices/references/plugins.md +166 -0
- package/skills/vue-best-practices/references/reactivity.md +344 -0
- package/skills/vue-best-practices/references/render-functions.md +201 -0
- package/skills/vue-best-practices/references/sfc.md +310 -0
- package/skills/vue-best-practices/references/state-management.md +135 -0
- package/skills/vue-best-practices/references/updated-hook-performance.md +187 -0
- package/skills/vue-pinia-best-practices/SKILL.md +21 -0
- package/skills/vue-pinia-best-practices/reference/pinia-no-active-pinia-error.md +248 -0
- package/skills/vue-pinia-best-practices/reference/pinia-setup-store-return-all-state.md +227 -0
- package/skills/vue-pinia-best-practices/reference/pinia-store-destructuring-breaks-reactivity.md +193 -0
- package/skills/vue-pinia-best-practices/reference/state-url-for-ephemeral-filters.md +238 -0
- package/skills/vue-pinia-best-practices/reference/state-use-pinia-for-large-apps.md +262 -0
- package/skills/vue-pinia-best-practices/reference/store-method-binding-parentheses.md +191 -0
- package/skills/zig-0.16/SKILL.md +840 -0
- package/skills/zig-0.16/scripts/check-zig-version.sh +21 -0
- package/src/cli/analyses.ts +280 -0
- package/src/cli/index.ts +108 -0
- package/src/cli/serve.ts +192 -0
- package/src/cli/smoke.ts +131 -0
- package/src/cli/status.test.ts +204 -0
- package/src/cli/status.ts +263 -0
- package/src/cli/vacuum.test.ts +82 -0
- package/src/cli/vacuum.ts +96 -0
- package/src/config/schema.test.ts +88 -0
- package/src/config/schema.ts +64 -0
- package/src/db/analyses-migration.test.ts +210 -0
- package/src/db/analyses.test.ts +466 -0
- package/src/db/analyses.ts +375 -0
- package/src/db/auto-checkpoint.ts +131 -0
- package/src/db/client.test.ts +129 -0
- package/src/db/client.ts +55 -0
- package/src/db/fts-escape.ts +20 -0
- package/src/db/incidents.test.ts +201 -0
- package/src/db/incidents.ts +93 -0
- package/src/db/index.ts +86 -0
- package/src/db/migrations-v13.test.ts +141 -0
- package/src/db/migrations-v8.test.ts +301 -0
- package/src/db/migrations.ts +147 -0
- package/src/db/plan-archive.test.ts +180 -0
- package/src/db/plan-archive.ts +274 -0
- package/src/db/plan-create.test.ts +276 -0
- package/src/db/plan-create.ts +78 -0
- package/src/db/plan-files.test.ts +289 -0
- package/src/db/plan-update-status.ts +287 -0
- package/src/db/plans.test.ts +490 -0
- package/src/db/plans.ts +534 -0
- package/src/db/resolve-project-dir.test.ts +143 -0
- package/src/db/resolve-project-dir.ts +75 -0
- package/src/db/rollbacks.test.ts +150 -0
- package/src/db/rollbacks.ts +67 -0
- package/src/db/schema.ts +907 -0
- package/src/db/sessions.test.ts +80 -0
- package/src/db/sessions.ts +135 -0
- package/src/db/shutdown.test.ts +147 -0
- package/src/db/shutdown.ts +45 -0
- package/src/db/tasks.test.ts +921 -0
- package/src/db/tasks.ts +747 -0
- package/src/db/types.ts +619 -0
- package/src/http/__tests__/auth.test.ts +196 -0
- package/src/http/__tests__/routes.test.ts +465 -0
- package/src/http/__tests__/sse.test.ts +317 -0
- package/src/http/auth.ts +72 -0
- package/src/http/middleware/cors.ts +53 -0
- package/src/http/middleware/security-headers.ts +21 -0
- package/src/http/routes/events.ts +112 -0
- package/src/http/routes/health.ts +51 -0
- package/src/http/routes/plans.ts +66 -0
- package/src/http/routes/sessions.ts +50 -0
- package/src/http/routes/tasks.ts +60 -0
- package/src/http/server.ts +95 -0
- package/src/http/sse.ts +116 -0
- package/src/index.ts +37 -0
- package/src/lib.ts +65 -0
- package/src/mem/scoped.ts +65 -0
- package/src/orchestrator/background.test.ts +268 -0
- package/src/orchestrator/background.ts +293 -0
- package/src/orchestrator/memory-hook.ts +182 -0
- package/src/orchestrator/reconciler.ts +123 -0
- package/src/orchestrator/scheduler.test.ts +300 -0
- package/src/orchestrator/scheduler.ts +243 -0
- package/src/plugin.test.ts +2574 -0
- package/src/plugin.ts +1690 -0
- package/src/sdk/client.ts +66 -0
- package/src/worktrees/manager.ts +236 -0
- package/src/worktrees/state.ts +87 -0
- package/tests/integration/ranger-flow.test.ts +257 -0
- package/tools/analysis_archive.ts +28 -0
- package/tools/analysis_create.ts +55 -0
- package/tools/analysis_get.ts +33 -0
- package/tools/analysis_link_plan.ts +44 -0
- package/tools/analysis_list.ts +48 -0
- package/tools/analysis_search.ts +36 -0
- package/tools/analysis_update.ts +44 -0
- package/tools/plan_approve.ts +31 -0
- package/tools/plan_create.ts +58 -0
- package/tools/plan_get.ts +40 -0
- package/tools/plan_list.ts +37 -0
- package/tools/plan_search.ts +34 -0
- package/tools/plan_update_status.ts +71 -0
- package/tools/session_checkpoint.ts +31 -0
- package/tools/session_end.ts +26 -0
- package/tools/session_start.ts +43 -0
- package/tools/task_create_batch.ts +70 -0
- package/tools/task_list.ts +35 -0
- package/tools/task_next_for_agent.ts +30 -0
- package/tools/task_search.ts +34 -0
- package/tools/task_update_status.ts +37 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: python-anti-patterns
|
|
3
|
+
description: Use this skill when reviewing Python code for common anti-patterns to avoid. Use as a checklist when reviewing code, before finalizing implementations, or when debugging issues that might stem from known bad practices.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Python Anti-Patterns Checklist
|
|
7
|
+
|
|
8
|
+
A reference checklist of common mistakes and anti-patterns in Python code. Review this before finalizing implementations to catch issues early.
|
|
9
|
+
|
|
10
|
+
## When to Use This Skill
|
|
11
|
+
|
|
12
|
+
- Reviewing code before merge
|
|
13
|
+
- Debugging mysterious issues
|
|
14
|
+
- Teaching or learning Python best practices
|
|
15
|
+
- Establishing team coding standards
|
|
16
|
+
- Refactoring legacy code
|
|
17
|
+
|
|
18
|
+
**Note:** This skill focuses on what to avoid. For guidance on positive patterns and architecture, see the `python-design-patterns` skill.
|
|
19
|
+
|
|
20
|
+
## Infrastructure Anti-Patterns
|
|
21
|
+
|
|
22
|
+
### Scattered Timeout/Retry Logic
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
# BAD: Timeout logic duplicated everywhere
|
|
26
|
+
def fetch_user(user_id):
|
|
27
|
+
try:
|
|
28
|
+
return requests.get(url, timeout=30)
|
|
29
|
+
except Timeout:
|
|
30
|
+
logger.warning("Timeout fetching user")
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
def fetch_orders(user_id):
|
|
34
|
+
try:
|
|
35
|
+
return requests.get(url, timeout=30)
|
|
36
|
+
except Timeout:
|
|
37
|
+
logger.warning("Timeout fetching orders")
|
|
38
|
+
return None
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Fix:** Centralize in decorators or client wrappers.
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
# GOOD: Centralized retry logic
|
|
45
|
+
@retry(stop=stop_after_attempt(3), wait=wait_exponential())
|
|
46
|
+
def http_get(url: str) -> Response:
|
|
47
|
+
return requests.get(url, timeout=30)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Double Retry
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
# BAD: Retrying at multiple layers
|
|
54
|
+
@retry(max_attempts=3) # Application retry
|
|
55
|
+
def call_service():
|
|
56
|
+
return client.request() # Client also has retry configured!
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
**Fix:** Retry at one layer only. Know your infrastructure's retry behavior.
|
|
60
|
+
|
|
61
|
+
### Hard-Coded Configuration
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
# BAD: Secrets and config in code
|
|
65
|
+
DB_HOST = "prod-db.example.com"
|
|
66
|
+
API_KEY = "sk-12345"
|
|
67
|
+
|
|
68
|
+
def connect():
|
|
69
|
+
return psycopg.connect(f"host={DB_HOST}...")
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Fix:** Use environment variables with typed settings.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
# GOOD
|
|
76
|
+
from pydantic_settings import BaseSettings
|
|
77
|
+
|
|
78
|
+
class Settings(BaseSettings):
|
|
79
|
+
db_host: str = Field(alias="DB_HOST")
|
|
80
|
+
api_key: str = Field(alias="API_KEY")
|
|
81
|
+
|
|
82
|
+
settings = Settings()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Architecture Anti-Patterns
|
|
86
|
+
|
|
87
|
+
### Exposed Internal Types
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
# BAD: Leaking ORM model to API
|
|
91
|
+
@app.get("/users/{id}")
|
|
92
|
+
def get_user(id: str) -> UserModel: # SQLAlchemy model
|
|
93
|
+
return db.query(UserModel).get(id)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**Fix:** Use DTOs/response models.
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
# GOOD
|
|
100
|
+
@app.get("/users/{id}")
|
|
101
|
+
def get_user(id: str) -> UserResponse:
|
|
102
|
+
user = db.query(UserModel).get(id)
|
|
103
|
+
return UserResponse.from_orm(user)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Mixed I/O and Business Logic
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
# BAD: SQL embedded in business logic
|
|
110
|
+
def calculate_discount(user_id: str) -> float:
|
|
111
|
+
user = db.query("SELECT * FROM users WHERE id = ?", user_id)
|
|
112
|
+
orders = db.query("SELECT * FROM orders WHERE user_id = ?", user_id)
|
|
113
|
+
# Business logic mixed with data access
|
|
114
|
+
if len(orders) > 10:
|
|
115
|
+
return 0.15
|
|
116
|
+
return 0.0
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**Fix:** Repository pattern. Keep business logic pure.
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
# GOOD
|
|
123
|
+
def calculate_discount(user: User, orders: list[Order]) -> float:
|
|
124
|
+
# Pure business logic, easily testable
|
|
125
|
+
if len(orders) > 10:
|
|
126
|
+
return 0.15
|
|
127
|
+
return 0.0
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Error Handling Anti-Patterns
|
|
131
|
+
|
|
132
|
+
### Bare Exception Handling
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
# BAD: Swallowing all exceptions
|
|
136
|
+
try:
|
|
137
|
+
process()
|
|
138
|
+
except Exception:
|
|
139
|
+
pass # Silent failure - bugs hidden forever
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
**Fix:** Catch specific exceptions. Log or handle appropriately.
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
# GOOD
|
|
146
|
+
try:
|
|
147
|
+
process()
|
|
148
|
+
except ConnectionError as e:
|
|
149
|
+
logger.warning("Connection failed, will retry", error=str(e))
|
|
150
|
+
raise
|
|
151
|
+
except ValueError as e:
|
|
152
|
+
logger.error("Invalid input", error=str(e))
|
|
153
|
+
raise BadRequestError(str(e))
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Ignored Partial Failures
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
# BAD: Stops on first error
|
|
160
|
+
def process_batch(items):
|
|
161
|
+
results = []
|
|
162
|
+
for item in items:
|
|
163
|
+
result = process(item) # Raises on error - batch aborted
|
|
164
|
+
results.append(result)
|
|
165
|
+
return results
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**Fix:** Capture both successes and failures.
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
# GOOD
|
|
172
|
+
def process_batch(items) -> BatchResult:
|
|
173
|
+
succeeded = {}
|
|
174
|
+
failed = {}
|
|
175
|
+
for idx, item in enumerate(items):
|
|
176
|
+
try:
|
|
177
|
+
succeeded[idx] = process(item)
|
|
178
|
+
except Exception as e:
|
|
179
|
+
failed[idx] = e
|
|
180
|
+
return BatchResult(succeeded, failed)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Missing Input Validation
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
# BAD: No validation
|
|
187
|
+
def create_user(data: dict):
|
|
188
|
+
return User(**data) # Crashes deep in code on bad input
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**Fix:** Validate early at API boundaries.
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
# GOOD
|
|
195
|
+
def create_user(data: dict) -> User:
|
|
196
|
+
validated = CreateUserInput.model_validate(data)
|
|
197
|
+
return User.from_input(validated)
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Resource Anti-Patterns
|
|
201
|
+
|
|
202
|
+
### Unclosed Resources
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
# BAD: File never closed
|
|
206
|
+
def read_file(path):
|
|
207
|
+
f = open(path)
|
|
208
|
+
return f.read() # What if this raises?
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
**Fix:** Use context managers.
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
# GOOD
|
|
215
|
+
def read_file(path):
|
|
216
|
+
with open(path) as f:
|
|
217
|
+
return f.read()
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Blocking in Async
|
|
221
|
+
|
|
222
|
+
```python
|
|
223
|
+
# BAD: Blocks the entire event loop
|
|
224
|
+
async def fetch_data():
|
|
225
|
+
time.sleep(1) # Blocks everything!
|
|
226
|
+
response = requests.get(url) # Also blocks!
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
**Fix:** Use async-native libraries.
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
# GOOD
|
|
233
|
+
async def fetch_data():
|
|
234
|
+
await asyncio.sleep(1)
|
|
235
|
+
async with httpx.AsyncClient() as client:
|
|
236
|
+
response = await client.get(url)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Type Safety Anti-Patterns
|
|
240
|
+
|
|
241
|
+
### Missing Type Hints
|
|
242
|
+
|
|
243
|
+
```python
|
|
244
|
+
# BAD: No types
|
|
245
|
+
def process(data):
|
|
246
|
+
return data["value"] * 2
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
**Fix:** Annotate all public functions.
|
|
250
|
+
|
|
251
|
+
```python
|
|
252
|
+
# GOOD
|
|
253
|
+
def process(data: dict[str, int]) -> int:
|
|
254
|
+
return data["value"] * 2
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Untyped Collections
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
# BAD: Generic list without type parameter
|
|
261
|
+
def get_users() -> list:
|
|
262
|
+
...
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**Fix:** Use type parameters.
|
|
266
|
+
|
|
267
|
+
```python
|
|
268
|
+
# GOOD
|
|
269
|
+
def get_users() -> list[User]:
|
|
270
|
+
...
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## Testing Anti-Patterns
|
|
274
|
+
|
|
275
|
+
### Only Testing Happy Paths
|
|
276
|
+
|
|
277
|
+
```python
|
|
278
|
+
# BAD: Only tests success case
|
|
279
|
+
def test_create_user():
|
|
280
|
+
user = service.create_user(valid_data)
|
|
281
|
+
assert user.id is not None
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
**Fix:** Test error conditions and edge cases.
|
|
285
|
+
|
|
286
|
+
```python
|
|
287
|
+
# GOOD
|
|
288
|
+
def test_create_user_success():
|
|
289
|
+
user = service.create_user(valid_data)
|
|
290
|
+
assert user.id is not None
|
|
291
|
+
|
|
292
|
+
def test_create_user_invalid_email():
|
|
293
|
+
with pytest.raises(ValueError, match="Invalid email"):
|
|
294
|
+
service.create_user(invalid_email_data)
|
|
295
|
+
|
|
296
|
+
def test_create_user_duplicate_email():
|
|
297
|
+
service.create_user(valid_data)
|
|
298
|
+
with pytest.raises(ConflictError):
|
|
299
|
+
service.create_user(valid_data)
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Over-Mocking
|
|
303
|
+
|
|
304
|
+
```python
|
|
305
|
+
# BAD: Mocking everything
|
|
306
|
+
def test_user_service():
|
|
307
|
+
mock_repo = Mock()
|
|
308
|
+
mock_cache = Mock()
|
|
309
|
+
mock_logger = Mock()
|
|
310
|
+
mock_metrics = Mock()
|
|
311
|
+
# Test doesn't verify real behavior
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
**Fix:** Use integration tests for critical paths. Mock only external services.
|
|
315
|
+
|
|
316
|
+
## Quick Review Checklist
|
|
317
|
+
|
|
318
|
+
Before finalizing code, verify:
|
|
319
|
+
|
|
320
|
+
- [ ] No scattered timeout/retry logic (centralized)
|
|
321
|
+
- [ ] No double retry (app + infrastructure)
|
|
322
|
+
- [ ] No hard-coded configuration or secrets
|
|
323
|
+
- [ ] No exposed internal types (ORM models, protobufs)
|
|
324
|
+
- [ ] No mixed I/O and business logic
|
|
325
|
+
- [ ] No bare `except Exception: pass`
|
|
326
|
+
- [ ] No ignored partial failures in batches
|
|
327
|
+
- [ ] No missing input validation
|
|
328
|
+
- [ ] No unclosed resources (using context managers)
|
|
329
|
+
- [ ] No blocking calls in async code
|
|
330
|
+
- [ ] All public functions have type hints
|
|
331
|
+
- [ ] Collections have type parameters
|
|
332
|
+
- [ ] Error paths are tested
|
|
333
|
+
- [ ] Edge cases are covered
|
|
334
|
+
|
|
335
|
+
## Common Fixes Summary
|
|
336
|
+
|
|
337
|
+
| Anti-Pattern | Fix |
|
|
338
|
+
|-------------|-----|
|
|
339
|
+
| Scattered retry logic | Centralized decorators |
|
|
340
|
+
| Hard-coded config | Environment variables + pydantic-settings |
|
|
341
|
+
| Exposed ORM models | DTO/response schemas |
|
|
342
|
+
| Mixed I/O + logic | Repository pattern |
|
|
343
|
+
| Bare except | Catch specific exceptions |
|
|
344
|
+
| Batch stops on error | Return BatchResult with successes/failures |
|
|
345
|
+
| No validation | Validate at boundaries with Pydantic |
|
|
346
|
+
| Unclosed resources | Context managers |
|
|
347
|
+
| Blocking in async | Async-native libraries |
|
|
348
|
+
| Missing types | Type annotations on all public APIs |
|
|
349
|
+
| Only happy path tests | Test errors and edge cases |
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: python-design-patterns
|
|
3
|
+
description: Python design patterns including KISS, Separation of Concerns, Single Responsibility, and composition over inheritance. Use this skill when designing a new service or component from scratch and choosing how to layer responsibilities, when refactoring a God class or monolithic function that has grown too large, when deciding whether to add a new abstraction or live with duplication, when evaluating a pull request for structural issues like tight coupling or leaking internal types, when choosing between inheritance and composition for a new class hierarchy, or when a codebase is becoming hard to test because of entangled I/O and business logic.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Python Design Patterns
|
|
7
|
+
|
|
8
|
+
Write maintainable Python code using fundamental design principles. These patterns help you build systems that are easy to understand, test, and modify.
|
|
9
|
+
|
|
10
|
+
## When to Use This Skill
|
|
11
|
+
|
|
12
|
+
- Designing new components or services
|
|
13
|
+
- Refactoring complex or tangled code
|
|
14
|
+
- Deciding whether to create an abstraction
|
|
15
|
+
- Choosing between inheritance and composition
|
|
16
|
+
- Evaluating code complexity and coupling
|
|
17
|
+
- Planning modular architectures
|
|
18
|
+
|
|
19
|
+
## Core Concepts
|
|
20
|
+
|
|
21
|
+
### 1. KISS (Keep It Simple)
|
|
22
|
+
|
|
23
|
+
Choose the simplest solution that works. Complexity must be justified by concrete requirements.
|
|
24
|
+
|
|
25
|
+
### 2. Single Responsibility (SRP)
|
|
26
|
+
|
|
27
|
+
Each unit should have one reason to change. Separate concerns into focused components.
|
|
28
|
+
|
|
29
|
+
### 3. Composition Over Inheritance
|
|
30
|
+
|
|
31
|
+
Build behavior by combining objects, not extending classes.
|
|
32
|
+
|
|
33
|
+
### 4. Rule of Three
|
|
34
|
+
|
|
35
|
+
Wait until you have three instances before abstracting. Duplication is often better than premature abstraction.
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
# Simple beats clever
|
|
41
|
+
# Instead of a factory/registry pattern:
|
|
42
|
+
FORMATTERS = {"json": JsonFormatter, "csv": CsvFormatter}
|
|
43
|
+
|
|
44
|
+
def get_formatter(name: str) -> Formatter:
|
|
45
|
+
return FORMATTERS[name]()
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Detailed patterns and worked examples
|
|
49
|
+
|
|
50
|
+
Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
|
|
51
|
+
|
|
52
|
+
## Best Practices Summary
|
|
53
|
+
|
|
54
|
+
1. **Keep it simple** - Choose the simplest solution that works
|
|
55
|
+
2. **Single responsibility** - Each unit has one reason to change
|
|
56
|
+
3. **Separate concerns** - Distinct layers with clear purposes
|
|
57
|
+
4. **Compose, don't inherit** - Combine objects for flexibility
|
|
58
|
+
5. **Rule of three** - Wait before abstracting
|
|
59
|
+
6. **Keep functions small** - 20-50 lines (varies by complexity), one purpose
|
|
60
|
+
7. **Inject dependencies** - Constructor injection for testability
|
|
61
|
+
8. **Delete before abstracting** - Remove dead code, then consider patterns
|
|
62
|
+
9. **Test each layer** - Isolated tests for each concern
|
|
63
|
+
10. **Explicit over clever** - Readable code beats elegant code
|
|
64
|
+
|
|
65
|
+
## Troubleshooting
|
|
66
|
+
|
|
67
|
+
**A class is growing and seems to have multiple responsibilities, but splitting it feels wrong.**
|
|
68
|
+
Apply the "reason to change" test: list every change that could require editing this class. If the list has items from different domains (e.g., HTTP parsing AND business rules AND formatting), split it. If all changes stem from the same domain concern, the class may be appropriately sized.
|
|
69
|
+
|
|
70
|
+
**Injecting all dependencies through the constructor is producing constructors with 7+ parameters.**
|
|
71
|
+
This is a sign of too many responsibilities in one class, not a problem with dependency injection. Split the class into smaller units first, then each constructor naturally becomes smaller.
|
|
72
|
+
|
|
73
|
+
**Composition is producing deeply nested wrapper objects that are hard to trace.**
|
|
74
|
+
Keep the composition shallow (2-3 levels). If wrapping is the only mechanism, consider whether a Protocol-based approach or simple function composition would be cleaner than a chain of decorator objects.
|
|
75
|
+
|
|
76
|
+
**The rule of three says not to abstract yet, but the duplication is causing bugs when one copy is updated but not the other.**
|
|
77
|
+
Duplication that diverges in dangerous ways should be abstracted sooner. The rule of three is a heuristic, not a law. If the copies are already diverging incorrectly, extract immediately and add a test that exercises the shared behavior.
|
|
78
|
+
|
|
79
|
+
**A service layer is importing from the API layer, breaking the dependency direction.**
|
|
80
|
+
This is a layering violation. The service layer must not import from handlers. Introduce a shared types/models layer that both can import from, keeping the dependency arrow pointing downward (API → Service → Repository).
|
|
81
|
+
|
|
82
|
+
## Related Skills
|
|
83
|
+
|
|
84
|
+
- [python-testing-patterns](../python-testing-patterns/SKILL.md) — Test each layer in isolation using the dependency injection structure established here
|
|
85
|
+
- [python-project-setup](../python-project-setup/SKILL.md) — Set up project structure and tooling that enforces layer boundaries from the start
|