Skip to content

Commit

Permalink
Bug pylint 4960 (#1176)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
hippo91 and Pierre-Sassoulas authored Sep 25, 2021
1 parent 4ffdf11 commit 05445e2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
10 changes: 9 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
28 changes: 28 additions & 0 deletions astroid/brain/brain_numpy_ma.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 0 additions & 4 deletions astroid/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
53 changes: 53 additions & 0 deletions tests/unittest_brain_numpy_ma.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 05445e2

Please sign in to comment.