overrides.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """Implementation of __array_function__ overrides from NEP-18."""
  2. import collections
  3. import functools
  4. import os
  5. from numpy.core._multiarray_umath import (
  6. add_docstring, implement_array_function, _get_implementing_args)
  7. from numpy.compat._inspect import getargspec
  8. ENABLE_ARRAY_FUNCTION = bool(
  9. int(os.environ.get('NUMPY_EXPERIMENTAL_ARRAY_FUNCTION', 0)))
  10. add_docstring(
  11. implement_array_function,
  12. """
  13. Implement a function with checks for __array_function__ overrides.
  14. All arguments are required, and can only be passed by position.
  15. Arguments
  16. ---------
  17. implementation : function
  18. Function that implements the operation on NumPy array without
  19. overrides when called like ``implementation(*args, **kwargs)``.
  20. public_api : function
  21. Function exposed by NumPy's public API originally called like
  22. ``public_api(*args, **kwargs)`` on which arguments are now being
  23. checked.
  24. relevant_args : iterable
  25. Iterable of arguments to check for __array_function__ methods.
  26. args : tuple
  27. Arbitrary positional arguments originally passed into ``public_api``.
  28. kwargs : dict
  29. Arbitrary keyword arguments originally passed into ``public_api``.
  30. Returns
  31. -------
  32. Result from calling ``implementation()`` or an ``__array_function__``
  33. method, as appropriate.
  34. Raises
  35. ------
  36. TypeError : if no implementation is found.
  37. """)
  38. # exposed for testing purposes; used internally by implement_array_function
  39. add_docstring(
  40. _get_implementing_args,
  41. """
  42. Collect arguments on which to call __array_function__.
  43. Parameters
  44. ----------
  45. relevant_args : iterable of array-like
  46. Iterable of possibly array-like arguments to check for
  47. __array_function__ methods.
  48. Returns
  49. -------
  50. Sequence of arguments with __array_function__ methods, in the order in
  51. which they should be called.
  52. """)
  53. ArgSpec = collections.namedtuple('ArgSpec', 'args varargs keywords defaults')
  54. def verify_matching_signatures(implementation, dispatcher):
  55. """Verify that a dispatcher function has the right signature."""
  56. implementation_spec = ArgSpec(*getargspec(implementation))
  57. dispatcher_spec = ArgSpec(*getargspec(dispatcher))
  58. if (implementation_spec.args != dispatcher_spec.args or
  59. implementation_spec.varargs != dispatcher_spec.varargs or
  60. implementation_spec.keywords != dispatcher_spec.keywords or
  61. (bool(implementation_spec.defaults) !=
  62. bool(dispatcher_spec.defaults)) or
  63. (implementation_spec.defaults is not None and
  64. len(implementation_spec.defaults) !=
  65. len(dispatcher_spec.defaults))):
  66. raise RuntimeError('implementation and dispatcher for %s have '
  67. 'different function signatures' % implementation)
  68. if implementation_spec.defaults is not None:
  69. if dispatcher_spec.defaults != (None,) * len(dispatcher_spec.defaults):
  70. raise RuntimeError('dispatcher functions can only use None for '
  71. 'default argument values')
  72. def set_module(module):
  73. """Decorator for overriding __module__ on a function or class.
  74. Example usage::
  75. @set_module('numpy')
  76. def example():
  77. pass
  78. assert example.__module__ == 'numpy'
  79. """
  80. def decorator(func):
  81. if module is not None:
  82. func.__module__ = module
  83. return func
  84. return decorator
  85. def array_function_dispatch(dispatcher, module=None, verify=True,
  86. docs_from_dispatcher=False):
  87. """Decorator for adding dispatch with the __array_function__ protocol.
  88. See NEP-18 for example usage.
  89. Parameters
  90. ----------
  91. dispatcher : callable
  92. Function that when called like ``dispatcher(*args, **kwargs)`` with
  93. arguments from the NumPy function call returns an iterable of
  94. array-like arguments to check for ``__array_function__``.
  95. module : str, optional
  96. __module__ attribute to set on new function, e.g., ``module='numpy'``.
  97. By default, module is copied from the decorated function.
  98. verify : bool, optional
  99. If True, verify the that the signature of the dispatcher and decorated
  100. function signatures match exactly: all required and optional arguments
  101. should appear in order with the same names, but the default values for
  102. all optional arguments should be ``None``. Only disable verification
  103. if the dispatcher's signature needs to deviate for some particular
  104. reason, e.g., because the function has a signature like
  105. ``func(*args, **kwargs)``.
  106. docs_from_dispatcher : bool, optional
  107. If True, copy docs from the dispatcher function onto the dispatched
  108. function, rather than from the implementation. This is useful for
  109. functions defined in C, which otherwise don't have docstrings.
  110. Returns
  111. -------
  112. Function suitable for decorating the implementation of a NumPy function.
  113. """
  114. if not ENABLE_ARRAY_FUNCTION:
  115. # __array_function__ requires an explicit opt-in for now
  116. def decorator(implementation):
  117. if module is not None:
  118. implementation.__module__ = module
  119. if docs_from_dispatcher:
  120. add_docstring(implementation, dispatcher.__doc__)
  121. return implementation
  122. return decorator
  123. def decorator(implementation):
  124. if verify:
  125. verify_matching_signatures(implementation, dispatcher)
  126. if docs_from_dispatcher:
  127. add_docstring(implementation, dispatcher.__doc__)
  128. @functools.wraps(implementation)
  129. def public_api(*args, **kwargs):
  130. relevant_args = dispatcher(*args, **kwargs)
  131. return implement_array_function(
  132. implementation, public_api, relevant_args, args, kwargs)
  133. if module is not None:
  134. public_api.__module__ = module
  135. # TODO: remove this when we drop Python 2 support (functools.wraps
  136. # adds __wrapped__ automatically in later versions)
  137. public_api.__wrapped__ = implementation
  138. return public_api
  139. return decorator
  140. def array_function_from_dispatcher(
  141. implementation, module=None, verify=True, docs_from_dispatcher=True):
  142. """Like array_function_dispatcher, but with function arguments flipped."""
  143. def decorator(dispatcher):
  144. return array_function_dispatch(
  145. dispatcher, module, verify=verify,
  146. docs_from_dispatcher=docs_from_dispatcher)(implementation)
  147. return decorator