METADATA 6.6 KB

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