Skip to main content

A Python library for verifying code properties using natural language assertions.

Project description

IntentGuard

GitHub Sponsors PyPI - Downloads GitHub License PyPI - Version PyPI - Python Version

IntentGuard is a Python library for verifying code properties using natural language assertions. It seamlessly integrates with popular testing frameworks like pytest and unittest, allowing developers to express complex code expectations in plain English while maintaining the structure of traditional test suites.

Why IntentGuard?

Traditional testing approaches often require extensive boilerplate code to verify complex properties. IntentGuard bridges this gap by allowing developers to express sophisticated test cases in natural language, particularly useful for scenarios where writing conventional test code would be impractical or time-consuming.

Key Features

  1. Natural Language Test Cases: Write test assertions in plain English.
  2. Framework Integration: Works with pytest, unittest, and other testing frameworks.
  3. Deterministic Results: Uses a voting mechanism and controlled sampling for consistent results.
  4. Flexible Verification: Test complex code properties that would be difficult to verify traditionally.
  5. Detailed Failure Explanations: Provides clear explanations when assertions fail, helping you understand the root cause and fix issues faster.
  6. Efficient Result Caching: Caches assertion results to avoid redundant processing and speed up test execution.

When to Use IntentGuard

IntentGuard is designed for scenarios where traditional test implementation would be impractical or require excessive code. For example:

# Traditional approach would require:
# 1. Iterating through all methods
# 2. Parsing AST for each method
# 3. Checking exception handling patterns
# 4. Verifying logging calls
# 5. Maintaining complex test code

# With IntentGuard:
def test_error_handling():
    ig.assert_code(
        "All methods in {module} should use the custom ErrorHandler class for exception management, and log errors before re-raising them",
        {"module": my_critical_module}
    )

# Another example - checking documentation consistency
def test_docstring_completeness():
    ig.assert_code(
        "All public methods in {module} should have docstrings that include Parameters, Returns, and Examples sections",
        {"module": my_api_module}
    )

How It Works

Deterministic Testing

IntentGuard employs several mechanisms to ensure consistent and reliable results:

  1. Voting Mechanism: Each assertion is evaluated multiple times (configurable through num_evaluations), and the majority result is used
  2. Temperature Control: Uses low temperature for LLM sampling to reduce randomness
  3. Structured Prompts: Converts natural language assertions into structured prompts for consistent LLM interpretation
# Configure determinism settings
options = IntentGuardOptions(
    num_evaluations=5,      # Number of evaluations per assertion
)

Installation

pip install intentguard

Basic Usage

With pytest

import intentguard as ig

def test_code_properties():
    guard = ig.IntentGuard()
    
    # Test code organization
    guard.assert_code(
        "Classes in {module} should follow the Single Responsibility Principle",
        {"module": my_module}
    )
    
    # Test security practices
    guard.assert_code(
        "All database queries in {module} should be parameterized to prevent SQL injection",
        {"module": db_module}
    )

With unittest

import unittest
import intentguard as ig

class TestCodeQuality(unittest.TestCase):
    def setUp(self):
        self.guard = ig.IntentGuard()
    
    def test_error_handling(self):
        self.guard.assert_code(
            "All API endpoints in {module} should have proper input validation",
            {"module": api_module}
        )

Advanced Usage

Custom Evaluation Options

import intentguard as ig

options = ig.IntentGuardOptions(
    num_evaluations=7,          # Increase number of evaluations
    model="gpt-4o-2024-08-06",  # Use a more capable model
)

guard = ig.IntentGuard(options)

Contributing

Contributions are welcome! Check out our roadmap for planned features.

License

MIT License

Acknowledgements

This project uses LiteLLM for LLM integration.


IntentGuard is designed to complement, not replace, traditional testing approaches. It's most effective when used for complex code properties that are difficult to verify through conventional means.

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

intentguard-1.3.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

intentguard-1.3.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file intentguard-1.3.0.tar.gz.

File metadata

  • Download URL: intentguard-1.3.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.15 Linux/6.5.0-1025-azure

File hashes

Hashes for intentguard-1.3.0.tar.gz
Algorithm Hash digest
SHA256 53fc32c0397b3c8a15d3271080221068c02f4f24e32993dea6cb31f117b1321a
MD5 4ffc19872d2f8320eaa72f4b0438eeee
BLAKE2b-256 b6ac9936329cbde7fc49ba59f33774a3c04d17e6736e0b1f65f3fc9af017ab5e

See more details on using hashes here.

File details

Details for the file intentguard-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: intentguard-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.15 Linux/6.5.0-1025-azure

File hashes

Hashes for intentguard-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10f03d58c1a8ecfddb83a5415205c8e8bbd593cb57ccb8cce38e7ad252ea5d9d
MD5 dab10ccb1da2568f3236cca8d117fd69
BLAKE2b-256 05f2f8fe078d69caa777038096fd31016c7381ca4997b63b536a9d47dd05454a

See more details on using hashes here.

Supported by

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