Skip to content

TST: ensure np.equal.reduce raises a TypeError #21981

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

Merged
merged 2 commits into from
Sep 7, 2022

Conversation

Saransh-cpp
Copy link
Contributor

Closes #20929

Added a couple of tests to ensure that np.equal.reduce raises TypeError, and the developers are notified if the API changes.

I am not very sure if I should be creating a new method for this test. Please let me know if I can add the tests in any existing method.

Thanks!

@charris
Copy link
Member

charris commented Jul 25, 2022

close/reopen

@charris charris closed this Jul 25, 2022
@charris charris reopened this Jul 25, 2022
# without specifying the dtype
a = np.array([0, 0])
assert_equal(np.equal.reduce(a, dtype=bool), True)
assert_raises(TypeError, np.equal.reduce, a)
Copy link
Member

Choose a reason for hiding this comment

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

This is causing a segfault in the debug builds.

Copy link
Member

Choose a reason for hiding this comment

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

hmmm sounds like hitting an assert, probably just a wrong one...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review, @charris and @seberg! I am very new to the C++ and Python combination. Could you please help me with the segfault, or link a similar error and fix so that I can have look? I tried running the testing suite by removing the assert_raises test and it does pass.

Copy link
Member

Choose a reason for hiding this comment

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

Hmmmpf, should be able to get a traceback using gdb: https://numpy.org/devdocs/dev/development_environment.html#debugging

But I cannot reproduce with a python3.9 debug version and see nothing with valgrind either. Can you undo the last commit, because I don't know anymore what is running on CI. At least if it is a linux debug print, we can also hook in gdb in CI (although I have to look up how to every time).
If you can reproduce a crash with -g locally, then running gdb will tell us more.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I can reproduce, it requires a full test-suite run though... maybe something wrong related to the dispatching caching.

Copy link
Member

@seberg seberg Aug 5, 2022

Choose a reason for hiding this comment

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

OK, I think the assert is indeed incorrect and it should be something like:

diff --git a/numpy/core/src/umath/dispatching.c b/numpy/core/src/umath/dispatching.c
index 5aecdd1fc..f2c6deada 100644
--- a/numpy/core/src/umath/dispatching.c
+++ b/numpy/core/src/umath/dispatching.c
@@ -1016,8 +1016,13 @@ promote_and_get_ufuncimpl(PyUFuncObject *ufunc,
             signature[i] = (PyArray_DTypeMeta *)PyTuple_GET_ITEM(all_dtypes, i);
             Py_INCREF(signature[i]);
         }
-        else {
-            assert((PyObject *)signature[i] == PyTuple_GET_ITEM(all_dtypes, i));
+        else if ((PyObject *)signature[i] != PyTuple_GET_ITEM(all_dtypes, i)) {
+            /*
+             * If signature is forced we may find an incompatible loop that
+             * would be picked by promotion (signature not forced), reject it.
+             */
+            raise_no_loop_found_error(ufunc, (PyObject **)op_dtypes);
+            return NULL;
         }
     }

We still get a slightly different error depending on the code path, but that is because the "normal" error is less precise.

EDIT: It seems without the assert we already end up in a check which correctly rejects the loop later (not quite sure where).

Copy link
Member

Choose a reason for hiding this comment

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

OK, maybe not the ideal thing, but I pushed a second commit that applies that fix (with a slightly different comment) and adds a more explicit test.

Copy link
Contributor Author

@Saransh-cpp Saransh-cpp Aug 5, 2022

Choose a reason for hiding this comment

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

Thank you for fixing this, @seberg!! I was caught up with something and couldn't undo the commit (thanks for fixing that too!) :)

Saransh-cpp and others added 2 commits August 5, 2022 08:07
If we cache a promoted version of the loop, that promoted can mismatch
the correct one.  This ends up being rejected later in the legacy paths
(should be the only path currently used), but we should reject it here
(or in principle we could reject it after cache lookup, but we are fixing
up the operation DTypes here anyway, so we are looking at the signature).

A call sequence reproducing this directly is:

np.add(1, 2, signature=(bool, int, None))  # should fail

np.add(True, 2)  # A promoted loop
np.add(1, 2, signature=(bool, int, None))  # should still fail

Not that the errors differ, because the first one comes from the old
type resolution code and is currently less precise
@seberg seberg force-pushed the test-for-TypeError branch from a355379 to e3bcb6a Compare August 5, 2022 04:58
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Aug 5, 2022
@charris charris added this to the 1.23.2 release milestone Aug 5, 2022
@charris charris added the triage review Issue/PR to be discussed at the next triage meeting label Sep 7, 2022
@mattip mattip merged commit db550d5 into numpy:main Sep 7, 2022
@InessaPawson InessaPawson added triaged Issue/PR that was discussed in a triage meeting and removed triage review Issue/PR to be discussed at the next triage meeting labels Sep 7, 2022
@Saransh-cpp Saransh-cpp deleted the test-for-TypeError branch September 7, 2022 16:20
@charris charris changed the title TST: ensure np.equal.reduce raises a TypeError TST: ensure np.equal.reduce raises a TypeError Sep 7, 2022
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Sep 7, 2022
@charris charris removed this from the 1.23.3 release milestone Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug 05 - Testing triaged Issue/PR that was discussed in a triage meeting
Projects
Development

Successfully merging this pull request may close these issues.

BUG: np.equal.reduce no longer works with int and float
5 participants