Coverage for pydantic/experimental/arguments_schema.py: 100.00%

12 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 08:39 +0000

1"""Experimental module exposing a function to generate a core schema that validates callable arguments.""" 

2 

3from __future__ import annotations 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

4 

5from collections.abc import Callable 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

6from typing import Any, Literal 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

7 

8from pydantic_core import CoreSchema 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

9 

10from pydantic import ConfigDict 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

11from pydantic._internal import _config, _generate_schema, _namespace_utils 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

12 

13 

14def generate_arguments_schema( 1abcdefghijklmnopqrstuvwxyzABCDEFG

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. 

21 

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. 

31 

32 Returns: 

33 The generated schema. 

34 """ 

35 generate_schema = _generate_schema.GenerateSchema( 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

36 _config.ConfigWrapper(config), 

37 ns_resolver=_namespace_utils.NsResolver(namespaces_tuple=_namespace_utils.ns_for_function(func)), 

38 ) 

39 

40 if schema_type == 'arguments': 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

41 schema = generate_schema._arguments_schema(func, parameters_callback) # pyright: ignore[reportArgumentType] 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

42 else: 

43 schema = generate_schema._arguments_v3_schema(func, parameters_callback) # pyright: ignore[reportArgumentType] 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG

44 return generate_schema.clean_schema(schema) 1abcdefghijkHIlmnopqrstuvwxyzABCDEFG