From 51ddeafc30fae2c93afd0f0e08cee72cbec98418 Mon Sep 17 00:00:00 2001 From: fazeelghafoor Date: Mon, 29 Jul 2024 06:25:37 +0500 Subject: [PATCH 01/15] resolve mypy error: Module has no attribute last_exc [attr-defined] --- src/_pytest/runner.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 716c4948f4a..e4d352cf777 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -167,7 +167,7 @@ def pytest_runtest_call(item: Item) -> None: 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] except AttributeError: pass try: @@ -177,7 +177,7 @@ def pytest_runtest_call(item: Item) -> None: 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] assert e.__traceback__ is not None # Skip *this* frame sys.last_traceback = e.__traceback__.tb_next From 82580cb032943037c8c07745a8fde13d32ecbfc3 Mon Sep 17 00:00:00 2001 From: fazeelghafoor Date: Mon, 29 Jul 2024 06:26:03 +0500 Subject: [PATCH 02/15] adjust __repr__ in ApproxScalar to format tolerances in decimal form for common ranges --- src/_pytest/python_api.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 4174a55b589..3b024736d55 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -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: + vetted_tolerance = f"{self.tolerance}" + else: + vetted_tolerance = f"{self.tolerance:.1e}" + if ( isinstance(self.expected, Complex) and self.expected.imag From 7c1a6dfc27f2d5b8534d501568feace02c3e2409 Mon Sep 17 00:00:00 2001 From: fazeelghafoor Date: Tue, 30 Jul 2024 01:03:10 +0500 Subject: [PATCH 03/15] add 'n' format specifier for readable tolerance numbers --- src/_pytest/python_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 3b024736d55..4474b23e5fa 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -407,7 +407,7 @@ def __repr__(self) -> str: # raise a ValueError. In this case, display '???'. try: if self.tolerance >= 1e-3 and self.tolerance < 1e3: - vetted_tolerance = f"{self.tolerance}" + vetted_tolerance = f"{self.tolerance:n}" else: vetted_tolerance = f"{self.tolerance:.1e}" From 738592d76c256071ec55e3ff3c626cf1a4c2d3c5 Mon Sep 17 00:00:00 2001 From: fazeelghafoor Date: Tue, 30 Jul 2024 03:29:57 +0500 Subject: [PATCH 04/15] add tests for improved tolerance approx repr output --- testing/python/approx.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/testing/python/approx.py b/testing/python/approx.py index 69743cdbe17..0b7855666cd 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -334,6 +334,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" From c71e990a20827256e2208eb7759d97b7616c5b6f Mon Sep 17 00:00:00 2001 From: fazeelghafoor Date: Wed, 31 Jul 2024 04:56:52 +0500 Subject: [PATCH 05/15] update existing tests for improved approx scalar repr output --- testing/python/approx.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/testing/python/approx.py b/testing/python/approx.py index 0b7855666cd..21932059cc6 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -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: @@ -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}", ], ) @@ -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}", ], ) @@ -352,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", From 228b3564ed7b61ae014c6b2305afe18b249e8d2d Mon Sep 17 00:00:00 2001 From: fazeelghafoor Date: Wed, 31 Jul 2024 16:35:57 +0500 Subject: [PATCH 06/15] add changelog --- doc/en/changelog.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/doc/en/changelog.rst b/doc/en/changelog.rst index 083d30abf86..ddd447879fa 100644 --- a/doc/en/changelog.rst +++ b/doc/en/changelog.rst @@ -31,6 +31,28 @@ with advance notice in the **Deprecations** section of releases. .. towncrier release notes start +pytest 8.3.2 (2024-07-31) +========================= +Improvements in existing functionality +-------------------------------------- + +- `#6985 `_: Improved the `ApproxScalar` class to enhance the readability of the `repr` method for intermediate ranges of values and tolerances. + * The `repr` method now provides clearer output for values within intermediate ranges, making it easier to interpret the results. + * 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`). + 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 + + -- by :user:`fazeelghafoor` + + pytest 8.3.1 (2024-07-20) ========================= From 42e80e7c150d7fd781905a80c470d5248905ed70 Mon Sep 17 00:00:00 2001 From: fazeelghafoor Date: Wed, 31 Jul 2024 16:37:26 +0500 Subject: [PATCH 07/15] remove type ignores --- src/_pytest/runner.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index e4d352cf777..716c4948f4a 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -167,7 +167,7 @@ def pytest_runtest_call(item: Item) -> None: del sys.last_value del sys.last_traceback if sys.version_info >= (3, 12, 0): - del sys.last_exc # type: ignore[attr-defined] + del sys.last_exc except AttributeError: pass try: @@ -177,7 +177,7 @@ def pytest_runtest_call(item: Item) -> None: sys.last_type = type(e) sys.last_value = e if sys.version_info >= (3, 12, 0): - sys.last_exc = e # type: ignore[attr-defined] + sys.last_exc = e assert e.__traceback__ is not None # Skip *this* frame sys.last_traceback = e.__traceback__.tb_next From 549b6c7f8f16145a61ab030fd44d3bd15ddbed98 Mon Sep 17 00:00:00 2001 From: fazeelghafoor Date: Wed, 31 Jul 2024 16:55:11 +0500 Subject: [PATCH 08/15] add changelog fragment --- changelog/6985.improvement.rst | 15 +++++++++++++++ doc/en/changelog.rst | 22 ---------------------- 2 files changed, 15 insertions(+), 22 deletions(-) create mode 100644 changelog/6985.improvement.rst diff --git a/changelog/6985.improvement.rst b/changelog/6985.improvement.rst new file mode 100644 index 00000000000..fe5326c6ffa --- /dev/null +++ b/changelog/6985.improvement.rst @@ -0,0 +1,15 @@ +Improved the `ApproxScalar` class to enhance the readability of the `repr` method for intermediate ranges of values and tolerances. + * The `repr` method now provides clearer output for values within intermediate ranges, making it easier to interpret the results. + * 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`). + 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 + + -- by :user:`fazeelghafoor` diff --git a/doc/en/changelog.rst b/doc/en/changelog.rst index ddd447879fa..083d30abf86 100644 --- a/doc/en/changelog.rst +++ b/doc/en/changelog.rst @@ -31,28 +31,6 @@ with advance notice in the **Deprecations** section of releases. .. towncrier release notes start -pytest 8.3.2 (2024-07-31) -========================= -Improvements in existing functionality --------------------------------------- - -- `#6985 `_: Improved the `ApproxScalar` class to enhance the readability of the `repr` method for intermediate ranges of values and tolerances. - * The `repr` method now provides clearer output for values within intermediate ranges, making it easier to interpret the results. - * 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`). - 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 - - -- by :user:`fazeelghafoor` - - pytest 8.3.1 (2024-07-20) ========================= From d17145cb96b1acad2d5d8b348365cc00fde5b2e4 Mon Sep 17 00:00:00 2001 From: fazeelghafoor <33656455+fazeelghafoor@users.noreply.github.com> Date: Wed, 31 Jul 2024 23:44:43 +0500 Subject: [PATCH 09/15] Update changelog/6985.improvement.rst Co-authored-by: Bruno Oliveira --- changelog/6985.improvement.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/6985.improvement.rst b/changelog/6985.improvement.rst index fe5326c6ffa..3fa4650e48e 100644 --- a/changelog/6985.improvement.rst +++ b/changelog/6985.improvement.rst @@ -1,4 +1,4 @@ -Improved the `ApproxScalar` class to enhance the readability of the `repr` method for intermediate ranges of values and tolerances. +Improved :func:`pytest.approx` to enhance the readability of intermediate value ranges and tolerances. * The `repr` method now provides clearer output for values within intermediate ranges, making it easier to interpret the results. * 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`). Example: From bc1af0b4ccdf9c25de900925f38c080eb7b795f7 Mon Sep 17 00:00:00 2001 From: fazeelghafoor <33656455+fazeelghafoor@users.noreply.github.com> Date: Wed, 31 Jul 2024 23:44:54 +0500 Subject: [PATCH 10/15] Update changelog/6985.improvement.rst Co-authored-by: Bruno Oliveira --- changelog/6985.improvement.rst | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/changelog/6985.improvement.rst b/changelog/6985.improvement.rst index 3fa4650e48e..fd08d6f61e3 100644 --- a/changelog/6985.improvement.rst +++ b/changelog/6985.improvement.rst @@ -1,15 +1,21 @@ Improved :func:`pytest.approx` to enhance the readability of intermediate value ranges and tolerances. * The `repr` method now provides clearer output for values within intermediate ranges, making it easier to interpret the results. * 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`). - 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 + 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 -- by :user:`fazeelghafoor` From 90402fe9fbd0f7dbea128238ebae366df63c8ad7 Mon Sep 17 00:00:00 2001 From: fazeelghafoor <33656455+fazeelghafoor@users.noreply.github.com> Date: Wed, 31 Jul 2024 23:45:03 +0500 Subject: [PATCH 11/15] Update src/_pytest/python_api.py Co-authored-by: Bruno Oliveira --- src/_pytest/python_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 4474b23e5fa..d2107c2fc78 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -406,7 +406,7 @@ def __repr__(self) -> str: # If a sensible tolerance can't be calculated, self.tolerance will # raise a ValueError. In this case, display '???'. try: - if self.tolerance >= 1e-3 and self.tolerance < 1e3: + if 1e-3 <= self.tolerance < 1e3: vetted_tolerance = f"{self.tolerance:n}" else: vetted_tolerance = f"{self.tolerance:.1e}" From 784a953a45d93fcb29a3e8e31271c9f4f60d198a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 31 Jul 2024 18:45:59 +0000 Subject: [PATCH 12/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog/6985.improvement.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/changelog/6985.improvement.rst b/changelog/6985.improvement.rst index fd08d6f61e3..87e44b417df 100644 --- a/changelog/6985.improvement.rst +++ b/changelog/6985.improvement.rst @@ -3,18 +3,18 @@ Improved :func:`pytest.approx` to enhance the readability of intermediate value * 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`). 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 From f104f71068dd7159234f720058fee70389f3ce1f Mon Sep 17 00:00:00 2001 From: fazeelghafoor <33656455+fazeelghafoor@users.noreply.github.com> Date: Thu, 1 Aug 2024 20:08:06 +0500 Subject: [PATCH 13/15] Update changelog/6985.improvement.rst Co-authored-by: Pierre Sassoulas --- changelog/6985.improvement.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/6985.improvement.rst b/changelog/6985.improvement.rst index 87e44b417df..14eb624f6b1 100644 --- a/changelog/6985.improvement.rst +++ b/changelog/6985.improvement.rst @@ -1,4 +1,4 @@ -Improved :func:`pytest.approx` to enhance the readability of intermediate value ranges and tolerances. +Improved :func:`pytest.approx` to enhance the readability of value ranges and tolerances between 0.001 and 1000. * The `repr` method now provides clearer output for values within intermediate ranges, making it easier to interpret the results. * 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`). From 915d4c7af83da8a402ec016e3e0bfc65070b2b31 Mon Sep 17 00:00:00 2001 From: fazeelghafoor <33656455+fazeelghafoor@users.noreply.github.com> Date: Thu, 1 Aug 2024 22:07:28 +0500 Subject: [PATCH 14/15] Update changelog/6985.improvement.rst Co-authored-by: Pierre Sassoulas --- changelog/6985.improvement.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/6985.improvement.rst b/changelog/6985.improvement.rst index 14eb624f6b1..b80368873f3 100644 --- a/changelog/6985.improvement.rst +++ b/changelog/6985.improvement.rst @@ -1,5 +1,5 @@ Improved :func:`pytest.approx` to enhance the readability of value ranges and tolerances between 0.001 and 1000. - * The `repr` method now provides clearer output for values within intermediate ranges, making it easier to interpret the results. + * The `repr` method now provides clearer output for values within those ranges, making it easier to interpret the results. * 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`). Example: From e91228fb9b4ed8c5d8229d880c5f09fb854873df Mon Sep 17 00:00:00 2001 From: fazeelghafoor <33656455+fazeelghafoor@users.noreply.github.com> Date: Thu, 1 Aug 2024 22:07:36 +0500 Subject: [PATCH 15/15] Update changelog/6985.improvement.rst Co-authored-by: Pierre Sassoulas --- changelog/6985.improvement.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/6985.improvement.rst b/changelog/6985.improvement.rst index b80368873f3..34ee8edc77d 100644 --- a/changelog/6985.improvement.rst +++ b/changelog/6985.improvement.rst @@ -1,6 +1,6 @@ Improved :func:`pytest.approx` to enhance the readability of value ranges and tolerances between 0.001 and 1000. * The `repr` method now provides clearer output for values within those ranges, making it easier to interpret the results. - * 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`). + * Previously, the output for those 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`). Example: