Skip to content

Commit 87dc319

Browse files
sebergrossbar
authored andcommitted
API: Disallow strings in logical ufuncs (numpy#21024)
* API: Disallow strings in logical ufuncs This restores pre 1.22 NumPy behaviour for strings passed into logical ufuncs. Logical ufuncs should be able to cast inputs to booleans without modifying their results. Thus they do this now (in NumPy 1.22+). The problem is that string to bool casts are very strange in NumPy currently. This should be reverted/removed once string to bool casts are well defined. But until then, it seems more reasonable to just reject strings. Closes numpygh-20898 Co-authored-by: Ross Barnowski <[email protected]>
1 parent 593cc07 commit 87dc319

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎numpy/core/src/umath/dispatching.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,11 @@ logical_ufunc_promoter(PyUFuncObject *NPY_UNUSED(ufunc),
10181018
/* bail out, this is _only_ to give future/deprecation warning! */
10191019
return -1;
10201020
}
1021+
if ((op_dtypes[0] != NULL && PyTypeNum_ISSTRING(op_dtypes[0]->type_num))
1022+
|| PyTypeNum_ISSTRING(op_dtypes[1]->type_num)) {
1023+
/* bail out on strings: currently casting them to bool is too weird */
1024+
return -1;
1025+
}
10211026

10221027
for (int i = 0; i < 3; i++) {
10231028
PyArray_DTypeMeta *item;

‎numpy/core/tests/test_ufunc.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ def test_logical_ufuncs_mixed_object_signatures(self, ufunc, signature):
21342134
[np.logical_and, np.logical_or, np.logical_xor])
21352135
def test_logical_ufuncs_support_anything(self, ufunc):
21362136
# The logical ufuncs support even input that can't be promoted:
2137-
a = np.array('1')
2137+
a = np.array(b'1', dtype="V3")
21382138
c = np.array([1., 2.])
21392139
assert_array_equal(ufunc(a, c), ufunc([True, True], True))
21402140
assert ufunc.reduce(a) == True
@@ -2150,6 +2150,23 @@ def test_logical_ufuncs_support_anything(self, ufunc):
21502150
out = np.zeros((), dtype=a.dtype)
21512151
assert ufunc.reduce(a, out=out) == 1
21522152

2153+
@pytest.mark.parametrize("ufunc",
2154+
[np.logical_and, np.logical_or, np.logical_xor])
2155+
def test_logical_ufuncs_reject_string(self, ufunc):
2156+
"""
2157+
Logical ufuncs are normally well defined by working with the boolean
2158+
equivalent, i.e. casting all inputs to bools should work.
2159+
2160+
However, casting strings to bools is *currently* weird, because it
2161+
actually uses `bool(int(str))`. Thus we explicitly reject strings.
2162+
This test should succeed (and can probably just be removed) as soon as
2163+
string to bool casts are well defined in NumPy.
2164+
"""
2165+
with pytest.raises(TypeError, match="contain a loop with signature"):
2166+
ufunc(["1"], ["3"])
2167+
with pytest.raises(TypeError, match="contain a loop with signature"):
2168+
ufunc.reduce(["1", "2", "0"])
2169+
21532170
@pytest.mark.parametrize("ufunc",
21542171
[np.logical_and, np.logical_or, np.logical_xor])
21552172
def test_logical_ufuncs_out_cast_check(self, ufunc):

0 commit comments

Comments
 (0)