def _get_arg_lengths(func):
"""Returns a two-tuple containing the number of positional arguments
as the first item and the number of variable positional arguments as
the second item.
"""
try:
funcsig = inspect.signature(func)
params_dict = funcsig.parameters
parameters = params_dict.values()
args_type = (inspect._POSITIONAL_OR_KEYWORD, inspect._POSITIONAL_ONLY)
args = [x for x in parameters if x.kind in args_type]
vararg = [x for x in parameters if x.kind == inspect._VAR_POSITIONAL]
vararg = vararg.pop() if vararg else None
except AttributeError:
try:
try: # For Python 3.2 and earlier.
args, vararg = inspect.getfullargspec(func)[:2]
except AttributeError: # For Python 2.7 and earlier.
args, vararg = inspect.getargspec(func)[:2]
except TypeError: # In 3.2 and earlier, raises TypeError
raise ValueError # but 3.3 and later raise a ValueError.
return (len(args), (1 if vararg else 0))
评论列表
文章目录