Coverage for pydantic/validate_call_decorator.py: 100.00%
41 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-20 10:39 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-20 10:39 +0000
1"""Decorator for validating function calls."""
3from __future__ import annotations as _annotations 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
5import inspect 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
6from functools import partial 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
7from types import BuiltinFunctionType 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
8from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast, overload 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
10from ._internal import _generate_schema, _typing_extra, _validate_call 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
11from .errors import PydanticUserError 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
13__all__ = ('validate_call',) 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
15if TYPE_CHECKING: 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
16 from .config import ConfigDict
18 AnyCallableT = TypeVar('AnyCallableT', bound=Callable[..., Any])
21_INVALID_TYPE_ERROR_CODE = 'validate-call-type' 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
24def _check_function_type(function: object) -> None: 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
25 """Check if the input function is a supported type for `validate_call`."""
26 if isinstance(function, _generate_schema.VALIDATE_CALL_SUPPORTED_TYPES): 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
27 try: 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
28 inspect.signature(cast(_generate_schema.ValidateCallSupportedTypes, function)) 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
29 except ValueError: 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
30 raise PydanticUserError( 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
31 f"Input function `{function}` doesn't have a valid signature", code=_INVALID_TYPE_ERROR_CODE
32 )
34 if isinstance(function, partial): 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
35 try: 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
36 assert not isinstance(partial.func, partial), 'Partial of partial' 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
37 _check_function_type(function.func) 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
38 except PydanticUserError as e: 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
39 raise PydanticUserError( 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
40 f'Partial of `{function.func}` is invalid because the type of `{function.func}` is not supported by `validate_call`',
41 code=_INVALID_TYPE_ERROR_CODE,
42 ) from e
44 return 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
46 if isinstance(function, BuiltinFunctionType): 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
47 raise PydanticUserError(f'Input built-in function `{function}` is not supported', code=_INVALID_TYPE_ERROR_CODE) 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
48 if isinstance(function, (classmethod, staticmethod, property)): 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
49 name = type(function).__name__ 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
50 raise PydanticUserError( 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
51 f'The `@{name}` decorator should be applied after `@validate_call` (put `@{name}` on top)',
52 code=_INVALID_TYPE_ERROR_CODE,
53 )
55 if inspect.isclass(function): 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
56 raise PydanticUserError( 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
57 f'Unable to validate {function}: `validate_call` should be applied to functions, not classes (put `@validate_call` on top of `__init__` or `__new__` instead)',
58 code=_INVALID_TYPE_ERROR_CODE,
59 )
60 if callable(function): 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
61 raise PydanticUserError( 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
62 f'Unable to validate {function}: `validate_call` should be applied to functions, not instances or other callables. Use `validate_call` explicitly on `__call__` instead.',
63 code=_INVALID_TYPE_ERROR_CODE,
64 )
66 raise PydanticUserError( 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
67 f'Unable to validate {function}: `validate_call` should be applied to one of the following: function, method, partial, or lambda',
68 code=_INVALID_TYPE_ERROR_CODE,
69 )
72@overload 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
73def validate_call( 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
74 *, config: ConfigDict | None = None, validate_return: bool = False 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
75) -> Callable[[AnyCallableT], AnyCallableT]: ... 1abcdevwfghijklmno
78@overload 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
79def validate_call(func: AnyCallableT, /) -> AnyCallableT: ... 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
82def validate_call( 1pqrstuabcdexyzABCfghijDEFGHIklmno
83 func: AnyCallableT | None = None,
84 /,
85 *,
86 config: ConfigDict | None = None,
87 validate_return: bool = False,
88) -> AnyCallableT | Callable[[AnyCallableT], AnyCallableT]:
89 """!!! abstract "Usage Documentation"
90 [Validation Decorator](../concepts/validation_decorator.md)
92 Returns a decorated wrapper around the function that validates the arguments and, optionally, the return value.
94 Usage may be either as a plain decorator `@validate_call` or with arguments `@validate_call(...)`.
96 Args:
97 func: The function to be decorated.
98 config: The configuration dictionary.
99 validate_return: Whether to validate the return value.
101 Returns:
102 The decorated function.
103 """
104 parent_namespace = _typing_extra.parent_frame_namespace() 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
106 def validate(function: AnyCallableT) -> AnyCallableT: 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
107 _check_function_type(function) 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
108 validate_call_wrapper = _validate_call.ValidateCallWrapper( 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
109 cast(_generate_schema.ValidateCallSupportedTypes, function), config, validate_return, parent_namespace
110 )
111 return _validate_call.update_wrapper_attributes(function, validate_call_wrapper.__call__) # type: ignore 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
113 if func is not None: 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
114 return validate(func) 1pqrstuabcdevwxyzABCfghijDEFGHIklmno
115 else:
116 return validate 1pqrstuabcdevwxyzABCfghijDEFGHIklmno