Python Smell Detector
Python Smell Detector is a VS Code extension designed to help developers identify and fix "code smells" in their Python code. By integrating with Pylint and providing advanced analysis like Control Flow Graphs (CFG) and refactoring suggestions, it bridges the gap between static analysis and developer education.
Features
1. Real-time Code Smell Detection
- Automatically detects code smells as you type or save.
- Highlights issues with squiggly lines directly in the editor.
- Error (Red): Fatal errors or syntax issues.
- Warning (Yellow): Potential logic errors or code smells.
- Info (Blue): Coding convention suggestions.
2. Visual Quality Report
- Generate a comprehensive HTML report for the current file.
- View a summary of all issues found.
- Command:
Python Smell Detector: Show Report
3. Smart Refactoring Suggestions
- Context-Aware: Automatically identifies functions with high complexity (e.g., too many branches or statements).
- Visualization: Generates a Control Flow Graph (CFG) to visualize the function's logic.
- Refactoring Preview: Provides "Fix It" suggestions with a preview of how to extract complex logic into helper functions.
4. Duplicate Function Detection
- Scans the directory of the current file for duplicate function definitions.
- Warns you if a function with the same name exists in multiple files, helping you avoid copy-paste errors and improve modularity.
Requirements
This extension requires a Python environment with pylint installed.
- Python 3.x must be installed and available in your system PATH.
- Pylint must be installed in your Python environment.
pip install pylint
Usage
- Open a Python file in VS Code.
- The extension will automatically analyze the file and highlight issues.
- To view the detailed report:
- Open the Command Palette (
Ctrl+Shift+P or Cmd+Shift+P).
- Run "Python Smell Detector: Show Report".
Extension Settings
This extension contributes the following settings:
pythonSmellDetector.pythonPath: Path to the Python interpreter. Defaults to python. Use this if you are using a virtual environment or a specific Python version.
Known Issues
- If you are using a virtual environment, ensure you configure
pythonSmellDetector.pythonPath to point to the python executable within your virtual environment (e.g., venv/bin/python or conda_env/python).
Recent Development Changes
This section documents the latest improvements made to fix activation and Unicode encoding issues.
Files Modified (Latest Session)
1. Created Files
2. Modified Files
vscode-extension/src/extension.ts (Major improvements)
Enhanced getPythonScriptPath() function:
- Extended search to 5 locations (previously 3)
- Added detailed debug logging for each searched path
- Added error notifications when
main.py is not found
Improved activate() function:
- Added startup logging (extension path, VS Code version)
- Added detailed activation information to Output panel
Enhanced runSmellDetection() function:
- Added analysis trigger notifications
- Added Python path and file path logging
- Improved error handling with user-friendly messages
- Added detection for missing pylint with install instructions
- Added emoji indicators for better log readability (⚡ ✅ ❌ 🐍 📄)
Better error handling:
- Empty output detection
- JSON parse error handling with output preview
- Process error notifications
vscode-extension/package.json
- Modified
activationEvents:
- Added
"onStartupFinished" to activate extension immediately on window startup
- Keeps
"onLanguage:python" for backward compatibility
vscode-extension/python_src/main.py (Unicode encoding fixes)
Fixed stdin reading (line 355):
sys.stdin.reconfigure(encoding='utf-8', errors='replace')
- Handles invalid Unicode surrogate characters
- Replaces invalid characters with
? instead of crashing
Fixed temporary file writing (line 250):
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False,
encoding='utf-8', errors='replace') as tmp:
- Prevents
UnicodeEncodeError when writing to temp files
- Ensures robust handling of special characters
Issue Resolution
Problem 1: Extension Not Activating
- Symptom: Output panel didn't show "Python Smell Detector" option
- Cause: Extension only activated when Python file was opened
- Fix: Added
"onStartupFinished" to activationEvents in package.json
Problem 2: Unicode Encoding Errors
- Symptom:
UnicodeEncodeError: 'utf-8' codec can't encode character '\udc80'
- Cause: Invalid Unicode surrogate characters in stdin/temp file operations
- Fix: Added
errors='replace' to all UTF-8 encoding operations in main.py
Problem 3: Difficult Debugging
- Symptom: Hard to diagnose why plugin wasn't working
- Fix: Added comprehensive debug logging throughout
extension.ts with clear status indicators
Testing Recommendations
After these changes:
- Press F5 to launch Extension Development Host
- Extension should activate immediately (check Output panel)
- Open any
.py file to see real-time analysis
- Use
test_extension.py for quick verification
- Check Output panel for detailed logs if issues occur
Release Notes
1.0.4
- Improved cross-platform compatibility by removing absolute path dependencies.
- Fixed
main.py detection logic to support various installation environments.
1.0.3
- Fixed issue where
main.py was not found on other machines.
- Added
pythonSmellDetector.pythonPath configuration setting.
1.0.2
1.0.1
- Added Duplicate Function Detection: Scans for functions with the same name across different files in the same directory.
- Fixed an issue where the HTML report generation might fail due to missing function definitions.
- Improved report visualization.
1.0.0
- Initial release of Python Smell Detector.
- Real-time Pylint integration.
- HTML Report generation.
- Control Flow Graph (CFG) visualization.
- Duplicate function detection.