Python Function Signatures (funcsigs)
funcsigs is a Python library that backports the function signature introspection features from Python 3.3's `inspect` module (PEP 362) to older Python versions. It allows developers to work with `Signature` and `Parameter` objects to understand callable arguments and return annotations, even in Python 2.6, 2.7, and 3.2+. Its latest version, 1.0.2, was released in April 2016, marking it as a mature and largely unmaintained project, as its core functionality has been integrated into Python's standard library for modern versions.
Warnings
- gotcha For Python 3.3 and newer, the built-in `inspect.signature` should be used instead of `funcsigs.signature`. Relying on `funcsigs` in newer Python versions is unnecessary and can introduce compatibility issues if the native `inspect` module's behavior changes or if `funcsigs` is not installed.
- deprecated The `funcsigs` library is no longer actively maintained since its functionality has been integrated into the Python standard library's `inspect` module (from Python 3.3 onwards). While functional for its intended older Python versions, new development should prefer `inspect.signature`.
- gotcha Compatibility quirks exist in specific environments: in Python 2.x, issues can arise when a function is assigned to a class's `__wrapped__` property after construction. Under PyPy, directly passing the `__call__` method of a builtin can also cause compatibility problems.
Install
-
pip install funcsigs
Imports
- signature
from funcsigs import signature
- Signature
from funcsigs import Signature
- Parameter
from funcsigs import Parameter
Quickstart
from funcsigs import signature
def foo(a, b=None, *args, **kwargs):
pass
sig = signature(foo)
print(f"Signature: {sig}")
print(f"Parameters: {sig.parameters}")
def bar(x: int, y: str = 'default') -> bool:
pass
sig_bar = signature(bar)
print(f"Signature (with annotations): {sig_bar}")
print(f"Return annotation: {sig_bar.return_annotation}")