codekingpro/portable-devtools
114k
1"""Decorator for validating function calls."""2 3from __future__ import annotations as _annotations4 5import inspect6from functools import partial7from types import BuiltinFunctionType8from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast, overload9 10from ._internal import _generate_schema, _typing_extra, _validate_call11from .errors import PydanticUserError12 13__all__ = ('validate_call',)14 15if TYPE_CHECKING:16 from .config import ConfigDict17 18 AnyCallableT = TypeVar('AnyCallableT', bound=Callable[..., Any])19 20 21_INVALID_TYPE_ERROR_CODE = 'validate-call-type'22 23 24def _check_function_type(function: object) -> None:25 """Check if the input function is a supported type for `validate_call`."""26 if isinstance(function, _generate_schema.VALIDATE_CALL_SUPPORTED_TYPES):27 try:28 _typing_extra.signature_no_eval(cast(_generate_schema.ValidateCallSupportedTypes, function))29 except (ValueError, TypeError):30 raise PydanticUserError(31 f"Input function `{function}` doesn't have a valid signature", code=_INVALID_TYPE_ERROR_CODE32 )33 34 if isinstance(function, partial):35 try:36 assert not isinstance(partial.func, partial), 'Partial of partial'37 _check_function_type(function.func)38 except PydanticUserError as e:39 raise PydanticUserError(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 e43 44 return45 46 if isinstance(function, BuiltinFunctionType):47 raise PydanticUserError(f'Input built-in function `{function}` is not supported', code=_INVALID_TYPE_ERROR_CODE)48 if isinstance(function, (classmethod, staticmethod, property)):49 name = type(function).__name__50 raise PydanticUserError(51 f'The `@{name}` decorator should be applied after `@validate_call` (put `@{name}` on top)',52 code=_INVALID_TYPE_ERROR_CODE,53 )54 55 if inspect.isclass(function):56 raise PydanticUserError(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):61 raise PydanticUserError(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 )65 66 raise PydanticUserError(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 )70 71 72@overload73def validate_call(74 *, config: ConfigDict | None = None, validate_return: bool = False75) -> Callable[[AnyCallableT], AnyCallableT]: ...76 77 78@overload79def validate_call(func: AnyCallableT, /) -> AnyCallableT: ...80 81 82def validate_call(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)91 92 Returns a decorated wrapper around the function that validates the arguments and, optionally, the return value.93 94 Usage may be either as a plain decorator `@validate_call` or with arguments `@validate_call(...)`.95 96 Args:97 func: The function to be decorated.98 config: The configuration dictionary.99 validate_return: Whether to validate the return value.100 101 Returns:102 The decorated function.103 """104 parent_namespace = _typing_extra.parent_frame_namespace()105 106 def validate(function: AnyCallableT) -> AnyCallableT:107 _check_function_type(function)108 validate_call_wrapper = _validate_call.ValidateCallWrapper(109 cast(_generate_schema.ValidateCallSupportedTypes, function), config, validate_return, parent_namespace110 )111 return _validate_call.update_wrapper_attributes(function, validate_call_wrapper.__call__) # type: ignore112 113 if func is not None:114 return validate(func)115 else:116 return validate117 