Skip to main content

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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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

whatif_analyzer-0.1.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

whatif_analyzer-0.1.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

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

Hashes for whatif_analyzer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eefd99705c2b5676f0017c731d44d31a502a8e04e5cbb7435bb92b57b60d0590
MD5 598bfd400d7e56fd817d00fef9533bb9
BLAKE2b-256 60658ae3613c5a76a223e5ad29cb937545a58394ed13c06c8bb2e8b4d62ae01f

See more details on using hashes here.

File details

Details for the file whatif_analyzer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for whatif_analyzer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b1c7cea7db0c4cd53f5dc4b2d8412a45180214120906414d74865afddfdf632
MD5 d738c77185c43ca04f675a509c246dfe
BLAKE2b-256 960bcd880e2f4d9c3b4aed1de30592cc9a13d5135cc9760fdd59325f8a75f1eb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page