summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhippo91 <guillaume.peillex@gmail.com>2019-02-24 14:51:37 +0100
committerGitHub <noreply@github.com>2019-02-24 14:51:37 +0100
commit67f7daa153de9b1d7ef3f5ee0656cf9d1d9bb22a (patch)
tree44e6fcfcbac261302c7b3a201c23eed4f90342d2
parent9a3fb9d14a30d10ea8c91f438c0df9e957d987ac (diff)
parentbb10f8057cfa18567fa11d4b70613a5942886c6c (diff)
downloadastroid-git-67f7daa153de9b1d7ef3f5ee0656cf9d1d9bb22a.tar.gz
Merge pull request #643 from hippo91/bug_pylint_2436
Bug pylint 2436
-rw-r--r--ChangeLog6
-rw-r--r--astroid/brain/brain_numpy.py64
-rw-r--r--astroid/tests/unittest_brain_numpy.py38
3 files changed, 108 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 3804be0e..fc12ffe2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,12 @@ What's New in astroid 2.2.0?
============================
Release Date: TBA
+<<<<<<< HEAD
+* Fix a bug concerning inference of calls to numpy function that should not return Tuple or List
+instances.
+
+ Close PyCQA/pylint#2436
+
* ``typed_ast`` gets installed for Python 3.7, meaning type comments can now work on 3.7.
* Fix a bug concerning inference of unary operators on numpy types.
diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py
index 7868ab72..b7d4df0a 100644
--- a/astroid/brain/brain_numpy.py
+++ b/astroid/brain/brain_numpy.py
@@ -8,6 +8,7 @@
"""Astroid hooks for numpy."""
+import functools
import astroid
@@ -481,6 +482,69 @@ def numpy_funcs():
)
+def _looks_like_numpy_function(func_name, numpy_module_name, node):
+ """
+ Return True if the current node correspond to the function inside
+ the numpy module in parameters
+
+ :param node: the current node
+ :type node: FunctionDef
+ :param func_name: name of the function
+ :type func_name: str
+ :param numpy_module_name: name of the numpy module
+ :type numpy_module_name: str
+ :return: True if the current node correspond to the function looked for
+ :rtype: bool
+ """
+ return node.name == func_name and node.parent.name == numpy_module_name
+
+
+def numpy_function_infer_call_result(node):
+ """
+ A wrapper around infer_call_result method bounded to the node.
+
+ :param node: the node which infer_call_result should be filtered
+ :type node: FunctionDef
+ :return: a function that filter the results of the call to node.infer_call_result
+ :rtype: function
+ """
+ #  Put the origin infer_call_result method into the closure
+ origin_infer_call_result = node.infer_call_result
+
+ def infer_call_result_wrapper(caller=None, context=None):
+ """
+ Call the origin infer_call_result method bounded to the node instance and
+ filter the results to remove List and Tuple instances
+ """
+ unfiltered_infer_call_result = origin_infer_call_result(caller, context)
+ return (
+ x
+ for x in unfiltered_infer_call_result
+ if not isinstance(x, (astroid.List, astroid.Tuple))
+ )
+
+ return infer_call_result_wrapper
+
+
+def _replace_numpy_function_infer_call_result(node, context=None):
+ node.infer_call_result = numpy_function_infer_call_result(node)
+ return
+
+
+astroid.MANAGER.register_transform(
+ astroid.FunctionDef,
+ _replace_numpy_function_infer_call_result,
+ functools.partial(
+ _looks_like_numpy_function, "linspace", "numpy.core.function_base"
+ ),
+)
+
+astroid.MANAGER.register_transform(
+ astroid.FunctionDef,
+ _replace_numpy_function_infer_call_result,
+ functools.partial(_looks_like_numpy_function, "array", "numpy.core.records"),
+)
+
astroid.register_module_extender(
astroid.MANAGER, "numpy.core.umath", numpy_core_umath_transform
)
diff --git a/astroid/tests/unittest_brain_numpy.py b/astroid/tests/unittest_brain_numpy.py
index f0a3ab35..d3eae9cc 100644
--- a/astroid/tests/unittest_brain_numpy.py
+++ b/astroid/tests/unittest_brain_numpy.py
@@ -17,6 +17,7 @@ except ImportError:
from astroid import builder
from astroid import nodes
+from astroid import node_classes
class SubTestWrapper(unittest.TestCase):
@@ -618,5 +619,42 @@ class NumpyBrainCoreNumericTypesTest(SubTestWrapper):
self.assertNotEqual(len(inferred.getattr(attr)), 0)
+@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.")
+class NumpyBrainFunctionReturningArrayTest(SubTestWrapper):
+ """
+ Test that calls to numpy functions returning arrays are correctly inferred
+ """
+
+ 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_not_inferred_as_list(self):
+ """
+ Test that some calls to numpy functions are not inferred as list nor tuple
+ """
+ for func_ in (("array", "[1, 2]"),):
+ with self.subTest(typ=func_):
+ for inferred in self._inferred_numpy_func_call(*func_):
+ self.assertFalse(isinstance(inferred, node_classes.List))
+
+ def test_numpy_function_calls_not_inferred_as_tuple(self):
+ """
+ Test that some calls to numpy functions are not inferred as list nor tuple
+ """
+ for func_ in (("array", "(1, 2)"), ("linspace", "1, 100")):
+ with self.subTest(typ=func_):
+ for inferred in self._inferred_numpy_func_call(*func_):
+ self.assertFalse(isinstance(inferred, node_classes.Tuple))
+
+
if __name__ == "__main__":
unittest.main()