METADATA 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. Metadata-Version: 2.1
  2. Name: wrapt
  3. Version: 1.13.3
  4. Summary: Module for decorators, wrappers and monkey patching.
  5. Home-page: https://github.com/GrahamDumpleton/wrapt
  6. Author: Graham Dumpleton
  7. Author-email: Graham.Dumpleton@gmail.com
  8. License: BSD
  9. Project-URL: Bug Tracker, https://github.com/GrahamDumpleton/wrapt/issues/
  10. Project-URL: Changelog, https://wrapt.readthedocs.io/en/latest/changes.html
  11. Project-URL: Documentation, https://wrapt.readthedocs.io/
  12. Keywords: wrapper,proxy,decorator
  13. Platform: any
  14. Classifier: Development Status :: 5 - Production/Stable
  15. Classifier: License :: OSI Approved :: BSD License
  16. Classifier: Programming Language :: Python :: 2
  17. Classifier: Programming Language :: Python :: 2.7
  18. Classifier: Programming Language :: Python :: 3
  19. Classifier: Programming Language :: Python :: 3.5
  20. Classifier: Programming Language :: Python :: 3.6
  21. Classifier: Programming Language :: Python :: 3.7
  22. Classifier: Programming Language :: Python :: 3.8
  23. Classifier: Programming Language :: Python :: 3.9
  24. Classifier: Programming Language :: Python :: 3.10
  25. Classifier: Programming Language :: Python :: Implementation :: CPython
  26. Classifier: Programming Language :: Python :: Implementation :: PyPy
  27. Requires-Python: !=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7
  28. Description-Content-Type: text/x-rst
  29. License-File: LICENSE
  30. wrapt
  31. =====
  32. |Actions| |PyPI|
  33. The aim of the **wrapt** module is to provide a transparent object proxy
  34. for Python, which can be used as the basis for the construction of function
  35. wrappers and decorator functions.
  36. The **wrapt** module focuses very much on correctness. It therefore goes
  37. way beyond existing mechanisms such as ``functools.wraps()`` to ensure that
  38. decorators preserve introspectability, signatures, type checking abilities
  39. etc. The decorators that can be constructed using this module will work in
  40. far more scenarios than typical decorators and provide more predictable and
  41. consistent behaviour.
  42. To ensure that the overhead is as minimal as possible, a C extension module
  43. is used for performance critical components. An automatic fallback to a
  44. pure Python implementation is also provided where a target system does not
  45. have a compiler to allow the C extension to be compiled.
  46. Documentation
  47. -------------
  48. For further information on the **wrapt** module see:
  49. * http://wrapt.readthedocs.org/
  50. Quick Start
  51. -----------
  52. To implement your decorator you need to first define a wrapper function.
  53. This will be called each time a decorated function is called. The wrapper
  54. function needs to take four positional arguments:
  55. * ``wrapped`` - The wrapped function which in turns needs to be called by your wrapper function.
  56. * ``instance`` - The object to which the wrapped function was bound when it was called.
  57. * ``args`` - The list of positional arguments supplied when the decorated function was called.
  58. * ``kwargs`` - The dictionary of keyword arguments supplied when the decorated function was called.
  59. The wrapper function would do whatever it needs to, but would usually in
  60. turn call the wrapped function that is passed in via the ``wrapped``
  61. argument.
  62. The decorator ``@wrapt.decorator`` then needs to be applied to the wrapper
  63. function to convert it into a decorator which can in turn be applied to
  64. other functions.
  65. .. code-block:: python
  66. import wrapt
  67. @wrapt.decorator
  68. def pass_through(wrapped, instance, args, kwargs):
  69. return wrapped(*args, **kwargs)
  70. @pass_through
  71. def function():
  72. pass
  73. If you wish to implement a decorator which accepts arguments, then wrap the
  74. definition of the decorator in a function closure. Any arguments supplied
  75. to the outer function when the decorator is applied, will be available to
  76. the inner wrapper when the wrapped function is called.
  77. .. code-block:: python
  78. import wrapt
  79. def with_arguments(myarg1, myarg2):
  80. @wrapt.decorator
  81. def wrapper(wrapped, instance, args, kwargs):
  82. return wrapped(*args, **kwargs)
  83. return wrapper
  84. @with_arguments(1, 2)
  85. def function():
  86. pass
  87. When applied to a normal function or static method, the wrapper function
  88. when called will be passed ``None`` as the ``instance`` argument.
  89. When applied to an instance method, the wrapper function when called will
  90. be passed the instance of the class the method is being called on as the
  91. ``instance`` argument. This will be the case even when the instance method
  92. was called explicitly via the class and the instance passed as the first
  93. argument. That is, the instance will never be passed as part of ``args``.
  94. When applied to a class method, the wrapper function when called will be
  95. passed the class type as the ``instance`` argument.
  96. When applied to a class, the wrapper function when called will be passed
  97. ``None`` as the ``instance`` argument. The ``wrapped`` argument in this
  98. case will be the class.
  99. The above rules can be summarised with the following example.
  100. .. code-block:: python
  101. import inspect
  102. @wrapt.decorator
  103. def universal(wrapped, instance, args, kwargs):
  104. if instance is None:
  105. if inspect.isclass(wrapped):
  106. # Decorator was applied to a class.
  107. return wrapped(*args, **kwargs)
  108. else:
  109. # Decorator was applied to a function or staticmethod.
  110. return wrapped(*args, **kwargs)
  111. else:
  112. if inspect.isclass(instance):
  113. # Decorator was applied to a classmethod.
  114. return wrapped(*args, **kwargs)
  115. else:
  116. # Decorator was applied to an instancemethod.
  117. return wrapped(*args, **kwargs)
  118. Using these checks it is therefore possible to create a universal decorator
  119. that can be applied in all situations. It is no longer necessary to create
  120. different variants of decorators for normal functions and instance methods,
  121. or use additional wrappers to convert a function decorator into one that
  122. will work for instance methods.
  123. In all cases, the wrapped function passed to the wrapper function is called
  124. in the same way, with ``args`` and ``kwargs`` being passed. The
  125. ``instance`` argument doesn't need to be used in calling the wrapped
  126. function.
  127. Repository
  128. ----------
  129. Full source code for the **wrapt** module, including documentation files
  130. and unit tests, can be obtained from github.
  131. * https://github.com/GrahamDumpleton/wrapt
  132. .. |Actions| image:: https://img.shields.io/github/workflow/status/GrahamDumpleton/wrapt/Test/develop?logo=github&cacheSeconds=600
  133. :target: https://github.com/GrahamDumpleton/wrapt/actions
  134. .. |PyPI| image:: https://img.shields.io/pypi/v/wrapt.svg?logo=python&cacheSeconds=3600
  135. :target: https://pypi.python.org/pypi/wrapt