From 3297bb24a9ad50aa20345ca5b58806ac782a9c36 Mon Sep 17 00:00:00 2001 From: Kian Eliasi Date: Mon, 21 Mar 2022 03:31:59 +0330 Subject: [PATCH] Remove unnecessary numpy import (#9798) Fix #9726 --- AUTHORS | 1 + changelog/9726.bugfix.rst | 1 + src/_pytest/python_api.py | 3 +-- testing/python/approx.py | 32 +++++++++++++++++--------------- 4 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 changelog/9726.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 1c82b006de1..69e71fa10d4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -185,6 +185,7 @@ Katerina Koukiou Keri Volans Kevin Cox Kevin J. Foley +Kian Eliasi Kian-Meng Ang Kodi B. Arfer Kojo Idrissa diff --git a/changelog/9726.bugfix.rst b/changelog/9726.bugfix.rst new file mode 100644 index 00000000000..66c04c4d90c --- /dev/null +++ b/changelog/9726.bugfix.rst @@ -0,0 +1 @@ +An unnecessary ``numpy`` import inside :func:`pytest.approx` was removed. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index be15761339e..5fa21961924 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -319,7 +319,6 @@ def __repr__(self) -> str: def _repr_compare(self, other_side: Sequence[float]) -> List[str]: import math - import numpy as np if len(self.expected) != len(other_side): return [ @@ -340,7 +339,7 @@ def _repr_compare(self, other_side: Sequence[float]) -> List[str]: abs_diff = abs(approx_value.expected - other_value) max_abs_diff = max(max_abs_diff, abs_diff) if other_value == 0.0: - max_rel_diff = np.inf + max_rel_diff = math.inf else: max_rel_diff = max(max_rel_diff, abs_diff / abs(other_value)) different_ids.append(i) diff --git a/testing/python/approx.py b/testing/python/approx.py index 2eec4e9f7d0..7b4fbad156e 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -92,9 +92,7 @@ def do_assert(lhs, rhs, expected_message, verbosity_level=0): class TestApprox: - def test_error_messages(self, assert_approx_raises_regex): - np = pytest.importorskip("numpy") - + def test_error_messages_native_dtypes(self, assert_approx_raises_regex): assert_approx_raises_regex( 2.0, 1.0, @@ -135,6 +133,22 @@ def test_error_messages(self, assert_approx_raises_regex): ], ) + # Specific test for comparison with 0.0 (relative diff will be 'inf') + assert_approx_raises_regex( + [0.0], + [1.0], + [ + r" comparison failed. Mismatched elements: 1 / 1:", + rf" Max absolute difference: {SOME_FLOAT}", + r" Max relative difference: inf", + r" Index \| Obtained\s+\| Expected ", + rf"\s*0\s*\| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}", + ], + ) + + def test_error_messages_numpy_dtypes(self, assert_approx_raises_regex): + np = pytest.importorskip("numpy") + a = np.linspace(0, 100, 20) b = np.linspace(0, 100, 20) a[10] += 0.5 @@ -175,18 +189,6 @@ def test_error_messages(self, assert_approx_raises_regex): ) # Specific test for comparison with 0.0 (relative diff will be 'inf') - assert_approx_raises_regex( - [0.0], - [1.0], - [ - r" comparison failed. Mismatched elements: 1 / 1:", - rf" Max absolute difference: {SOME_FLOAT}", - r" Max relative difference: inf", - r" Index \| Obtained\s+\| Expected ", - rf"\s*0\s*\| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}", - ], - ) - assert_approx_raises_regex( np.array([0.0]), np.array([1.0]),