Skip to content

ENH: testing: argument err_msg of assertion functions can be any object#24877

Merged
rgommers merged 6 commits intonumpy:mainfrom
mdhaber:gh17011
Oct 14, 2023
Merged

ENH: testing: argument err_msg of assertion functions can be any object#24877
rgommers merged 6 commits intonumpy:mainfrom
mdhaber:gh17011

Conversation

@mdhaber
Copy link
Contributor

@mdhaber mdhaber commented Oct 7, 2023

Supersedes gh-17011

gh-17011 proposed calling str automatically on the err_msg argument of assertion functions to make them more tolerant and mimic the behavior of Python assert statements.

It looks like the PR was inactive, so I took the liberty of merging main (to preserve @asmeurer 's) commits, making the suggested change , and making a patch to ensure that this works for all assertion functions that accept err_msg.

Details For local testing:
import numpy as np

assertions = [np.testing.assert_allclose,
              np.testing.assert_equal,
              np.testing.assert_array_equal,
              np.testing.assert_array_less,
              np.testing.assert_array_almost_equal,
              np.testing.assert_almost_equal,
              np.testing.assert_approx_equal]
x = 3
y = 2
err_msg = np.array([1, 2, 3])

for assertion in assertions:
    try:
        assertion(x, y, err_msg=err_msg)
    except AssertionError as e:
        print(e)

I could add this as a unit test, if desired.

@mattip it looks like you may have been willing to review the original PR. Would you be able to take a look at this one?

@asmeurer
Copy link
Member

asmeurer commented Oct 8, 2023

Cool thanks

Copy link
Contributor

@tylerjereddy tylerjereddy left a comment

Choose a reason for hiding this comment

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

In case it helps, this test fails on main and passes here:

--- a/numpy/testing/tests/test_utils.py
+++ b/numpy/testing/tests/test_utils.py
@@ -1648,3 +1648,12 @@ def __del__(self):
         finally:
             # make sure that we stop creating reference cycles
             ReferenceCycleInDel.make_cycle = False
+
+@pytest.mark.parametrize("assert_func", [
+    assert_equal,
+    assert_allclose,
+    assert_array_equal])
+def test_err_msg_obj(assert_func):
+    a = np.zeros(10)
+    with pytest.raises(AssertionError):
+        assert_func(a, a + 7, err_msg=1)

@mdhaber
Copy link
Contributor Author

mdhaber commented Oct 9, 2023

Thanks @tylerjereddy. I'd be happy to add that and/or the one in #24877 (comment) (Details) as a unit test if desired. (I would have done that if I were starting from scratch, but It looked like the original PR was ready to merge except for a CI glitch, so I didn't think I should add more code to review if it wasn't considered necessary.)

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

This LGTM, and all comments on the original PR were addressed. No need for a test I think - let's give this a go. Thanks @mdhaber & all!

@rgommers rgommers merged commit 59a938c into numpy:main Oct 14, 2023
@rgommers rgommers added this to the 2.0.0 release milestone Oct 14, 2023
BvB93 added a commit to BvB93/numpy that referenced this pull request Dec 21, 2023
BvB93 added a commit to BvB93/numpy that referenced this pull request Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants