Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve approx scalar repr method for better readability #12665

Merged
merged 17 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions changelog/6985.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Improved the `ApproxScalar` class to enhance the readability of the `repr` method for intermediate ranges of values and tolerances.
fazeelghafoor marked this conversation as resolved.
Show resolved Hide resolved
* The `repr` method now provides clearer output for values within intermediate ranges, making it easier to interpret the results.
fazeelghafoor marked this conversation as resolved.
Show resolved Hide resolved
* Previously, the output for intermediate ranges of values and tolerances was displayed in scientific notation (e.g., `42 ± 1.0e+00`). The updated method now presents the tolerance as a decimal for better readability (e.g., `42 ± 1`).
fazeelghafoor marked this conversation as resolved.
Show resolved Hide resolved
Example:
**Previous Output:**
.. code-block:: console
>>> pytest.approx(42, abs=1)
42 ± 1.0e+00

**Current Output:**
.. code-block:: console
>>> pytest.approx(42, abs=1)
42 ± 1
fazeelghafoor marked this conversation as resolved.
Show resolved Hide resolved

-- by :user:`fazeelghafoor`
6 changes: 5 additions & 1 deletion src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@
# If a sensible tolerance can't be calculated, self.tolerance will
# raise a ValueError. In this case, display '???'.
try:
vetted_tolerance = f"{self.tolerance:.1e}"
if self.tolerance >= 1e-3 and self.tolerance < 1e3:
fazeelghafoor marked this conversation as resolved.
Show resolved Hide resolved
vetted_tolerance = f"{self.tolerance:n}"

Check warning on line 410 in src/_pytest/python_api.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/python_api.py#L410

Added line #L410 was not covered by tests
else:
vetted_tolerance = f"{self.tolerance:.1e}"

Check warning on line 412 in src/_pytest/python_api.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/python_api.py#L412

Added line #L412 was not covered by tests

if (
isinstance(self.expected, Complex)
and self.expected.imag
Expand Down
16 changes: 11 additions & 5 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def do_assert(lhs, rhs, expected_message, verbosity_level=0):

SOME_FLOAT = r"[+-]?([0-9]*[.])?[0-9]+\s*"
SOME_INT = r"[0-9]+\s*"
SOME_TOLERANCE = rf"({SOME_FLOAT}|[+-]?[0-9]+(\.[0-9]+)?[eE][+-]?[0-9]+\s*)"


class TestApprox:
Expand All @@ -103,7 +104,7 @@ def test_error_messages_native_dtypes(self, assert_approx_raises_regex):
"",
" comparison failed",
f" Obtained: {SOME_FLOAT}",
f" Expected: {SOME_FLOAT} ± {SOME_FLOAT}",
f" Expected: {SOME_FLOAT} ± {SOME_TOLERANCE}",
],
)

Expand All @@ -119,9 +120,9 @@ def test_error_messages_native_dtypes(self, assert_approx_raises_regex):
r" comparison failed. Mismatched elements: 2 / 3:",
rf" Max absolute difference: {SOME_FLOAT}",
rf" Max relative difference: {SOME_FLOAT}",
r" Index \| Obtained\s+\| Expected ",
rf" a \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}",
rf" c \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}",
r" Index \| Obtained\s+\| Expected\s+",
rf" a \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_TOLERANCE}",
rf" c \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_TOLERANCE}",
],
)

Expand Down Expand Up @@ -334,6 +335,11 @@ def test_repr_string(self):
"approx({'b': 2.0 ± 2.0e-06, 'a': 1.0 ± 1.0e-06})",
)

assert repr(approx(42, abs=1)) == "42 ± 1"
assert repr(approx(5, rel=0.01)) == "5 ± 0.05"
assert repr(approx(24000, abs=500)) == "24000 ± 500"
assert repr(approx(1500, abs=555)) == "1500 ± 555"

def test_repr_complex_numbers(self):
assert repr(approx(inf + 1j)) == "(inf+1j)"
assert repr(approx(1.0j, rel=inf)) == "1j ± inf"
Expand All @@ -347,7 +353,7 @@ def test_repr_complex_numbers(self):
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) ± 2.0e-02 ∠ ±180°"
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",
Expand Down
Loading