home / skills / physics91 / claude-vibe / python-reviewer

python-reviewer skill

/skills/python-reviewer

This skill reviews Python code for PEP8, type hints, and Pythonic idioms, improving maintainability and reducing errors.

npx playbooks add skill physics91/claude-vibe --skill python-reviewer

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
5.4 KB
---
name: python-reviewer
description: |
  WHEN: General Python code review, PEP8 compliance, type hints, Pythonic patterns
  WHAT: PEP8/style check + Type hint validation + Pythonic idioms + Error handling + Documentation
  WHEN NOT: FastAPI → fastapi-reviewer, Django → django-reviewer, Data science → python-data-reviewer
---

# Python Reviewer Skill

## Purpose
Reviews Python code for style, idioms, type safety, and best practices.

## When to Use
- Python code review requests
- PEP8 compliance check
- Type hint review
- "Is this Pythonic?" questions
- General Python project review

## Project Detection
- `requirements.txt`, `pyproject.toml`, `setup.py`, `setup.cfg`
- `.py` files in project
- `__init__.py` module structure

## Workflow

### Step 1: Analyze Project
```
**Python Version**: 3.11+
**Package Manager**: pip/poetry/uv
**Type Checking**: mypy/pyright
**Linter**: ruff/flake8/pylint
**Formatter**: black/ruff
```

### Step 2: Select Review Areas
**AskUserQuestion:**
```
"Which areas to review?"
Options:
- Full Python review (recommended)
- PEP8/Style compliance
- Type hints and safety
- Error handling patterns
- Performance and idioms
multiSelect: true
```

## Detection Rules

### PEP8 & Style
| Check | Recommendation | Severity |
|-------|----------------|----------|
| Line > 88 chars | Break line or refactor | LOW |
| Missing docstring | Add module/function docstring | MEDIUM |
| Import order wrong | Use isort or ruff | LOW |
| Inconsistent naming | snake_case for functions/vars | MEDIUM |

```python
# BAD: Inconsistent naming
def getUserName(userId):
    pass

# GOOD: PEP8 naming
def get_user_name(user_id: int) -> str:
    pass
```

### Type Hints
| Check | Recommendation | Severity |
|-------|----------------|----------|
| Missing return type | Add -> ReturnType | MEDIUM |
| Any type overuse | Use specific types | MEDIUM |
| Optional without None check | Add None handling | HIGH |
| Missing generic types | Use list[T], dict[K,V] | LOW |

```python
# BAD: No type hints
def process(data):
    return data.get("name")

# GOOD: Full type hints
def process(data: dict[str, Any]) -> str | None:
    return data.get("name")
```

### Pythonic Idioms
| Check | Recommendation | Severity |
|-------|----------------|----------|
| Manual loop for list | Use list comprehension | LOW |
| if x == True | Use if x | LOW |
| Manual dict iteration | Use .items(), .keys(), .values() | LOW |
| try/except pass | Handle or log exception | HIGH |
| Mutable default arg | Use None default | CRITICAL |

```python
# BAD: Mutable default argument
def append_to(item, target=[]):
    target.append(item)
    return target

# GOOD: None default
def append_to(item, target: list | None = None) -> list:
    if target is None:
        target = []
    target.append(item)
    return target

# BAD: Manual loop
result = []
for x in items:
    if x > 0:
        result.append(x * 2)

# GOOD: List comprehension
result = [x * 2 for x in items if x > 0]
```

### Error Handling
| Check | Recommendation | Severity |
|-------|----------------|----------|
| Bare except | Catch specific exceptions | HIGH |
| except Exception | Be more specific | MEDIUM |
| No logging in except | Add logging | MEDIUM |
| Missing finally | Add cleanup if needed | LOW |

```python
# BAD: Bare except
try:
    process()
except:
    pass

# GOOD: Specific exception with logging
try:
    process()
except ValueError as e:
    logger.error(f"Invalid value: {e}")
    raise
except IOError as e:
    logger.warning(f"IO error: {e}")
    return None
```

### Modern Python (3.10+)
| Check | Recommendation | Severity |
|-------|----------------|----------|
| Union[X, Y] | Use X \| Y | LOW |
| Optional[X] | Use X \| None | LOW |
| Dict, List from typing | Use dict, list builtin | LOW |
| No match statement | Consider match for complex branching | LOW |

```python
# OLD: typing imports
from typing import Optional, Union, List, Dict

def func(x: Optional[int]) -> Union[str, None]:
    pass

# MODERN: Built-in syntax (3.10+)
def func(x: int | None) -> str | None:
    pass

# Match statement (3.10+)
match status:
    case 200:
        return "OK"
    case 404:
        return "Not Found"
    case _:
        return "Unknown"
```

## Response Template
```
## Python Code Review Results

**Project**: [name]
**Python**: 3.11 | **Tools**: ruff, mypy, pytest

### Style & PEP8
| Status | File | Issue |
|--------|------|-------|
| LOW | utils.py:45 | Line exceeds 88 characters |

### Type Hints
| Status | File | Issue |
|--------|------|-------|
| MEDIUM | service.py:23 | Missing return type annotation |

### Pythonic Idioms
| Status | File | Issue |
|--------|------|-------|
| CRITICAL | models.py:12 | Mutable default argument |

### Error Handling
| Status | File | Issue |
|--------|------|-------|
| HIGH | api.py:67 | Bare except clause |

### Recommended Actions
1. [ ] Fix mutable default arguments
2. [ ] Add specific exception handling
3. [ ] Add type hints to public functions
4. [ ] Run ruff --fix for style issues
```

## Best Practices
1. **Type Hints**: Use for all public APIs
2. **Docstrings**: Google or NumPy style
3. **Error Handling**: Specific exceptions, always log
4. **Testing**: pytest with fixtures
5. **Tooling**: ruff (lint+format), mypy (types)

## Integration
- `fastapi-reviewer`: FastAPI specific patterns
- `django-reviewer`: Django specific patterns
- `python-data-reviewer`: Pandas/NumPy patterns
- `security-scanner`: Python security checks

Overview

This skill reviews general Python code for style, type safety, idiomatic patterns, error handling, and documentation. It highlights PEP8 issues, missing or incorrect type hints, Pythonic anti-patterns, and risky error handling. Recommendations are practical and prioritized by severity to help developers improve maintainability and correctness.

How this skill works

I scan project files and common manifests (pyproject.toml, requirements.txt, setup.py) and inspect .py modules for style, typing, and runtime smells. Checks include line length, naming, import order, missing docstrings, type hint presence and specificity, mutable defaults, bare excepts, and opportunities for modern Python constructs. Results are grouped by category with file locations, severity, and actionable fixes.

When to use it

  • General Python code reviews for libraries and applications
  • Enforcing PEP8 and consistent style across a codebase
  • Validating type hints and mypy/pyright readiness
  • Detecting Pythonic idioms and mutable default risks
  • Improving error handling and logging practices

Best practices

  • Add type hints to all public APIs and prefer precise types over Any
  • Use docstrings (Google or NumPy style) for modules, classes, and public functions
  • Avoid mutable default arguments; use None and initialize inside the function
  • Catch specific exceptions and log context; avoid bare except or broad Exception handlers
  • Use modern Python syntax (X | Y unions, built-in generics, match) and leverage ruff/black/isort automation

Example use cases

  • Run a full review on a Python package before release to catch style and typing gaps
  • Audit a pull request to surface missing type annotations and exceptions handling issues
  • Refactor legacy modules to replace mutable defaults and manual loops with comprehensions
  • Validate a codebase for readiness with mypy/pyright and recommend fixes
  • Provide a prioritized checklist of fixes (critical -> low) to guide maintainers

FAQ

What Python versions are targeted?

Reviews assume modern Python (3.10+) and recommend syntax available from 3.10/3.11, but will note compatibility issues if older versions are detected.

Does this handle framework-specific checks?

No. This skill targets general Python patterns. Use framework-specific reviewers (FastAPI, Django, data-science) for framework idioms and best practices.