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

Code before:
                     [-6, 7, 9]])
        datsp = self.spcreator(dat)

        def check(dtype):
            dat_sum = dat.sum(dtype=dtype)
            datsp_sum = datsp.sum(dtype=dtype)

            assert_array_almost_equal(dat_sum, datsp_sum)
            assert_equal(dat_sum.dtype, datsp_sum.dtype)

        for dtype in self.checked_dtypes:
            check(dtype)

    def test_sum_dtype_pair_of_dtypes(self):
        # Test sum with dtype on data of various input dtypes 
        # to ensure elements are cast before summing
        base_dat = array([[0.6, 0.7],
                          [0.8, 0.9]])

        for input_dtype in self.checked_dtypes:
            dat = base_dat.astype(input_dtype)
            datsp = self.spcreator(dat)

            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
                if isinstance(datsp, spmatrix):
                    dat = np.matrix(dat)

            for output_dtype in self.checked_dtypes:
                # Skip problematic dtype combinations (complex to real, etc.)
                # by catching exceptions when numpy itself fails
                try:
                    with np.errstate(all='raise'):
                        _ = dat.sum(dtype=output_dtype)
                except Exception:
                    # Skip this combination if NumPy can't handle it
                    continue
                
                # 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)


Review comment (suggestion):
```suggestion
                if not self.is_array_test:
```

Code after:
                     [-6, 7, 9]])
        datsp = self.spcreator(dat)

        def check(dtype):
            dat_sum = dat.sum(dtype=dtype)
            datsp_sum = datsp.sum(dtype=dtype)

            assert_array_almost_equal(dat_sum, datsp_sum)
            assert_equal(dat_sum.dtype, datsp_sum.dtype)

        for dtype in self.checked_dtypes:
            check(dtype)

    def test_sum_dtype_pair_of_dtypes(self):
        # Test sum with dtype on data of various input dtypes 
        # to ensure elements are cast before summing
        base_dat = array([[0.6, 0.7],
                          [0.8, 0.9]])

        for input_dtype in self.checked_dtypes:
            dat = base_dat.astype(input_dtype)
            datsp = self.spcreator(dat)

            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
                if not self.is_array_test:
                    dat = np.matrix(dat)

            for output_dtype in self.checked_dtypes:
                # Skip problematic dtype combinations (complex to real, etc.)
                # by catching exceptions when numpy itself fails
                try:
                    with np.errstate(all='raise'):
                        _ = dat.sum(dtype=output_dtype)
                except Exception:
                    # Skip this combination if NumPy can't handle it
                    continue
                
                # 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)