Utilities for working with inspect.Signature objects
sigtools is a Python package that enhances introspection capabilities for determining function signatures, especially when decorators modify them. It provides utilities for boosting callable signature introspection, backports keyword-only parameters for older Python versions (though current versions are Python 3+), and tools for combining functions while preserving correct signatures. The current version is 4.0.1, with releases occurring periodically to maintain compatibility and add features.
Warnings
- breaking Sigtools versions 4.0.0 and above officially require Python 3.6 or newer. Older versions offered Python 2 compatibility or backports, but this support has been removed.
- gotcha When `sigtools` decorators (like `@signature`, `@specifiers.forwards_to`, `@modifiers.kwoargs`) are applied to a callable, the standard `inspect.signature` will often *not* reflect the modified or intended effective signature. Always use `sigtools.signature` for accurate introspection of callables managed by `sigtools`.
- gotcha Combining functions with `sigtools.wrappers` or managing argument forwarding with `sigtools.specifiers` can introduce complexity in understanding how arguments are mapped or interpreted. Misunderstanding these mechanisms can lead to unexpected `TypeError` or `AttributeError` exceptions.
Install
-
pip install sigtools
Imports
- signature
from sigtools import signature
- specifiers
from sigtools import specifiers
- wrappers
from sigtools import wrappers
- modifiers
from sigtools import modifiers
Quickstart
from sigtools import signature
def original_func(a, b):
pass
@signature(original_func)
def wrapped_func(c, d):
# This function's signature is now effectively (a, b) due to @signature(original_func)
pass
print(f"Original signature: {signature(original_func)}")
print(f"Wrapped signature: {signature(wrapped_func)}")