Skip to content
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

ENH: Add strict parameter to assert_array_equal. Fixes #9542 #21595

Merged
merged 12 commits into from Jun 24, 2022

Conversation

jontwo
Copy link
Contributor

@jontwo jontwo commented May 25, 2022

It's useful to have assert_array_equal broadcast a scalar to match every value in an array, but it would be nice to be able to turn this behaviour off. Sometimes you need to have an exact match.

@jontwo
Copy link
Contributor Author

jontwo commented May 25, 2022

Not sure why those tests are failing - it's picked up the change to the tests but not to numpy.testing.

@seberg
Copy link
Member

seberg commented May 25, 2022

No, it is picking up things just fine the problem is that TestArrayEqual gets inherited below for TestEqual. (May look weird these days, but this code is older than pytest and other test fixtures.)

@jontwo
Copy link
Contributor Author

jontwo commented May 25, 2022

Ok, could I add strict further up the chain and pass it though?

@jontwo
Copy link
Contributor Author

jontwo commented May 25, 2022

Not sure what to do about these test fails. Can we rerun the pipeline when there is a network issue?

@charris
Copy link
Member

charris commented May 26, 2022

Needs a release note in doc/release/upcoming_changes.

@WarrenWeckesser
Copy link
Member

I've often wanted a strict version of the array equality test functions. I would like it to be even stricter than the change proposed here, and only pass if the shape and data types match exactly. So if x is np.array([2.0, 2.0, 2.0], dtype=np.float32), both the checks assert_array_equal(x, 2, strict=True) and assert_array_equal(x, [2, 2, 2], strict=True) would fail. The strict version of the test would be assert_array_equal(x, np.array([2, 2, 2], dtype=np.float32), strict=True).

@jontwo
Copy link
Contributor Author

jontwo commented May 27, 2022

I've often wanted a strict version of the array equality test functions. I would like it to be even stricter than the change proposed here, and only pass if the shape and data types match exactly. So if x is np.array([2.0, 2.0, 2.0], dtype=np.float32), both the checks assert_array_equal(x, 2, strict=True) and assert_array_equal(x, [2, 2, 2], strict=True) would fail. The strict version of the test would be assert_array_equal(x, np.array([2, 2, 2], dtype=np.float32), strict=True).

This sounds like a good idea to me. It makes sense that a "strict" check would also enforce the types.

I was also wondering if we should add the same parameter to assert_array_almost_equal in case users are being tripped up by the same problem. What does everyone think?

Copy link
Member

@BvB93 BvB93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add the new function parameters to the utils.pyi stub file?

numpy/testing/_private/utils.py Outdated Show resolved Hide resolved
numpy/testing/_private/utils.py Outdated Show resolved Hide resolved
numpy/testing/_private/utils.py Outdated Show resolved Hide resolved
jontwo and others added 3 commits May 27, 2022 20:10
Make new arguments keyword-only

Co-authored-by: Bas van Beek <43369155+BvB93@users.noreply.github.com>
@jontwo
Copy link
Contributor Author

jontwo commented May 28, 2022

Needs a release note in doc/release/upcoming_changes.

Does it count as a new_feature? The README says this is for new features "like kwargs". Or is it better as improvement or change?

@BvB93 BvB93 added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label May 28, 2022
@charris charris removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Jun 16, 2022
@mattip
Copy link
Member

mattip commented Jun 17, 2022

The mailing list discussion is here. I think this is the best available solution.

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur with Matti. I did not remember that it also makes the dtype strict.
This seems like a good idea and I suspect it makes using strict=True useful in practice.

Copy link
Member

@WarrenWeckesser WarrenWeckesser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a few nitpicky inline comments. The more significant (but still not major) issue that should be fixed is that when the shapes of two arrays match but the dtypes do not, the error message is not correct. E.g.

In [19]: assert_array_equal(np.eye(2), np.eye(2, dtype=np.float32), strict=True)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<...snip...> 
AssertionError: 
Arrays are not equal

(shapes (2, 2), (2, 2) mismatch)
 x: array([[1., 0.],
       [0., 1.]])
 y: array([[1., 0.],
       [0., 1.]], dtype=float32)

The error message says (shapes (2, 2), (2, 2) mismatch) but the shapes do match. In this case, the error message should report that the dtypes do not match.

doc/release/upcoming_changes/21595.new_feature.rst Outdated Show resolved Hide resolved
numpy/testing/_private/utils.py Outdated Show resolved Hide resolved
numpy/testing/_private/utils.py Outdated Show resolved Hide resolved
@WarrenWeckesser
Copy link
Member

Thanks @jontwo, the updates look good. I'll merge in a couple days if no one does it before then.

@seberg seberg added the 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. label Jun 22, 2022
@WarrenWeckesser WarrenWeckesser merged commit cafec60 into numpy:main Jun 24, 2022
@WarrenWeckesser
Copy link
Member

Merged. Thanks @jontwo and @BvB93.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. component: numpy.testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants