From 1b08455dc8ea6ea6fce508a563750983e5f66372 Mon Sep 17 00:00:00 2001 From: hippo91 Date: Thu, 16 Sep 2021 15:55:27 +0200 Subject: [PATCH 1/7] Revert modifications of PR 1148. While it is probably still a good idea to prevent nodes that are dynamically imported to be inferred through an astroid's brain, the way it was done in builder.py was incorrect. The way it was done, lead to prevent the use of astroid legetimate brains even for node that was not dynamically loaded. --- astroid/builder.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/astroid/builder.py b/astroid/builder.py index 916cd53428..56b85dc9af 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -160,10 +160,6 @@ def _post_build(self, module, encoding): # Visit the transforms if self._apply_transforms: - if modutils.is_module_name_part_of_extension_package_whitelist( - module.name, self._manager.extension_package_whitelist - ): - return module module = self._manager.visit_transforms(module) return module From 2ebcf80beb2f5093f16d3f4a52c790a55817d13f Mon Sep 17 00:00:00 2001 From: hippo91 Date: Thu, 16 Sep 2021 16:01:29 +0200 Subject: [PATCH 2/7] Adds a brain to infer the numpy.ma.masked_where function --- astroid/brain/brain_numpy_ma.py | 30 +++++++++++++++++ tests/unittest_brain_numpy_ma.py | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 astroid/brain/brain_numpy_ma.py create mode 100644 tests/unittest_brain_numpy_ma.py diff --git a/astroid/brain/brain_numpy_ma.py b/astroid/brain/brain_numpy_ma.py new file mode 100644 index 0000000000..c7e85fbf0c --- /dev/null +++ b/astroid/brain/brain_numpy_ma.py @@ -0,0 +1,30 @@ +# Copyright (c) 2021 hippo91 + +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE +"""Astroid hooks for numpy ma module""" +import functools + +from astroid.builder import parse +from astroid.manager import AstroidManager +from astroid.brain.helpers import register_module_extender + + +def numpy_ma_transform(): + """ + Infer the call of the masked_where function + + :param node: node to infer + :param context: inference context + """ + return parse(""" + import numpy.ma + def masked_where(condition, a, copy=True): + return numpy.ma.masked_array(a, mask=[]) + """) + + +register_module_extender( + AstroidManager(), "numpy.ma", numpy_ma_transform +) + diff --git a/tests/unittest_brain_numpy_ma.py b/tests/unittest_brain_numpy_ma.py new file mode 100644 index 0000000000..ef5c2cec46 --- /dev/null +++ b/tests/unittest_brain_numpy_ma.py @@ -0,0 +1,55 @@ +# Copyright (c) 2021 hippo91 + +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE +import unittest + +try: + import numpy # pylint: disable=unused-import + + HAS_NUMPY = True +except ImportError: + HAS_NUMPY = False + +from astroid import builder + + +@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.") +class BrainNumpyMaTest(unittest.TestCase): + """ + Test the numpy ma brain module + """ + + def test_numpy_ma_masked_where_returns_maskedarray(self): + """ + Test that calls to numpy ma masked_where returns a MaskedArray object. + + The "masked_where" node is an Attribute + """ + src = """ + import numpy as np + data = np.ndarray((1,2)) + np.ma.masked_where([1, 0, 0], data) + """ + node=builder.extract_node(src) + cls_node = node.inferred()[0] + self.assertEqual(cls_node.pytype(), "numpy.ma.core.MaskedArray") + + def test_numpy_ma_masked_where_returns_maskedarray_bis(self): + """ + Test that calls to numpy ma masked_where returns a MaskedArray object + + The "masked_where" node is a Name + """ + src = """ + from numpy.ma import masked_where + data = np.ndarray((1,2)) + masked_where([1, 0, 0], data) + """ + node=builder.extract_node(src) + cls_node = node.inferred()[0] + self.assertEqual(cls_node.pytype(), "numpy.ma.core.MaskedArray") + + +if __name__ == "__main__": + unittest.main() From db5f37cc231154647dc3538894f9578ca885737c Mon Sep 17 00:00:00 2001 From: hippo91 Date: Thu, 16 Sep 2021 16:08:21 +0200 Subject: [PATCH 3/7] Adds two entries --- ChangeLog | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 35d46c40c8..df12265fc2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -48,7 +48,16 @@ Release date: 2021-09-14 * Fixed bug in inference of dataclass field calls. - Closes PyCQA/pylint#4963 + Closes PyCQA/pylint#4963 + +* Suppress the conditional between applied brains and dynamic import authorized +modules. (Revert the "The transforms related to a module are applied only if + this module has not been explicitly authorized to be imported" of version + 2.7.3) + +* Adds a brain to infer the `numpy.ma.masked_where` function. + + Closes PyCQA/pylint#3342 What's New in astroid 2.7.3? From dc8d45e5afa5bd9ca8a20fcc54f62ca49044504a Mon Sep 17 00:00:00 2001 From: hippo91 Date: Thu, 16 Sep 2021 16:16:31 +0200 Subject: [PATCH 4/7] Reformat --- astroid/brain/brain_numpy_ma.py | 14 ++++++-------- tests/unittest_brain_numpy_ma.py | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/astroid/brain/brain_numpy_ma.py b/astroid/brain/brain_numpy_ma.py index c7e85fbf0c..8ae946599e 100644 --- a/astroid/brain/brain_numpy_ma.py +++ b/astroid/brain/brain_numpy_ma.py @@ -3,11 +3,10 @@ # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE """Astroid hooks for numpy ma module""" -import functools +from astroid.brain.helpers import register_module_extender from astroid.builder import parse from astroid.manager import AstroidManager -from astroid.brain.helpers import register_module_extender def numpy_ma_transform(): @@ -17,14 +16,13 @@ def numpy_ma_transform(): :param node: node to infer :param context: inference context """ - return parse(""" + return parse( + """ import numpy.ma def masked_where(condition, a, copy=True): return numpy.ma.masked_array(a, mask=[]) - """) - + """ + ) -register_module_extender( - AstroidManager(), "numpy.ma", numpy_ma_transform -) +register_module_extender(AstroidManager(), "numpy.ma", numpy_ma_transform) diff --git a/tests/unittest_brain_numpy_ma.py b/tests/unittest_brain_numpy_ma.py index ef5c2cec46..af4d6839ec 100644 --- a/tests/unittest_brain_numpy_ma.py +++ b/tests/unittest_brain_numpy_ma.py @@ -31,7 +31,7 @@ def test_numpy_ma_masked_where_returns_maskedarray(self): data = np.ndarray((1,2)) np.ma.masked_where([1, 0, 0], data) """ - node=builder.extract_node(src) + node = builder.extract_node(src) cls_node = node.inferred()[0] self.assertEqual(cls_node.pytype(), "numpy.ma.core.MaskedArray") @@ -46,7 +46,7 @@ def test_numpy_ma_masked_where_returns_maskedarray_bis(self): data = np.ndarray((1,2)) masked_where([1, 0, 0], data) """ - node=builder.extract_node(src) + node = builder.extract_node(src) cls_node = node.inferred()[0] self.assertEqual(cls_node.pytype(), "numpy.ma.core.MaskedArray") From 0a82f0657474d5e9c3b2dbbbe453e0af1934e719 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 19 Sep 2021 19:15:23 +0200 Subject: [PATCH 5/7] Update ChangeLog --- ChangeLog | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index df12265fc2..c1f681192a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -51,9 +51,8 @@ Release date: 2021-09-14 Closes PyCQA/pylint#4963 * Suppress the conditional between applied brains and dynamic import authorized -modules. (Revert the "The transforms related to a module are applied only if - this module has not been explicitly authorized to be imported" of version - 2.7.3) + modules. (Revert the "The transforms related to a module are applied only if this + module has not been explicitly authorized to be imported" of version 2.7.3) * Adds a brain to infer the `numpy.ma.masked_where` function. From df1bf6719c3e779f741559e90bb6cd156f115682 Mon Sep 17 00:00:00 2001 From: hippo91 Date: Mon, 20 Sep 2021 19:19:59 +0200 Subject: [PATCH 6/7] Takes into account cdce8p remarks --- ChangeLog | 2 +- tests/unittest_brain_numpy_ma.py | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index c1f681192a..32099ccbe3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -54,7 +54,7 @@ Release date: 2021-09-14 modules. (Revert the "The transforms related to a module are applied only if this module has not been explicitly authorized to be imported" of version 2.7.3) -* Adds a brain to infer the `numpy.ma.masked_where` function. +* Adds a brain to infer the ``numpy.ma.masked_where`` function. Closes PyCQA/pylint#3342 diff --git a/tests/unittest_brain_numpy_ma.py b/tests/unittest_brain_numpy_ma.py index af4d6839ec..a79c08e184 100644 --- a/tests/unittest_brain_numpy_ma.py +++ b/tests/unittest_brain_numpy_ma.py @@ -2,7 +2,7 @@ # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE -import unittest +import pytest try: import numpy # pylint: disable=unused-import @@ -14,13 +14,13 @@ from astroid import builder -@unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.") -class BrainNumpyMaTest(unittest.TestCase): +@pytest.mark.skipif(not HAS_NUMPY, reason="This test requires the numpy library.") +class TestBrainNumpyMa: """ Test the numpy ma brain module """ - - def test_numpy_ma_masked_where_returns_maskedarray(self): + @staticmethod + def test_numpy_ma_masked_where_returns_maskedarray(): """ Test that calls to numpy ma masked_where returns a MaskedArray object. @@ -33,9 +33,10 @@ def test_numpy_ma_masked_where_returns_maskedarray(self): """ node = builder.extract_node(src) cls_node = node.inferred()[0] - self.assertEqual(cls_node.pytype(), "numpy.ma.core.MaskedArray") + assert cls_node.pytype() == "numpy.ma.core.MaskedArray" - def test_numpy_ma_masked_where_returns_maskedarray_bis(self): + @staticmethod + def test_numpy_ma_masked_where_returns_maskedarray_bis(): """ Test that calls to numpy ma masked_where returns a MaskedArray object @@ -48,8 +49,5 @@ def test_numpy_ma_masked_where_returns_maskedarray_bis(self): """ node = builder.extract_node(src) cls_node = node.inferred()[0] - self.assertEqual(cls_node.pytype(), "numpy.ma.core.MaskedArray") - + assert cls_node.pytype() == "numpy.ma.core.MaskedArray" -if __name__ == "__main__": - unittest.main() From 1d1a40f1cca40c1b4e024652a89ae14155de47a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Sep 2021 17:21:14 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unittest_brain_numpy_ma.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittest_brain_numpy_ma.py b/tests/unittest_brain_numpy_ma.py index a79c08e184..f1297a04be 100644 --- a/tests/unittest_brain_numpy_ma.py +++ b/tests/unittest_brain_numpy_ma.py @@ -19,6 +19,7 @@ class TestBrainNumpyMa: """ Test the numpy ma brain module """ + @staticmethod def test_numpy_ma_masked_where_returns_maskedarray(): """ @@ -50,4 +51,3 @@ def test_numpy_ma_masked_where_returns_maskedarray_bis(): node = builder.extract_node(src) cls_node = node.inferred()[0] assert cls_node.pytype() == "numpy.ma.core.MaskedArray" -