Skip to content

Commit

Permalink
Merge f30ca92 into 380e886
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Dec 30, 2022
2 parents 380e886 + f30ca92 commit c509a72
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Release date: TBA

Closes PyCQA/pylint#1902

* Add the ``masked_invalid`` function in the ``numpy.ma`` brain.

Closes PyCQA/pylint#5715

What's New in astroid 2.12.13?
==============================
Expand Down
5 changes: 4 additions & 1 deletion astroid/brain/brain_numpy_ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def numpy_ma_transform():
"""
Infer the call of the masked_where function
Infer the call of various numpy.ma functions
:param node: node to infer
:param context: inference context
Expand All @@ -21,6 +21,9 @@ def numpy_ma_transform():
import numpy.ma
def masked_where(condition, a, copy=True):
return numpy.ma.masked_array(a, mask=[])
def masked_invalid(a, copy=True):
return numpy.ma.masked_array(a, mask=[])
"""
)

Expand Down
41 changes: 18 additions & 23 deletions tests/unittest_brain_numpy_ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,29 @@ 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)
def _assert_maskedarray(self, code):
node = builder.extract_node(code)
cls_node = node.inferred()[0]
assert cls_node.pytype() == "numpy.ma.core.MaskedArray"

@staticmethod
def test_numpy_ma_masked_where_returns_maskedarray_bis():
@pytest.mark.parametrize("alias_import", [True, False])
@pytest.mark.parametrize("ma_function", ["masked_invalid", "masked_where"])
def test_numpy_ma_returns_maskedarray(self, alias_import, ma_function):
"""
Test that calls to numpy ma masked_where returns a MaskedArray object
Test that calls to numpy ma functions return a MaskedArray object.
The "masked_where" node is a Name
The `ma_function` node is an Attribute or a Name
"""
src = """
from numpy.ma import masked_where
import_str = (
"import numpy as np"
if alias_import
else f"from numpy.ma import {ma_function}"
)
func = f"np.ma.{ma_function}" if alias_import else ma_function

src = f"""
{import_str}
data = np.ndarray((1,2))
masked_where([1, 0, 0], data)
{func}([1, 0, 0], data)
"""
node = builder.extract_node(src)
cls_node = node.inferred()[0]
assert cls_node.pytype() == "numpy.ma.core.MaskedArray"
self._assert_maskedarray(src)

0 comments on commit c509a72

Please sign in to comment.