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

Code before:
        y = np.isclose(x, x, equal_nan=True)
        assert_(type(x) is type(y))
        # Ensure that the mask isn't modified...
        assert_array_equal([True, True, False], y.mask)

    def test_scalar_return(self):
        assert_(np.isscalar(np.isclose(1, 1)))

    def test_no_parameter_modification(self):
        x = np.array([np.inf, 1])
        y = np.array([0, np.inf])
        np.isclose(x, y)
        assert_array_equal(x, np.array([np.inf, 1]))
        assert_array_equal(y, np.array([0, np.inf]))

    def test_non_finite_scalar(self):
        # GH7014, when two scalars are compared the output should also be a
        # scalar
        assert_(np.isclose(np.inf, -np.inf) is np.False_)
        assert_(np.isclose(0, np.inf) is np.False_)
        assert_(type(np.isclose(0, np.inf)) is np.bool)

    def test_timedelta(self):
        # Allclose currently works for timedelta64 as long as `atol` is
        # an integer or also a timedelta64
        a = np.array([[1, 2, 3, "NaT"]], dtype="m8[ns]")
        assert np.isclose(a, a, atol=0, equal_nan=True).all()
        assert np.isclose(a, a, atol=np.timedelta64(1, "ns"), equal_nan=True).all()
        assert np.allclose(a, a, atol=0, equal_nan=True)
        assert np.allclose(a, a, atol=np.timedelta64(1, "ns"), equal_nan=True)

    def test_tol_warnings(self):
        a = np.array([1, 2, 3])
        #Testing when rtol is infinite
        with warnings.catch_warnings(record=True) as w:

            warnings.simplefilter("always")
            b = np.isclose(a, a, rtol=np.inf)
            assert len(w) == 1
            assert issubclass(w[-1].category, UserWarning)
            assert "At least one of rtol and atol are infinite" in str(w[-1].message)
        #Testing when atol is infinite
        with warnings.catch_warnings(record=True) as w:

            warnings.simplefilter("always")
            b = np.isclose(a, a, atol=np.inf)
            assert len(w) == 1
            assert issubclass(w[-1].category, UserWarning)
            assert "At least one of rtol and atol are infinite" in str(w[-1].message)
        #Testing when both are infinite
        with warnings.catch_warnings(record=True) as w:

Review comment (bug):
There’s a lot of copy/paste here. Consider using a for loop or pytest parameterization so reviewers don’t have to carefully make sure all the repeated code is bug-free.

Code after:
        # Allclose currently works for timedelta64 as long as `atol` is
        # an integer or also a timedelta64
        a = np.array([[1, 2, 3, "NaT"]], dtype="m8[ns]")
        assert np.isclose(a, a, atol=0, equal_nan=True).all()
        assert np.isclose(a, a, atol=np.timedelta64(1, "ns"), equal_nan=True).all()
        assert np.allclose(a, a, atol=0, equal_nan=True)
        assert np.allclose(a, a, atol=np.timedelta64(1, "ns"), equal_nan=True)

    def test_tol_warnings(self):
        a = np.array([1, 2, 3])
        b = np.array([np.inf, np.nan, 1])

        for i in b:
            for j in b:
                # Making sure that i and j are not both numbers, because that won't create a warning
                if (i == 1) and (j == 1):
                    continue
                
                with warnings.catch_warnings(record=True) as w:

                    warnings.simplefilter("always")
                    c = np.isclose(a, a, atol=i, rtol=j)
                    assert len(w) == 1
                    assert issubclass(w[-1].category, RuntimeWarning)
                    assert f"One of rtol or atol is not valid, atol: {i}, rtol: {j}" in str(w[-1].message)


class TestStdVar:
    def setup_method(self):
        self.A = np.array([1, -1, 1, -1])
        self.real_var = 1

    def test_basic(self):
        assert_almost_equal(np.var(self.A), self.real_var)
        assert_almost_equal(np.std(self.A)**2, self.real_var)

    def test_scalars(self):
        assert_equal(np.var(1), 0)
        assert_equal(np.std(1), 0)

    def test_ddof1(self):
        assert_almost_equal(np.var(self.A, ddof=1),
                            self.real_var * len(self.A) / (len(self.A) - 1))
        assert_almost_equal(np.std(self.A, ddof=1)**2,
                            self.real_var * len(self.A) / (len(self.A) - 1))

    def test_ddof2(self):
        assert_almost_equal(np.var(self.A, ddof=2),
                            self.real_var * len(self.A) / (len(self.A) - 2))
        assert_almost_equal(np.std(self.A, ddof=2)**2,
                            self.real_var * len(self.A) / (len(self.A) - 2))