From 1f27474e36669c77eee7a1cfe34c1453e81c5a6c Mon Sep 17 00:00:00 2001 From: malangsiddharth Date: Tue, 14 Jan 2025 19:41:41 +0530 Subject: [PATCH 1/2] Fix issue #13047: Handle numpy booleans in pytest.approx --- src/_pytest/python_api.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 25cf9f04d61..748974e6ad1 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -21,6 +21,7 @@ from typing import TypeVar import _pytest._code +import numpy as np from _pytest.outcomes import fail @@ -438,7 +439,7 @@ def __eq__(self, actual) -> bool: return all(self.__eq__(a) for a in asarray.flat) # Short-circuit exact equality, except for bool - if isinstance(self.expected, bool) and not isinstance(actual, bool): + if isinstance(self.expected, (bool, np.bool_)) and not isinstance(actual, (bool, np.bool_)): return False elif actual == self.expected: return True @@ -447,9 +448,9 @@ def __eq__(self, actual) -> bool: # NB: we need Complex, rather than just Number, to ensure that __abs__, # __sub__, and __float__ are defined. Also, consider bool to be # nonnumeric, even though it has the required arithmetic. - if isinstance(self.expected, bool) or not ( - isinstance(self.expected, (Complex, Decimal)) - and isinstance(actual, (Complex, Decimal)) + if isinstance(self.expected, (bool, np.bool_)) or not ( + isinstance(self.expected, (Complex, Decimal)) + and isinstance(actual, (Complex, Decimal)) ): return False From 37196fbdca669d7f817b296ed2a2c857b1803d1a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:17:10 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/python_api.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 748974e6ad1..28d85821eb9 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -20,8 +20,9 @@ from typing import TYPE_CHECKING from typing import TypeVar -import _pytest._code import numpy as np + +import _pytest._code from _pytest.outcomes import fail @@ -439,7 +440,9 @@ def __eq__(self, actual) -> bool: return all(self.__eq__(a) for a in asarray.flat) # Short-circuit exact equality, except for bool - if isinstance(self.expected, (bool, np.bool_)) and not isinstance(actual, (bool, np.bool_)): + if isinstance(self.expected, (bool, np.bool_)) and not isinstance( + actual, (bool, np.bool_) + ): return False elif actual == self.expected: return True @@ -449,8 +452,8 @@ def __eq__(self, actual) -> bool: # __sub__, and __float__ are defined. Also, consider bool to be # nonnumeric, even though it has the required arithmetic. if isinstance(self.expected, (bool, np.bool_)) or not ( - isinstance(self.expected, (Complex, Decimal)) - and isinstance(actual, (Complex, Decimal)) + isinstance(self.expected, (Complex, Decimal)) + and isinstance(actual, (Complex, Decimal)) ): return False