Repository navigation
Conversation
Unbox the actual array scalar before mismatch and difference calculations, matching the existing equality path. Implementation and local verification performed with OpenAI Codex assistance; pending human review.
Codex assisted the implementation and verification of the NumPy integer failure-diagnostics fix. L-zeus reviewed the changes and reproduced the bug. Co-authored-by: Codex <noreply@openai.com>
|
The unboxing fix only reaches Repro on head import warnings
import numpy as np
import pytest
warnings.simplefilter("error")
print(pytest.approx([0])._repr_compare(np.array([255], dtype=np.uint8)))
print(pytest.approx([127])._repr_compare(np.array([-128], dtype=np.int8)))Obtained: Expected (same values as your new Under def test_sequence_expected_numpy_actual():
actual = np.array([255], dtype=np.uint8)
assert actual == pytest.approx([0])
One-line fix in the sequence-like loop, mirroring the numpy path: other_value = _unbox_numpy_scalar(other_value)
if approx_value != other_value:It is a no-op for plain Python values ( Suggest also adding a list-expected case to |
|
Thanks for working on this. One heads-up: this PR fixes More importantly, I have a fix for #15314 that unboxes in |
|
Thanks @ege-arhan and @doubledare704 for the detailed reports and suggestions. I have a local follow-up covering scalar comparisons and sequence/mapping diagnostics, but haven’t pushed it yet I also noticed #15318 now covers the sequence path, so there’s some overlap. Before pushing, I’d like to coordinate the scope. Would the maintainers prefer keeping this PR focused on NumPy-array diagnostics and addressing the remaining cases separately, or extending it here? |
Problem
When comparing NumPy integer arrays with
pytest.approx(), the failure message can show incorrect differences or fail to render because of integer overflow.For example, comparing
np.uint8(255)with0should report an absolute difference of255. However, the failure-reporting path performs arithmetic on a fixed-width NumPy scalar, which can overflow.The comparison result itself is correct. The problem is limited to how the failure details are calculated.
Change
The existing comparison path already converts NumPy scalars to Python values using
_unbox_numpy_scalar().This change reuses that helper in
ApproxNumpy._repr_compare()before calculating the differences.It fixes the reported values without changing the equality comparison or tolerance calculations.
Tests
Added regression tests covering:
uint8,int8,uint64, andint64boundary values.-Werror.I reproduced the issue on the original code and checked the result after applying the fix. The failure message now reports the correct absolute difference of
255and relative difference of1.0.Codex ran the automated checks on the submission branch:
testing/python/approx.py: 187 passed.AI assistance and review
I used OpenAI Codex to assist with the implementation and automated testing.
I reviewed the source changes and regression tests, checked why the comparison path was already correct while the failure-reporting path could overflow, and reproduced the before-and-after behavior locally.
I understand the change and will handle review feedback and any necessary follow-up changes myself. Codex is also credited in a follow-up commit