Skip to content

np.testing error messages lose "Actual" and "Desired" metadata when comparing arrays #19171

@almostsquare

Description

@almostsquare

Most np.testing functions print error messages with ACTUAL and DESIRED to help aid in debugging. From the docstring example:

>>> np.testing.assert_equal([4,5], [4,6])
Traceback (most recent call last):
   ...
AssertionError:
Items are not equal:
item=1
 ACTUAL: 5
 DESIRED: 6

However, this only holds when processing scalars, lists, and tuples. When processing actual numpy arrays, most functions make a call to assert_array_compare() which replaces the default names=('ACTUAL', 'DESIRED') in build_err_msg() with x and y. As a result, error messages from assert_allclose() or similar functions will strip this metadata.

https://github.com/numpy/numpy/blob/main/numpy/testing/_private/utils.py#L851

Reproducing code example:

import numpy as np
actual = np.array([4, 5])
desired = np.array([4, 6])
np.testing.assert_allclose(actual, desired)

Observed

AssertionError: 
Not equal to tolerance rtol=1e-07, atol=0

Mismatched elements: 1 / 2 (50%)
Max absolute difference: 1
Max relative difference: 0.16666667
 x: array([4, 5])
 y: array([4, 6])

Expected

AssertionError: 
Not equal to tolerance rtol=1e-07, atol=0

Mismatched elements: 1 / 2 (50%)
Max absolute difference: 1
Max relative difference: 0.16666667
 ACTUAL: array([4, 5])
 DESIRED: array([4, 6])

When debugging test outputs or looking at test logs, people not familiar with the numpy convention may not be aware that x is ACTUAL and y is DESIRED.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions