diff options
author | Jenkins <jenkins@review.openstack.org> | 2015-12-09 15:37:26 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2015-12-09 15:37:26 +0000 |
commit | d423b3b74cfae284c4b46d3bad5232e3840e05f3 (patch) | |
tree | b4feaae80d40b9b17856d509a2cb1dbc4204ca08 | |
parent | fa73d96f144e9357db79da6d981f8d43aed5d1a1 (diff) | |
parent | 43451e5cc767747b64d7b5700800c001b2d1d895 (diff) | |
download | oslo-utils-d423b3b74cfae284c4b46d3bad5232e3840e05f3.tar.gz |
Merge "Expose function signature fetching function"
-rw-r--r-- | oslo_utils/reflection.py | 45 | ||||
-rw-r--r-- | requirements.txt | 1 |
2 files changed, 23 insertions, 23 deletions
diff --git a/oslo_utils/reflection.py b/oslo_utils/reflection.py index 3333ba6..b0dcada 100644 --- a/oslo_utils/reflection.py +++ b/oslo_utils/reflection.py @@ -35,6 +35,17 @@ except AttributeError: # others)... _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): """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) -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): """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) are not included into output. """ - argspec, bound = _get_arg_spec(function) - f_args = argspec.args - if required_only and argspec.defaults: - f_args = f_args[:-len(argspec.defaults)] - if bound: - f_args = f_args[1:] - return f_args + sig = get_signature(function) + function_args = list(six.iterkeys(sig.parameters)) + for param_name, p in six.iteritems(sig.parameters): + if (p.kind in (Parameter.VAR_POSITIONAL, Parameter.VAR_KEYWORD) + or (required_only and p.default is not Parameter.empty)): + function_args.remove(param_name) + return function_args def accepts_kwargs(function): - """Returns True if function accepts kwargs.""" - argspec, _bound = _get_arg_spec(function) - return bool(argspec.keywords) + """Returns ``True`` if function accepts kwargs otherwise ``False``.""" + sig = get_signature(function) + return any(p.kind == Parameter.VAR_KEYWORD + for p in six.itervalues(sig.parameters)) diff --git a/requirements.txt b/requirements.txt index ed3f76a..581071e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,7 @@ pbr>=1.6 six>=1.9.0 +funcsigs>=0.4;python_version=='2.7' or python_version=='2.6' iso8601>=0.1.9 oslo.i18n>=1.5.0 # Apache-2.0 monotonic>=0.3 # Apache-2.0 |