Introducing funcsigs
The Funcsigs Package
funcsigs
is a backport of the PEP 362 function signature features from
Python 3.3's inspect module. The backport is compatible with Python 2.6, 2.7
as well as 3.3 and up. 3.2 was supported by version 0.4, but with setuptools and
pip no longer supporting 3.2, we cannot make any statement about 3.2
compatibility.
Compatibility
The funcsigs
backport has been tested against:
- CPython 2.6
- CPython 2.7
- CPython 3.3
- CPython 3.4
- CPython 3.5
- CPython nightlies
- PyPy and PyPy3(currently failing CI)
Continuous integration testing is provided by Travis CI.
Under Python 2.x there is a compatibility issue when a function is assigned to
the __wrapped__
property of a class after it has been constructed.
Similiarily there under PyPy directly passing the __call__
method of a
builtin is also a compatibility issues. Otherwise the functionality is
believed to be uniform between both Python2 and Python3.
Issues
Source code for funcsigs
is hosted on GitHub. Any bug reports or feature
requests can be made using GitHub's issues system.
Example
To obtain a Signature object, pass the target function to the
funcsigs.signature
function.
>>> from funcsigs import signature
>>> def foo(a, b=None, *args, **kwargs):
... pass
...
>>> sig = signature(foo)
>>> sig
<funcsigs.Signature object at 0x...>
>>> sig.parameters
OrderedDict([('a', <Parameter at 0x... 'a'>), ('b', <Parameter at 0x... 'b'>), ('args', <Parameter at 0x... 'args'>), ('kwargs', <Parameter at 0x... 'kwargs'>)])
>>> sig.return_annotation
<class 'funcsigs._empty'>
Introspecting callables with the Signature object
Note
This section of documentation is a direct reproduction of the Python standard library documentation for the inspect module.
The Signature object represents the call signature of a callable object and its return annotation. To retrieve a Signature object, use the :func:`signature` function.
A Signature object represents the call signature of a function and its return annotation. For each parameter accepted by the function it stores a :class:`Parameter` object in its :attr:`parameters` collection.
Signature objects are immutable. Use :meth:`Signature.replace` to make a modified copy.
Parameter objects are immutable. Instead of modifying a Parameter object, you can use :meth:`Parameter.replace` to create a modified copy.
Result of a :meth:`Signature.bind` or :meth:`Signature.bind_partial` call. Holds the mapping of arguments to the function's parameters.
The :attr:`args` and :attr:`kwargs` properties can be used to invoke functions:
def test(a, *, b): ... sig = signature(test) ba = sig.bind(10, b=20) test(*ba.args, **ba.kwargs)
Copyright
funcsigs is a derived work of CPython under the terms of the PSF License Agreement. The original CPython inspect module, its unit tests and documentation are the copyright of the Python Software Foundation. The derived work is distributed under the Apache License Version 2.0.