Skip to content

Commit 1a828f5

Browse files
committed
DEP: Ensure the string promotion FutureWarning is raised
Promotion errors are currently ignored and instead we use `object` as dtype. This means that the FutureWarning is ignored when raised. It also means that there is no way to opt into "future" behaviour, although arguably, we may want to force `dtype=object` in any case if a promotion error occurred (or some signal at least). Closes numpygh-18721 Addresses numpygh-18425 (Does not actually fix it, but chances are we won't)
1 parent 6f5ad73 commit 1a828f5

5 files changed

Lines changed: 20 additions & 19 deletions

File tree

‎doc/release/upcoming_changes/18116.future.rst‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ are:
1111
a string result.
1212
* `numpy.array` and related functions will start returning ``object``
1313
arrays because these functions use ``object`` as a fallback when
14-
no common dtype can be found. (In this case setting the
15-
``FutureWarning`` to be raised will unfortunately lead to the new
16-
behaviour)
14+
no common dtype can be found. However, it may happen that future
15+
releases of NumPy will generally error in these cases.
1716

1817
This will mainly affect code such as::
1918

@@ -24,6 +23,7 @@ and::
2423
np.concatenate((['string'], [0]))
2524

2625
in both cases adding ``dtype="U"`` or ``dtype="S"`` will give the
27-
previous (string) result.
26+
previous (string) result, while ``dtype=object`` will ensure an array with
27+
object dtype is returned.
2828

2929
Comparisons, universal functions, and casting are not affected by this.

‎numpy/core/src/multiarray/array_coercion.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,12 @@ handle_promotion(PyArray_Descr **out_descr, PyArray_Descr *descr,
622622
}
623623
PyArray_Descr *new_descr = PyArray_PromoteTypes(descr, *out_descr);
624624
if (NPY_UNLIKELY(new_descr == NULL)) {
625-
if (fixed_DType != NULL) {
626-
/* If a DType is fixed, promotion must not fail. */
625+
if (fixed_DType != NULL || PyErr_ExceptionMatches(PyExc_FutureWarning)) {
626+
/*
627+
* If a DType is fixed, promotion must not fail. Do not catch
628+
* FutureWarning (raised for string+numeric promotions). We could
629+
* only catch TypeError here or even always raise the error.
630+
*/
627631
return -1;
628632
}
629633
PyErr_Clear();

‎numpy/core/tests/test_deprecations.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,10 +1121,7 @@ def test_deprecated(self, dtype, string_dt):
11211121
self.assert_deprecated(lambda: np.concatenate((arr1, arr2), axis=0))
11221122
self.assert_deprecated(lambda: np.concatenate((arr1, arr2), axis=None))
11231123

1124-
# coercing to an array is similar, but will fall-back to `object`
1125-
# (when raising the FutureWarning, this already happens)
1126-
self.assert_deprecated(lambda: np.array([arr1[0], arr2[0]]),
1127-
exceptions=())
1124+
self.assert_deprecated(lambda: np.array([arr1[0], arr2[0]]))
11281125

11291126
@pytest.mark.parametrize("dtype", "?bhilqpBHILQPefdgFDG")
11301127
@pytest.mark.parametrize("string_dt", ["S", "U"])

‎numpy/core/tests/test_regression.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,8 @@ def test_swap_real(self):
503503
assert_equal(np.arange(4, dtype='<c8').real.max(), 3.0)
504504

505505
def test_object_array_from_list(self):
506-
# Ticket #270
507-
assert_(np.array([1, 'A', None]).shape == (3,))
506+
# Ticket #270 (gh-868)
507+
assert_(np.array([1, None, 'A']).shape == (3,))
508508

509509
def test_multiple_assign(self):
510510
# Ticket #273
@@ -2052,18 +2052,18 @@ def test_search_sorted_invalid_arguments(self):
20522052

20532053
def test_string_truncation(self):
20542054
# Ticket #1990 - Data can be truncated in creation of an array from a
2055-
# mixed sequence of numeric values and strings
2055+
# mixed sequence of numeric values and strings (gh-2583)
20562056
for val in [True, 1234, 123.4, complex(1, 234)]:
2057-
for tostr in [asunicode, asbytes]:
2058-
b = np.array([val, tostr('xx')])
2057+
for tostr, dtype in [(asunicode, "U"), (asbytes, "S")]:
2058+
b = np.array([val, tostr('xx')], dtype=dtype)
20592059
assert_equal(tostr(b[0]), tostr(val))
2060-
b = np.array([tostr('xx'), val])
2060+
b = np.array([tostr('xx'), val], dtype=dtype)
20612061
assert_equal(tostr(b[1]), tostr(val))
20622062

20632063
# test also with longer strings
2064-
b = np.array([val, tostr('xxxxxxxxxx')])
2064+
b = np.array([val, tostr('xxxxxxxxxx')], dtype=dtype)
20652065
assert_equal(tostr(b[0]), tostr(val))
2066-
b = np.array([tostr('xxxxxxxxxx'), val])
2066+
b = np.array([tostr('xxxxxxxxxx'), val], dtype=dtype)
20672067
assert_equal(tostr(b[1]), tostr(val))
20682068

20692069
def test_string_truncation_ucs2(self):

‎numpy/ma/tests/test_mrecords.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def test_fromarrays(self):
405405
for (f, l) in zip(('a', 'b', 'c'), (_a, _b, _c)):
406406
assert_equal(getattr(mrec, f)._mask, l._mask)
407407
# One record only
408-
_x = ma.array([1, 1.1, 'one'], mask=[1, 0, 0],)
408+
_x = ma.array([1, 1.1, 'one'], mask=[1, 0, 0], dtype=object)
409409
assert_equal_records(fromarrays(_x, dtype=mrec.dtype), mrec[0])
410410

411411
def test_fromrecords(self):

0 commit comments

Comments
 (0)