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

Code before:
        # absolute tolerance is not scaled
        assert repr(approx(3.3 + 4.4 * 1j, abs=0.02)) == "(3.3+4.4j) ± 0.02 ∠ ±180°"

    @pytest.mark.parametrize(
        "value, expected_repr_string",
        [
            (5.0, "approx(5.0 ± 5.0e-06)"),
            ([5.0], "approx([5.0 ± 5.0e-06])"),
            ([[5.0]], "approx([[5.0 ± 5.0e-06]])"),
            ([[5.0, 6.0]], "approx([[5.0 ± 5.0e-06, 6.0 ± 6.0e-06]])"),
            ([[5.0], [6.0]], "approx([[5.0 ± 5.0e-06], [6.0 ± 6.0e-06]])"),
        ],
    )
    def test_repr_nd_array(self, value, expected_repr_string):
        """Make sure that arrays of all different dimensions are repr'd correctly."""
        np = pytest.importorskip("numpy")
        np_array = np.array(value)
        assert repr(approx(np_array)) == expected_repr_string

    def test_bool(self):
        with pytest.raises(AssertionError) as err:
            assert approx(1)

        assert err.match(r"approx\(\) is not supported in a boolean context")

    def test_list_with_str(self, assert_approx_raises_regex):
        assert_approx_raises_regex(
            [1.1, 2, "word"],
            [1.0, 2, "different"],
            [
                "",
                r"  comparison failed. Mismatched elements: 2 / 3:",
                rf"  Max absolute difference: {SOME_FLOAT}",
                rf"  Max relative difference: {SOME_FLOAT}",
                r"  Index \| Obtained\s+\| Expected\s+",
                r"\s*0\s*\|\s*1\.1\s*\|\s*1\.0\s*±\s*1\.0e\-06\s*",
                r"\s*2\s*\|\s*word\s*\|\s*different\s*",
            ],
            verbosity_level=2,
        )

    def test_operator_overloading(self):
        assert 1 == approx(1, rel=1e-6, abs=1e-12)
        assert not (1 != approx(1, rel=1e-6, abs=1e-12))
        assert 10 != approx(1, rel=1e-6, abs=1e-12)
        assert not (10 == approx(1, rel=1e-6, abs=1e-12))

    def test_exactly_equal(self):
        examples = [
            (2.0, 2.0),
            (0.1e200, 0.1e200),

Review comment (suggestion):
```suggestion
    def test_list_with_str(self, assert_approx_raises_regex) -> None:
        """approx should work on sequences that also contain non-numbers (#13010)."""
```

Code after:

    def test_repr_complex_numbers(self):
        assert repr(approx(inf + 1j)) == "(inf+1j)"
        assert repr(approx(1.0j, rel=inf)) == "1j ± inf"

        # can't compute a sensible tolerance
        assert repr(approx(nan + 1j)) == "(nan+1j) ± ???"

        assert repr(approx(1.0j)) == "1j ± 1.0e-06 ∠ ±180°"

        # relative tolerance is scaled to |3+4j| = 5
        assert repr(approx(3 + 4 * 1j)) == "(3+4j) ± 5.0e-06 ∠ ±180°"

        # absolute tolerance is not scaled
        assert repr(approx(3.3 + 4.4 * 1j, abs=0.02)) == "(3.3+4.4j) ± 0.02 ∠ ±180°"

    @pytest.mark.parametrize(
        "value, expected_repr_string",
        [
            (5.0, "approx(5.0 ± 5.0e-06)"),
            ([5.0], "approx([5.0 ± 5.0e-06])"),
            ([[5.0]], "approx([[5.0 ± 5.0e-06]])"),
            ([[5.0, 6.0]], "approx([[5.0 ± 5.0e-06, 6.0 ± 6.0e-06]])"),
            ([[5.0], [6.0]], "approx([[5.0 ± 5.0e-06], [6.0 ± 6.0e-06]])"),
        ],
    )
    def test_repr_nd_array(self, value, expected_repr_string):
        """Make sure that arrays of all different dimensions are repr'd correctly."""
        np = pytest.importorskip("numpy")
        np_array = np.array(value)
        assert repr(approx(np_array)) == expected_repr_string

    def test_bool(self):
        with pytest.raises(AssertionError) as err:
            assert approx(1)

        assert err.match(r"approx\(\) is not supported in a boolean context")

    def test_mixed_sequence(self, assert_approx_raises_regex) -> None:
        """Approx should work on sequences that also contain non-numbers (#13010)."""
        assert_approx_raises_regex(
            [1.1, 2, "word"],
            [1.0, 2, "different"],
            [
                "",
                r"  comparison failed. Mismatched elements: 2 / 3:",
                rf"  Max absolute difference: {SOME_FLOAT}",
                rf"  Max relative difference: {SOME_FLOAT}",
                r"  Index \| Obtained\s+\| Expected\s+",
                r"\s*0\s*\|\s*1\.1\s*\|\s*1\.0\s*±\s*1\.0e\-06\s*",
                r"\s*2\s*\|\s*word\s*\|\s*different\s*",