Debian Codegraph @

tests/brain/numpy/test_core_fromnumeric.py

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