wrapt
wrapt is a Python module designed for decorators, wrappers, and monkey patching. It emphasizes correctness and transparency, ensuring that decorators preserve introspectability and function signatures. The current version is 2.1.2, with a release cadence that reflects active maintenance and updates.
Warnings
- breaking In Python 3.13, the behavior of @classmethod.__get__() was reverted to its pre-3.9 state, affecting decorators applied inside @classmethod.
- gotcha Using a decorated class with super() requires accessing the original wrapped class to avoid issues.
- gotcha Deriving from a decorated class necessitates using the original wrapped class as the base to prevent inheritance issues.
- gotcha Using issubclass() on abstract classes decorated with wrapt can raise TypeError due to Python's handling of abstract base classes.
- gotcha Uninstalling wrapt improperly can lead to Python runtime errors, especially if other packages depend on it.
Install
-
pip install wrapt
Imports
- decorator
from wrapt import decorator
Quickstart
import wrapt
@wrapt.decorator
def pass_through(wrapped, instance, args, kwargs):
return wrapped(*args, **kwargs)
@pass_through
def function():
print("Function executed")
function()