Skip to main content

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

AttributeTypeDescription
raw_functionAnyCallableThe original callable object being wrapped for validation.
arg_mappingdict[int, str] = {}A mapping of positional argument indices to their corresponding parameter names.
positional_only_argsset[str] = set()A set of parameter names that are defined as positional-only in the function signature.
v_args_namestring = "args"The name used for the variable positional arguments field, defaulting to 'args' unless it clashes with an existing parameter.
v_kwargs_namestring = "kwargs"The name used for the variable keyword arguments field, defaulting to 'kwargs' unless it clashes with an existing parameter.
modelType[[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

NameTypeDescription
functionAnyCallableThe callable function to be validated.
configConfigTypeThe 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

NameTypeDescription
*argsAnyPositional arguments passed to the original function.
**kwargsAnyKeyword arguments passed to the original function.

Returns

TypeDescription
[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

NameTypeDescription
*argsAnyPositional arguments to be validated and passed to the function.
**kwargsAnyKeyword arguments to be validated and passed to the function.

Returns

TypeDescription
AnyThe 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

NameTypeDescription
argstuple[Any, ...]The sequence of positional arguments to map to function parameters.
kwargsdict[str, Any]The dictionary of keyword arguments to map to function parameters.

Returns

TypeDescription
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

NameTypeDescription
m[BaseModel](../../v1/main/basemodel.md?sid=pydantic_v1_main_basemodel)The validated Pydantic model instance containing the function's arguments.

Returns

TypeDescription
AnyThe 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

NameTypeDescription
fieldsdict[str, Any]A mapping of field names to their type annotations and default values.
takes_argsboolIndicates if the function accepts variable positional arguments (*args).
takes_kwargsboolIndicates if the function accepts variable keyword arguments (**kwargs).
configConfigTypeThe Pydantic configuration object used to customize model behavior.