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

15 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-11-21 11:56 +0000

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

2 

3from __future__ import annotations 1abcdefghijklmnopqr

4 

5import warnings 1abcdefghijklmnopqr

6from collections.abc import Callable 1abcdefghijklmnopqr

7from typing import Any, Literal 1abcdefghijklmnopqr

8 

9from pydantic_core import CoreSchema 1abcdefghijklmnopqr

10 

11from pydantic import ConfigDict 1abcdefghijklmnopqr

12from pydantic._internal import _config, _generate_schema, _namespace_utils 1abcdefghijklmnopqr

13 

14 

15def generate_arguments_schema( 1abcdefghijklmnopqr

16 func: Callable[..., Any], 

17 schema_type: Literal['arguments', 'arguments-v3'] = 'arguments-v3', 

18 parameters_callback: Callable[[int, str, Any], Literal['skip'] | None] | None = None, 

19 config: ConfigDict | None = None, 

20) -> CoreSchema: 

21 """Generate the schema for the arguments of a function. 

22 

23 Args: 

24 func: The function to generate the schema for. 

25 schema_type: The type of schema to generate. 

26 parameters_callback: A callable that will be invoked for each parameter. The callback 

27 should take three required arguments: the index, the name and the type annotation 

28 (or [`Parameter.empty`][inspect.Parameter.empty] if not annotated) of the parameter. 

29 The callback can optionally return `'skip'`, so that the parameter gets excluded 

30 from the resulting schema. 

31 config: The configuration to use. 

32 

33 Returns: 

34 The generated schema. 

35 """ 

36 generate_schema = _generate_schema.GenerateSchema( 1abcdefghijklmnopqr

37 _config.ConfigWrapper(config), 

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

39 ) 

40 

41 if schema_type == 'arguments': 1abcdefghijklmnopqr

42 with warnings.catch_warnings(): 1abcdefghijklmnopqr

43 # Don't emit the deprecation warnings from the `pydantic_core.core_schema` functions: 

44 warnings.filterwarnings( 1abcdefghijklmnopqr

45 'ignore', message=r'`arguments_(schema|parameter)\(\)` is deprecated', category=DeprecationWarning 

46 ) 

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

48 else: 

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

50 return generate_schema.clean_schema(schema) 1abcdefghijklmnopqr