METADATA 6.2 KB

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