🐍 Python Code Analyzer — VS Code Extension
A VS Code extension that performs deep static analysis on the currently
open Python file using Python's own ast module, and renders a structured
report — file stats, control-flow counts, OOP usage, complexity metrics,
and actionable code-quality suggestions.
Run Python: Analyze Current File from the Command Palette (or
right-click a .py file) to get a report like this:
📄 File Information 🔁 Loops 🔀 Conditions
Total Lines : 125 For Loop : 4 If : 5
Blank Lines : 18 While : 1 Elif : 2
Comments : 14 Else : 4
⚠ Exception Handling 📂 File Handling 📚 Collections
try : 2 open() : 3 Lists : 5
except : 2 Dict : 4
finally : 1
🏗 OOP ⭐ Complexity
Classes : 2 Nested Loops : 2
Objects : 3 Max Depth : 3
Inheritance : Yes Avg Complexity: 3.2
💡 Suggestions
• Remove unused import 'random'.
• Function 'test()' is too long (63 lines).
• Variable 'a' should have a more meaningful name.
✨ Features
Structure
- Line / blank / comment counts
- Imports, module-level variables
- Function & class counts, loop & conditional counts
- Exception handling (
try / except / finally)
- File I/O (
open()), collection literal counts
- OOP: class count, heuristic object count, inheritance detection
Complexity
- Nested loop detection + max nesting depth
- Per-function cyclomatic complexity (basic: decision points + boolean
operators)
- Average function length & average complexity across the file
Code Quality Suggestions (v2)
- ✅ Unused imports
- ✅ Unused variables
- ✅ Duplicate functions (structurally identical bodies)
- ✅ Long functions (> 50 lines)
- ✅ TODO comment collection
- ✅ Average function length
- ✅ Basic cyclomatic complexity per function
- ✅ Estimated skill level: Beginner / Intermediate / Advanced, based on
language features actually used (comprehensions, decorators, async/await,
inheritance, context managers, etc.)
🧱 Architecture
python-analyzer-extension/
├── package.json # extension manifest (commands, menus, settings)
├── src/
│ └── extension.ts # extension host: runs analyzer.py, renders Webview
├── python/
│ └── analyzer.py # ast-based static analysis, emits JSON on stdout
└── out/ # compiled JS (generated by `npm run compile`)
The extension shells out to a bundled Python script rather than
re-implementing a Python parser in TypeScript — this keeps the analysis
accurate (real AST, not regex guessing) at the cost of requiring Python 3
on the user's machine, which is a safe assumption for anyone editing .py
files in VS Code.
🚀 Getting Started (development)
npm install
npm run compile
Then press F5 in VS Code (with this folder open) to launch an
Extension Development Host. Open any .py file there and run
Python: Analyze Current File from the Command Palette
(Ctrl+Shift+P / Cmd+Shift+P).
Requirements
- Python 3 available as
python3 (or python on Windows) on PATH.
If it's somewhere else, set pythonAnalyzer.pythonPath in Settings.
📦 Packaging
npm install -g @vscode/vsce
vsce package
This produces a .vsix file you can install via
Extensions → ... → Install from VSIX, or publish to the Marketplace.
🗺 Roadmap / Ideas for v3
- Inline diagnostics (squiggly underlines) instead of/alongside the report
panel, using the
vscode.languages.createDiagnosticCollection API
- Per-function complexity heatmap in the sidebar
- Configurable thresholds (long-function line count, complexity limit) via
pythonAnalyzer.* settings
- "Explain this suggestion" hover popovers
- Workspace-wide analysis (aggregate report across all
.py files)
- Unit tests for
analyzer.py (pytest) and the extension (@vscode/test-electron)
License
MIT — do whatever you like with it, attribution appreciated.