Skip to content

fix: correct NumPy integer differences in approx failures - #15312

Open
L-zeus wants to merge 3 commits into
pytest-dev:mainfrom
L-zeus:publish/numpy-integer-approx-diagnostics-20261030
Open

L-zeus wants to merge 3 commits into
pytest-dev:mainfrom
L-zeus:publish/numpy-integer-approx-diagnostics-20261030

Conversation

@L-zeus

@L-zeus L-zeus commented Nov 1, 2026 •

Copy link
Copy Markdown

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) with 0 should report an absolute difference of 255. 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, and int64 boundary values.
  • The actual pytest assertion output with -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 255 and relative difference of 1.0.

Codex ran the automated checks on the submission branch:

  • testing/python/approx.py: 187 passed.
  • Applicable pre-commit checks 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

L-zeus and others added 3 commits October 30, 2026 20:32
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>
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant