or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

decorators.mdfunction-wrappers.mdindex.mdpatching.mdproxy-objects.mdutilities.md
tile.json

tessl/pypi-wrapt

Module for decorators, wrappers and monkey patching.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/wrapt@1.17.x

To install, run

npx @tessl/cli install tessl/pypi-wrapt@1.17.0

index.mddocs/

Wrapt

A 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.

Package Information

  • Package Name: wrapt
  • Language: Python
  • Installation: pip install wrapt

Core Imports

import wrapt

Common imports for specific functionality:

from wrapt import ObjectProxy, FunctionWrapper, decorator
from wrapt import wrap_function_wrapper, synchronized

Basic Usage

import 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)

Architecture

The wrapt module is built around several key design patterns:

  • Transparent Proxies: ObjectProxy provides seamless delegation to wrapped objects while allowing interception
  • Function Wrappers: Specialized wrappers that handle method binding, descriptor protocol, and function metadata preservation
  • Universal Decorators: The decorator function creates decorators that work correctly across functions, methods, and classes
  • Patching Framework: Comprehensive tools for monkey patching with proper cleanup and context management

Capabilities

Proxy Objects

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): ...

Proxy Objects

Function Wrappers

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__

Function Wrappers

Patching and Monkey Patching

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): ...

Patching and Monkey Patching

Decorator Creation

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: ...

Decorator Creation

Utilities

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): ...

Utilities

Version Information

__version__: str = "1.17.3"
__version_info__: tuple = ('1', '17', '3')

Types

# 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'