Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #13047: Handle numpy booleans in pytest.approx #13132

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from typing import TYPE_CHECKING
from typing import TypeVar

import numpy as np

import _pytest._code
from _pytest.outcomes import fail

Expand Down Expand Up @@ -438,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) 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
Expand All @@ -447,7 +451,7 @@ 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 (
if isinstance(self.expected, (bool, np.bool_)) or not (
isinstance(self.expected, (Complex, Decimal))
and isinstance(actual, (Complex, Decimal))
):
Expand Down
Loading