diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index bd74ec296..9ce9c62e0 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -450,26 +450,25 @@ class ApproxScalar(ApproxBase): # for compatibility with complex numbers. if math.isinf(abs(self.expected)): # type: ignore[arg-type] return False - - tolerance = self.tolerance + + tolerance = self.tolerance expected = self.expected - - if (isinstance(self.expected, Decimal) and not isinstance(actual, Decimal)): + + if isinstance(self.expected, Decimal) and not isinstance(actual, Decimal): try: actual = Decimal(str(actual)) tolerance = Decimal(str(tolerance)) - except TypeError: # + except TypeError: # return False - elif (isinstance(actual, Decimal) and not isinstance(self.expected, Decimal)): - try : + elif isinstance(actual, Decimal) and not isinstance(self.expected, Decimal): + try: expected = Decimal(str(expected)) tolerance = Decimal(str(tolerance)) except TypeError: return False - # Return true if the two numbers are within the tolerance. - result: bool = (abs(actual - expected) <= tolerance ) # type: ignore[arg-type] + result: bool = abs(actual - expected) <= tolerance # type: ignore[arg-type] return result # Ignore type because of https://github.com/python/mypy/issues/4266. @@ -508,11 +507,11 @@ class ApproxScalar(ApproxBase): # we've made sure the user didn't ask for an absolute tolerance only, # because we don't want to raise errors about the relative tolerance if # we aren't even going to use it. - + if isinstance(self.rel, Decimal): relative_tolerance = set_default( self.rel, self.DEFAULT_RELATIVE_TOLERANCE - )*Decimal(str(abs(self.expected))) + ) * Decimal(str(abs(self.expected))) else: relative_tolerance = set_default( self.rel, self.DEFAULT_RELATIVE_TOLERANCE diff --git a/testing/python/approx.py b/testing/python/approx.py index eadaba1da..6bfd5a9bf 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -568,14 +568,14 @@ class TestApprox: expected = [Decimal("1"), Decimal("2")] assert actual == approx(expected) - + expected = [1, 2] - + assert actual == approx(expected) - + expected = [Decimal("1"), Decimal("2")] actual = [1.000001, 2.000001] - + assert actual == approx(expected) def test_list_wrong_len(self): @@ -618,14 +618,14 @@ class TestApprox: expected = {"b": Decimal("2"), "a": Decimal("1")} assert actual == approx(expected) - + actual = {"a": 1.000001, "b": 2.000001} - + assert actual == approx(expected) - + actual = {"a": Decimal("1.000001"), "b": Decimal("2.000001")} expected = {"b": 2, "a": 1} - + assert actual == approx(expected) def test_dict_wrong_len(self): @@ -902,4 +902,3 @@ class TestApprox: """pytest.approx() should raise an error on unordered sequences (#9692).""" with pytest.raises(TypeError, match="only supports ordered sequences"): assert {1, 2, 3} == approx({1, 2, 3}) - \ No newline at end of file