| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
|
| 5 |
import unittest |
| 6 |
|
| 7 |
try: |
| 8 |
import numpy # pylint: disable=unused-import |
| 9 |
|
| 10 |
HAS_NUMPY = True |
| 11 |
except ImportError: |
| 12 |
HAS_NUMPY = False |
| 13 |
|
| 14 |
from astroid import bases, builder, nodes |
| 15 |
|
| 16 |
|
| 17 |
@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.") |
| 18 |
class NumpyBrainCoreUmathTest(unittest.TestCase): |
| 19 |
"""Test of all members of numpy.core.umath module.""" |
| 20 |
|
| 21 |
one_arg_ufunc = ( |
| 22 |
"arccos", |
| 23 |
"arccosh", |
| 24 |
"arcsin", |
| 25 |
"arcsinh", |
| 26 |
"arctan", |
| 27 |
"arctanh", |
| 28 |
"cbrt", |
| 29 |
"conj", |
| 30 |
"conjugate", |
| 31 |
"cosh", |
| 32 |
"deg2rad", |
| 33 |
"degrees", |
| 34 |
"exp2", |
| 35 |
"expm1", |
| 36 |
"fabs", |
| 37 |
"frexp", |
| 38 |
"isfinite", |
| 39 |
"isinf", |
| 40 |
"log", |
| 41 |
"log1p", |
| 42 |
"log2", |
| 43 |
"logical_not", |
| 44 |
"modf", |
| 45 |
"negative", |
| 46 |
"positive", |
| 47 |
"rad2deg", |
| 48 |
"radians", |
| 49 |
"reciprocal", |
| 50 |
"rint", |
| 51 |
"sign", |
| 52 |
"signbit", |
| 53 |
"spacing", |
| 54 |
"square", |
| 55 |
"tan", |
| 56 |
"tanh", |
| 57 |
"trunc", |
| 58 |
) |
| 59 |
|
| 60 |
two_args_ufunc = ( |
| 61 |
"add", |
| 62 |
"bitwise_and", |
| 63 |
"bitwise_or", |
| 64 |
"bitwise_xor", |
| 65 |
"copysign", |
| 66 |
"divide", |
| 67 |
"divmod", |
| 68 |
"equal", |
| 69 |
"float_power", |
| 70 |
"floor_divide", |
| 71 |
"fmax", |
| 72 |
"fmin", |
| 73 |
"fmod", |
| 74 |
"gcd", |
| 75 |
"greater", |
| 76 |
"heaviside", |
| 77 |
"hypot", |
| 78 |
"lcm", |
| 79 |
"ldexp", |
| 80 |
"left_shift", |
| 81 |
"less", |
| 82 |
"logaddexp", |
| 83 |
"logaddexp2", |
| 84 |
"logical_and", |
| 85 |
"logical_or", |
| 86 |
"logical_xor", |
| 87 |
"maximum", |
| 88 |
"minimum", |
| 89 |
"multiply", |
| 90 |
"nextafter", |
| 91 |
"not_equal", |
| 92 |
"power", |
| 93 |
"remainder", |
| 94 |
"right_shift", |
| 95 |
"subtract", |
| 96 |
"true_divide", |
| 97 |
) |
| 98 |
|
| 99 |
all_ufunc = one_arg_ufunc + two_args_ufunc |
| 100 |
|
| 101 |
constants = ("e", "euler_gamma") |
| 102 |
|
| 103 |
def _inferred_numpy_attribute(self, func_name): |
| 104 |
node = builder.extract_node(f""" |
| 105 |
import numpy.core.umath as tested_module |
| 106 |
func = tested_module.{func_name:s} |
| 107 |
func""") |
| 108 |
return next(node.infer()) |
| 109 |
|
| 110 |
def test_numpy_core_umath_constants(self): |
| 111 |
"""Test that constants have Const type.""" |
| 112 |
for const in self.constants: |
| 113 |
with self.subTest(const=const): |
| 114 |
inferred = self._inferred_numpy_attribute(const) |
| 115 |
self.assertIsInstance(inferred, nodes.Const) |
| 116 |
|
| 117 |
def test_numpy_core_umath_constants_values(self): |
| 118 |
"""Test the values of the constants.""" |
| 119 |
exact_values = {"e": 2.718281828459045, "euler_gamma": 0.5772156649015329} |
| 120 |
for const in self.constants: |
| 121 |
with self.subTest(const=const): |
| 122 |
inferred = self._inferred_numpy_attribute(const) |
| 123 |
self.assertEqual(inferred.value, exact_values[const]) |
| 124 |
|
| 125 |
def test_numpy_core_umath_functions(self): |
| 126 |
"""Test that functions have FunctionDef type.""" |
| 127 |
for func in self.all_ufunc: |
| 128 |
with self.subTest(func=func): |
| 129 |
inferred = self._inferred_numpy_attribute(func) |
| 130 |
self.assertIsInstance(inferred, bases.Instance) |
| 131 |
|
| 132 |
def test_numpy_core_umath_functions_one_arg(self): |
| 133 |
"""Test the arguments names of functions.""" |
| 134 |
exact_arg_names = [ |
| 135 |
"self", |
| 136 |
"x", |
| 137 |
"out", |
| 138 |
"where", |
| 139 |
"casting", |
| 140 |
"order", |
| 141 |
"dtype", |
| 142 |
"subok", |
| 143 |
] |
| 144 |
for func in self.one_arg_ufunc: |
| 145 |
with self.subTest(func=func): |
| 146 |
inferred = self._inferred_numpy_attribute(func) |
| 147 |
self.assertEqual( |
| 148 |
inferred.getattr("__call__")[0].argnames(), exact_arg_names |
| 149 |
) |
| 150 |
|
| 151 |
def test_numpy_core_umath_functions_two_args(self): |
| 152 |
"""Test the arguments names of functions.""" |
| 153 |
exact_arg_names = [ |
| 154 |
"self", |
| 155 |
"x1", |
| 156 |
"x2", |
| 157 |
"out", |
| 158 |
"where", |
| 159 |
"casting", |
| 160 |
"order", |
| 161 |
"dtype", |
| 162 |
"subok", |
| 163 |
] |
| 164 |
for func in self.two_args_ufunc: |
| 165 |
with self.subTest(func=func): |
| 166 |
inferred = self._inferred_numpy_attribute(func) |
| 167 |
self.assertEqual( |
| 168 |
inferred.getattr("__call__")[0].argnames(), exact_arg_names |
| 169 |
) |
| 170 |
|
| 171 |
def test_numpy_core_umath_functions_kwargs_default_values(self): |
| 172 |
"""Test the default values for keyword arguments.""" |
| 173 |
exact_kwargs_default_values = [None, True, "same_kind", "K", None, True] |
| 174 |
for func in self.one_arg_ufunc + self.two_args_ufunc: |
| 175 |
with self.subTest(func=func): |
| 176 |
inferred = self._inferred_numpy_attribute(func) |
| 177 |
default_args_values = [ |
| 178 |
default.value |
| 179 |
for default in inferred.getattr("__call__")[0].args.defaults |
| 180 |
] |
| 181 |
self.assertEqual(default_args_values, exact_kwargs_default_values) |
| 182 |
|
| 183 |
def _inferred_numpy_func_call(self, func_name, *func_args): |
| 184 |
node = builder.extract_node(f""" |
| 185 |
import numpy as np |
| 186 |
func = np.{func_name:s} |
| 187 |
func() |
| 188 |
""") |
| 189 |
return node.infer() |
| 190 |
|
| 191 |
def test_numpy_core_umath_functions_return_type(self): |
| 192 |
"""Test that functions which should return a ndarray do return it.""" |
| 193 |
ndarray_returning_func = [ |
| 194 |
f for f in self.all_ufunc if f not in ("frexp", "modf") |
| 195 |
] |
| 196 |
for func_ in ndarray_returning_func: |
| 197 |
with self.subTest(typ=func_): |
| 198 |
inferred_values = list(self._inferred_numpy_func_call(func_)) |
| 199 |
self.assertTrue( |
| 200 |
len(inferred_values) == 1, |
| 201 |
msg=f"Too much inferred values ({inferred_values[-1].pytype()}) for {func_:s}", |
| 202 |
) |
| 203 |
self.assertTrue( |
| 204 |
inferred_values[0].pytype() == ".ndarray", |
| 205 |
msg=f"Illicit type for {func_:s} ({inferred_values[-1].pytype()})", |
| 206 |
) |
| 207 |
|
| 208 |
def test_numpy_core_umath_functions_return_type_tuple(self): |
| 209 |
"""Test that functions which should return a pair of ndarray do return it.""" |
| 210 |
ndarray_returning_func = ("frexp", "modf") |
| 211 |
|
| 212 |
for func_ in ndarray_returning_func: |
| 213 |
with self.subTest(typ=func_): |
| 214 |
inferred_values = list(self._inferred_numpy_func_call(func_)) |
| 215 |
self.assertTrue( |
| 216 |
len(inferred_values) == 1, |
| 217 |
msg=f"Too much inferred values ({inferred_values}) for {func_:s}", |
| 218 |
) |
| 219 |
self.assertTrue( |
| 220 |
inferred_values[-1].pytype() == "builtins.tuple", |
| 221 |
msg=f"Illicit type for {func_:s} ({inferred_values[-1].pytype()})", |
| 222 |
) |
| 223 |
self.assertTrue( |
| 224 |
len(inferred_values[0].elts) == 2, |
| 225 |
msg=f"{func_} should return a pair of values. That's not the case.", |
| 226 |
) |
| 227 |
for array in inferred_values[-1].elts: |
| 228 |
effective_infer = [m.pytype() for m in array.inferred()] |
| 229 |
self.assertTrue( |
| 230 |
".ndarray" in effective_infer, |
| 231 |
msg=( |
| 232 |
f"Each item in the return of {func_} should be inferred" |
| 233 |
f" as a ndarray and not as {effective_infer}" |
| 234 |
), |
| 235 |
) |