def get_kwargs_index(target) -> int:
"""
Returns the index of the "**kwargs" parameter if such a parameter exists in
the function arguments or -1 otherwise.
:param target:
The target function for which the kwargs index should be determined
:return:
The keyword arguments index if it exists or -1 if not
"""
code = target.__code__
if not bool(code.co_flags & inspect.CO_VARKEYWORDS):
return -1
return (
code.co_argcount +
code.co_kwonlyargcount +
(1 if code.co_flags & inspect.CO_VARARGS else 0)
)
评论列表
文章目录