Python inspect.ArgSpec() Examples

The following are 28 code examples of inspect.ArgSpec(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module inspect , or try the search function .
Example #1
Source File: python.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def _getargspec_py23(func):
    """_getargspec_py23(function) -> named tuple ArgSpec(args, varargs, keywords,
                                                        defaults)

    Identical to inspect.getargspec() in python2, but uses
    inspect.getfullargspec() for python3 behind the scenes to avoid
    DeprecationWarning.

    >>> def f(a, b=2, *ar, **kw):
    ...     pass

    >>> _getargspec_py23(f)
    ArgSpec(args=['a', 'b'], varargs='ar', keywords='kw', defaults=(2,))
    """
    if six.PY2:
        return inspect.getargspec(func)

    return inspect.ArgSpec(*inspect.getfullargspec(func)[:4]) 
Example #2
Source File: python.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def _getargspec_py23(func):
    """_getargspec_py23(function) -> named tuple ArgSpec(args, varargs, keywords,
                                                        defaults)

    Identical to inspect.getargspec() in python2, but uses
    inspect.getfullargspec() for python3 behind the scenes to avoid
    DeprecationWarning.

    >>> def f(a, b=2, *ar, **kw):
    ...     pass

    >>> _getargspec_py23(f)
    ArgSpec(args=['a', 'b'], varargs='ar', keywords='kw', defaults=(2,))
    """
    if six.PY2:
        return inspect.getargspec(func)

    return inspect.ArgSpec(*inspect.getfullargspec(func)[:4]) 
Example #3
Source File: test_inspection.py    From qcore with Apache License 2.0 6 votes vote down vote up
def test_getargspec():
    empty = inspect.ArgSpec(args=[], varargs=None, keywords=None, defaults=None)
    assert_eq(empty, qcore.inspection.getargspec(test_get_subclass_tree))
    assert_eq(empty, qcore.inspection.getargspec(qcore.inspection.lazy_stack))

    emptymethod = inspect.ArgSpec(
        args=["self"], varargs=None, keywords=None, defaults=None
    )
    assert_eq(emptymethod, qcore.inspection.getargspec(X.myinstancemethod))
    assert_eq(emptymethod, qcore.inspection.getargspec(X().myinstancemethod))

    emptyclsmethod = inspect.ArgSpec(
        args=["cls"], varargs=None, keywords=None, defaults=None
    )
    assert_eq(emptyclsmethod, qcore.inspection.getargspec(X.myclassmethod))

    spec = inspect.ArgSpec(
        args=["a", "b", "c", "d"], varargs=None, keywords="f", defaults=("e",)
    )
    assert_eq(spec, qcore.inspection.getargspec(fun_with_args)) 
Example #4
Source File: inspection.py    From qcore with Apache License 2.0 6 votes vote down vote up
def getargspec(func):
    """Variation of inspect.getargspec that works for more functions.

    This function works for Cythonized, non-cpdef functions, which expose argspec information but
    are not accepted by getargspec. It also works for Python 3 functions that use annotations, which
    are simply ignored. However, keyword-only arguments are not supported.

    """
    if inspect.ismethod(func):
        func = func.__func__
    # Cythonized functions have a .__code__, but don't pass inspect.isfunction()
    try:
        code = func.__code__
    except AttributeError:
        raise TypeError("{!r} is not a Python function".format(func))
    if hasattr(code, "co_kwonlyargcount") and code.co_kwonlyargcount > 0:
        raise ValueError("keyword-only arguments are not supported by getargspec()")
    args, varargs, varkw = inspect.getargs(code)
    return inspect.ArgSpec(args, varargs, varkw, func.__defaults__) 
Example #5
Source File: _functions.py    From sagemaker-containers with Apache License 2.0 6 votes vote down vote up
def getargspec(  # pylint: disable=inconsistent-return-statements
    fn
):  # type: (Callable) -> inspect.ArgSpec
    """Get the names and default values of a function's arguments.

    Args:
        fn (function): a function

    Returns:
        `inspect.ArgSpec`:  A collections.namedtuple with the following attributes:

            * Args:
                args (list): a list of the argument names (it may contain nested lists).
                varargs (str): name of the * argument or None.
                keywords (str): names of the ** argument or None.
                defaults (tuple): an n-tuple of the default values of the last n arguments.
    """
    if six.PY2:
        return inspect.getargspec(fn)  # pylint: disable=deprecated-method
    elif six.PY3:
        full_arg_spec = inspect.getfullargspec(fn)
        return inspect.ArgSpec(
            full_arg_spec.args, full_arg_spec.varargs, full_arg_spec.varkw, full_arg_spec.defaults
        ) 
Example #6
Source File: spaces.py    From holoviews with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def argspec(self):
        return inspect.ArgSpec(args=[], varargs=None, keywords=None, defaults=None) 
Example #7
Source File: tf_inspect.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def getargspec(object):  # pylint: disable=redefined-builtin
  """TFDecorator-aware replacement for inspect.getargspec.

  Args:
    object: A callable, possibly decorated.

  Returns:
    The `ArgSpec` that describes the signature of the outermost decorator that
    changes the callable's signature. If the callable is not decorated,
    `inspect.getargspec()` will be called directly on the callable.
  """
  decorators, target = tf_decorator.unwrap(object)
  return next((d.decorator_argspec for d in decorators
               if d.decorator_argspec is not None), _inspect.getargspec(target)) 
Example #8
Source File: pyir.py    From neutron-lib with Apache License 2.0 5 votes vote down vote up
def _notify(self, signature_type, qualified_name, member, arg_spec=None):
        for listener in self.listeners:
            notify = getattr(listener, 'parse_' + signature_type)
            notify(APISignature(signature_type, qualified_name,
                                member, arg_spec or
                                inspect.ArgSpec(None, None, None, None))) 
Example #9
Source File: pyir.py    From neutron-lib with Apache License 2.0 5 votes vote down vote up
def arg_spec_from_dict(arg_spec_dict):
        defaults = arg_spec_dict['defaults']
        if defaults is not None:
            defaults = tuple(defaults)
        return inspect.ArgSpec(arg_spec_dict['args'],
                               arg_spec_dict['varargs'],
                               arg_spec_dict['keywords'],
                               defaults) 
Example #10
Source File: utils.py    From android_universal with MIT License 5 votes vote down vote up
def test_callable_args(func, args):
    """
    Return True when this function can be called with the given arguments.
    """
    assert isinstance(args, (list, tuple))
    signature = getattr(inspect, 'signature', None)

    if signature is not None:
        # For Python 3, use inspect.signature.
        try:
            sig = _signatures_cache[func]
        except KeyError:
            sig = signature(func)
            _signatures_cache[func] = sig

        try:
            sig.bind(*args)
        except TypeError:
            return False
        else:
            return True
    else:
        # For older Python versions, fall back to using getargspec.
        spec = inspect.getargspec(func)

        # Drop the 'self'
        def drop_self(spec):
            args, varargs, varkw, defaults = spec
            if args[0:1] == ['self']:
                args = args[1:]
            return inspect.ArgSpec(args, varargs, varkw, defaults)

        spec = drop_self(spec)

        # When taking *args, always return True.
        if spec.varargs is not None:
            return True

        # Test whether the given amount of args is between the min and max
        # accepted argument counts.
        return len(spec.args) - len(spec.defaults or []) <= len(args) <= len(spec.args) 
Example #11
Source File: test_inspection.py    From qcore with Apache License 2.0 5 votes vote down vote up
def test_getargspec_py3_only():
        spec = inspect.ArgSpec(
            args=["a", "b"], varargs="args", keywords=None, defaults=None
        )
        assert_eq(spec, qcore.inspection.getargspec(fun_with_annotations))
        with AssertRaises(ValueError):
            qcore.inspection.getargspec(fun_with_kwonly_args) 
Example #12
Source File: inspect.py    From dragon with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _getargspec(target):
        """A python3 version of getargspec."""
        fullargspecs = getfullargspec(target)
        return ArgSpec(
            args=fullargspecs.args,
            varargs=fullargspecs.varargs,
            keywords=fullargspecs.varkw,
            defaults=fullargspecs.defaults
        ) 
Example #13
Source File: execute.py    From simpleflow with MIT License 5 votes vote down vote up
def check_keyword_arguments(argspec, kwargs):
    # type: (inspect.ArgSpec, dict) -> None
    # func() or func(*args) or func(a, b)
    if not argspec.keywords and not argspec.defaults and kwargs:
        raise TypeError('command does not take keyword arguments')

    arguments_defaults = zip_arguments_defaults(argspec)
    not_found = (set(name for name, value in arguments_defaults if
                     value is RequiredArgument) -
                 set(kwargs))
    # Calling func(a=1, b) with func(2) instead of func(a=0, 2)
    if not_found:
        raise TypeError('argument{} "{}" not found'.format(
            's' if len(not_found) > 1 else '',
            ', '.join(not_found))) 
Example #14
Source File: execute.py    From simpleflow with MIT License 5 votes vote down vote up
def check_arguments(argspec, args):
    # type: (inspect.ArgSpec, Any) -> None
    """Validates there is the right number of arguments"""
    # func() or func(**kwargs) or func(a=1, b=2)
    if not argspec.varargs and not argspec.args and args:
        raise TypeError('command does not take varargs')

    # Calling func(a, b) with func(1, 2, 3)
    if not argspec.varargs and argspec.args and len(args) != len(argspec.args):
        raise TypeError('command takes {} arguments: {} passed'.format(
            len(argspec.args),
            len(args))) 
Example #15
Source File: execute.py    From simpleflow with MIT License 5 votes vote down vote up
def zip_arguments_defaults(argspec):
    # type: (inspect.ArgSpec) -> Iterable
    if not argspec.defaults:
        return []

    return zip(
        argspec.args[-len(argspec.defaults):],
        argspec.defaults) 
Example #16
Source File: tf_inspect.py    From lambda-packs with MIT License 5 votes vote down vote up
def getargspec(object):  # pylint: disable=redefined-builtin
  """TFDecorator-aware replacement for inspect.getargspec.

  Args:
    object: A callable, possibly decorated.

  Returns:
    The `ArgSpec` that describes the signature of the outermost decorator that
    changes the callable's signature. If the callable is not decorated,
    `inspect.getargspec()` will be called directly on the callable.
  """
  decorators, target = tf_decorator.unwrap(object)
  return next((d.decorator_argspec for d in decorators
               if d.decorator_argspec is not None), _inspect.getargspec(target)) 
Example #17
Source File: spaces.py    From holoviews with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def noargs(self):
        "Returns True if the callable takes no arguments"
        noargs = inspect.ArgSpec(args=[], varargs=None, keywords=None, defaults=None)
        return self.argspec == noargs 
Example #18
Source File: util.py    From holoviews with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def argspec(callable_obj):
    """
    Returns an ArgSpec object for functions, staticmethods, instance
    methods, classmethods and partials.

    Note that the args list for instance and class methods are those as
    seen by the user. In other words, the first argument which is
    conventionally called 'self' or 'cls' is omitted in these cases.
    """
    if (isinstance(callable_obj, type)
        and issubclass(callable_obj, param.ParameterizedFunction)):
        # Parameterized function.__call__ considered function in py3 but not py2
        spec = _getargspec(callable_obj.__call__)
        args = spec.args[1:]
    elif inspect.isfunction(callable_obj):    # functions and staticmethods
        spec = _getargspec(callable_obj)
        args = spec.args
    elif isinstance(callable_obj, partial): # partials
        arglen = len(callable_obj.args)
        spec =  _getargspec(callable_obj.func)
        args = [arg for arg in spec.args[arglen:] if arg not in callable_obj.keywords]
    elif inspect.ismethod(callable_obj):    # instance and class methods
        spec = _getargspec(callable_obj)
        args = spec.args[1:]
    else:                                   # callable objects
        return argspec(callable_obj.__call__)

    return inspect.ArgSpec(args=args,
                           varargs=spec.varargs,
                           keywords=get_keywords(spec),
                           defaults=spec.defaults) 
Example #19
Source File: abstract_input_generator.py    From tensor2robot with Apache License 2.0 5 votes vote down vote up
def set_preprocess_fn(self, preprocess_fn):  # pytype: disable=invalid-annotation
    """Register the preprocess_fn used during the input data generation.

    Note, the preprocess_fn can only have `features` and optionally `labels` as
    inputs. The `mode` has to be abstracted by using a closure or
    functools.partial prior to passing a preprocessor.preprocess function.
    For example using functools:
    set_preprocess_fn(
      functools.partial(preprocessor.preprocess,
                        mode=tf.estimator.ModeKeys.TRAIN))

    Args:
      preprocess_fn: The function called during the input dataset generation to
        preprocess the data.
    """

    if isinstance(preprocess_fn, functools.partial):  # pytype: disable=wrong-arg-types
      # Note, we do not combine both conditions into one since
      # inspect.getargspec does not work for functools.partial objects.
      if 'mode' not in preprocess_fn.keywords:
        raise ValueError('The preprocess_fn mode has to be set if a partial'
                         'function has been passed.')
    else:
      if six.PY3:
        argspec = inspect.getfullargspec(preprocess_fn)
        # first 4 element of fullspec corresponds to spec:
        # https://docs.python.org/3.4/library/inspect.html
        argspec = inspect.ArgSpec(*argspec[:4])
      else:
        argspec = inspect.getargspec(preprocess_fn)  # pylint: disable=deprecated-method
      if 'mode' in argspec.args:
        raise ValueError('The passed preprocess_fn has an open argument `mode`'
                         'which should be patched by a closure or with '
                         'functools.partial.')

    self._preprocess_fn = preprocess_fn 
Example #20
Source File: lang.py    From girlfriend with MIT License 5 votes vote down vote up
def get_default_args(o):
    """获取函数的默认参数名-值映射
    """
    argspec = o
    if not isinstance(o, inspect.ArgSpec):
        argspec = inspect.getargspec(o)
    if not argspec.defaults:
        return {}
    return dict(zip(argspec.args[-len(argspec.defaults):],
                    argspec.defaults))

# 线性集合类型 
Example #21
Source File: deprecate.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _passedArgSpec(argspec, positional, keyword):
    """
    Take an I{inspect.ArgSpec}, a tuple of positional arguments, and a dict of
    keyword arguments, and return a mapping of arguments that were actually
    passed to their passed values.

    @param argspec: The argument specification for the function to inspect.
    @type argspec: I{inspect.ArgSpec}

    @param positional: The positional arguments that were passed.
    @type positional: L{tuple}

    @param keyword: The keyword arguments that were passed.
    @type keyword: L{dict}

    @return: A dictionary mapping argument names (those declared in C{argspec})
        to values that were passed explicitly by the user.
    @rtype: L{dict} mapping L{str} to L{object}
    """
    result = {}
    unpassed = len(argspec.args) - len(positional)
    if argspec.keywords is not None:
        kwargs = result[argspec.keywords] = {}
    if unpassed < 0:
        if argspec.varargs is None:
            raise TypeError("Too many arguments.")
        else:
            result[argspec.varargs] = positional[len(argspec.args):]
    for name, value in zip(argspec.args, positional):
        result[name] = value
    for name, value in keyword.items():
        if name in argspec.args:
            if name in result:
                raise TypeError("Already passed.")
            result[name] = value
        elif argspec.keywords is not None:
            kwargs[name] = value
        else:
            raise TypeError("no such param")
    return result 
Example #22
Source File: util.py    From okcupyd with MIT License 5 votes vote down vote up
def add_request_to_signature(function):
    argspec = inspect.getargspec(function)
    return inspect.ArgSpec(argspec.args + ['request'], argspec.varargs, argspec.keywords, argspec.defaults) 
Example #23
Source File: codegen.py    From safrs with GNU General Public License v3.0 5 votes vote down vote up
def _getargspec_init(method):
        try:
            return inspect.getargspec(method)
        except TypeError:
            if method is object.__init__:
                return ArgSpec(["self"], None, None, None)
            else:
                return ArgSpec(["self"], "args", "kwargs", None) 
Example #24
Source File: deprecate.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def _passed(argspec, positional, keyword):
    """
    Take an I{inspect.ArgSpec}, a tuple of positional arguments, and a dict of
    keyword arguments, and return a mapping of arguments that were actually
    passed to their passed values.

    @param argspec: The argument specification for the function to inspect.
    @type argspec: I{inspect.ArgSpec}

    @param positional: The positional arguments that were passed.
    @type positional: L{tuple}

    @param keyword: The keyword arguments that were passed.
    @type keyword: L{dict}

    @return: A dictionary mapping argument names (those declared in C{argspec})
        to values that were passed explicitly by the user.
    @rtype: L{dict} mapping L{str} to L{object}
    """
    result = {}
    unpassed = len(argspec.args) - len(positional)
    if argspec.keywords is not None:
        kwargs = result[argspec.keywords] = {}
    if unpassed < 0:
        if argspec.varargs is None:
            raise TypeError("Too many arguments.")
        else:
            result[argspec.varargs] = positional[len(argspec.args):]
    for name, value in zip(argspec.args, positional):
        result[name] = value
    for name, value in keyword.items():
        if name in argspec.args:
            if name in result:
                raise TypeError("Already passed.")
            result[name] = value
        elif argspec.keywords is not None:
            kwargs[name] = value
        else:
            raise TypeError("no such param")
    return result 
Example #25
Source File: execute.py    From simpleflow with MIT License 4 votes vote down vote up
def program(path=None, argument_format=format_arguments):
    r"""
    Decorate a callable to execute it as an external program.

    :param path: of the program to execute. If it is ``None`` the name of the
                 executable will be the name of the callable.
    :type  path: str.
    :param argument_format: takes the arguments of the callable and converts
                            them to command line arguments.
    :type  argument_format: callable(*args, **kwargs).

    :returns:
        :rtype: callable(*args, **kwargs).

    Examples
    --------

    >>> @program()
    ... def ls(path):
    ...     pass
    >>> ls('/etc/resolv.conf')
    '/etc/resolv.conf\n'

    It will execute the ``ls`` command and requires a single positional
    argument *path*.

    """
    import inspect

    def wrap_callable(func):
        @functools.wraps(func)
        def execute(*args, **kwargs):
            check_arguments(argspec, args)
            check_keyword_arguments(argspec, kwargs)

            command = path or func.__name__
            return subprocess.check_output(
                [command] + argument_format(*args, **kwargs),
                universal_newlines=True)

        try:
            args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = inspect.getfullargspec(func)
            argspec = inspect.ArgSpec(args, varargs, varkw, defaults)
        except AttributeError:
            # noinspection PyDeprecation
            argspec = inspect.getargspec(func)

        # Not automatically assigned in python < 3.2.
        execute.__wrapped__ = func
        return execute

    return wrap_callable 
Example #26
Source File: _mixin_common.py    From keras_experiments with The Unlicense 4 votes vote down vote up
def mixedomatic(ignore_kargs_spec=False):
    """Mixed-in class decorator.

    Must specify arguments as keywords. Calls the __init__ of all classes
    according to reverse(__bases__) which should match mro. The kwargs are
    matched to each class. If a particular class accepts **kwargs then kwargs
    are passed through unless ignore_kargs_spec is True.
    """
    def mixedomatic_(cls):
        '''Convenience decorator to invoke the __init__ of all sup-classes.'''
        classinit = cls.__init__ if '__init__' in cls.__dict__ else None

        def getargs(aspec, kwargs):
            """Get key-word args specific to the args list in the args spec.

            :param aspec: Argspec returned by inspect.getargspec.
            :type aspec: :class:`inspect.ArgSpec`
            :param dict kwargs: Dictionary of keyword arguments.
            """
            if aspec.keywords is not None and not ignore_kargs_spec:
                return kwargs

            _kwargs = {iarg: kwargs[iarg]
                       for iarg in aspec.args if iarg in kwargs}
            return _kwargs

        # define an __init__ function for the class
        def __init__(self, **kwargs):
            # call the __init__ functions of all the bases
            for base_ in reversed(cls.__bases__):
                aspec = inspect.getargspec(base_.__init__)
                base_kwargs = getargs(aspec, kwargs)
                base_.__init__(self, **base_kwargs)

            # also call any __init__ function that was in the class
            if classinit:
                aspec = inspect.getargspec(cls.__init__)
                _kwargs = getargs(aspec, kwargs)
                classinit(self, **_kwargs)

        # make the local function the class's __init__
        setattr(cls, '__init__', __init__)
        return cls

    return mixedomatic_ 
Example #27
Source File: utils.py    From allure-python with Apache License 2.0 4 votes vote down vote up
def getargspec(func):
    """
    Used because getargspec for python 2.7 does not accept functools.partial
    which is the type for pytest fixtures.

    getargspec excerpted from:

    sphinx.util.inspect
    ~~~~~~~~~~~~~~~~~~~
    Helpers for inspecting Python modules.
    :copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.

    Like inspect.getargspec but supports functools.partial as well.
    """
    # noqa: E731 type: (Any) -> Any
    if inspect.ismethod(func):
        func = func.__func__
    parts = 0, ()  # noqa: E731 type: Tuple[int, Tuple[unicode, ...]]
    if type(func) is partial:
        keywords = func.keywords
        if keywords is None:
            keywords = {}
        parts = len(func.args), keywords.keys()
        func = func.func
    if not inspect.isfunction(func):
        raise TypeError('%r is not a Python function' % func)
    args, varargs, varkw = inspect.getargs(func.__code__)
    func_defaults = func.__defaults__
    if func_defaults is None:
        func_defaults = []
    else:
        func_defaults = list(func_defaults)
    if parts[0]:
        args = args[parts[0]:]
    if parts[1]:
        for arg in parts[1]:
            i = args.index(arg) - len(args)  # type: ignore
            del args[i]
            try:
                del func_defaults[i]
            except IndexError:
                pass
    return inspect.ArgSpec(args, varargs, varkw, func_defaults)  # type: ignore 
Example #28
Source File: callable.py    From testplan with Apache License 2.0 4 votes vote down vote up
def getargspec(callable_):
    """
    Return an Argspec for any callable object

    :param callable_: a callable object
    :type callable_: ``Callable``

    :return: argspec for the callable
    :rtype: ``ArgSpec``
    """
    if not callable(callable_):
        raise ValueError("{} is not callable".format(callable_))

    if inspect.ismethod(callable_) or inspect.isfunction(callable_):
        func = callable_
    else:
        func = callable_.__call__

    # In Python 3.7 inspect.getargspec() is deprecated and will be removed in
    # 3.8, due to the addition of keyword-only args and type annotations
    # (see PEPs 3102 and 484 for more information). To retain backwards
    # compatibility we convert from a FullArgSpec to a python2 ArgSpec.
    if six.PY3:
        full_argspec = inspect.getfullargspec(func)

        # Raise a ValueError if the function has any keyword-only args defined,
        # since we can't easily handle them in a way that is also python 2
        # compatible. On the other hand, we can just discard any information on
        # type annotations.
        if full_argspec.kwonlyargs:
            raise ValueError(
                "Cannot get argspec for function with keyword-only args "
                "defined: {}".format(func)
            )
        return ArgSpec(
            args=full_argspec.args,
            varargs=full_argspec.varargs,
            keywords=full_argspec.varkw,
            defaults=full_argspec.defaults,
        )
    else:
        return inspect.getargspec(func)


# backport from python 3.6, 2.7 version does not catch AttributeError