summaryrefslogtreecommitdiff
path: root/doc/source/user
diff options
context:
space:
mode:
authorNathan Goldbaum <nathan.goldbaum@gmail.com>2022-11-04 12:13:56 -0600
committerNathan Goldbaum <nathan.goldbaum@gmail.com>2022-11-16 09:14:25 -0700
commit1a8d3ca45f0a7294784bc200ec436dc8563f654a (patch)
treeae1e1a7d00205ca9ce3cbf4a9345904f4a09e2e8 /doc/source/user
parent6aacc5167983d7c6f8689d7039294f2fc0d5f5fb (diff)
downloadnumpy-1a8d3ca45f0a7294784bc200ec436dc8563f654a.tar.gz
API: Add numpy.testing.overrides to aid testing of custom array containers
Closes #15544
Diffstat (limited to 'doc/source/user')
-rw-r--r--doc/source/user/basics.dispatch.rst30
1 files changed, 30 insertions, 0 deletions
diff --git a/doc/source/user/basics.dispatch.rst b/doc/source/user/basics.dispatch.rst
index 0d0ddfcdb..7c30272ad 100644
--- a/doc/source/user/basics.dispatch.rst
+++ b/doc/source/user/basics.dispatch.rst
@@ -271,6 +271,36 @@ array([[1., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
+
+The implementation of ``DiagonalArray`` in this example only handles the
+``np.sum`` and ``np.mean`` functions for brevity. Many other functions in the
+Numpy API are also available to wrap and a full-fledged custom array container
+can explicitly support all functions that Numpy makes available to wrap.
+
+Numpy provides some utilities to aid testing of custom array containers that
+implement the ``__array_ufunc__`` and ``__array_function__`` protocols in the
+``numpy.testing.overrides`` namespace.
+
+To check if a Numpy function can be overriden via ``__array_ufunc__``, you can
+use :func:`~numpy.testing.overrides.allows_array_ufunc_override`:
+
+>>> from np.testing.overrides import allows_array_ufunc_override
+>>> allows_array_ufunc_override(np.add)
+True
+
+Similarly, you can check if a function can be overriden via
+``__array_function__`` using
+:func:`~numpy.testing.overrides.allows_array_function_override`.
+
+Lists of every overridable function in the Numpy API are also available via
+:func:`~numpy.testing.overrides.get_overridable_numpy_array_functions` for
+functions that support the ``__array_function__`` protocol and
+:func:`~numpy.testing.overrides.get_overridable_numpy_ufuncs` for functions that
+support the ``__array_ufunc__`` protocol. Both functions return sets of
+functions that are present in the Numpy public API. User-defined ufuncs or
+ufuncs defined in other libraries that depend on Numpy are not present in
+these sets.
+
Refer to the `dask source code <https://github.com/dask/dask>`_ and
`cupy source code <https://github.com/cupy/cupy>`_ for more fully-worked
examples of custom array containers.