chunklet.common.validation
Functions:
-
pretty_errors–Formats Pydantic validation errors into a human-readable string.
-
restricted_iterable–Creates a Pydantic Annotated type that represents a RestrictedIterable
-
safely_count_iterable–Counts elements in an iterable while preserving its state and forcing validation.
-
validate_input–A decorator that validates function inputs and outputs
pretty_errors
Formats Pydantic validation errors into a human-readable string.
Source code in src/chunklet/common/validation.py
restricted_iterable
Creates a Pydantic Annotated type that represents a RestrictedIterable containing the specified hints (*hints), and applies a PlainValidator to reject str input.
Source code in src/chunklet/common/validation.py
safely_count_iterable
Counts elements in an iterable while preserving its state and forcing validation.
If the input is an Iterator, it is duplicated using itertools.tee to prevent
consumption during counting. The iteration simultaneously triggers any
underlying Pydantic item validation.
Parameters:
-
(namestr) –Descriptive name for the iterable (used in error context).
-
(iterableIterable) –The iterable or iterator to count and validate.
Returns:
-
tuple[int, Iterable]–tuple[int, Iterable]: The element count and the original (or preserved)
Raises:
-
InvalidInputError–If any element fails validation during the counting process.
Examples:
>>> # With a list
>>> my_list = [1, 2, 3, 4, 5]
>>> count, preserved_list = safely_count_iterable("my_list", my_list)
>>> print(f"Count: {count}")
Count: 5
>>> print(f"Original list is preserved: {list(preserved_list)}")
Original list is preserved: [1, 2, 3, 4, 5]
>>> # With an iterator (generator)
>>> my_iterator = (x for x in range(10))
>>> count, preserved_iterator = safely_count_iterable("my_iterator", my_iterator)
>>> print(f"Count: {count}")
Count: 10
>>> # The iterator is preserved and can still be consumed
>>> print(f"Sum of preserved iterator: {sum(preserved_iterator)}")
Sum of preserved iterator: 45
Source code in src/chunklet/common/validation.py
validate_input
A decorator that validates function inputs and outputs
A wrapper around Pydantic's validate_call that catchesValidationError and re-raises it as a more user-friendly InvalidInputError.