Skip to content
Open
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 @@ -296,6 +296,7 @@ lovetheguitar
Luana Pinho Bueno Penha
Lukas Bednar
Luke Murphy
L-zeus
Maciek Fijalkowski
Madan Kumar
Maggie Chung
Expand Down
1 change: 1 addition & 0 deletions changelog/15312.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed incorrect difference values and integer overflow warnings in :func:`pytest.approx` failure messages for NumPy integer arrays.
5 changes: 4 additions & 1 deletion src/_pytest/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ def get_value_from_nested_list(
different_ids = []
for index in itertools.product(*(range(i) for i in np_array_shape)):
approx_value = get_value_from_nested_list(approx_side_as_seq, index)
other_value = get_value_from_nested_list(other_side_as_array, index)
# As in _yield_comparisons, avoid overflow in numpy integer arithmetic.
other_value = _unbox_numpy_scalar(
get_value_from_nested_list(other_side_as_array, index)
)
if approx_value != other_value:
abs_diff = abs(approx_value.expected - other_value)
max_abs_diff = _max_diff(max_abs_diff, abs_diff)
Expand Down
49 changes: 49 additions & 0 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,55 @@ def test_error_messages_numpy_dtypes(self, assert_approx_raises_regex):
],
)

@pytest.mark.parametrize(
("dtype", "actual", "expected", "absolute_difference", "relative_difference"),
[
("uint8", 255, 0, 255, 1.0),
("int8", -128, 127, 255, 1.9921875),
("uint64", 2**64 - 1, 0, 2**64 - 1, 1.0),
("int64", -(2**63), 2**63 - 1, 2**64 - 1, 2.0),
],
)
def test_numpy_integer_failure_details(
self, dtype, actual, expected, absolute_difference, relative_difference
) -> None:
np = pytest.importorskip("numpy")
actual_array = np.array([actual], dtype=dtype)
expected_array = np.array([expected], dtype=dtype)
comparison = approx(expected_array)

assert actual_array != comparison
explanation = comparison._repr_compare(actual_array)
assert explanation[:3] == [
"comparison failed. Mismatched elements: 1 / 1:",
f"Max absolute difference: {absolute_difference}",
f"Max relative difference: {relative_difference}",
]

def test_numpy_integer_assertion_details(self, pytester: Pytester) -> None:
pytest.importorskip("numpy")
pytester.makepyfile(
"""
import numpy as np
import pytest

def test_difference():
assert np.array([255], dtype=np.uint8) == pytest.approx(
np.array([0], dtype=np.uint8)
)
"""
)
result = pytester.runpytest("-Werror")
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(
[
"*Mismatched elements: 1 / 1:*",
"*Max absolute difference: 255*",
"*Max relative difference: 1.0*",
]
)
assert "representation of details failed" not in result.stdout.str()

def test_error_messages_invalid_args(self, assert_approx_raises_regex):
np = pytest.importorskip("numpy")
with pytest.raises(AssertionError) as e:
Expand Down
Loading