Utils Validation#
validation
#
| FUNCTION | DESCRIPTION |
|---|---|
validate_args |
A decorator for type validation based on type annotations. |
Functions#
validate_args(func: Callable) -> Callable
#
A decorator for type validation based on type annotations.
Validates both positional and keyword arguments against their type annotations. Preserves function metadata through functools.wraps.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
The function to be decorated with type validation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable
|
The decorated function that performs type validation on its arguments before executing the original function. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If any argument does not match its expected type annotation. |
Notes
This decorator only validates arguments that have type annotations. Arguments without annotations are not validated. Does not support Union types or Optional parameters - for advanced validation consider using pydantic.
Examples:
>>> @validate_args
... def add_numbers(a: int, b: int) -> int:
... return a + b
>>> add_numbers(1, 2)
... 3
>>> add_numbers(1, "2")
... TypeError: Argument `b=2` must be of type `<class 'int'>`.