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 voting mechanism and controlled sampling for consistent results
  4. Flexible Verification: Test complex code properties that would be difficult to verify traditionally

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 quorum_size), 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(
    quorum_size=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(
    quorum_size=7,              # Increase voting sample size
    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.2.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

intentguard-1.2.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: intentguard-1.2.0.tar.gz
  • Upload date:
  • Size: 9.0 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.2.0.tar.gz
Algorithm Hash digest
SHA256 8de6e8d7f29bba12752dfd9def2530c2839a9566464df41c65f7adcd38b2dbe3
MD5 c72f8d238cb1f3db4f2836a8251c558c
BLAKE2b-256 3e20d74f4f9d4a14d0acee371904e416eeb1296328fa2deb804e66ff1397c813

See more details on using hashes here.

File details

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

File metadata

  • Download URL: intentguard-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ce6c89c51bb3e8e46220de5a4ef6c29ad87d9bad24f353fc631776de219e8c8
MD5 e4535820709c03872f6d6c1c483bfac8
BLAKE2b-256 07a1a48bc24a4ca4caf6e10b7651d1f8cc9320b4dd8ae0ce9537a0431e6f5756

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