Remove unnecessary numpy import (#9798)

Fix #9726
This commit is contained in:
Kian Eliasi 2022-03-21 03:31:59 +03:30 committed by GitHub
parent 6a6a32ceca
commit 3297bb24a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 17 deletions

View File

@ -185,6 +185,7 @@ Katerina Koukiou
Keri Volans Keri Volans
Kevin Cox Kevin Cox
Kevin J. Foley Kevin J. Foley
Kian Eliasi
Kian-Meng Ang Kian-Meng Ang
Kodi B. Arfer Kodi B. Arfer
Kojo Idrissa Kojo Idrissa

View File

@ -0,0 +1 @@
An unnecessary ``numpy`` import inside :func:`pytest.approx` was removed.

View File

@ -319,7 +319,6 @@ class ApproxSequenceLike(ApproxBase):
def _repr_compare(self, other_side: Sequence[float]) -> List[str]: def _repr_compare(self, other_side: Sequence[float]) -> List[str]:
import math import math
import numpy as np
if len(self.expected) != len(other_side): if len(self.expected) != len(other_side):
return [ return [
@ -340,7 +339,7 @@ class ApproxSequenceLike(ApproxBase):
abs_diff = abs(approx_value.expected - other_value) abs_diff = abs(approx_value.expected - other_value)
max_abs_diff = max(max_abs_diff, abs_diff) max_abs_diff = max(max_abs_diff, abs_diff)
if other_value == 0.0: if other_value == 0.0:
max_rel_diff = np.inf max_rel_diff = math.inf
else: else:
max_rel_diff = max(max_rel_diff, abs_diff / abs(other_value)) max_rel_diff = max(max_rel_diff, abs_diff / abs(other_value))
different_ids.append(i) different_ids.append(i)

View File

@ -92,9 +92,7 @@ SOME_INT = r"[0-9]+\s*"
class TestApprox: class TestApprox:
def test_error_messages(self, assert_approx_raises_regex): def test_error_messages_native_dtypes(self, assert_approx_raises_regex):
np = pytest.importorskip("numpy")
assert_approx_raises_regex( assert_approx_raises_regex(
2.0, 2.0,
1.0, 1.0,
@ -135,6 +133,22 @@ class TestApprox:
], ],
) )
# 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) a = np.linspace(0, 100, 20)
b = np.linspace(0, 100, 20) b = np.linspace(0, 100, 20)
a[10] += 0.5 a[10] += 0.5
@ -175,18 +189,6 @@ class TestApprox:
) )
# Specific test for comparison with 0.0 (relative diff will be 'inf') # 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( assert_approx_raises_regex(
np.array([0.0]), np.array([0.0]),
np.array([1.0]), np.array([1.0]),