| 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 builder |
| 15 |
|
| 16 |
|
| 17 |
@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.") |
| 18 |
class BrainNumpyCoreMultiarrayTest(unittest.TestCase): |
| 19 |
"""Test the numpy core multiarray brain module.""" |
| 20 |
|
| 21 |
numpy_functions_returning_array = ( |
| 22 |
("array", "[1, 2]"), |
| 23 |
("bincount", "[1, 2]"), |
| 24 |
("busday_count", "('2011-01', '2011-02')"), |
| 25 |
("busday_offset", "'2012-03', -1, roll='forward'"), |
| 26 |
("concatenate", "([1, 2], [1, 2])"), |
| 27 |
("datetime_as_string", "['2012-02', '2012-03']"), |
| 28 |
("dot", "[1, 2]", "[1, 2]"), |
| 29 |
("empty_like", "[1, 2]"), |
| 30 |
("fromfile", '"data.bin"'), |
| 31 |
("inner", "[1, 2]", "[1, 2]"), |
| 32 |
("is_busday", "['2011-07-01', '2011-07-02', '2011-07-18']"), |
| 33 |
("lexsort", "(('toto', 'tutu'), ('riri', 'fifi'))"), |
| 34 |
("packbits", "np.array([1, 2])"), |
| 35 |
("unpackbits", "np.array([[1], [2], [3]], dtype=np.uint8)"), |
| 36 |
("vdot", "[1, 2]", "[1, 2]"), |
| 37 |
("where", "[True, False]", "[1, 2]", "[2, 1]"), |
| 38 |
("empty", "[1, 2]"), |
| 39 |
("zeros", "[1, 2]"), |
| 40 |
) |
| 41 |
|
| 42 |
numpy_functions_returning_bool = ( |
| 43 |
("can_cast", "np.int32, np.int64"), |
| 44 |
("may_share_memory", "np.array([1, 2])", "np.array([3, 4])"), |
| 45 |
("shares_memory", "np.array([1, 2])", "np.array([3, 4])"), |
| 46 |
) |
| 47 |
|
| 48 |
numpy_functions_returning_dtype = ( |
| 49 |
# ("min_scalar_type", "10"), # Not yet tested as it returns np.dtype |
| 50 |
# ("result_type", "'i4'", "'c8'"), # Not yet tested as it returns np.dtype |
| 51 |
) |
| 52 |
|
| 53 |
numpy_functions_returning_none = (("copyto", "([1, 2], [1, 3])"),) |
| 54 |
|
| 55 |
numpy_functions_returning_tuple = ( |
| 56 |
( |
| 57 |
"unravel_index", |
| 58 |
"[22, 33, 44]", |
| 59 |
"(6, 7)", |
| 60 |
), # Not yet tested as is returns a tuple |
| 61 |
) |
| 62 |
|
| 63 |
def _inferred_numpy_func_call(self, func_name, *func_args): |
| 64 |
node = builder.extract_node(f""" |
| 65 |
import numpy as np |
| 66 |
func = np.{func_name:s} |
| 67 |
func({','.join(func_args):s}) |
| 68 |
""") |
| 69 |
return node.infer() |
| 70 |
|
| 71 |
def _inferred_numpy_no_alias_func_call(self, func_name, *func_args): |
| 72 |
node = builder.extract_node(f""" |
| 73 |
import numpy |
| 74 |
func = numpy.{func_name:s} |
| 75 |
func({','.join(func_args):s}) |
| 76 |
""") |
| 77 |
return node.infer() |
| 78 |
|
| 79 |
def test_numpy_function_calls_inferred_as_ndarray(self): |
| 80 |
"""Test that calls to numpy functions are inferred as numpy.ndarray.""" |
| 81 |
for infer_wrapper in ( |
| 82 |
self._inferred_numpy_func_call, |
| 83 |
self._inferred_numpy_no_alias_func_call, |
| 84 |
): |
| 85 |
for func_ in self.numpy_functions_returning_array: |
| 86 |
with self.subTest(typ=func_): |
| 87 |
inferred_values = list(infer_wrapper(*func_)) |
| 88 |
self.assertTrue( |
| 89 |
len(inferred_values) == 1, |
| 90 |
msg=f"Too much inferred values ({inferred_values}) for {func_[0]:s}", |
| 91 |
) |
| 92 |
self.assertTrue( |
| 93 |
inferred_values[-1].pytype() == ".ndarray", |
| 94 |
msg=f"Illicit type for {func_[0]:s} ({inferred_values[-1].pytype()})", |
| 95 |
) |
| 96 |
|
| 97 |
def test_numpy_function_calls_inferred_as_bool(self): |
| 98 |
"""Test that calls to numpy functions are inferred as bool.""" |
| 99 |
for infer_wrapper in ( |
| 100 |
self._inferred_numpy_func_call, |
| 101 |
self._inferred_numpy_no_alias_func_call, |
| 102 |
): |
| 103 |
for func_ in self.numpy_functions_returning_bool: |
| 104 |
with self.subTest(typ=func_): |
| 105 |
inferred_values = list(infer_wrapper(*func_)) |
| 106 |
self.assertTrue( |
| 107 |
len(inferred_values) == 1, |
| 108 |
msg=f"Too much inferred values ({inferred_values}) for {func_[0]:s}", |
| 109 |
) |
| 110 |
self.assertTrue( |
| 111 |
inferred_values[-1].pytype() == "builtins.bool", |
| 112 |
msg=f"Illicit type for {func_[0]:s} ({inferred_values[-1].pytype()})", |
| 113 |
) |
| 114 |
|
| 115 |
def test_numpy_function_calls_inferred_as_dtype(self): |
| 116 |
"""Test that calls to numpy functions are inferred as numpy.dtype.""" |
| 117 |
for infer_wrapper in ( |
| 118 |
self._inferred_numpy_func_call, |
| 119 |
self._inferred_numpy_no_alias_func_call, |
| 120 |
): |
| 121 |
for func_ in self.numpy_functions_returning_dtype: |
| 122 |
with self.subTest(typ=func_): |
| 123 |
inferred_values = list(infer_wrapper(*func_)) |
| 124 |
self.assertTrue( |
| 125 |
len(inferred_values) == 1, |
| 126 |
msg=f"Too much inferred values ({inferred_values}) for {func_[0]:s}", |
| 127 |
) |
| 128 |
self.assertTrue( |
| 129 |
inferred_values[-1].pytype() == "numpy.dtype", |
| 130 |
msg=f"Illicit type for {func_[0]:s} ({inferred_values[-1].pytype()})", |
| 131 |
) |
| 132 |
|
| 133 |
def test_numpy_function_calls_inferred_as_none(self): |
| 134 |
"""Test that calls to numpy functions are inferred as None.""" |
| 135 |
for infer_wrapper in ( |
| 136 |
self._inferred_numpy_func_call, |
| 137 |
self._inferred_numpy_no_alias_func_call, |
| 138 |
): |
| 139 |
for func_ in self.numpy_functions_returning_none: |
| 140 |
with self.subTest(typ=func_): |
| 141 |
inferred_values = list(infer_wrapper(*func_)) |
| 142 |
self.assertTrue( |
| 143 |
len(inferred_values) == 1, |
| 144 |
msg=f"Too much inferred values ({inferred_values}) for {func_[0]:s}", |
| 145 |
) |
| 146 |
self.assertTrue( |
| 147 |
inferred_values[-1].pytype() == "builtins.NoneType", |
| 148 |
msg=f"Illicit type for {func_[0]:s} ({inferred_values[-1].pytype()})", |
| 149 |
) |
| 150 |
|
| 151 |
def test_numpy_function_calls_inferred_as_tuple(self): |
| 152 |
"""Test that calls to numpy functions are inferred as tuple.""" |
| 153 |
for infer_wrapper in ( |
| 154 |
self._inferred_numpy_func_call, |
| 155 |
self._inferred_numpy_no_alias_func_call, |
| 156 |
): |
| 157 |
for func_ in self.numpy_functions_returning_tuple: |
| 158 |
with self.subTest(typ=func_): |
| 159 |
inferred_values = list(infer_wrapper(*func_)) |
| 160 |
self.assertTrue( |
| 161 |
len(inferred_values) == 1, |
| 162 |
msg=f"Too much inferred values ({inferred_values}) for {func_[0]:s}", |
| 163 |
) |
| 164 |
self.assertTrue( |
| 165 |
inferred_values[-1].pytype() == "builtins.tuple", |
| 166 |
msg=f"Illicit type for {func_[0]:s} ({inferred_values[-1].pytype()})", |
| 167 |
) |