2020-07-05 11:49:36 +08:00
|
|
|
import functools
|
2015-06-11 05:24:04 +08:00
|
|
|
import inspect
|
|
|
|
|
|
|
|
|
2020-07-05 11:49:36 +08:00
|
|
|
@functools.lru_cache(maxsize=512)
|
2021-03-19 19:23:28 +08:00
|
|
|
def _get_func_parameters(func, remove_first):
|
|
|
|
parameters = tuple(inspect.signature(func).parameters.values())
|
|
|
|
if remove_first:
|
|
|
|
parameters = parameters[1:]
|
|
|
|
return parameters
|
|
|
|
|
|
|
|
|
|
|
|
def _get_callable_parameters(meth_or_func):
|
|
|
|
is_method = inspect.ismethod(meth_or_func)
|
|
|
|
func = meth_or_func.__func__ if is_method else meth_or_func
|
|
|
|
return _get_func_parameters(func, remove_first=is_method)
|
2020-07-05 11:49:36 +08:00
|
|
|
|
|
|
|
|
2015-06-11 05:24:04 +08:00
|
|
|
def get_func_args(func):
|
2021-03-19 19:23:28 +08:00
|
|
|
params = _get_callable_parameters(func)
|
2015-06-11 05:24:04 +08:00
|
|
|
return [
|
2021-03-19 19:23:28 +08:00
|
|
|
param.name for param in params
|
2015-06-11 05:24:04 +08:00
|
|
|
if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2014-10-31 03:45:01 +08:00
|
|
|
def get_func_full_args(func):
|
|
|
|
"""
|
|
|
|
Return a list of (argument name, default value) tuples. If the argument
|
|
|
|
does not have a default value, omit it in the tuple. Arguments such as
|
|
|
|
*args and **kwargs are also included.
|
|
|
|
"""
|
2021-03-19 19:23:28 +08:00
|
|
|
params = _get_callable_parameters(func)
|
2014-10-31 03:45:01 +08:00
|
|
|
args = []
|
2021-03-19 19:23:28 +08:00
|
|
|
for param in params:
|
|
|
|
name = param.name
|
2014-10-31 03:45:01 +08:00
|
|
|
# Ignore 'self'
|
|
|
|
if name == 'self':
|
|
|
|
continue
|
|
|
|
if param.kind == inspect.Parameter.VAR_POSITIONAL:
|
|
|
|
name = '*' + name
|
|
|
|
elif param.kind == inspect.Parameter.VAR_KEYWORD:
|
|
|
|
name = '**' + name
|
|
|
|
if param.default != inspect.Parameter.empty:
|
|
|
|
args.append((name, param.default))
|
|
|
|
else:
|
|
|
|
args.append((name,))
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
2015-06-11 05:24:04 +08:00
|
|
|
def func_accepts_kwargs(func):
|
2020-12-14 14:34:35 +08:00
|
|
|
"""Return True if function 'func' accepts keyword arguments **kwargs."""
|
2015-06-11 05:24:04 +08:00
|
|
|
return any(
|
2021-03-19 19:23:28 +08:00
|
|
|
p for p in _get_callable_parameters(func)
|
2015-06-11 05:24:04 +08:00
|
|
|
if p.kind == p.VAR_KEYWORD
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-10-31 03:45:01 +08:00
|
|
|
def func_accepts_var_args(func):
|
|
|
|
"""
|
|
|
|
Return True if function 'func' accepts positional arguments *args.
|
|
|
|
"""
|
|
|
|
return any(
|
2021-03-19 19:23:28 +08:00
|
|
|
p for p in _get_callable_parameters(func)
|
2014-10-31 03:45:01 +08:00
|
|
|
if p.kind == p.VAR_POSITIONAL
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-08-07 11:12:51 +08:00
|
|
|
def method_has_no_args(meth):
|
|
|
|
"""Return True if a method only accepts 'self'."""
|
2018-08-07 11:12:51 +08:00
|
|
|
count = len([
|
2021-03-19 19:23:28 +08:00
|
|
|
p for p in _get_callable_parameters(meth)
|
2014-10-31 03:45:01 +08:00
|
|
|
if p.kind == p.POSITIONAL_OR_KEYWORD
|
2018-08-07 11:12:51 +08:00
|
|
|
])
|
|
|
|
return count == 0 if inspect.ismethod(meth) else count == 1
|
2015-06-11 05:24:04 +08:00
|
|
|
|
|
|
|
|
2021-03-19 19:23:28 +08:00
|
|
|
def func_supports_parameter(func, name):
|
|
|
|
return any(param.name == name for param in _get_callable_parameters(func))
|