8000 ENH: Throw TypeError on operator concat on Numpy Arrays by vrakesh · Pull Request #16570 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Throw TypeError on operator concat on Numpy Arrays #16570

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 1 commit into from
Jun 27, 2020
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
17 changes: 16 additions & 1 deletion numpy/core/src/multiarray/sequence.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,24 @@ array_contains(PyArrayObject *self, PyObject *el)
return ret;
}

static PyObject *
array_concat(PyObject *self, PyObject *other)
{
/*
* Throw a type error, when trying to concat NDArrays
* NOTE: This error is not Thrown when running with PyPy
*/
PyErr_SetString(PyExc_TypeError,
"Concatenation operation is not implemented for NumPy arrays, "
"use np.concatenate() instead. Please do not rely on this error; "
"it may not be given on all Python implementations.");
return NULL;
}


NPY_NO_EXPORT PySequenceMethods array_as_sequence = {
(lenfunc)array_length, /*sq_length*/
(binaryfunc)NULL, /*sq_concat is handled by nb_add*/
(binaryfunc)array_concat, /*sq_concat for operator.concat*/
(ssizeargfunc)NULL,
(ssizeargfunc)array_item,
(ssizessizeargfunc)NULL,
Expand Down
15 changes: 14 additions & 1 deletion numpy/core/tests/test_shape_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
_block_concatenate, _block_slicing)
from numpy.testing import (
assert_, assert_raises, assert_array_equal, assert_equal,
assert_raises_regex, assert_warns
assert_raises_regex, assert_warns, IS_PYPY
)


Expand Down Expand Up @@ -320,6 +320,19 @@ def test_concatenate(self):
assert_(out is rout)
assert_equal(res, rout)

@pytest.mark.skipif(IS_PYPY, reason="PYPY handles sq_concat, nb_add differently than cpython")
def test_operator_concat(self):
import operator
a = array([1, 2])
b = array([3, 4])
n = [1,2]
res = array([1, 2, 3, 4])
assert_raises(TypeError, operator.concat, a, b)
assert_raises(TypeError, operator.concat, a, n)
assert_raises(TypeError, operator.concat, n, a)
assert_raises(TypeError, operator.concat, a, 1)
assert_raises(TypeError, operator.concat, 1, a)

def test_bad_out_shape(self):
a = array([1, 2])
b = array([3, 4])
Expand Down
0