8000 DEP: Deprecate size-one ragged array coercion by seberg · Pull Request #16943 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DEP: Deprecate size-one ragged array coercion #16943

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion numpy/core/src/multiarray/array_coercion.c
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,58 @@ PyArray_DiscoverDTypeAndShape(
}
else if (fixed_DType->type_num != NPY_OBJECT) {
/* Only object DType supports ragged cases unify error */
if (!too_deep) {

/*
* We used to let certain ragged arrays pass if they also
* support e.g. conversion using `float(arr)`, which currently
* works for arrays with only one element.
* Thus we catch at least most of such cases here and give a
* DeprecationWarning instead of an error.
* Note that some of these will actually error later on when
* attempting to do the actual assign.
*/
int deprecate_single_element_ragged = 0;
coercion_cache_obj *current = *coercion_cache_head;
while (current != NULL) {
if (current->sequence) {
if (current->depth == ndim) {
/*
* Assume that only array-likes will allow the deprecated
* behaviour
*/
deprecate_single_element_ragged = 0;
break;
}
/* check next converted sequence/array-like */
current = current->next;
continue;
}
PyArrayObject *arr = (PyArrayObject *)(current->arr_or_sequence);
assert(PyArray_NDIM(arr) + current->depth >= ndim);
if (PyArray_NDIM(arr) != ndim - current->depth) {
/* This array is not compatible with the final shape */
if (PyArray_SIZE(arr) != 1) {
deprecate_single_element_ragged = 0;
break;
}
deprecate_single_element_ragged = 1;
}
current = current->next;
}

if (deprecate_single_element_ragged) {
/* Deprecated 2020-07-24, NumPy 1.20 */
if (DEPRECATE(
"setting an array element with a sequence. "
"This was supported in some cases where the elements "
"are arrays with a single element. For example "
"`np.array([1, np.array([2])], dtype=int)`. "
"In the future this will raise the same ValueError as "
"`np.array([1, [2]], dtype=int)`.") < 0) {
goto fail;
}
}
else if (!too_deep) {
PyObject *shape = PyArray_IntTupleFromIntp(ndim, out_shape);
PyErr_Format(PyExc_ValueError,
"setting an array element with a sequence. The "
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/tests/test_array_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def test_nested_arraylikes(self, arraylike):
for i in range(np.MAXDIMS - 1):
nested = [nested]

with pytest.raises(ValueError):
with pytest.warns(DeprecationWarning):
# It will refuse to assign the array into
np.array(nested, dtype="float64")

Expand Down
13 changes: 13 additions & 0 deletions numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,16 @@ def test_deprecated(self):
self.assert_deprecated(np.add.outer, args=(m, arr))
self.assert_not_deprecated(np.add.outer, args=(arr, arr))


class TestRaggedArray(_DeprecationTestCase):
# 2020-07-24, NumPy 1.20.0
message = "setting an array element with a sequence"

def test_deprecated(self):
arr = np.ones((1, 1))
# Deprecated if the array is a leave node:
self.assert_deprecated(lambda: np.array([arr, 0], dtype=np.float64))
self.assert_deprecated(lambda: np.array([0, arr], dtype=np.float64))
# And when it is an assignment into a lower dimensional subarray:
self.assert_deprecated(lambda: np.array([arr, [0]], dtype=np.float64))
self.assert_deprecated(lambda: np.array([[0], arr], dtype=np.float64))
0