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 5 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
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 @@ def __repr__(self) -> str:
# 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}"
else:
vetted_tolerance = f"{self.tolerance:.1e}"

if (
isinstance(self.expected, Complex)
and self.expected.imag
Expand Down
4 changes: 2 additions & 2 deletions src/_pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
del sys.last_value
del sys.last_traceback
if sys.version_info >= (3, 12, 0):
del sys.last_exc
del sys.last_exc # type: ignore[attr-defined]

Check warning on line 170 in src/_pytest/runner.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/runner.py#L170

Added line #L170 was not covered by tests
except AttributeError:
pass
try:
Expand All @@ -177,7 +177,7 @@
sys.last_type = type(e)
sys.last_value = e
if sys.version_info >= (3, 12, 0):
sys.last_exc = e
sys.last_exc = e # type: ignore[attr-defined]

Check warning on line 180 in src/_pytest/runner.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/runner.py#L180

Added line #L180 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this type ignores are unused in the CI (see pre-commit job).

assert e.__traceback__ is not None
# Skip *this* frame
sys.last_traceback = e.__traceback__.tb_next
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