11"""Provide decorators for validating parameters."""
22
33from decorator import decorator
4+ from functools import wraps
45from plone .api .exc import InvalidParameterError
56from plone .api .exc import MissingParameterError
67
@@ -12,7 +13,17 @@ def _get_arg_spec(func, validator_args):
1213
1314 and check that the decorator doesn't refer to non-existent args.
1415 """
15- signature_args = inspect .getfullargspec (func ).args
16+ signature = inspect .signature (func )
17+ signature_args = [
18+ name
19+ for name , param in signature .parameters .items ()
20+ if param .kind
21+ in (
22+ inspect .Parameter .POSITIONAL_ONLY ,
23+ inspect .Parameter .POSITIONAL_OR_KEYWORD ,
24+ inspect .Parameter .KEYWORD_ONLY ,
25+ )
26+ ]
1627 extra_args = set (validator_args ) - set (signature_args )
1728 if extra_args :
1829 raise ValueError (
@@ -32,9 +43,10 @@ def _get_supplied_args(signature_params, args, kwargs):
3243 either as positional or keyword arguments, and are not None.
3344 """
3445 supplied_args = []
35- for index in range (len (args )):
36- if args [index ] is not None :
37- supplied_args .append (signature_params [index ])
46+ for index , arg in enumerate (args ):
47+ if arg is not None :
48+ arg_name = signature_params [index ]
49+ supplied_args .append (arg_name )
3850
3951 for keyword in kwargs :
4052 if kwargs [keyword ] is not None :
@@ -58,7 +70,8 @@ def _required_parameters(func):
5870 """Provide actual decorator."""
5971 signature_params = _get_arg_spec (func , required_params )
6072
61- def wrapped (function , * args , ** kwargs ):
73+ @wraps (func )
74+ def wrapped (* args , ** kwargs ):
6275 """Provide wrapped function (whose docstring will get replaced)."""
6376 supplied_args = _get_supplied_args (signature_params , args , kwargs )
6477
@@ -70,9 +83,9 @@ def wrapped(function, *args, **kwargs):
7083 ),
7184 )
7285
73- return function (* args , ** kwargs )
86+ return func (* args , ** kwargs )
7487
75- return decorator ( wrapped , func )
88+ return wrapped
7689
7790 return _required_parameters
7891
0 commit comments