A Python package for analyzing functions with what-if scenarios and edge cases
Project description
WhatIF Analyzer
A powerful Python package for analyzing functions by automatically generating and running "what-if" scenarios, with a focus on edge cases and robustness testing.
🌟 Features
- 🔍 Static Analysis: Automatically infers parameter types and generates edge cases
- 🎯 Edge Case Testing: Tests with None, empty values, extreme numbers, and more
- 🧪 Lightweight Fuzzing: Optional fuzzing for additional test cases
- 📊 Detailed Reports: Human-readable summaries of test results
- 🛠️ Multiple Interfaces: Use as a decorator, CLI tool, or Python API
- 📝 Comprehensive Documentation: Clear examples and usage instructions
📋 Requirements
- Python 3.8 or higher
- Dependencies:
- typing-extensions >= 4.0.0
- inspect2 >= 0.1.0
- rich >= 10.0.0
- click >= 8.0.0
- pytest >= 7.0.0 (for development)
🚀 Installation
From PyPI
pip install whatif-analyzer
From Source
git clone https://github.com/Arjunmehta312/what-if-python-package.git
cd what-if-python-package
pip install -e .
Development Installation
pip install -e .[dev]
📖 Quick Start
Using the Decorator
from whatif_analyzer import analyze
@analyze
def divide(a: float, b: float) -> float:
return a / b
# The function will be automatically analyzed when called
result = divide(10, 2)
Using the CLI
# Analyze a Python file
whatif analyze path/to/your/file.py
# Analyze a specific function
whatif analyze path/to/your/file.py:function_name
Using the API
from whatif_analyzer import WhatIfAnalyzer
def my_function(x: int, y: str) -> bool:
return len(y) > x
analyzer = WhatIfAnalyzer()
report = analyzer.analyze_function(my_function)
print(report)
📊 Example Report
from whatif_analyzer import analyze
@analyze
def process_data(data: list, threshold: int) -> float:
if not data:
return 0.0
return sum(x for x in data if x > threshold) / len(data)
# The analysis will run automatically
result = process_data([1, 2, 3, 4, 5], 2)
Sample report output:
WhatIF Analysis Report
=====================
Function: process_data
Parameters:
- data: list
- threshold: int
Edge Cases Tested:
✓ Empty list
✓ None values
✓ Negative threshold
✓ Large threshold
✓ Mixed data types
Exceptions Found:
- TypeError: When data contains non-numeric values
- ZeroDivisionError: When data is empty and threshold is 0
Recommendations:
1. Add type checking for list elements
2. Handle empty list case explicitly
3. Consider adding input validation for threshold
🔧 Advanced Usage
Custom Edge Cases
from whatif_analyzer import WhatIfAnalyzer, EdgeCase
analyzer = WhatIfAnalyzer()
# Define custom edge cases
custom_cases = [
EdgeCase("special_value", lambda x: x == 42),
EdgeCase("negative_list", lambda x: all(i < 0 for i in x))
]
# Analyze with custom cases
report = analyzer.analyze_function(
my_function,
edge_cases=custom_cases
)
Configuration Options
from whatif_analyzer import WhatIfAnalyzer
analyzer = WhatIfAnalyzer(
enable_fuzzing=True,
max_fuzz_cases=100,
timeout_seconds=5,
verbose=True
)
🧪 Testing
Run the test suite:
pytest
Run with coverage:
pytest --cov=whatif_analyzer
📚 Documentation
For detailed documentation, visit our documentation site.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- 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
Release history Release notifications | RSS feed
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 whatif_analyzer-0.1.0.tar.gz.
File metadata
- Download URL: whatif_analyzer-0.1.0.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eefd99705c2b5676f0017c731d44d31a502a8e04e5cbb7435bb92b57b60d0590
|
|
| MD5 |
598bfd400d7e56fd817d00fef9533bb9
|
|
| BLAKE2b-256 |
60658ae3613c5a76a223e5ad29cb937545a58394ed13c06c8bb2e8b4d62ae01f
|
File details
Details for the file whatif_analyzer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: whatif_analyzer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b1c7cea7db0c4cd53f5dc4b2d8412a45180214120906414d74865afddfdf632
|
|
| MD5 |
d738c77185c43ca04f675a509c246dfe
|
|
| BLAKE2b-256 |
960bcd880e2f4d9c3b4aed1de30592cc9a13d5135cc9760fdd59325f8a75f1eb
|