Repository: numpy/numpy
File: numpy/_core/tests/test_ufunc.py (Python)

Code before:
        # With casting and allocated output
        a = np.array([1], dtype=np.int64)
        m = np.array([True], dtype=bool)
        assert_equal(np.sqrt(a, where=m, out=None), [1])

        # No casting and allocated output
        a = np.array([1], dtype=np.float64)
        m = np.array([True], dtype=bool)
        assert_equal(np.sqrt(a, where=m, out=None), [1])

    def test_where_with_broadcasting(self):
        # See gh-17198
        a = np.random.random((5000, 4))
        b = np.random.random((5000, 1))

        where = a > 0.3
        out = np.full_like(a, 0)
        np.less(a, b, where=where, out=out)
        b_where = np.broadcast_to(b, a.shape)[where]
        assert_array_equal((a[where] < b_where), out[where].astype(bool))
        assert not out[~where].any()  # outside mask, out remains all 0

    def test_where_warns(self):
        a = np.arange(7)
        mask = a % 2 == 0
        with warnings.catch_warnings(record=True) as w_where:
            warnings.simplefilter('always')
            result = np.add(a, a, where=mask)
        assert len(w_where) == 1
        assert "'where'" in str(w_where[0])

    @staticmethod
    def identityless_reduce_arrs():
        yield np.empty((2, 3, 4), order='C')
        yield np.empty((2, 3, 4), order='F')
        # Mixed order (reduce order differs outer)
        yield np.empty((2, 4, 3), order='C').swapaxes(1, 2)
        # Reversed order
        yield np.empty((2, 3, 4), order='C')[::-1, ::-1, ::-1]
        # Not contiguous
        yield np.empty((3, 5, 4), order='C').swapaxes(1, 2)[1:, 1:, 1:]
        # Not contiguous and not aligned
        a = np.empty((3 * 4 * 5 * 8 + 1,), dtype='i1')
        a = a[1:].view(dtype='f8')
        a.shape = (3, 4, 5)
        a = a[1:, 1:, 1:]
        yield a

    @pytest.mark.parametrize("a", identityless_reduce_arrs())
    @pytest.mark.parametrize("pos", [(1, 0, 0), (0, 1, 0), (0, 0, 1)])
    def test_identityless_reduction(self, a, pos):

Review comment (suggestion):
Please use `with pytest.warns()`, also removes the need for that `'where' in str(...)`

Code after:
        # With casting and allocated output
        a = np.array([1], dtype=np.int64)
        m = np.array([True], dtype=bool)
        assert_equal(np.sqrt(a, where=m, out=None), [1])

        # No casting and allocated output
        a = np.array([1], dtype=np.float64)
        m = np.array([True], dtype=bool)
        assert_equal(np.sqrt(a, where=m, out=None), [1])

    def test_where_with_broadcasting(self):
        # See gh-17198
        a = np.random.random((5000, 4))
        b = np.random.random((5000, 1))

        where = a > 0.3
        out = np.full_like(a, 0)
        np.less(a, b, where=where, out=out)
        b_where = np.broadcast_to(b, a.shape)[where]
        assert_array_equal((a[where] < b_where), out[where].astype(bool))
        assert not out[~where].any()  # outside mask, out remains all 0

    def test_where_warns(self):
        a = np.arange(7)
        mask = a % 2 == 0
        with pytest.warns(UserWarning, match="'where' used without 'out'"):
            result1 = np.add(a, a, where=mask)
        # Does not warn
        result2 = np.add(a, a, where=mask, out=None)
        # Sanity check
        assert np.all(result1[::2] == [0, 4, 8, 12])
        assert np.all(result2[::2] == [0, 4, 8, 12])

    @staticmethod
    def identityless_reduce_arrs():
        yield np.empty((2, 3, 4), order='C')
        yield np.empty((2, 3, 4), order='F')
        # Mixed order (reduce order differs outer)
        yield np.empty((2, 4, 3), order='C').swapaxes(1, 2)
        # Reversed order
        yield np.empty((2, 3, 4), order='C')[::-1, ::-1, ::-1]
        # Not contiguous
        yield np.empty((3, 5, 4), order='C').swapaxes(1, 2)[1:, 1:, 1:]
        # Not contiguous and not aligned
        a = np.empty((3 * 4 * 5 * 8 + 1,), dtype='i1')
        a = a[1:].view(dtype='f8')
        a.shape = (3, 4, 5)
        a = a[1:, 1:, 1:]
        yield a

    @pytest.mark.parametrize("a", identityless_reduce_arrs())