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
Metadata
Release files for whatif-analyzer 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| whatif_analyzer-0.1.0.tar.gz | 14.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| whatif_analyzer-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.5 kB
Release files / whatif_analyzer-0.1.0.tar.gz
| Download URL | whatif_analyzer-0.1.0.tar.gz |
|---|---|
| Size | 14.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eefd99705c2b5676f0017c731d44d31a502a8e04e5cbb7435bb92b57b60d0590
|
|
BLAKE2b-256 checksum How to use checksums |
60658ae3613c5a76a223e5ad29cb937545a58394ed13c06c8bb2e8b4d62ae01f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Release files / whatif_analyzer-0.1.0-py3-none-any.whl
| Download URL | whatif_analyzer-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5b1c7cea7db0c4cd53f5dc4b2d8412a45180214120906414d74865afddfdf632
|
|
BLAKE2b-256 checksum How to use checksums |
960bcd880e2f4d9c3b4aed1de30592cc9a13d5135cc9760fdd59325f8a75f1eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Security for whatif-analyzer 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.