Merge "Expose function signature fetching function"
This commit is contained in:
commit
d423b3b74c
@ -35,6 +35,17 @@ except AttributeError:
|
|||||||
# others)...
|
# others)...
|
||||||
_BUILTIN_MODULES = ('builtins', '__builtin__', '__builtins__', 'exceptions')
|
_BUILTIN_MODULES = ('builtins', '__builtin__', '__builtins__', 'exceptions')
|
||||||
|
|
||||||
|
if six.PY3:
|
||||||
|
Parameter = inspect.Parameter
|
||||||
|
Signature = inspect.Signature
|
||||||
|
get_signature = inspect.signature
|
||||||
|
else:
|
||||||
|
# Provide an equivalent but use funcsigs instead...
|
||||||
|
import funcsigs
|
||||||
|
Parameter = funcsigs.Parameter
|
||||||
|
Signature = funcsigs.Signature
|
||||||
|
get_signature = funcsigs.signature
|
||||||
|
|
||||||
|
|
||||||
def get_members(obj, exclude_hidden=True):
|
def get_members(obj, exclude_hidden=True):
|
||||||
"""Yields the members of an object, filtering by hidden/not hidden.
|
"""Yields the members of an object, filtering by hidden/not hidden.
|
||||||
@ -182,19 +193,6 @@ def is_subclass(obj, cls):
|
|||||||
return inspect.isclass(obj) and issubclass(obj, cls)
|
return inspect.isclass(obj) and issubclass(obj, cls)
|
||||||
|
|
||||||
|
|
||||||
def _get_arg_spec(function):
|
|
||||||
if isinstance(function, _TYPE_TYPE):
|
|
||||||
bound = True
|
|
||||||
function = function.__init__
|
|
||||||
elif isinstance(function, (types.FunctionType, types.MethodType)):
|
|
||||||
bound = is_bound_method(function)
|
|
||||||
function = getattr(function, '__wrapped__', function)
|
|
||||||
else:
|
|
||||||
function = function.__call__
|
|
||||||
bound = is_bound_method(function)
|
|
||||||
return inspect.getargspec(function), bound
|
|
||||||
|
|
||||||
|
|
||||||
def get_callable_args(function, required_only=False):
|
def get_callable_args(function, required_only=False):
|
||||||
"""Get names of callable arguments.
|
"""Get names of callable arguments.
|
||||||
|
|
||||||
@ -204,16 +202,17 @@ def get_callable_args(function, required_only=False):
|
|||||||
If required_only is True, optional arguments (with default values)
|
If required_only is True, optional arguments (with default values)
|
||||||
are not included into output.
|
are not included into output.
|
||||||
"""
|
"""
|
||||||
argspec, bound = _get_arg_spec(function)
|
sig = get_signature(function)
|
||||||
f_args = argspec.args
|
function_args = list(six.iterkeys(sig.parameters))
|
||||||
if required_only and argspec.defaults:
|
for param_name, p in six.iteritems(sig.parameters):
|
||||||
f_args = f_args[:-len(argspec.defaults)]
|
if (p.kind in (Parameter.VAR_POSITIONAL, Parameter.VAR_KEYWORD)
|
||||||
if bound:
|
or (required_only and p.default is not Parameter.empty)):
|
||||||
f_args = f_args[1:]
|
function_args.remove(param_name)
|
||||||
return f_args
|
return function_args
|
||||||
|
|
||||||
|
|
||||||
def accepts_kwargs(function):
|
def accepts_kwargs(function):
|
||||||
"""Returns True if function accepts kwargs."""
|
"""Returns ``True`` if function accepts kwargs otherwise ``False``."""
|
||||||
argspec, _bound = _get_arg_spec(function)
|
sig = get_signature(function)
|
||||||
return bool(argspec.keywords)
|
return any(p.kind == Parameter.VAR_KEYWORD
|
||||||
|
for p in six.itervalues(sig.parameters))
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
pbr>=1.6
|
pbr>=1.6
|
||||||
six>=1.9.0
|
six>=1.9.0
|
||||||
|
funcsigs>=0.4;python_version=='2.7' or python_version=='2.6'
|
||||||
iso8601>=0.1.9
|
iso8601>=0.1.9
|
||||||
oslo.i18n>=1.5.0 # Apache-2.0
|
oslo.i18n>=1.5.0 # Apache-2.0
|
||||||
monotonic>=0.3 # Apache-2.0
|
monotonic>=0.3 # Apache-2.0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user