class_inspector.transform module

class_inspector.transform.add_boilerplate(obj: ModuleType | LambdaType, /, add_debugs: bool = True, add_guards: bool = False) str

Add boilerplate to the object.

Parameters:
  • obj (Union[ModuleType, FunctionType]) – The object to add boilerplate to.

  • add_debugs (bool, optional) – Add debugs to each of the functions or methods. Defaults to True.

  • add_guards (bool, optional) – Add guard conditions to each of the functions, will check the type hints if supplied. Defaults to False.

Returns:

The class, function or module with modifications.

Return type:

str

Usage:
from class_inspector import add_boilerplate

def example_function(a: int, b: str = "default") -> str:
    if a > 0:
        return str(a) + b
    return a

print(add_boilerplate(example_function, add_debugs=True, add_guards=True))
Output:
def example_function(a: int, b: str = "default") -> str:
    logger.debug(locals())
    if not all([isinstance(a, int), isinstance(b, str)]):
        raise TypeError(
            "example_function expects arg types: [int, str], "
            f"received: [{type(a).__name__}, {type(b).__name__}]"
        )
    if a > 0:
        return str(a) + b
    return a
class_inspector.transform.get_parametrized_tests(obj: ModuleType | LambdaType, /, test_raises: bool = True, raises_arg_types: bool = False) str

_summary_

Parameters:
  • obj (Union[ModuleType, FunctionType]) – The object to get tests for.

  • test_raises (bool, optional) – Create tests for each of the exceptions raised in the function. Defaults to True.

  • raises_arg_types (bool, optional) – Create tests to check the type of each of the input arguments. Defaults to False.

Returns:

The parametrized tests for the given object, returns a test per function if given a module or per method if given classes

Return type:

str

Usage:
from class_inspector import get_parametrized_tests

def example_function(a: int, b: str = "default") -> str:
    if a > 0:
        return str(a) + b
    return a

print(get_parametrized_tests(example_function, raises_arg_types=False))
Output:
from contextlib import nullcontext as does_not_raise

import pytest


@pytest.mark.parametrize(
    "a, b, expected_result, expected_context",
    [
        pytest.param(
            a, b, expected_result, does_not_raise(), id="Ensure x when `a` is y"
        ),
        pytest.param(
            a, b, expected_result, does_not_raise(), id="Ensure x when `b` is y"
        ),
    ],
)
def test_example_function(a, b, expected_result, expected_context):
    with expected_context:
        assert example_function(a, b) == expected_result