Debian Codegraph @

tests/brain/numpy/test_ma.py

raw
1 # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2 # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3 # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
4
5 import pytest
6
7 try:
8 import numpy # pylint: disable=unused-import
9
10 HAS_NUMPY = True
11 except ImportError:
12 HAS_NUMPY = False
13
14 from astroid import builder
15
16
17 @pytest.mark.skipif(HAS_NUMPY is False, reason="This test requires the numpy library.")
18 class TestBrainNumpyMa:
19 """Test the numpy ma brain module."""
20
21 def _assert_maskedarray(self, code):
22 node = builder.extract_node(code)
23 cls_node = node.inferred()[0]
24 assert cls_node.pytype() == "numpy.ma.core.MaskedArray"
25
26 @pytest.mark.parametrize("alias_import", [True, False])
27 @pytest.mark.parametrize("ma_function", ["masked_invalid", "masked_where"])
28 def test_numpy_ma_returns_maskedarray(self, alias_import, ma_function):
29 """
30 Test that calls to numpy ma functions return a MaskedArray object.
31
32 The `ma_function` node is an Attribute or a Name
33 """
34 import_str = (
35 "import numpy as np"
36 if alias_import
37 else f"from numpy.ma import {ma_function}"
38 )
39 func = f"np.ma.{ma_function}" if alias_import else ma_function
40
41 src = f"""
42 {import_str}
43 data = np.ndarray((1,2))
44 {func}([1, 0, 0], data)
45 """
46 self._assert_maskedarray(src)