class-inspector documentation

class_inspector.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.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
class_inspector.validate_bool_func(bool_func) Callable

Validate the value using a custom boolean function.

Parameters:

bool_func – The boolean function to apply to the value.

Returns:

A validation function.

Return type:

Callable

Raises:

TypeError – If the provided boolean function is not callable.

class_inspector.validate_collection(instance, attribute, value) None

Validate that the value is a subclass of Collection.

Parameters:
  • instance – The instance the attribute belongs to.

  • attribute – The attribute being validated.

  • value – The value of the attribute.

Raises:

TypeError – If the value is not a subclass of Collection.

class_inspector.validate_collection_of_type(allowed_type: Type) Callable

Validate that the value is a collection of a specific type.

Parameters:

allowed_type (Type) – The type each item in the collection should be.

Returns:

A validation function.

Return type:

Callable

Raises:

TypeError – If the value is not a subclass of Collection.

class_inspector.validate_generic(generic_type: Type) Callable

Validate that the value is a subclass of a specific generic type.

Parameters:

generic_type (Type) – The generic type.

Returns:

A validation function.

Return type:

Callable

class_inspector.validate_generic_bool_func(generic_type: Type, bool_func: Callable) Callable

Validate that the value is a collection of a specific generic type using a custom boolean function.

Parameters:
  • generic_type (Type) – The generic type.

  • bool_func (Callable) – The boolean function to apply to each item.

Returns:

A validation function.

Return type:

Callable

Raises:

TypeError – If the provided boolean function is not callable.

class_inspector.validate_generic_of_type(generic_type: Type, allowed_type: Type) Callable

Validate that the value is a collection of a specific generic type.

Parameters:
  • generic_type (Type) – The generic type.

  • allowed_type (Type) – The type each item in the collection should be.

Returns:

A validation function.

Return type:

Callable

class_inspector.validate_iterable(instance, attribute, value) None

Validate that the value is a subclass of Iterable.

Parameters:
  • instance – The instance the attribute belongs to.

  • attribute – The attribute being validated.

  • value – The value of the attribute.

Raises:

TypeError – If the value is not a subclass of Iterable.

class_inspector.validate_iterable_of_type(allowed_type: Type) Callable

Validate that the value is an iterable of a specific type.

Parameters:

allowed_type (Type) – The type each item in the iterable should be.

Returns:

A validation function.

Return type:

Callable

Raises:

TypeError – If the value is not a subclass of Iterable.

class_inspector.validate_sequence(instance, attribute, value) None

Validate that the value is a subclass of Sequence.

Parameters:
  • instance – The instance the attribute belongs to.

  • attribute – The attribute being validated.

  • value – The value of the attribute.

Raises:

TypeError – If the value is not a subclass of Sequence.

class_inspector.validate_sequence_of_type(allowed_type: Type) Callable

Validate that the value is a sequence of a specific type.

Parameters:

allowed_type (Type) – The type each item in the sequence should be.

Returns:

A validation function.

Return type:

Callable

Raises:

TypeError – If the value is not a subclass of Sequence.

Indices and tables