Python backend reviewer

From Qredence/agentic-fleet · View interactive version

Python backend reviewer is an AI agent skill from the GitHub repository Qredence/agentic-fleet. Expert Python backend code reviewer that identifies over-complexity, duplicates, bad optimizations, and violations of best practices. Use when asked to review Python code quality, check for duplicate code, analyze module complexity, optimize backend code, identify anti-patterns, or ensure adherence

How to use this skill

Skill file

Python Backend Code Reviewer

Expert analysis and refactoring of Python backend code to eliminate duplication, reduce complexity, and enforce best practices.

Overview

This skill helps identify and fix common issues in Python backend code, particularly problems introduced by AI code generation:

The skill provides automated analysis tools and comprehensive refactoring guidance.

⚠️ Architecture-Aware Prioritization

Static analysis finds issues, but architectural context determines priority.

Before prioritizing fixes, identify:

  1. Critical paths: Which code runs on every request?
  1. Secondary paths: Less critical code
  1. Concurrency model: How is state shared?

Prioritization rule: Correctness in critical paths > Complexity in secondary paths

| Finding | Critical Path | Secondary Path |

| --------------------- | ---------------------------- | --------------- |

| Shared state mutation | 🔴 Fix immediately | 🟡 Review |

| High complexity (>25) | 🟡 Refactor carefully | 🟢 Backlog |

| Duplicates | 🟡 Extract if >3 occurrences | 🟢 Nice to have |

| God class | 🟡 Migrate to façade | 🟢 Low priority |

Pragmatic Thresholds

For orchestration/workflow code, use realistic thresholds:

| Metric | Strict Threshold | Pragmatic Threshold | Notes |

| --------------------- | ---------------- | ------------------- | -------------------------------------------- |

| Cyclomatic complexity | 10 | 25 | Orchestrators naturally have decision points |

| Function length | 50 lines | 150 lines | Async flows can be longer |

| Nesting depth | 4 | 5 | Guard clauses help more than extracting |

| God class methods | 20 | N/A | OK if it's a façade that delegates |

Hard limits (always fix):

Quick Start

1. Run Automated Analysis

Start with automated tools to identify issues:

# Detect duplicate code blocks
uv run python scripts/detect_duplicates.py <path>

# Analyze imports and utility reimplementation
uv run python scripts/analyze_imports.py <path>

# Check code complexity
uv run python scripts/complexity_analyzer.py <path>

# Check for concurrency issues (shared state mutation)
uv run python scripts/concurrency_analyzer.py <path>

2. Review Analysis Results

Each tool outputs:

3. Apply Fixes

Use the reference guides to refactor issues:

Main Workflows

Review a Python File

When a user asks to review a specific file:

  1. Run all analysis tools on the file:

```bash

python scripts/detect_duplicates.py path/to/file.py

python scripts/analyze_imports.py path/to/file.py

python scripts/complexity_analyzer.py path/to/file.py

```

  1. Analyze results and categorize issues:
  1. Provide specific fixes:
  1. Offer to implement fixes if requested

Check Backend for Duplicates

When a user asks to check a project/module for duplicates:

  1. Run duplicate detection on the entire directory:

```bash

python scripts/detect_duplicates.py src/

```

  1. Group duplicates by severity:
  1. Recommend consolidation strategy:

Analyze Module Over-Engineering

When code appears over-engineered:

  1. Run complexity analysis:

```bash

python scripts/complexity_analyzer.py --max-complexity 10 --max-length 50 path/

```

  1. Identify over-engineering patterns:
  1. Suggest simplifications:
  1. Reference specific patterns from python_antipatterns.md

Optimize Following Best Practices

When asked to optimize code or ensure best practices:

  1. Run all analysis tools to get baseline metrics
  1. Check against best practices:
  1. Prioritize optimizations:
  1. Reference best_practices.md for specific guidelines

Analyze Concurrency Safety

When reviewing async code that handles concurrent requests:

  1. Run concurrency analysis:

```bash

uv run python scripts/concurrency_analyzer.py services/ workflows/

```

  1. Prioritize by severity:
  1. Common fixes for shared state mutation:

```python

# ❌ Before: Mutating shared instance state

class Workflow:

async def run(self, task):

self.current_task = task # Race condition!

# ✅ After: Request-scoped state

class Workflow:

async def run(self, task):

execution = ExecutionContext(task=task)

return await self._execute(execution)

```

  1. Alternative patterns:

Analysis Tools

detect_duplicates.py

Finds duplicate code blocks using AST analysis.

Usage:

uv run python scripts/detect_duplicates.py <path>
uv run python scripts/detect_duplicates.py --min-lines 10 <path>

Detects:

Options:

analyze_imports.py

Analyzes import organization and detects recreated utilities.

Usage:

uv run python scripts/analyze_imports.py <path>

Detects:

Common utilities flagged:

complexity_analyzer.py

Measures cyclomatic complexity, function length, and nesting depth.

Usage:

uv run python scripts/complexity_analyzer.py <path>
uv run python scripts/complexity_analyzer.py --max-complexity 10 --max-length 50 <path>

Metrics:

Options:

concurrency_analyzer.py

Detects concurrency issues in async Python code.

Usage:

uv run python scripts/concurrency_analyzer.py <path>

Detects:

Severity levels:

When to use: Run on services, handlers, and workflow code that handles concurrent requests.

Reference Documentation

python_antipatterns.md

Comprehensive catalog of anti-patterns with examples:

Use when: You identify an issue but need to see the anti-pattern and solution

refactoring_patterns.md

Step-by-step refactoring techniques:

Use when: You know what's wrong and need concrete refactoring steps

best_practices.md

Python backend best practices and principles:

Use when: Establishing coding standards or need authoritative guidance

Example Reviews

Example 1: Duplicate Validation Logic

User request: "Review this code for quality issues"

Analysis:

uv run python scripts/detect_duplicates.py api/

Finding: Email validation duplicated in 5 files

Recommendation:

# Extract to utils/validation.py
def validate_email(email: str) -> None:
    if not email or "@" not in email:
        raise ValueError("Invalid email")

# Import everywhere
from utils.validation import validate_email

Example 2: Recreated Retry Logic

User request: "Check if we're recreating utility functions"

Analysis:

uv run python scripts/analyze_imports.py services/

Finding: Custom retry logic in 3 services

Recommendation:

# Replace with tenacity
from tenacity import retry, stop_after_attempt, wait_exponential

@retry(stop=stop_after_attempt(3), wait=wait_exponential())
async def fetch_data(url: str):
    return await client.get(url)

Example 3: Complex Function

User request: "This function is hard to understand"

Analysis:

uv run python scripts/complexity_analyzer.py utils/processor.py

Finding: Complexity 23, nesting depth 6

Recommendation: Extract nested logic into helper functions (see refactoring_patterns.md)

When NOT to Refactor

⚠️ Avoid refactoring when:

Output Format

When reviewing code, structure feedback as:

  1. Summary: Brief overview of findings
  2. Critical Issues: Must-fix problems (duplicates, security)
  3. Important Issues: Should-fix problems (complexity, utilities)
  4. Suggestions: Nice-to-have improvements
  5. Code Examples: Specific before/after for each issue
  6. Next Steps: Recommended action plan

Always include:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.