Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/numpy/testing/overrides.py: 54%
13 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-23 06:43 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-23 06:43 +0000
1"""Tools for testing implementations of __array_function__ and ufunc overrides
4"""
6from numpy.core.overrides import ARRAY_FUNCTIONS as _array_functions
7from numpy import ufunc as _ufunc
8import numpy.core.umath as _umath
10def get_overridable_numpy_ufuncs():
11 """List all numpy ufuncs overridable via `__array_ufunc__`
13 Parameters
14 ----------
15 None
17 Returns
18 -------
19 set
20 A set containing all overridable ufuncs in the public numpy API.
21 """
22 ufuncs = {obj for obj in _umath.__dict__.values()
23 if isinstance(obj, _ufunc)}
24 return ufuncs
27def allows_array_ufunc_override(func):
28 """Determine if a function can be overridden via `__array_ufunc__`
30 Parameters
31 ----------
32 func : callable
33 Function that may be overridable via `__array_ufunc__`
35 Returns
36 -------
37 bool
38 `True` if `func` is overridable via `__array_ufunc__` and
39 `False` otherwise.
41 Notes
42 -----
43 This function is equivalent to ``isinstance(func, np.ufunc)`` and
44 will work correctly for ufuncs defined outside of Numpy.
46 """
47 return isinstance(func, np.ufunc)
50def get_overridable_numpy_array_functions():
51 """List all numpy functions overridable via `__array_function__`
53 Parameters
54 ----------
55 None
57 Returns
58 -------
59 set
60 A set containing all functions in the public numpy API that are
61 overridable via `__array_function__`.
63 """
64 # 'import numpy' doesn't import recfunctions, so make sure it's imported
65 # so ufuncs defined there show up in the ufunc listing
66 from numpy.lib import recfunctions
67 return _array_functions.copy()
69def allows_array_function_override(func):
70 """Determine if a Numpy function can be overridden via `__array_function__`
72 Parameters
73 ----------
74 func : callable
75 Function that may be overridable via `__array_function__`
77 Returns
78 -------
79 bool
80 `True` if `func` is a function in the Numpy API that is
81 overridable via `__array_function__` and `False` otherwise.
82 """
83 return func in _array_functions