A Python library for detecting logical and syntactical errors in Python code
Project description
PyBugHunt
Advanced Python Code Error Detection and Analysis
PyBugHunt is a sophisticated Python library designed to detect, analyze, and suggest fixes for both syntactical and logical errors in Python code. Leveraging a combination of static code analysis techniques and machine learning approaches, PyBugHunt offers developers a powerful tool to improve code quality and reduce debugging time.
Table of Contents
- Features
- Technology Stack
- Installation
- Usage
- Error Detection Capabilities
- Machine Learning Approach
- Development
- Customization
- License
- Contributing
Features
PyBugHunt offers comprehensive error detection capabilities:
-
Robust Syntax Error Detection and Analysis
- Identifies and reports syntax errors with precise line and column information
- Provides detailed descriptions of common syntax errors
- Suggests potential fixes for various syntax issues
-
Intelligent Logical Error Detection
- Identifies common logical error patterns using AST-based analysis
- Employs machine learning models to detect subtle logical errors
- Recognizes error patterns that might lead to runtime failures
-
Fix Suggestion System
- Provides context-aware suggestions to fix identified errors
- Offers example code snippets for improved code patterns
-
Flexible Integration Options
- Command-line interface for analyzing Python files
- Python API for seamless integration into other tools and workflows
- Multiple output formats including text and JSON
-
Customization and Training
- Support for training custom error detection models with your codebase
- Extensible architecture for adding new error patterns and detection strategies
Technology Stack
PyBugHunt utilizes a wide range of technologies and libraries:
Core Technologies
- Python 3.8+: Modern Python language features for robust implementation
- Abstract Syntax Tree (AST): For parsing and analyzing Python code structure
- Python Standard Library:
ast: For parsing and traversing Python codetokenize: For lexical analysis of Python codere: For pattern matching in error analysispickle: For model serialization
Machine Learning
- NumPy: For numerical operations and data processing
- scikit-learn: For machine learning model implementation
- Random Forest classifiers for error pattern recognition
- TF-IDF vectorization for code feature extraction
- Pipeline API for streamlined model training and inference
- PyTorch: For advanced deep learning models (transformer-based error detection)
- Transformers: For pre-trained language models adapted to code understanding
Static Analysis
- Astroid: For enhanced AST capabilities and more detailed code analysis
- PyLint: For additional pattern recognition and best practice enforcement
Development and Testing
- setuptools: For package building and distribution
- unittest: For comprehensive test coverage
- black & isort: For consistent code formatting and organization
Installation
From PyPI (Recommended)
pip install pybughunt
From Source
# Clone the repository
git clone https://github.com/Preksha-7/pybughunt.git
cd pybughunt
# Install in development mode
pip install -e .
Dependencies
All dependencies will be automatically installed when you install PyBugHunt. The main dependencies include:
numpy>=1.20.0
scikit-learn>=1.0.0
torch>=1.9.0
transformers>=4.12.0
astroid>=2.8.0
pylint>=2.11.0
Usage
Command Line Interface
PyBugHunt provides a versatile command-line interface for analyzing Python files:
# Analyze a single Python file
pybughunt analyze file.py
# Analyze multiple Python files
pybughunt analyze file1.py file2.py file3.py
# Output results in JSON format to a file
pybughunt analyze file.py --format json --output results.json
# Use a custom trained model
pybughunt analyze file.py --model path/to/custom_model.pkl
Training a custom model on your codebase:
# Train a model on a directory containing Python files
pybughunt train --dataset /path/to/python/files --output my_model.pkl
Python API
PyBugHunt can be easily integrated into your Python applications:
from pybughunt import CodeErrorDetector
# Initialize the detector (optionally with a custom model)
detector = CodeErrorDetector() # or CodeErrorDetector("path/to/model.pkl")
# Analyze code from a string
code = '''
def example():
print("Hello, world!" # Missing closing parenthesis
x = 10 # Unused variable
while True:
# Infinite loop without break
pass
'''
# Get analysis results
results = detector.analyze(code)
# Process syntax errors
if results["syntax_errors"]:
print(f"Found {len(results['syntax_errors'])} syntax errors:")
for error in results["syntax_errors"]:
print(f" Line {error['line']}: {error['message']}")
# Process logical errors
if results["logic_errors"]:
print(f"Found {len(results['logic_errors'])} logical errors:")
for error in results["logic_errors"]:
print(f" Line {error['line']}: {error['message']}")
# Get fix suggestions
if results["syntax_errors"] or results["logic_errors"]:
suggestions = detector.fix_suggestions(code, results)
for error_key, suggestion in suggestions.items():
print(f"Suggestion for {error_key}: {suggestion}")
For quick analysis, you can also use the shorthand function:
from pybughunt import quick_detect
# Quickly analyze a file
quick_detect(file_path="my_script.py")
# Or analyze code directly
quick_detect(code='''
def hello():
print("Hello"
''')
Error Detection Capabilities
Syntax Error Detection
PyBugHunt identifies and provides detailed information about a wide range of syntax errors:
-
Missing Delimiters
- Unclosed parentheses, brackets, and braces
- Unterminated string literals and quotes
- Missing colons in control flow statements
-
Indentation Issues
- Inconsistent indentation
- Missing indentation after control flow statements
- Unexpected indentation
-
Invalid Syntax
- Invalid tokens and characters
- Python 2 to Python 3 migration issues (e.g., print statements)
- Keyword misuse
-
Imports and Module Errors
- Invalid import syntax
- Circular imports (in multi-file analysis)
Each detected syntax error includes:
- Line and column information
- Error type classification
- Descriptive message explaining the issue
- Suggested fixes when possible
Logical Error Detection
The logical error detection system identifies common patterns that could lead to runtime errors or unexpected behavior:
-
Control Flow Issues
- Infinite loops (while True without break)
- Unreachable code after return/break/continue
- Empty code blocks with pass statements
-
Variable Usage
- Unused variables
- Variables used before assignment
- Shadowed variables
-
Algorithm Problems
- Potential off-by-one errors in loops (e.g., range(len(x)))
- Division by zero risks
- Potential null reference errors
-
Performance Issues
- Inefficient list operations
- Nested loops with redundant computations
-
Bug-Prone Patterns
- Mutable default arguments
- Improper exception handling
- Improper resource management
Machine Learning Approach
PyBugHunt employs a sophisticated machine learning approach to detect logical errors that might be missed by traditional static analysis:
Feature Extraction
- Code Tokenization: Conversion of Python code into tokenized sequences
- TF-IDF Vectorization: Transformation of code tokens into numerical features
- AST-Based Features: Extraction of code structure features from AST
- Function and class counts
- Loop and branch counts
- Exception handling patterns
- Code complexity metrics
Model Architecture
The default model uses a pipeline of:
- TF-IDF Vectorizer: Converts tokenized code into numerical features
- Random Forest Classifier: Identifies error patterns based on extracted features
For advanced use cases, PyBugHunt can be configured to use:
- Transformer-based Models: Adapted from pre-trained language models for code understanding
- Custom Neural Networks: For project-specific error pattern recognition
Training Process
Models are trained on a diverse corpus of Python code samples with known errors, including:
- Syntax errors
- Runtime errors
- Logical bugs
- Best practice violations
The training process involves:
- Data Collection: Gathering Python code samples with and without errors
- Feature Extraction: Converting code samples into feature vectors
- Model Training: Supervised learning with labeled error examples
- Evaluation: Testing on held-out validation set
- Optimization: Hyperparameter tuning for optimal error detection
Development
Project Structure
The PyBugHunt project follows a modular structure:
pybughunt/
│
├── src/
│ └── pybughunt/
│ ├── __init__.py # Package initialization
│ ├── __main__.py # Entry point for module execution
│ ├── cli.py # Command line interface
│ ├── detector.py # Main detector class
│ ├── driver.py # Quick detection functionality
│ ├── logic_analyzer.py # Logical error detection
│ ├── syntax_analyzer.py # Syntax error detection
│ ├── utils.py # Utility functions
│ ├── sample_buggy.py # Example buggy code
│ │
│ └── models/ # Machine learning components
│ ├── __init__.py
│ ├── model_loader.py # Model loading functionality
│ └── model_trainer.py # Model training functionality
│
├── tests/ # Test suite
│ ├── __init__.py
│ ├── test_detector.py
│ ├── test_logic_analyzer.py
│ └── test_syntax_analyzer.py
│
├── .gitignore # Git ignore file
├── README.md # Project documentation
├── pyproject.toml # Project metadata and configuration
└── setup.py # Package setup script
Testing
PyBugHunt includes a comprehensive test suite to ensure reliability:
# Run all tests
python -m unittest discover
# Run specific test file
python -m unittest tests/test_detector.py
Test coverage includes:
- Unit tests for syntax error detection
- Unit tests for logical error detection
- Integration tests for the full error detection pipeline
Customization
Training Custom Models
You can train custom models on your specific codebase to improve error detection:
from pybughunt.models.model_trainer import ModelTrainer
# Create a trainer
trainer = ModelTrainer(output_path="my_custom_model.pkl")
# Prepare your dataset (code samples and error labels)
code_samples = [...] # List of Python code strings
labels = [...] # 1 for code with errors, 0 for clean code
# Train the model
model = trainer.train(code_samples, labels)
# Save the model
trainer.save_model()
Using the command line:
# Train on a directory of Python files
pybughunt train --dataset /path/to/python/files --output my_model.pkl
Extending Error Patterns
You can extend PyBugHunt with custom error patterns by subclassing the analyzers:
from pybughunt.logic_analyzer import LogicAnalyzer
import ast
class CustomLogicAnalyzer(LogicAnalyzer):
def __init__(self, model):
super().__init__(model)
# Add your custom pattern check
self.common_patterns["my_custom_pattern"] = self._check_my_pattern
def _check_my_pattern(self, tree: ast.AST) -> list:
# Implement your custom pattern detection
errors = []
# ... detection logic
return errors
License
PyBugHunt is released under the MIT License. See the LICENSE file for details.
Contributing
Contributions to PyBugHunt are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pybughunt-0.1.2.tar.gz.
File metadata
- Download URL: pybughunt-0.1.2.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6f43a337cde816c8c7e94f2649cc96a3a4f7571c59a42f686dad381f1b25e1e
|
|
| MD5 |
7c9c3ad432e437edc41f7484bda63ab1
|
|
| BLAKE2b-256 |
765da2178f70136944c386a7578941a5298571d4c1380b889da24b1e36fab925
|
File details
Details for the file pybughunt-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pybughunt-0.1.2-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
108039bc6074ce9e30ea73265911f7389c291ea4996364a8bedefaab2bd7c2e4
|
|
| MD5 |
5bb272d2ee85640231e1539f23933c05
|
|
| BLAKE2b-256 |
26e7b2d9408b5f0724d6a79f212835b472cf89bc71030084c2069827989b5339
|