existential-birds/beagle / plugins/beagle-python/skills/python-code-review
python-code-review
UnverifiedReviews Python code for type safety, async patterns, error handling, and common mistakes. Use when reviewing .py files, checking type hints, async/await usage, or exception handling.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 4.6 KB |
| references/async-patterns.md | markdown | 2.4 KB |
| references/common-mistakes.md | markdown | 2.7 KB |
| references/error-handling.md | markdown | 2.4 KB |
| references/pep8-style.md | markdown | 4.0 KB |
| references/type-safety.md | markdown | 1.9 KB |
references/error-handling.md
View on GitHub# Error Handling
## Critical Anti-Patterns
### 1. Bare Except Clause
**Problem**: Catches everything including KeyboardInterrupt, SystemExit.
```python
# BAD
try:
process()
except:
pass
# GOOD - specific exception
try:
process()
except ValueError as e:
logger.error(f"Invalid value: {e}")
raise
# ACCEPTABLE - if you must catch all
try:
process()
except Exception as e: # Still allows KeyboardInterrupt
logger.error(f"Unexpected error: {e}")
raise
```
### 2. Swallowing Exceptions
**Problem**: Hides errors, makes debugging impossible.
```python
# BAD
try:
result = risky_operation()
except Exception:
pass # Error silently ignored!
# GOOD - log and handle
try:
result = risky_operation()
except OperationError as e:
logger.warning(f"Operation failed: {e}")
result = default_value
```
### 3. Losing Exception Context
**Problem**: Original stack trace lost.
```python
# BAD - loses original traceback
try:
parse_config()
except ValueError:
raise ConfigError("Invalid config")
# GOOD - preserves chain
try:
parse_config()
except ValueError as e:
raise ConfigError("Invalid config") from e
```
### 4. Missing Context in Error Messages
**Problem**: Can't diagnose issue from logs.
```python
# BAD
except KeyError:
raise ValueError("Missing key")
# GOOD - include context
except KeyError as e:
raise ValueError(f"Missing required key: {e.args[0]}") from e
```
### 5. Not Logging Before Re-raising
**Problem**: Exception might be caught elsewhere without logging.
```python
# BAD - no record if caught upstream
try:
process(item)
except ProcessError:
raise
# GOOD - log before re-raising
try:
process(item)
except ProcessError as e:
logger.error(f"Failed to process item {item.id}: {e}")
raise
```
## Logging Best Practices
```python
from loguru import logger
# BAD
print(f"Processing {item}")
print(f"Error: {e}")
# GOOD
logger.debug(f"Processing item {item.id}")
logger.info(f"Completed batch of {count} items")
logger.warning(f"Retry {attempt}/3 for {operation}")
logger.error(f"Failed to process {item.id}: {e}")
# With exception info
logger.exception(f"Unexpected error processing {item.id}")
```
## Review Questions
1. Are there any bare `except:` clauses?
2. Is exception context preserved with `raise ... from e`?
3. Do error messages include enough context to diagnose?
4. Is logging used instead of print statements?
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |