Skip to content

[PR #13338/0c454979 backport][8.3.x] Fix broken approx when mixing numpy booleans and booleans #13342

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

Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Ashish Kurmi
Aviral Verma
Aviv Palivoda
Babak Keyvani
Bahram Farahmand
Barney Gale
Ben Brown
Ben Gartner
Expand Down
17 changes: 17 additions & 0 deletions changelog/13047.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Restore :func:`pytest.approx` handling of equality checks between `bool` and `numpy.bool_` types.

Comparing `bool` and `numpy.bool_` using :func:`pytest.approx` accidentally changed in version `8.3.4` and `8.3.5` to no longer match:

.. code-block:: pycon

>>> import numpy as np
>>> from pytest import approx
>>> [np.True_, np.True_] == pytest.approx([True, True])
False

This has now been fixed:

.. code-block:: pycon

>>> [np.True_, np.True_] == pytest.approx([True, True])
True
20 changes: 16 additions & 4 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,23 +426,35 @@ def __repr__(self) -> str:
def __eq__(self, actual) -> bool:
"""Return whether the given value is equal to the expected value
within the pre-specified tolerance."""

def is_bool(val: Any) -> bool:
# Check if `val` is a native bool or numpy bool.
if isinstance(val, bool):
return True
try:
import numpy as np

return isinstance(val, np.bool_)
except ImportError:
return False

asarray = _as_numpy_array(actual)
if asarray is not None:
# Call ``__eq__()`` manually to prevent infinite-recursion with
# numpy<1.13. See #3748.
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):
# Short-circuit exact equality, except for bool and np.bool_
if is_bool(self.expected) and not is_bool(actual):
return False
elif actual == self.expected:
return True

# If either type is non-numeric, fall back to strict equality.
# 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 (
# non-numeric, even though it has the required arithmetic.
if is_bool(self.expected) or not (
isinstance(self.expected, (Complex, Decimal))
and isinstance(actual, (Complex, Decimal))
):
Expand Down
9 changes: 9 additions & 0 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,15 @@ def test_expecting_bool(self) -> None:
assert True != approx(False, abs=2) # noqa: E712
assert 1 != approx(True)

def test_expecting_bool_numpy(self) -> None:
"""Check approx comparing with numpy.bool (#13047)."""
np = pytest.importorskip("numpy")
assert np.False_ != approx(True)
assert np.True_ != approx(False)
assert np.True_ == approx(True)
assert np.False_ == approx(False)
assert np.True_ != approx(False, abs=2)

def test_list(self):
actual = [1 + 1e-7, 2 + 1e-8]
expected = [1, 2]
Expand Down
Loading