diff --git a/pydantic/decorator.py b/pydantic/decorator.py index 1f901c8..d99ab1d 100644 --- a/pydantic/decorator.py +++ b/pydantic/decorator.py @@ -14,27 +14,20 @@ if TYPE_CHECKING: Callable = TypeVar('Callable', bound=AnyCallable) -def validate_arguments(func: 'Callable' = None, **config_params: Any) -> 'Callable': +def validate_arguments(function: 'Callable') -> 'Callable': """ Decorator to validate the arguments passed to a function. """ + vd = ValidatedFunction(function) - def validate(_func: 'Callable') -> 'Callable': - vd = ValidatedFunction(_func, **config_params) + @wraps(function) + def wrapper_function(*args: Any, **kwargs: Any) -> Any: + return vd.call(*args, **kwargs) - @wraps(_func) - def wrapper_function(*args: Any, **kwargs: Any) -> Any: - return vd.call(*args, **kwargs) - - wrapper_function.vd = vd # type: ignore - wrapper_function.raw_function = vd.raw_function # type: ignore - wrapper_function.model = vd.model # type: ignore - return cast('Callable', wrapper_function) - - if func: - return validate(func) - else: - return cast('Callable', validate) + wrapper_function.vd = vd # type: ignore + wrapper_function.raw_function = vd.raw_function # type: ignore + wrapper_function.model = vd.model # type: ignore + return cast('Callable', wrapper_function) ALT_V_ARGS = 'v__args' @@ -43,7 +36,7 @@ V_POSITIONAL_ONLY_NAME = 'v__positional_only' class ValidatedFunction: - def __init__(self, function: 'Callable', **config_params: Any): + def __init__(self, function: 'Callable'): from inspect import signature, Parameter parameters: Mapping[str, Parameter] = signature(function).parameters @@ -107,7 +100,7 @@ class ValidatedFunction: # same with kwargs fields[self.v_kwargs_name] = Dict[Any, Any], None - self.create_model(fields, takes_args, takes_kwargs, **config_params) + self.create_model(fields, takes_args, takes_kwargs) def call(self, *args: Any, **kwargs: Any) -> Any: values = self.build_values(args, kwargs) @@ -177,17 +170,9 @@ class ValidatedFunction: else: return self.raw_function(**d) - def create_model(self, fields: Dict[str, Any], takes_args: bool, takes_kwargs: bool, **config_params: Any) -> None: + def create_model(self, fields: Dict[str, Any], takes_args: bool, takes_kwargs: bool) -> None: pos_args = len(self.arg_mapping) - if TYPE_CHECKING: - - class CustomConfig: - pass - - else: - CustomConfig = type('Config', (), config_params) - class DecoratorBaseModel(BaseModel): @validator(self.v_args_name, check_fields=False, allow_reuse=True) def check_args(cls, v: List[Any]) -> List[Any]: @@ -211,7 +196,7 @@ class ValidatedFunction: keys = ', '.join(map(repr, v)) raise TypeError(f'positional-only argument{plural} passed as keyword argument{plural}: {keys}') - class Config(CustomConfig): + class Config: extra = Extra.forbid self.model = create_model(to_camel(self.raw_function.__name__), __base__=DecoratorBaseModel, **fields)