diff options
author | hippo91 <guillaume.peillex@gmail.com> | 2021-09-25 13:26:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-25 13:26:38 +0200 |
commit | 05445e23fe1292eda236f8d15674aa2fea811720 (patch) | |
tree | 5bcf15be7573345f81465d44e51964fbb9e9ac11 | |
parent | 4ffdf1108a6084b85e282b5835427c1d747bb22c (diff) | |
download | astroid-05445e23fe1292eda236f8d15674aa2fea811720.tar.gz |
Bug pylint 4960 (#1176)
* 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.
* Adds a brain to infer the numpy.ma.masked_where function
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | astroid/brain/brain_numpy_ma.py | 28 | ||||
-rw-r--r-- | astroid/builder.py | 4 | ||||
-rw-r--r-- | tests/unittest_brain_numpy_ma.py | 53 |
4 files changed, 90 insertions, 5 deletions
@@ -52,7 +52,15 @@ 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? diff --git a/astroid/brain/brain_numpy_ma.py b/astroid/brain/brain_numpy_ma.py new file mode 100644 index 00000000..8ae94659 --- /dev/null +++ b/astroid/brain/brain_numpy_ma.py @@ -0,0 +1,28 @@ +# Copyright (c) 2021 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/main/LICENSE +"""Astroid hooks for numpy ma module""" + +from astroid.brain.helpers import register_module_extender +from astroid.builder import parse +from astroid.manager import AstroidManager + + +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/astroid/builder.py b/astroid/builder.py index 916cd534..56b85dc9 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -160,10 +160,6 @@ class AstroidBuilder(raw_building.InspectBuilder): # 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 diff --git a/tests/unittest_brain_numpy_ma.py b/tests/unittest_brain_numpy_ma.py new file mode 100644 index 00000000..96dddd28 --- /dev/null +++ b/tests/unittest_brain_numpy_ma.py @@ -0,0 +1,53 @@ +# Copyright (c) 2021 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/main/LICENSE +import pytest + +try: + import numpy # pylint: disable=unused-import + + HAS_NUMPY = True +except ImportError: + HAS_NUMPY = False + +from astroid import builder + + +@pytest.mark.skipif(HAS_NUMPY is False, reason="This test requires the numpy library.") +class TestBrainNumpyMa: + """ + Test the numpy ma brain module + """ + + @staticmethod + def test_numpy_ma_masked_where_returns_maskedarray(): + """ + 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] + assert cls_node.pytype() == "numpy.ma.core.MaskedArray" + + @staticmethod + def test_numpy_ma_masked_where_returns_maskedarray_bis(): + """ + 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] + assert cls_node.pytype() == "numpy.ma.core.MaskedArray" |