Skip to content

Commit db550d5

Browse files
authored
Merge pull request #21981 from Saransh-cpp/test-for-TypeError
TST: ensure `np.equal.reduce` raises a `TypeError`
2 parents 5e9ec76 + e3bcb6a commit db550d5

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

numpy/core/src/umath/dispatching.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,13 @@ promote_and_get_ufuncimpl(PyUFuncObject *ufunc,
10161016
signature[i] = (PyArray_DTypeMeta *)PyTuple_GET_ITEM(all_dtypes, i);
10171017
Py_INCREF(signature[i]);
10181018
}
1019-
else {
1020-
assert((PyObject *)signature[i] == PyTuple_GET_ITEM(all_dtypes, i));
1019+
else if ((PyObject *)signature[i] != PyTuple_GET_ITEM(all_dtypes, i)) {
1020+
/*
1021+
* If signature is forced the cache may contain an incompatible
1022+
* loop found via promotion (signature not enforced). Reject it.
1023+
*/
1024+
raise_no_loop_found_error(ufunc, (PyObject **)op_dtypes);
1025+
return NULL;
10211026
}
10221027
}
10231028

numpy/core/tests/test_ufunc.py

+9
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,15 @@ def test_partial_signature_mismatch(self, casting):
492492
with pytest.raises(TypeError):
493493
np.ldexp(1., np.uint64(3), signature=(None, None, "d"))
494494

495+
def test_partial_signature_mismatch_with_cache(self):
496+
with pytest.raises(TypeError):
497+
np.add(np.float16(1), np.uint64(2), sig=("e", "d", None))
498+
# Ensure e,d->None is in the dispatching cache (double loop)
499+
np.add(np.float16(1), np.float64(2))
500+
# The error must still be raised:
501+
with pytest.raises(TypeError):
502+
np.add(np.float16(1), np.uint64(2), sig=("e", "d", None))
503+
495504
def test_use_output_signature_for_all_arguments(self):
496505
# Test that providing only `dtype=` or `signature=(None, None, dtype)`
497506
# is sufficient if falling back to a homogeneous signature works.

numpy/core/tests/test_umath.py

+8
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ def __ne__(self, other):
272272
a = np.array([np.nan], dtype=object)
273273
assert_equal(np.not_equal(a, a), [True])
274274

275+
def test_error_in_equal_reduce(self):
276+
# gh-20929
277+
# make sure np.equal.reduce raises a TypeError if an array is passed
278+
# without specifying the dtype
279+
a = np.array([0, 0])
280+
assert_equal(np.equal.reduce(a, dtype=bool), True)
281+
assert_raises(TypeError, np.equal.reduce, a)
282+
275283

276284
class TestAdd:
277285
def test_reduce_alignment(self):

0 commit comments

Comments
 (0)