From c8bc7f9f52936d566827e6d7c7a3d74d863c5a40 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Thu, 29 Dec 2022 14:54:15 -0300 Subject: [PATCH 1/3] brain tip for numpy masked_invalid --- astroid/brain/brain_numpy_ma.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/astroid/brain/brain_numpy_ma.py b/astroid/brain/brain_numpy_ma.py index 241665c452..39dac9217c 100644 --- a/astroid/brain/brain_numpy_ma.py +++ b/astroid/brain/brain_numpy_ma.py @@ -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=[]) """ ) From 48b813ed3149ae610dcbc1e715a4012760dd7ff5 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Fri, 30 Dec 2022 08:30:02 -0300 Subject: [PATCH 2/3] add unit test --- astroid/brain/brain_numpy_ma.py | 2 +- tests/unittest_brain_numpy_ma.py | 53 +++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/astroid/brain/brain_numpy_ma.py b/astroid/brain/brain_numpy_ma.py index 39dac9217c..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 diff --git a/tests/unittest_brain_numpy_ma.py b/tests/unittest_brain_numpy_ma.py index 830e729915..e90e0dabc3 100644 --- a/tests/unittest_brain_numpy_ma.py +++ b/tests/unittest_brain_numpy_ma.py @@ -13,6 +13,8 @@ from astroid import builder +parametrize = pytest.mark.parametrize("alias_import", [True, False]) + @pytest.mark.skipif(HAS_NUMPY is False, reason="This test requires the numpy library.") class TestBrainNumpyMa: @@ -20,34 +22,49 @@ class TestBrainNumpyMa: Test the numpy ma brain module """ - @staticmethod - def test_numpy_ma_masked_where_returns_maskedarray(): + def _assert_maskedarray(self, code): + node = builder.extract_node(code) + cls_node = node.inferred()[0] + assert cls_node.pytype() == "numpy.ma.core.MaskedArray" + + @parametrize + def test_numpy_ma_masked_where_returns_maskedarray(self, alias_import): """ Test that calls to numpy ma masked_where returns a MaskedArray object. The "masked_where" node is an Attribute """ - src = """ - import numpy as np + import_str = ( + "import numpy as np" + if alias_import + else "from numpy.ma import masked_where" + ) + func_call = "np.ma.masked_where" if alias_import else "masked_where" + + src = f""" + {import_str} data = np.ndarray((1,2)) - np.ma.masked_where([1, 0, 0], data) + {func_call}([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) - @staticmethod - def test_numpy_ma_masked_where_returns_maskedarray_bis(): + @parametrize + def test_numpy_ma_masked_invalid_returns_maskedarray(self, alias_import): """ - Test that calls to numpy ma masked_where returns a MaskedArray object + Test that calls to numpy ma masked_invalid returns a MaskedArray object. - The "masked_where" node is a Name + The "masked_invalid" node is an Attribute """ - src = """ - from numpy.ma import masked_where + import_str = ( + "import numpy as np" + if alias_import + else "from numpy.ma import masked_invalid" + ) + func_call = "np.ma.masked_invalid" if alias_import else "masked_invalid" + + src = f""" + {import_str} data = np.ndarray((1,2)) - masked_where([1, 0, 0], data) + {func_call}([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) From f30ca92ff4245b1bddcd0a1defd6f0b6710a360e Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Fri, 30 Dec 2022 11:29:43 -0300 Subject: [PATCH 3/3] refactor tests and update changelog --- ChangeLog | 3 +++ tests/unittest_brain_numpy_ma.py | 38 +++++++------------------------- 2 files changed, 11 insertions(+), 30 deletions(-) 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/tests/unittest_brain_numpy_ma.py b/tests/unittest_brain_numpy_ma.py index e90e0dabc3..7f7b36cb67 100644 --- a/tests/unittest_brain_numpy_ma.py +++ b/tests/unittest_brain_numpy_ma.py @@ -13,8 +13,6 @@ from astroid import builder -parametrize = pytest.mark.parametrize("alias_import", [True, False]) - @pytest.mark.skipif(HAS_NUMPY is False, reason="This test requires the numpy library.") class TestBrainNumpyMa: @@ -27,44 +25,24 @@ def _assert_maskedarray(self, code): cls_node = node.inferred()[0] assert cls_node.pytype() == "numpy.ma.core.MaskedArray" - @parametrize - def test_numpy_ma_masked_where_returns_maskedarray(self, alias_import): - """ - Test that calls to numpy ma masked_where returns a MaskedArray object. - - The "masked_where" node is an Attribute - """ - import_str = ( - "import numpy as np" - if alias_import - else "from numpy.ma import masked_where" - ) - func_call = "np.ma.masked_where" if alias_import else "masked_where" - - src = f""" - {import_str} - data = np.ndarray((1,2)) - {func_call}([1, 0, 0], data) - """ - self._assert_maskedarray(src) - - @parametrize - def test_numpy_ma_masked_invalid_returns_maskedarray(self, alias_import): + @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_invalid returns a MaskedArray object. + Test that calls to numpy ma functions return a MaskedArray object. - The "masked_invalid" node is an Attribute + The `ma_function` node is an Attribute or a Name """ import_str = ( "import numpy as np" if alias_import - else "from numpy.ma import masked_invalid" + else f"from numpy.ma import {ma_function}" ) - func_call = "np.ma.masked_invalid" if alias_import else "masked_invalid" + func = f"np.ma.{ma_function}" if alias_import else ma_function src = f""" {import_str} data = np.ndarray((1,2)) - {func_call}([1, 0, 0], data) + {func}([1, 0, 0], data) """ self._assert_maskedarray(src)