summaryrefslogtreecommitdiff
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
parentc54af820fcbaa1dd7a4644670b58a40b4f587ff9 (diff)
downloadastroid-git-3d74305efdbc6aa58784a0222e4a1eef6a33e2fa.tar.gz
Add missing methods of the numpy.core.multiarray module.
Closes PyCQA/pylint#3208
-rw-r--r--ChangeLog4
-rw-r--r--astroid/brain/brain_numpy_core_multiarray.py32
-rw-r--r--tests/unittest_brain_numpy_core_multiarray.py125
3 files changed, 154 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 96b7eb7a..b612aee0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,10 @@ What's New in astroid 2.4.0?
============================
Release Date: TBA
+* Added some functions of the ``numpy.core.multiarray`` module
+
+ Close PyCQA/pylint#3208
+
* All the ``numpy ufunc`` functions derived now from a common class that
implements the specific ``reduce``, ``accumulate``, ``reduceat``,
``outer`` and ``at`` methods.
diff --git a/astroid/brain/brain_numpy_core_multiarray.py b/astroid/brain/brain_numpy_core_multiarray.py
index 3032acc9..18218436 100644
--- a/astroid/brain/brain_numpy_core_multiarray.py
+++ b/astroid/brain/brain_numpy_core_multiarray.py
@@ -42,6 +42,38 @@ METHODS_TO_BE_INFERRED = {
return numpy.ndarray([0, 0])""",
"empty": """def empty(shape, dtype=float, order='C'):
return numpy.ndarray([0, 0])""",
+ "bincount": """def bincount(x, weights=None, minlength=0):
+ return numpy.ndarray([0, 0])""",
+ "busday_count": """def busday_count(begindates, enddates, weekmask='1111100', holidays=[], busdaycal=None, out=None):
+ return numpy.ndarray([0, 0])""",
+ "busday_offset": """def busday_offset(dates, offsets, roll='raise', weekmask='1111100', holidays=None, busdaycal=None, out=None):
+ return numpy.ndarray([0, 0])""",
+ "can_cast": """def can_cast(from_, to, casting='safe'):
+ return True""",
+ "copyto": """def copyto(dst, src, casting='same_kind', where=True):
+ return None""",
+ "datetime_as_string": """def datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind'):
+ return numpy.ndarray([0, 0])""",
+ "is_busday": """def is_busday(dates, weekmask='1111100', holidays=None, busdaycal=None, out=None):
+ return numpy.ndarray([0, 0])""",
+ "lexsort": """def lexsort(keys, axis=-1):
+ return numpy.ndarray([0, 0])""",
+ "may_share_memory": """def may_share_memory(a, b, max_work=None):
+ return True""",
+ # Not yet available because dtype is not yet present in those brains
+ # "min_scalar_type": """def min_scalar_type(a):
+ # return numpy.dtype('int16')""",
+ "packbits": """def packbits(a, axis=None, bitorder='big'):
+ return numpy.ndarray([0, 0])""",
+ # Not yet available because dtype is not yet present in those brains
+ # "result_type": """def result_type(*arrays_and_dtypes):
+ # return numpy.dtype('int16')""",
+ "shares_memory": """def shares_memory(a, b, max_work=None):
+ return True""",
+ "unpackbits": """def unpackbits(a, axis=None, count=None, bitorder='big'):
+ return numpy.ndarray([0, 0])""",
+ "unravel_index": """def unravel_index(indices, shape, order='C'):
+ return (numpy.ndarray([0, 0]),)""",
"zeros": """def zeros(shape, dtype=float, order='C'):
return numpy.ndarray([0, 0])""",
}
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()
),