diff --git a/ChangeLog b/ChangeLog index 3b40594248..9488436e4f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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? ============================== diff --git a/astroid/brain/brain_numpy_ma.py b/astroid/brain/brain_numpy_ma.py index 241665c452..bb344e6607 100644 --- a/astroid/brain/brain_numpy_ma.py +++ b/astroid/brain/brain_numpy_ma.py @@ -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 @@ -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=[]) """ ) diff --git a/tests/unittest_brain_numpy_ma.py b/tests/unittest_brain_numpy_ma.py index 830e729915..7f7b36cb67 100644 --- a/tests/unittest_brain_numpy_ma.py +++ b/tests/unittest_brain_numpy_ma.py @@ -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)