Repository: pytest-dev/pytest
File: testing/python/approx.py (Python)

Code before:
    def test_numpy_scalar_with_array(self):
        np = pytest.importorskip("numpy")

        actual = 1.0
        expected = np.array([1 + 1e-7, 1 - 1e-8])

        assert actual == approx(expected, rel=5e-7, abs=0)
        assert actual != approx(expected, rel=5e-8, abs=0)
        assert approx(expected, rel=5e-7, abs=0) == actual
        assert approx(expected, rel=5e-8, abs=0) != actual

    def test_generic_ordered_sequence(self):
        class MySequence:
            def __getitem__(self, i):
                return [1, 2, 3, 4][i]

            def __len__(self):
                return 4

        expected = MySequence()
        assert [1, 2, 3, 4] == approx(expected, abs=1e-4)

        expected_repr = "approx([1 ± 1.0e-06, 2 ± 2.0e-06, 3 ± 3.0e-06, 4 ± 4.0e-06])"
        assert repr(approx(expected)) == expected_repr

    def test_decimal_approx_repr(self, monkeypatch):
        monkeypatch.setitem(decimal.getcontext().traps, decimal.FloatOperation, True)
        approx_obj = pytest.approx(decimal.Decimal("2.60"))
        print(f"Attempting to represent pytest.approx(Decimal): {approx_obj}")
        assert decimal.Decimal("2.600001") == approx_obj

    def test_allow_ordered_sequences_only(self) -> None:
        """pytest.approx() should raise an error on unordered sequences (#9692)."""
        with pytest.raises(TypeError, match="only supports ordered sequences"):
            assert {1, 2, 3} == approx({1, 2, 3})

    def test_strange_sequence(self):
        """https://github.com/pytest-dev/pytest/issues/11797"""
        a = MyVec3(1, 2, 3)
        b = MyVec3(0, 1, 2)

        # this would trigger the error inside the test
        pytest.approx(a, abs=0.5)._repr_compare(b)

        assert b == pytest.approx(a, abs=2)
        assert b != pytest.approx(a, abs=0.5)


class MyVec3:  # incomplete
    """sequence like"""


Review comment (suggestion):
```suggestion
    def test_decimal_approx_repr(self, monkeypatch) -> None:
```

Code after:

    def test_numpy_scalar_with_array(self):
        np = pytest.importorskip("numpy")

        actual = 1.0
        expected = np.array([1 + 1e-7, 1 - 1e-8])

        assert actual == approx(expected, rel=5e-7, abs=0)
        assert actual != approx(expected, rel=5e-8, abs=0)
        assert approx(expected, rel=5e-7, abs=0) == actual
        assert approx(expected, rel=5e-8, abs=0) != actual

    def test_generic_ordered_sequence(self):
        class MySequence:
            def __getitem__(self, i):
                return [1, 2, 3, 4][i]

            def __len__(self):
                return 4

        expected = MySequence()
        assert [1, 2, 3, 4] == approx(expected, abs=1e-4)

        expected_repr = "approx([1 ± 1.0e-06, 2 ± 2.0e-06, 3 ± 3.0e-06, 4 ± 4.0e-06])"
        assert repr(approx(expected)) == expected_repr

    def test_decimal_approx_repr(self, monkeypatch) -> None:
        monkeypatch.setitem(decimal.getcontext().traps, decimal.FloatOperation, True)
        approx_obj = pytest.approx(decimal.Decimal("2.60"))
        assert decimal.Decimal("2.600001") == approx_obj

    def test_allow_ordered_sequences_only(self) -> None:
        """pytest.approx() should raise an error on unordered sequences (#9692)."""
        with pytest.raises(TypeError, match="only supports ordered sequences"):
            assert {1, 2, 3} == approx({1, 2, 3})

    def test_strange_sequence(self):
        """https://github.com/pytest-dev/pytest/issues/11797"""
        a = MyVec3(1, 2, 3)
        b = MyVec3(0, 1, 2)

        # this would trigger the error inside the test
        pytest.approx(a, abs=0.5)._repr_compare(b)

        assert b == pytest.approx(a, abs=2)
        assert b != pytest.approx(a, abs=0.5)


class MyVec3:  # incomplete
    """sequence like"""