| 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 BrainNumpyCoreFunctionBaseTest(unittest.TestCase): |
| 19 |
"""Test the numpy core numeric brain module.""" |
| 20 |
|
| 21 |
numpy_functions = ( |
| 22 |
("linspace", "1, 100"), |
| 23 |
("logspace", "1, 100"), |
| 24 |
("geomspace", "1, 100"), |
| 25 |
) |
| 26 |
|
| 27 |
def _inferred_numpy_func_call(self, func_name, *func_args): |
| 28 |
node = builder.extract_node(f""" |
| 29 |
import numpy as np |
| 30 |
func = np.{func_name:s} |
| 31 |
func({','.join(func_args):s}) |
| 32 |
""") |
| 33 |
return node.infer() |
| 34 |
|
| 35 |
def test_numpy_function_calls_inferred_as_ndarray(self): |
| 36 |
"""Test that calls to numpy functions are inferred as numpy.ndarray.""" |
| 37 |
licit_array_types = (".ndarray",) |
| 38 |
for func_ in self.numpy_functions: |
| 39 |
with self.subTest(typ=func_): |
| 40 |
inferred_values = list(self._inferred_numpy_func_call(*func_)) |
| 41 |
self.assertTrue( |
| 42 |
len(inferred_values) == 1, |
| 43 |
msg=f"Too much inferred value for {func_[0]:s}", |
| 44 |
) |
| 45 |
self.assertTrue( |
| 46 |
inferred_values[-1].pytype() in licit_array_types, |
| 47 |
msg=f"Illicit type for {func_[0]:s} ({inferred_values[-1].pytype()})", |
| 48 |
) |