wrapt 1.17.2


pip install wrapt

  Latest version

Released: Jan 14, 2025


Meta
Author: Graham Dumpleton
Requires Python: >=3.8

Classifiers

Development Status
  • 5 - Production/Stable

License
  • OSI Approved :: BSD License

Programming Language
  • Python :: 3
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: Implementation :: CPython
  • Python :: Implementation :: PyPy

PyPI

The aim of the wrapt module is to provide 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 therefore 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.

Documentation

For further information on the wrapt module see:

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.

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.

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.

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.

Repository

Full source code for the wrapt module, including documentation files and unit tests, can be obtained from github.

Wheel compatibility matrix

Platform CPython 3.8 CPython 3.9 CPython 3.10 CPython 3.11 CPython 3.12 CPython 3.13 CPython (additional flags: t) 3.13 Python 3
any
macosx_10_13_universal2
macosx_10_13_x86_64
macosx_10_9_universal2
macosx_10_9_x86_64
macosx_11_0_arm64
manylinux1_i686
manylinux1_x86_64
manylinux2014_aarch64
manylinux2014_i686
manylinux2014_x86_64
manylinux_2_17_aarch64
manylinux_2_17_i686
manylinux_2_17_x86_64
manylinux_2_5_i686
manylinux_2_5_x86_64
musllinux_1_2_aarch64
musllinux_1_2_i686
musllinux_1_2_x86_64
win32
win_amd64

Files in release

wrapt-1.17.2-cp310-cp310-macosx_10_9_universal2.whl (52.1KiB)
wrapt-1.17.2-cp310-cp310-macosx_10_9_x86_64.whl (37.6KiB)
wrapt-1.17.2-cp310-cp310-macosx_11_0_arm64.whl (37.9KiB)
wrapt-1.17.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (81.4KiB)
wrapt-1.17.2-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (73.2KiB)
wrapt-1.17.2-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (80.8KiB)
wrapt-1.17.2-cp310-cp310-musllinux_1_2_aarch64.whl (79.8KiB)
wrapt-1.17.2-cp310-cp310-musllinux_1_2_i686.whl (72.8KiB)
wrapt-1.17.2-cp310-cp310-musllinux_1_2_x86_64.whl (79.4KiB)
wrapt-1.17.2-cp310-cp310-win32.whl (35.6KiB)
wrapt-1.17.2-cp310-cp310-win_amd64.whl (37.9KiB)
wrapt-1.17.2-cp311-cp311-macosx_10_9_universal2.whl (52.1KiB)
wrapt-1.17.2-cp311-cp311-macosx_10_9_x86_64.whl (37.6KiB)
wrapt-1.17.2-cp311-cp311-macosx_11_0_arm64.whl (37.9KiB)
wrapt-1.17.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (81.8KiB)
wrapt-1.17.2-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (73.7KiB)
wrapt-1.17.2-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (81.2KiB)
wrapt-1.17.2-cp311-cp311-musllinux_1_2_aarch64.whl (80.4KiB)
wrapt-1.17.2-cp311-cp311-musllinux_1_2_i686.whl (73.3KiB)
wrapt-1.17.2-cp311-cp311-musllinux_1_2_x86_64.whl (80.0KiB)
wrapt-1.17.2-cp311-cp311-win32.whl (35.6KiB)
wrapt-1.17.2-cp311-cp311-win_amd64.whl (37.9KiB)
wrapt-1.17.2-cp312-cp312-macosx_10_13_universal2.whl (52.5KiB)
wrapt-1.17.2-cp312-cp312-macosx_10_13_x86_64.whl (37.9KiB)
wrapt-1.17.2-cp312-cp312-macosx_11_0_arm64.whl (38.0KiB)
wrapt-1.17.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (86.6KiB)
wrapt-1.17.2-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (79.0KiB)
wrapt-1.17.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (87.1KiB)
wrapt-1.17.2-cp312-cp312-musllinux_1_2_aarch64.whl (84.7KiB)
wrapt-1.17.2-cp312-cp312-musllinux_1_2_i686.whl (77.8KiB)
wrapt-1.17.2-cp312-cp312-musllinux_1_2_x86_64.whl (85.5KiB)
wrapt-1.17.2-cp312-cp312-win32.whl (35.8KiB)
wrapt-1.17.2-cp312-cp312-win_amd64.whl (38.0KiB)
wrapt-1.17.2-cp313-cp313-macosx_10_13_universal2.whl (52.5KiB)
wrapt-1.17.2-cp313-cp313-macosx_10_13_x86_64.whl (37.9KiB)
wrapt-1.17.2-cp313-cp313-macosx_11_0_arm64.whl (38.0KiB)
wrapt-1.17.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (86.6KiB)
wrapt-1.17.2-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (79.0KiB)
wrapt-1.17.2-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (87.1KiB)
wrapt-1.17.2-cp313-cp313-musllinux_1_2_aarch64.whl (84.7KiB)
wrapt-1.17.2-cp313-cp313-musllinux_1_2_i686.whl (77.9KiB)
wrapt-1.17.2-cp313-cp313-musllinux_1_2_x86_64.whl (85.5KiB)
wrapt-1.17.2-cp313-cp313-win32.whl (35.8KiB)
wrapt-1.17.2-cp313-cp313-win_amd64.whl (38.0KiB)
wrapt-1.17.2-cp313-cp313t-macosx_10_13_universal2.whl (55.0KiB)
wrapt-1.17.2-cp313-cp313t-macosx_10_13_x86_64.whl (39.1KiB)
wrapt-1.17.2-cp313-cp313t-macosx_11_0_arm64.whl (39.2KiB)
wrapt-1.17.2-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (110.8KiB)
wrapt-1.17.2-cp313-cp313t-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (98.8KiB)
wrapt-1.17.2-cp313-cp313t-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (106.8KiB)
wrapt-1.17.2-cp313-cp313t-musllinux_1_2_aarch64.whl (107.6KiB)
wrapt-1.17.2-cp313-cp313t-musllinux_1_2_i686.whl (98.1KiB)
wrapt-1.17.2-cp313-cp313t-musllinux_1_2_x86_64.whl (103.9KiB)
wrapt-1.17.2-cp313-cp313t-win32.whl (37.1KiB)
wrapt-1.17.2-cp313-cp313t-win_amd64.whl (39.8KiB)
wrapt-1.17.2-cp38-cp38-macosx_10_9_universal2.whl (52.0KiB)
wrapt-1.17.2-cp38-cp38-macosx_10_9_x86_64.whl (37.6KiB)
wrapt-1.17.2-cp38-cp38-macosx_11_0_arm64.whl (37.8KiB)
wrapt-1.17.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (84.2KiB)
wrapt-1.17.2-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (76.2KiB)
wrapt-1.17.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (83.6KiB)
wrapt-1.17.2-cp38-cp38-musllinux_1_2_aarch64.whl (81.9KiB)
wrapt-1.17.2-cp38-cp38-musllinux_1_2_i686.whl (75.0KiB)
wrapt-1.17.2-cp38-cp38-musllinux_1_2_x86_64.whl (81.6KiB)
wrapt-1.17.2-cp38-cp38-win32.whl (35.6KiB)
wrapt-1.17.2-cp38-cp38-win_amd64.whl (37.8KiB)
wrapt-1.17.2-cp39-cp39-macosx_10_9_universal2.whl (52.1KiB)
wrapt-1.17.2-cp39-cp39-macosx_10_9_x86_64.whl (37.6KiB)
wrapt-1.17.2-cp39-cp39-macosx_11_0_arm64.whl (37.9KiB)
wrapt-1.17.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (81.1KiB)
wrapt-1.17.2-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (73.0KiB)
wrapt-1.17.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (80.7KiB)
wrapt-1.17.2-cp39-cp39-musllinux_1_2_aarch64.whl (79.6KiB)
wrapt-1.17.2-cp39-cp39-musllinux_1_2_i686.whl (72.6KiB)
wrapt-1.17.2-cp39-cp39-musllinux_1_2_x86_64.whl (79.2KiB)
wrapt-1.17.2-cp39-cp39-win32.whl (35.6KiB)
wrapt-1.17.2-cp39-cp39-win_amd64.whl (37.9KiB)
wrapt-1.17.2-py3-none-any.whl (23.0KiB)
wrapt-1.17.2.tar.gz (54.2KiB)
No dependencies