ValidatedFunction
This class provides a mechanism for validating function arguments by wrapping a callable and mapping its parameters to a dynamically created data model. It handles complex signature patterns including positional-only, keyword-only, and variable arguments while enforcing type hints and configuration constraints. The class facilitates the validation process by building model instances from provided arguments and executing the underlying function with the validated data.
Attributes
| Attribute | Type | Description |
|---|---|---|
| raw_function | AnyCallable | The original callable object being wrapped for validation. |
| arg_mapping | dict[int, str] = {} | A mapping of positional argument indices to their corresponding parameter names. |
| positional_only_args | set[str] = set() | A set of parameter names that are defined as positional-only in the function signature. |
| v_args_name | string = "args" | The name used for the variable positional arguments field, defaulting to 'args' unless it clashes with an existing parameter. |
| v_kwargs_name | string = "kwargs" | The name used for the variable keyword arguments field, defaulting to 'kwargs' unless it clashes with an existing parameter. |
| model | Type[[BaseModel](../../v1/main/basemodel.md?sid=pydantic_v1_main_basemodel)] | The dynamically created Pydantic model used to validate the arguments passed to the function. |
Constructor
Signature
def ValidatedFunction(
function: AnyCallable,
config: ConfigType
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| function | AnyCallable | The callable function to be validated. |
| config | ConfigType | The configuration dictionary or object for the underlying Pydantic model. |
Methods
init_model_instance()
@classmethod
def init_model_instance(
*args: Any,
**kwargs: Any
) - > [BaseModel](../../v1/main/basemodel.md?sid=pydantic_v1_main_basemodel)
Initializes a Pydantic model instance using the provided arguments and keyword arguments. This method maps raw inputs to the internal validation model to trigger type checking.
Parameters
| Name | Type | Description |
|---|---|---|
| *args | Any | Positional arguments passed to the original function. |
| **kwargs | Any | Keyword arguments passed to the original function. |
Returns
| Type | Description |
|---|---|
[BaseModel](../../v1/main/basemodel.md?sid=pydantic_v1_main_basemodel) | An instance of the dynamically created Pydantic model containing validated data. |
call()
@classmethod
def call(
*args: Any,
**kwargs: Any
) - > Any
Validates the provided arguments and executes the wrapped function. This is the primary entry point for invoking the validated function logic.
Parameters
| Name | Type | Description |
|---|---|---|
| *args | Any | Positional arguments to be validated and passed to the function. |
| **kwargs | Any | Keyword arguments to be validated and passed to the function. |
Returns
| Type | Description |
|---|---|
Any | The return value of the original wrapped function after successful validation. |
build_values()
@classmethod
def build_values(
args: tuple[Any, ...],
kwargs: dict[str, Any]
) - > dict[str, Any]
Maps raw positional and keyword arguments into a dictionary structure that matches the internal Pydantic model's fields. It identifies positional-only violations and duplicate keyword arguments.
Parameters
| Name | Type | Description |
|---|---|---|
| args | tuple[Any, ...] | The sequence of positional arguments to map to function parameters. |
| kwargs | dict[str, Any] | The dictionary of keyword arguments to map to function parameters. |
Returns
| Type | Description |
|---|---|
dict[str, Any] | A dictionary of processed arguments ready for model instantiation, including metadata for error reporting. |
execute()
@classmethod
def execute(
m: [BaseModel](../../v1/main/basemodel.md?sid=pydantic_v1_main_basemodel)
) - > Any
Invokes the raw function using data extracted from a validated Pydantic model instance. It correctly handles the reconstruction of positional-only, variadic, and keyword arguments.
Parameters
| Name | Type | Description |
|---|---|---|
| m | [BaseModel](../../v1/main/basemodel.md?sid=pydantic_v1_main_basemodel) | The validated Pydantic model instance containing the function's arguments. |
Returns
| Type | Description |
|---|---|
Any | The result of the raw function execution. |
create_model()
@classmethod
def create_model(
fields: dict[str, Any],
takes_args: bool,
takes_kwargs: bool,
config: ConfigType
)
Dynamically constructs a Pydantic model used to validate the function's signature. It configures validators for positional-only arguments, duplicate keywords, and extra arguments.
Parameters
| Name | Type | Description |
|---|---|---|
| fields | dict[str, Any] | A mapping of field names to their type annotations and default values. |
| takes_args | bool | Indicates if the function accepts variable positional arguments (*args). |
| takes_kwargs | bool | Indicates if the function accepts variable keyword arguments (**kwargs). |
| config | ConfigType | The Pydantic configuration object used to customize model behavior. |