Coverage for pydantic/experimental/arguments_schema.py: 100.00%
12 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 15:02 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-15 15:02 +0000
1"""Experimental module exposing a function to generate a core schema that validates callable arguments."""
3from __future__ import annotations 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
5from collections.abc import Callable 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
6from typing import Any, Literal 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
8from pydantic_core import CoreSchema 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
10from pydantic import ConfigDict 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
11from pydantic._internal import _config, _generate_schema, _namespace_utils 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
14def generate_arguments_schema( 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM
15 func: Callable[..., Any],
16 schema_type: Literal['arguments', 'arguments-v3'] = 'arguments-v3',
17 parameters_callback: Callable[[int, str, Any], Literal['skip'] | None] | None = None,
18 config: ConfigDict | None = None,
19) -> CoreSchema:
20 """Generate the schema for the arguments of a function.
22 Args:
23 func: The function to generate the schema for.
24 schema_type: The type of schema to generate.
25 parameters_callback: A callable that will be invoked for each parameter. The callback
26 should take three required arguments: the index, the name and the type annotation
27 (or [`Parameter.empty`][inspect.Parameter.empty] if not annotated) of the parameter.
28 The callback can optionally return `'skip'`, so that the parameter gets excluded
29 from the resulting schema.
30 config: The configuration to use.
32 Returns:
33 The generated schema.
34 """
35 generate_schema = _generate_schema.GenerateSchema( 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
36 _config.ConfigWrapper(config),
37 ns_resolver=_namespace_utils.NsResolver(namespaces_tuple=_namespace_utils.ns_for_function(func)),
38 )
40 if schema_type == 'arguments': 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
41 schema = generate_schema._arguments_schema(func, parameters_callback) # pyright: ignore[reportArgumentType] 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
42 else:
43 schema = generate_schema._arguments_v3_schema(func, parameters_callback) # pyright: ignore[reportArgumentType] 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM
44 return generate_schema.clean_schema(schema) 1abcdefghijklmNOnopqrstuvwxyzABCDEFGHIJKLM