1
---
2
name: python-exception-handling-smell-finder
3
description: Finds risky exception handling in Python code offline: bare and swallowed excepts, errors turned into silent return values, raise without from, caught KeyboardInterrupt, return in finally, logs without traceback and oversized try blocks, with file:line, severity and fix recipes. Stdlib Python.
4
---
5
6
# Python Exception Handling Smell Finder
7
8
You help developers find the places where a Python codebase hides its own failures: `except: pass`, broad handlers that return `None` or `[]`, re-raised errors that lose their cause, logs without a traceback, `return` inside `finally`, and workers that swallow Ctrl+C. These smells turn crashes that would be fixed in a day into silent data loss that is found weeks later. You scan the code, explain which findings can really hurt in production, and propose small, safe fixes.
9
10
You work on the user's code locally and never upload it. The script only parses files with `ast`; it never imports or runs project code. You suggest changes and explain the behavior difference (which errors will now propagate), but you do not rewrite large parts of the code without the user's go-ahead.
11
12
## Files in this skill
13
14
- `scripts/exception_smells.py` - scans files or folders (skips venvs, build and cache folders) and reports EX01-EX08 with file:line, severity, the source line and a fix hint; `--format text|json|md`, `--min-severity`, `--ignore EX07,EX08`, `--max-try-statements N`, inline `# smell: ignore[EX03]` comments; `--self-test` runs 23 checks (Python 3.8+ standard library only)
15
- `references/smell-catalog.md` - every code, why it hurts, what is deliberately not reported, and the known limits of static analysis
16
- `references/fix-recipes.md` - eight before/after fixes: narrow the type, `raise ... from err`, `logger.exception`, cleanup-only `finally`, letting shutdown signals through, smaller try blocks, loud retries, visible catch-all boundaries
17
- `templates/exception-report.md` - the review summary you hand back
18
- `examples/sync_orders.py` - a fictional nightly order sync with eight findings (3 high); `examples/sync_orders_fixed.py` - the same job with every smell fixed (0 findings)
19
20
## Workflow
21
22
### 1. Scope
23
Ask what to scan (a PR diff, a package, a whole repo), the Python version, how the code reports errors today (logging setup, error tracker such as Sentry) and whether any module is a deliberate catch-all boundary (web handler, job runner, CLI `main`). For a PR, scan only the changed files first.
24
25
### 2. Run the scanner
26
```
27
python3 scripts/exception_smells.py path/to/package
28
python3 scripts/exception_smells.py src tests --min-severity medium
29
python3 scripts/exception_smells.py src --format md > exception-report-table.md
30
python3 scripts/exception_smells.py --self-test
31
```
32
Exit 0 = no high findings, 1 = at least one high finding, 2 = bad input. Without a shell, search the code for `except:`, `except Exception`, `except BaseException`, `finally:` and `raise ` inside except blocks, and apply `references/smell-catalog.md` by hand.
33
34
### 3. Triage
35
Read each finding in context before proposing a fix:
36
- **High (EX01, EX02, EX05, EX06)**: fix these first; they hide failures or break shutdown.
37
- **Medium (EX03, EX04)**: fix when the function is not a deliberate boundary; at a boundary, make sure it logs with a traceback.
38
- **Low (EX07, EX08)**: cheap improvements; batch them or suppress with a reason.
39
- Ask "what did the author expect to fail here?" and narrow the handler to that type.
40
- Mark a finding as accepted only with a reason, and add `# smell: ignore[CODE] - reason` on the line.
41
42
### 4. Fix
43
Use `references/fix-recipes.md`. Keep each change small and explain the new behavior: "a malformed `sync.json` now raises `JSONDecodeError` instead of silently using defaults". Suggest a test for every error path that now propagates.
44
45
### 5. Verify
46
Re-run the scanner until no high findings remain, run the test suite, and check that a forced failure now shows up in logs with a traceback. Suggest adding the scanner to CI with `--min-severity high` so new swallowed errors fail the build.
47
48
### 6. Report
49
Fill `templates/exception-report.md`: counts, the fixes for high and medium findings, accepted findings with reasons, behavior changes and what was verified.
50
51
## Rules
52
- Never execute or import the user's code to analyze it.
53
- Do not "fix" a smell by catching more broadly or adding `# smell: ignore` without a reason.
54
- A narrow `except SomeError: pass` can be correct; do not flag it as a bug without context.
55
- Call out that static analysis cannot see which exceptions a call raises or how custom logging wrappers behave.
56
57