Skip to content

Commit 1f27474

Browse files
Fix issue pytest-dev#13047: Handle numpy booleans in pytest.approx
1 parent 517b006 commit 1f27474

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/_pytest/python_api.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import TypeVar
2222

2323
import _pytest._code
24+
import numpy as np
2425
from _pytest.outcomes import fail
2526

2627

@@ -438,7 +439,7 @@ def __eq__(self, actual) -> bool:
438439
return all(self.__eq__(a) for a in asarray.flat)
439440

440441
# Short-circuit exact equality, except for bool
441-
if isinstance(self.expected, bool) and not isinstance(actual, bool):
442+
if isinstance(self.expected, (bool, np.bool_)) and not isinstance(actual, (bool, np.bool_)):
442443
return False
443444
elif actual == self.expected:
444445
return True
@@ -447,9 +448,9 @@ def __eq__(self, actual) -> bool:
447448
# NB: we need Complex, rather than just Number, to ensure that __abs__,
448449
# __sub__, and __float__ are defined. Also, consider bool to be
449450
# nonnumeric, even though it has the required arithmetic.
450-
if isinstance(self.expected, bool) or not (
451-
isinstance(self.expected, (Complex, Decimal))
452-
and isinstance(actual, (Complex, Decimal))
451+
if isinstance(self.expected, (bool, np.bool_)) or not (
452+
isinstance(self.expected, (Complex, Decimal))
453+
and isinstance(actual, (Complex, Decimal))
453454
):
454455
return False
455456

0 commit comments

Comments
 (0)