Repository: numpy/numpy
File: numpy/testing/tests/test_utils.py (Python)

Code before:
        # While we cannot guarantee testing functions will always work for
        # subclasses, the tests should ideally rely only on subclasses having
        # comparison operators, not on them being able to store booleans
        # (which, e.g., astropy Quantity cannot usefully do). See gh-8452.
        class MyArray(np.ndarray):
            def __eq__(self, other):
                return bool(np.equal(self, other).all())

            def __ne__(self, other):
                return not self == other

        a = np.array([1., 2.]).view(MyArray)
        b = np.array([2., 3.]).view(MyArray)
        assert_(type(a == a), bool)
        assert_(a == a)
        assert_(a != b)
        self._test_equal(a, a)
        self._test_not_equal(a, b)
        self._test_not_equal(b, a)

        expected_msg = ('Mismatched elements: 1 / 2 (50%)\n'
                        'Max absolute difference among violations: 1.\n'
                        'Max relative difference among violations: 0.5')
        with pytest.raises(AssertionError, match=re.escape(expected_msg)):
            self._test_equal(a, b)

        c = np.array([0., 2.9]).view(MyArray)
        expected_msg = ('Mismatched elements: 1 / 2 (50%)\n'
                        'Max absolute difference among violations: 2.\n'
                        'Max relative difference among violations: inf')
        with pytest.raises(AssertionError, match=re.escape(expected_msg)):
            self._test_equal(b, c)

    def test_subclass_that_does_not_implement_npall(self):
        class MyArray(np.ndarray):
            def __array_function__(self, *args, **kwargs):
                return NotImplemented

        a = np.array([1., 2.]).view(MyArray)
        b = np.array([2., 3.]).view(MyArray)
        with assert_raises(TypeError):
            np.all(a)
        self._test_equal(a, a)
        self._test_not_equal(a, b)
        self._test_not_equal(b, a)

    def test_suppress_overflow_warnings(self):
        # Based on issue #18992
        with pytest.raises(AssertionError):
            with np.errstate(all="raise"):
                np.testing.assert_array_equal(

Review comment (suggestion):
Similarly, here, there seems no reason not to take things out of the loop,
```
b_val = [3., 4., np.nan, np.inf, -np.inf]
for a_val, b_mask in itertools.product(
    [3., np.nan, np.inf],
    [False, True]
):
```

Code after:
    def test_masked_scalar(self):
        # Test masked scalar vs. plain/masked scalar
        for a_val, b_val, b_masked in itertools.product(
            [3., np.nan, np.inf],
            [3., 4., np.nan, np.inf, -np.inf],
            [False, True],
        ):
            a = np.ma.MaskedArray(a_val, mask=True)
            b = np.ma.MaskedArray(b_val, mask=True) if b_masked else np.array(b_val)
            self._test_equal(a, b)
            self._test_equal(b, a)

        # Test masked scalar vs. plain array
        for a_val, b_val in itertools.product(
            [3., np.nan, -np.inf],
            itertools.product([3., 4., np.nan, np.inf, -np.inf], repeat=2),
        ):
            a = np.ma.MaskedArray(a_val, mask=True)
            b = np.array(b_val)
            self._test_equal(a, b)
            self._test_equal(b, a)

        # Test masked scalar vs. masked array
        for a_val, b_val, b_mask in itertools.product(
            [3., np.nan, np.inf],
            itertools.product([3., 4., np.nan, np.inf, -np.inf], repeat=2),
            itertools.product([False, True], repeat=2),
        ):
            a = np.ma.MaskedArray(a_val, mask=True)
            b = np.ma.MaskedArray(b_val, mask=b_mask)
            self._test_equal(a, b)
            self._test_equal(b, a)

    def test_subclass_that_overrides_eq(self):
        # While we cannot guarantee testing functions will always work for
        # subclasses, the tests should ideally rely only on subclasses having
        # comparison operators, not on them being able to store booleans
        # (which, e.g., astropy Quantity cannot usefully do). See gh-8452.
        class MyArray(np.ndarray):
            def __eq__(self, other):
                return bool(np.equal(self, other).all())

            def __ne__(self, other):
                return not self == other

        a = np.array([1., 2.]).view(MyArray)
        b = np.array([2., 3.]).view(MyArray)
        assert_(type(a == a), bool)
        assert_(a == a)
        assert_(a != b)
        self._test_equal(a, a)