Repository: scipy/scipy
File: scipy/sparse/tests/test_base.py (Python)

Code before:
                # Check axis=None
                with np.errstate(over='ignore'):
                    dat_sum = dat.sum(dtype=output_dtype)
                    datsp_sum = datsp.sum(dtype=output_dtype)
                    assert_allclose(datsp_sum, dat_sum)
                    assert_equal(datsp_sum.dtype, dat_sum.dtype)

                    # Check axis=0
                    dat_sum = dat.sum(axis=0, dtype=output_dtype)
                    datsp_sum = datsp.sum(axis=0, dtype=output_dtype)
                    assert_allclose(datsp_sum, dat_sum)
                    assert_equal(datsp_sum.dtype, dat_sum.dtype)

                    # Check axis=1
                    dat_sum = dat.sum(axis=1, dtype=output_dtype)
                    datsp_sum = datsp.sum(axis=1, dtype=output_dtype)
                    assert_allclose(datsp_sum, dat_sum)
                    assert_equal(datsp_sum.dtype, dat_sum.dtype)

    def test_sum_dtype_fractional_to_int(self):
        # Test sum with dtype=int on data of float input dtype
        # to ensure elements are cast before summing
        datsp = self.spcreator([[0.6, 0.7],
                          [0.8, 0.9]])
        
        correct_res = array([0, 0])
        t_correct_res = np.transpose(correct_res)
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
            if isinstance(datsp, spmatrix):
                correct_res = np.matrix(correct_res)
            
        assert_equal(datsp.sum(dtype=int), 0) # Check axis=None
        assert_equal(datsp.sum(axis=0, dtype=int), correct_res) # Check axis=0
        assert_equal(datsp.sum(axis=1, dtype=int), t_correct_res) # Check axis=1

    def test_sum_out(self):
        keep = not self.is_array_test
        dat = array([[0, 1, 2],
                     [3, -4, 5],
                     [-6, 7, 9]])
        datsp = self.spcreator(dat)

        dat_out = array(0) if self.is_array_test else array([[0]])
        datsp_out = array(0) if self.is_array_test else matrix([[0]])

        dat.sum(out=dat_out, keepdims=keep)
        datsp.sum(out=datsp_out)
        assert_array_almost_equal(dat_out, datsp_out)

        dat_out = np.zeros((3,)) if self.is_array_test else np.zeros((3, 1))

Review comment (suggestion):
I think this will be sufficient. We don't need to use the deprecated np.matrix object.
```
        correct_res = array([0, 0] if self.is_array_test else [[0, 0]])
        t_correct_res = array([0, 0] if self.is_array_test else [[0], [0]])
```

Code after:
                # Check axis=None
                with np.errstate(over='ignore'):
                    dat_sum = dat.sum(dtype=output_dtype)
                    datsp_sum = datsp.sum(dtype=output_dtype)
                    assert_allclose(datsp_sum, dat_sum)
                    assert_equal(datsp_sum.dtype, dat_sum.dtype)

                    # Check axis=0
                    dat_sum = dat.sum(axis=0, dtype=output_dtype)
                    datsp_sum = datsp.sum(axis=0, dtype=output_dtype)
                    assert_allclose(datsp_sum, dat_sum)
                    assert_equal(datsp_sum.dtype, dat_sum.dtype)

                    # Check axis=1
                    dat_sum = dat.sum(axis=1, dtype=output_dtype)
                    datsp_sum = datsp.sum(axis=1, dtype=output_dtype)
                    assert_allclose(datsp_sum, dat_sum)
                    assert_equal(datsp_sum.dtype, dat_sum.dtype)

    def test_sum_dtype_fractional_to_int(self):
        # Test sum with dtype=int on data of float input dtype
        # to ensure elements are cast before summing
        datsp = self.spcreator([[0.6, 0.7],
                          [0.8, 0.9]])
        
        correct_res = array([0, 0] if self.is_array_test else [[0, 0]])
        t_correct_res = array([0, 0] if self.is_array_test else [[0], [0]])
            
        assert_equal(datsp.sum(dtype=int), 0) # Check axis=None
        assert_equal(datsp.sum(axis=0, dtype=int), correct_res) # Check axis=0
        assert_equal(datsp.sum(axis=1, dtype=int), t_correct_res) # Check axis=1

    def test_sum_out(self):
        keep = not self.is_array_test
        dat = array([[0, 1, 2],
                     [3, -4, 5],
                     [-6, 7, 9]])
        datsp = self.spcreator(dat)

        dat_out = array(0) if self.is_array_test else array([[0]])
        datsp_out = array(0) if self.is_array_test else matrix([[0]])

        dat.sum(out=dat_out, keepdims=keep)
        datsp.sum(out=datsp_out)
        assert_array_almost_equal(dat_out, datsp_out)

        dat_out = np.zeros((3,)) if self.is_array_test else np.zeros((3, 1))
        datsp_out = np.zeros((3,)) if self.is_array_test else matrix(np.zeros((3, 1)))

        dat.sum(axis=1, out=dat_out, keepdims=keep)
        datsp.sum(axis=1, out=datsp_out)