Module for decorators, wrappers and monkey patching.
npx @tessl/cli install tessl/pypi-wrapt@1.17.0A module for transparent object proxies and decorator wrappers that preserves function signatures, supports introspection, and enables sophisticated monkey patching. The wrapt module focuses heavily on correctness, maintaining function metadata, and providing consistent behavior across different wrapping scenarios.
pip install wraptimport wraptCommon imports for specific functionality:
from wrapt import ObjectProxy, FunctionWrapper, decorator
from wrapt import wrap_function_wrapper, synchronizedimport wrapt
# Using ObjectProxy for transparent wrapping
class MyProxy(wrapt.ObjectProxy):
def __init__(self, wrapped):
super().__init__(wrapped)
self._self_extra_data = "custom data"
# Creating a function decorator
@wrapt.decorator
def my_decorator(wrapped, instance, args, kwargs):
print(f"Calling {wrapped.__name__}")
return wrapped(*args, **kwargs)
@my_decorator
def my_function():
return "Hello, World!"
# Monkey patching with proper wrapper
def timing_wrapper(wrapped, instance, args, kwargs):
import time
start = time.time()
result = wrapped(*args, **kwargs)
end = time.time()
print(f"{wrapped.__name__} took {end - start:.4f} seconds")
return result
wrapt.wrap_function_wrapper('time', 'sleep', timing_wrapper)The wrapt module is built around several key design patterns:
Transparent proxy objects that wrap other objects and delegate operations while allowing interception and modification of behavior.
class ObjectProxy:
def __init__(self, wrapped): ...
class CallableObjectProxy(ObjectProxy):
def __init__(self, wrapped): ...
class PartialCallableObjectProxy(ObjectProxy):
def __init__(self, wrapped, *args, **kwargs): ...Specialized wrappers for functions that handle method binding, descriptor protocol, and function-specific behavior with proper signature preservation.
class FunctionWrapper(ObjectProxy):
def __init__(self, wrapped, wrapper, enabled=None): ...
class BoundFunctionWrapper:
pass # Created automatically by FunctionWrapper.__get__Comprehensive utilities for applying patches, wrapping objects, and monkey patching with proper cleanup and context management.
def wrap_function_wrapper(module, name, wrapper): ...
def wrap_object(module, name, factory, args=(), kwargs={}): ...
def patch_function_wrapper(module, name, enabled=None): ...
def transient_function_wrapper(module, name): ...Universal decorator factory and synchronization utilities for creating robust decorators with proper signature preservation.
def decorator(wrapper=None, enabled=None, adapter=None, proxy=FunctionWrapper): ...
def synchronized(wrapped): ...
class AdapterFactory: ...Import hooks, weak references, and utility functions for advanced wrapping scenarios and compatibility.
class WeakFunctionProxy:
def __init__(self, wrapped, callback=None): ...
def register_post_import_hook(hook, name): ...
def when_imported(name): ...
def formatargspec(args, varargs=None, varkw=None, defaults=None, **kwargs): ...__version__: str = "1.17.3"
__version_info__: tuple = ('1', '17', '3')# Core type aliases and interfaces
from typing import Callable, Any, Optional, Union
WrapperFunction = Callable[[Any, Optional[object], tuple, dict], Any]
# Signature: (wrapped, instance, args, kwargs) -> result
AdapterFunction = Callable[[Any], Any]
# Function used for signature adaptation
EnabledFunction = Callable[[], bool]
# Function returning boolean for enable/disable logic
CallbackFunction = Callable[[Any], None]
# Callback function for weak reference expiration
HookFunction = Union[Callable[[Any], None], str]
# Hook function or string in format 'module:function'