summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhippo91 <guillaume.peillex@gmail.com>2019-04-07 17:18:33 +0200
committerhippo91 <guillaume.peillex@gmail.com>2019-04-07 17:18:33 +0200
commit2aba1f90740d45c832c39bdc9b084177227c9491 (patch)
tree028a97fba4db962c336e65ab36db39698b2f6012
parent0e0edeb6158c01eb53ae8c798e035a77a93b0065 (diff)
downloadastroid-git-2aba1f90740d45c832c39bdc9b084177227c9491.tar.gz
Split the unittest_brain_numpy module
-rw-r--r--astroid/tests/unittest_brain_numpy.py14
-rw-r--r--astroid/tests/unittest_brain_numpy_core_function_base.py79
-rw-r--r--astroid/tests/unittest_brain_numpy_core_multiarray.py83
3 files changed, 164 insertions, 12 deletions
diff --git a/astroid/tests/unittest_brain_numpy.py b/astroid/tests/unittest_brain_numpy.py
index 4797e64e..5aa4293e 100644
--- a/astroid/tests/unittest_brain_numpy.py
+++ b/astroid/tests/unittest_brain_numpy.py
@@ -45,18 +45,8 @@ class NumpyBrainFunctionReturningArrayTest(SubTestWrapper):
"""
Test that calls to numpy functions returning arrays are correctly inferred
"""
- numpy_functions = (("array", "[1, 2]"),
- ("linspace", "1, 100"),
- ('empty_like', "[1, 2]"),
- ('logical_or', "[1, 2]", "[1, 2]"),
- ('logical_xor', "[1, 2]", "[1, 2]"),
- ('logical_and', "[1, 2]", "[1, 2]"),
- ('dot', "[1, 2]", "[1, 2]"),
- ('vdot', "[1, 2]", "[1, 2]"),
- ('concatenate', "([1, 2], [1, 2])"),
- ('inner', "[1, 2]", "[1, 2]"),
- ('where', '[True, False]', "[1, 2]", "[2, 1]"),
- ('sum', '[[1, 2], [2, 1]]', "axis=0")
+ numpy_functions = (
+ ('sum', '[[1, 2], [2, 1]]', "axis=0"),
)
def _inferred_numpy_func_call(self, func_name, *func_args):
diff --git a/astroid/tests/unittest_brain_numpy_core_function_base.py b/astroid/tests/unittest_brain_numpy_core_function_base.py
new file mode 100644
index 00000000..7cf548c7
--- /dev/null
+++ b/astroid/tests/unittest_brain_numpy_core_function_base.py
@@ -0,0 +1,79 @@
+# -*- encoding=utf-8 -*-
+# Copyright (c) 2017-2018 hippo91 <guillaume.peillex@gmail.com>
+
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
+# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
+import unittest
+import contextlib
+
+try:
+ import numpy # pylint: disable=unused-import
+
+ HAS_NUMPY = True
+except ImportError:
+ HAS_NUMPY = False
+
+from astroid import builder
+
+
+class SubTestWrapper(unittest.TestCase):
+ """
+ A class for supporting all unittest version wether or not subTest is available
+ """
+
+ def subTest(self, msg=None, **params):
+ try:
+ # For python versions above 3.5 this should be ok
+ return super(SubTestWrapper, self).subTest(msg, **params)
+ except AttributeError:
+ #  For python versions below 3.5
+ return subTestMock(msg)
+
+
+@contextlib.contextmanager
+def subTestMock(msg=None):
+ """
+ A mock for subTest which do nothing
+ """
+ yield msg
+
+
+@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.")
+class BrainNumpyCoreFunctionBaseTest(SubTestWrapper):
+ """
+ Test the numpy core numeric brain module
+ """
+ numpy_functions = (
+ ("linspace", "1, 100"),
+ ("logspace", "1, 100"),
+ ("geomspace", "1, 100"),
+ )
+
+ def _inferred_numpy_func_call(self, func_name, *func_args):
+ node = builder.extract_node(
+ """
+ import numpy as np
+ func = np.{:s}
+ func({:s})
+ """.format(
+ func_name, ",".join(func_args)
+ )
+ )
+ return node.infer()
+
+ def test_numpy_function_calls_inferred_as_ndarray(self):
+ """
+ Test that calls to numpy functions are inferred as numpy.ndarray
+ """
+ licit_array_types = ('.ndarray',)
+ for func_ in self.numpy_functions:
+ 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]))
+ self.assertTrue(inferred_values[-1].pytype() in licit_array_types,
+ msg="Illicit type for {:s} ({})".format(func_[0], inferred_values[-1].pytype()))
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/astroid/tests/unittest_brain_numpy_core_multiarray.py b/astroid/tests/unittest_brain_numpy_core_multiarray.py
new file mode 100644
index 00000000..cb7af020
--- /dev/null
+++ b/astroid/tests/unittest_brain_numpy_core_multiarray.py
@@ -0,0 +1,83 @@
+# -*- encoding=utf-8 -*-
+# Copyright (c) 2017-2018 hippo91 <guillaume.peillex@gmail.com>
+
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
+# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
+import unittest
+import contextlib
+
+try:
+ import numpy # pylint: disable=unused-import
+
+ HAS_NUMPY = True
+except ImportError:
+ HAS_NUMPY = False
+
+from astroid import builder
+
+
+class SubTestWrapper(unittest.TestCase):
+ """
+ A class for supporting all unittest version wether or not subTest is available
+ """
+
+ def subTest(self, msg=None, **params):
+ try:
+ # For python versions above 3.5 this should be ok
+ return super(SubTestWrapper, self).subTest(msg, **params)
+ except AttributeError:
+ #  For python versions below 3.5
+ return subTestMock(msg)
+
+
+@contextlib.contextmanager
+def subTestMock(msg=None):
+ """
+ A mock for subTest which do nothing
+ """
+ yield msg
+
+
+@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.")
+class BrainNumpyCoreMultiarrayTest(SubTestWrapper):
+ """
+ Test the numpy core numeric brain module
+ """
+ numpy_functions = (
+ ('array', "[1, 2]"),
+ ('inner', "[1, 2]", "[1, 2]"),
+ ('vdot', "[1, 2]", "[1, 2]"),
+ ('concatenate', "([1, 2], [1, 2])"),
+ ('dot', "[1, 2]", "[1, 2]"),
+ ('empty_like', "[1, 2]"),
+ ('where', '[True, False]', "[1, 2]", "[2, 1]"),
+ )
+
+ def _inferred_numpy_func_call(self, func_name, *func_args):
+ node = builder.extract_node(
+ """
+ import numpy as np
+ func = np.{:s}
+ func({:s})
+ """.format(
+ func_name, ",".join(func_args)
+ )
+ )
+ return node.infer()
+
+ def test_numpy_function_calls_inferred_as_ndarray(self):
+ """
+ Test that calls to numpy functions are inferred as numpy.ndarray
+ """
+ licit_array_types = ('.ndarray',)
+ for func_ in self.numpy_functions:
+ 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]))
+ self.assertTrue(inferred_values[-1].pytype() in licit_array_types,
+ msg="Illicit type for {:s} ({})".format(func_[0], inferred_values[-1].pytype()))
+
+
+if __name__ == "__main__":
+ unittest.main()