summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorhippo91 <guillaume.peillex@gmail.com>2019-12-24 10:08:04 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2019-12-24 10:08:04 +0100
commit3d74305efdbc6aa58784a0222e4a1eef6a33e2fa (patch)
treea5eb88d2abe51f3281a3f3a782af678840575293 /tests
parentc54af820fcbaa1dd7a4644670b58a40b4f587ff9 (diff)
downloadastroid-git-3d74305efdbc6aa58784a0222e4a1eef6a33e2fa.tar.gz
Add missing methods of the numpy.core.multiarray module.
Closes PyCQA/pylint#3208
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_brain_numpy_core_multiarray.py125
1 files changed, 118 insertions, 7 deletions
diff --git a/tests/unittest_brain_numpy_core_multiarray.py b/tests/unittest_brain_numpy_core_multiarray.py
index d7009815..e02db6cc 100644
--- a/tests/unittest_brain_numpy_core_multiarray.py
+++ b/tests/unittest_brain_numpy_core_multiarray.py
@@ -21,18 +21,48 @@ class BrainNumpyCoreMultiarrayTest(unittest.TestCase):
Test the numpy core multiarray brain module
"""
- numpy_functions = (
+ numpy_functions_returning_array = (
("array", "[1, 2]"),
- ("inner", "[1, 2]", "[1, 2]"),
- ("vdot", "[1, 2]", "[1, 2]"),
+ ("bincount", "[1, 2]"),
+ ("busday_count", "('2011-01', '2011-02')"),
+ ("busday_offset", "'2012-03', -1, roll='forward'"),
("concatenate", "([1, 2], [1, 2])"),
+ ("datetime_as_string", "['2012-02', '2012-03']"),
("dot", "[1, 2]", "[1, 2]"),
("empty_like", "[1, 2]"),
+ ("inner", "[1, 2]", "[1, 2]"),
+ ("is_busday", "['2011-07-01', '2011-07-02', '2011-07-18']"),
+ ("lexsort", "(('toto', 'tutu'), ('riri', 'fifi'))"),
+ ("packbits", "np.array([1, 2])"),
+ ("ravel_multi_index", "np.array([[1, 2], [2, 1]])", "(3, 4)"),
+ ("unpackbits", "np.array([[1], [2], [3]], dtype=np.uint8)"),
+ ("vdot", "[1, 2]", "[1, 2]"),
("where", "[True, False]", "[1, 2]", "[2, 1]"),
("empty", "[1, 2]"),
("zeros", "[1, 2]"),
)
+ numpy_functions_returning_bool = (
+ ("can_cast", "np.int32, np.int64"),
+ ("may_share_memory", "np.array([1, 2])", "np.array([3, 4])"),
+ ("shares_memory", "np.array([1, 2])", "np.array([3, 4])"),
+ )
+
+ numpy_functions_returning_dtype = (
+ # ("min_scalar_type", "10"), # Not yet tested as it returns np.dtype
+ # ("result_type", "'i4'", "'c8'"), # Not yet tested as it returns np.dtype
+ )
+
+ numpy_functions_returning_none = (("copyto", "([1, 2], [1, 3])"),)
+
+ numpy_functions_returning_tuple = (
+ (
+ "unravel_index",
+ "[22, 33, 44]",
+ "(6, 7)",
+ ), # Not yet tested as is returns a tuple
+ )
+
def _inferred_numpy_func_call(self, func_name, *func_args):
node = builder.extract_node(
"""
@@ -49,16 +79,97 @@ class BrainNumpyCoreMultiarrayTest(unittest.TestCase):
"""
Test that calls to numpy functions are inferred as numpy.ndarray
"""
- licit_array_types = (".ndarray",)
- for func_ in self.numpy_functions:
+ for func_ in self.numpy_functions_returning_array:
+ with self.subTest(typ=func_):
+ inferred_values = list(self._inferred_numpy_func_call(*func_))
+ self.assertTrue(
+ len(inferred_values) == 1,
+ msg="Too much inferred values ({}) for {:s}".format(
+ inferred_values, func_[0]
+ ),
+ )
+ self.assertTrue(
+ inferred_values[-1].pytype() == ".ndarray",
+ msg="Illicit type for {:s} ({})".format(
+ func_[0], inferred_values[-1].pytype()
+ ),
+ )
+
+ def test_numpy_function_calls_inferred_as_bool(self):
+ """
+ Test that calls to numpy functions are inferred as bool
+ """
+ for func_ in self.numpy_functions_returning_bool:
+ with self.subTest(typ=func_):
+ inferred_values = list(self._inferred_numpy_func_call(*func_))
+ self.assertTrue(
+ len(inferred_values) == 1,
+ msg="Too much inferred values ({}) for {:s}".format(
+ inferred_values, func_[0]
+ ),
+ )
+ self.assertTrue(
+ inferred_values[-1].pytype() == "builtins.bool",
+ msg="Illicit type for {:s} ({})".format(
+ func_[0], inferred_values[-1].pytype()
+ ),
+ )
+
+ def test_numpy_function_calls_inferred_as_dtype(self):
+ """
+ Test that calls to numpy functions are inferred as numpy.dtype
+ """
+ for func_ in self.numpy_functions_returning_dtype:
+ with self.subTest(typ=func_):
+ inferred_values = list(self._inferred_numpy_func_call(*func_))
+ self.assertTrue(
+ len(inferred_values) == 1,
+ msg="Too much inferred values ({}) for {:s}".format(
+ inferred_values, func_[0]
+ ),
+ )
+ self.assertTrue(
+ inferred_values[-1].pytype() == "numpy.dtype",
+ msg="Illicit type for {:s} ({})".format(
+ func_[0], inferred_values[-1].pytype()
+ ),
+ )
+
+ def test_numpy_function_calls_inferred_as_none(self):
+ """
+ Test that calls to numpy functions are inferred as None
+ """
+ for func_ in self.numpy_functions_returning_none:
with self.subTest(typ=func_):
inferred_values = list(self._inferred_numpy_func_call(*func_))
self.assertTrue(
len(inferred_values) == 1,
- msg="Too much inferred value for {:s}".format(func_[0]),
+ msg="Too much inferred values ({}) for {:s}".format(
+ inferred_values, func_[0]
+ ),
+ )
+ self.assertTrue(
+ inferred_values[-1].pytype() == "builtins.NoneType",
+ msg="Illicit type for {:s} ({})".format(
+ func_[0], inferred_values[-1].pytype()
+ ),
+ )
+
+ def test_numpy_function_calls_inferred_as_tuple(self):
+ """
+ Test that calls to numpy functions are inferred as tuple
+ """
+ for func_ in self.numpy_functions_returning_tuple:
+ with self.subTest(typ=func_):
+ inferred_values = list(self._inferred_numpy_func_call(*func_))
+ self.assertTrue(
+ len(inferred_values) == 1,
+ msg="Too much inferred values ({}) for {:s}".format(
+ inferred_values, func_[0]
+ ),
)
self.assertTrue(
- inferred_values[-1].pytype() in licit_array_types,
+ inferred_values[-1].pytype() == "builtins.tuple",
msg="Illicit type for {:s} ({})".format(
func_[0], inferred_values[-1].pytype()
),