wrapt

Module for decorators, wrappers and monkey patching.

99 个版本 Python >=3.9
安装
pip install wrapt
poetry add wrapt
pipenv install wrapt
conda install wrapt
描述

wrapt

|PyPI| |Documentation|

A Python module for decorators, wrappers and monkey patching.

Overview

The wrapt module provides a transparent object proxy for Python, which can be used as the basis for the construction of function wrappers and decorator functions.

The wrapt module focuses very much on correctness. It goes way beyond existing mechanisms such as functools.wraps() to ensure that decorators preserve introspectability, signatures, type checking abilities etc. The decorators that can be constructed using this module will work in far more scenarios than typical decorators and provide more predictable and consistent behaviour.

To ensure that the overhead is as minimal as possible, a C extension module is used for performance critical components. An automatic fallback to a pure Python implementation is also provided where a target system does not have a compiler to allow the C extension to be compiled.

Key Features

  • Universal decorators that work with functions, methods, classmethods, staticmethods, and classes
  • Transparent object proxies for advanced wrapping scenarios
  • Monkey patching utilities for safe runtime modifications
  • C extension for optimal performance with Python fallback
  • Comprehensive introspection preservation (signatures, annotations, etc.)
  • Thread-safe decorator implementations

Installation

Install from PyPI using pip::

pip install wrapt

Supported Python Versions

  • Python 3.9+
  • CPython and PyPy implementations

Documentation

For comprehensive documentation, examples, and advanced usage patterns, visit:

Quick Start

To implement your decorator you need to first define a wrapper function. This will be called each time a decorated function is called. The wrapper function needs to take four positional arguments:

  • wrapped - The wrapped function which in turns needs to be called by your wrapper function.
  • instance - The object to which the wrapped function was bound when it was called.
  • args - The list of positional arguments supplied when the decorated function was called.
  • kwargs - The dictionary of keyword arguments supplied when the decorated function was called.

The wrapper function would do whatever it needs to, but would usually in turn call the wrapped function that is passed in via the wrapped argument.

The decorator @wrapt.decorator then needs to be applied to the wrapper function to convert it into a decorator which can in turn be applied to other functions.

.. code-block:: python

import wrapt

@wrapt.decorator
def pass_through(wrapped, instance, args, kwargs):
    return wrapped(*args, **kwargs)

@pass_through
def function():
    pass

If you wish to implement a decorator which accepts arguments, then wrap the definition of the decorator in a function closure. Any arguments supplied to the outer function when the decorator is applied, will be available to the inner wrapper when the wrapped function is called.

.. code-block:: python

import wrapt

def with_arguments(myarg1, myarg2):
    @wrapt.decorator
    def wrapper(wrapped, instance, args, kwargs):
        return wrapped(*args, **kwargs)
    return wrapper

@with_arguments(1, 2)
def function():
    pass

When applied to a normal function or static method, the wrapper function when called will be passed None as the instance argument.

When applied to an instance method, the wrapper function when called will be passed the instance of the class the method is being called on as the instance argument. This will be the case even when the instance method was called explicitly via the class and the instance passed as the first argument. That is, the instance will never be passed as part of args.

When applied to a class method, the wrapper function when called will be passed the class type as the instance argument.

When applied to a class, the wrapper function when called will be passed None as the instance argument. The wrapped argument in this case will be the class.

The above rules can be summarised with the following example.

.. code-block:: python

import inspect

@wrapt.decorator
def universal(wrapped, instance, args, kwargs):
    if instance is None:
        if inspect.isclass(wrapped):
            # Decorator was applied to a class.
            return wrapped(*args, **kwargs)
        else:
            # Decorator was applied to a function or staticmethod.
            return wrapped(*args, **kwargs)
    else:
        if inspect.isclass(instance):
            # Decorator was applied to a classmethod.
            return wrapped(*args, **kwargs)
        else:
            # Decorator was applied to an instancemethod.
            return wrapped(*args, **kwargs)

Using these checks it is therefore possible to create a universal decorator that can be applied in all situations. It is no longer necessary to create different variants of decorators for normal functions and instance methods, or use additional wrappers to convert a function decorator into one that will work for instance methods.

In all cases, the wrapped function passed to the wrapper function is called in the same way, with args and kwargs being passed. The instance argument doesn't need to be used in calling the wrapped function.

.. |PyPI| image:: https://img.shields.io/pypi/v/wrapt.svg?logo=python&cacheSeconds=3600 :target: https://pypi.python.org/pypi/wrapt .. |Documentation| image:: https://img.shields.io/badge/docs-wrapt.readthedocs.io-blue.svg :target: https://wrapt.readthedocs.io/

版本列表
2.2.1 2026-05-22
2.2.0 2026-05-21
2.2.0.dev1 2026-05-21
2.2.2rc3 2026-06-16
2.2.2rc2 2026-06-16
2.2.2rc1 2026-06-15
2.2.1rc1 2026-05-22
2.2.0rc9 2026-04-16
2.2.0rc8 2026-04-16
2.2.0rc7 2026-04-15
2.2.0rc6 2026-04-14
2.2.0rc5 2026-04-12
2.2.0rc4 2026-04-11
2.2.0rc3 2026-04-10
2.2.0rc2 2026-04-09
2.2.0rc12 2026-05-21
2.2.0rc11 2026-04-24
2.2.0rc10 2026-04-17
2.2.0rc1 2026-04-06
2.1.2 2026-03-06
2.1.1 2026-02-03
2.1.0 2026-01-31
2.1.0.dev2 2026-01-18
2.1.0.dev1 2026-01-15
2.1.2rc1 2026-03-04
2.1.0rc1 2026-01-24
2.0.1 2025-11-07
2.0.0 2025-10-19
2.0.1rc1 2025-11-06
2.0.0rc6 2025-10-17
2.0.0rc5 2025-10-16
2.0.0rc4 2025-10-05
2.0.0rc3 2025-10-03
2.0.0rc2 2025-08-19
2.0.0rc1 2025-08-11
1.17.3 2025-08-12
1.17.2 2025-01-14
1.17.1 2025-01-11
1.17.0 2024-11-22
1.17.0.dev4 2024-10-07
1.17.0.dev3 2024-10-06
1.17.4rc1 2026-03-06
1.17.0rc1 2024-10-17
1.16.0 2023-11-09
1.16.0rc2 2023-11-05
1.16.0rc1 2023-10-07
1.15.0 2023-02-27
1.15.0rc1 2023-01-12
1.14.2 2025-08-12
1.14.1 2022-05-02
1.14.0 2022-03-10
1.14.0rc1 2022-03-05
1.13.3 2021-10-29
1.13.2 2021-10-13
1.13.1 2021-10-05
1.13.0 2021-10-04
1.13.3rc2 2021-10-23
1.13.3rc1 2021-10-17
1.13.2rc1 2021-10-11
1.13.0rc3 2021-08-17
1.13.0rc2 2021-08-06
1.13.0rc1 2021-08-06
1.12.1 2020-03-09
1.12.0 2020-02-16
1.11.2 2019-06-18
1.11.1 2019-01-19
1.11.0 2019-01-10
1.10.11 2017-08-11
1.10.10 2017-03-15
1.10.9 2017-03-15
1.10.8 2016-04-10
1.10.7 2016-03-31
1.10.6 2015-12-08
1.10.5 2015-07-03
1.10.4 2015-03-18
1.10.2 2014-12-11
1.10.1 2014-11-26
1.10.0 2014-11-20
1.9.0 2014-08-22
1.8.0 2014-05-03
1.7.0 2014-04-26
1.6.0 2014-01-26
1.5.1 2014-01-14
1.5.0 2014-01-02
1.4.2 2013-12-28
1.4.1 2013-12-23
1.4.0 2013-12-20
1.3.1 2013-12-19
1.3.0 2013-12-19
1.2.1 2013-10-16
1.2.0 2013-10-11
1.1.4 2013-09-27
1.1.3 2013-09-24
1.1.2 2013-09-20
1.1.1 2013-09-19
1.1.0 2013-09-18
1.0.0 2013-09-06