summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxwell Bileschi <mlbileschi@google.com>2020-03-25 11:52:31 -0400
committerGitHub <noreply@github.com>2020-03-25 10:52:31 -0500
commit68439794b84836f43ec389be0e702ff64ee76d44 (patch)
treefb4c655710cb659cdec111983f5f11ea5df9ba13
parentb03c575834a68f3a8dad5e2974ccee7fa76003ac (diff)
downloadnumpy-68439794b84836f43ec389be0e702ff64ee76d44.tar.gz
ENH: Use TypeError in `np.array` for python consistency (#15784)
Currently, when writing something like ``` pd.DataFrame({'arr': np.array(1., 2., 3.)}) ``` ``` ValueError Traceback (most recent call last) <ipython-input-1-ffdb00ae9b74> in <module>() 1 import numpy as np 2 import pandas as pd ----> 3 pd.DataFrame({'arr': np.array(1., 2., 3.)}) ValueError: only 2 non-keyword arguments accepted ``` This stack trace that doesn't include a frame for the np constructor, because the constructor is generated python code. This may lead users to look elsewhere for the issuer of the ValueError, which may create red-herrings in that folks may look elsewhere. This changes makes it more obvious where the error is coming from. * reflects eric-wieser's suggestion about common error messages * Documents required non-keyword args for np.array * Update numpy/core/src/multiarray/multiarraymodule.c Co-Authored-By: Eric Wieser <wieser.eric@gmail.com> * Update numpy/core/src/multiarray/multiarraymodule.c Co-Authored-By: Eric Wieser <wieser.eric@gmail.com> * Update numpy/core/src/multiarray/multiarraymodule.c Co-Authored-By: Eric Wieser <wieser.eric@gmail.com> * Update multiarraymodule.c Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
-rw-r--r--numpy/core/_add_newdocs.py2
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c5
2 files changed, 4 insertions, 3 deletions
diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py
index ba5c0fa68..253ff64b0 100644
--- a/numpy/core/_add_newdocs.py
+++ b/numpy/core/_add_newdocs.py
@@ -784,7 +784,7 @@ add_newdoc('numpy.core', 'broadcast', ('reset',
add_newdoc('numpy.core.multiarray', 'array',
"""
- array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
+ array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0)
Create an array.
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index 7792fcdcb..2856a123f 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -1582,8 +1582,9 @@ _array_fromobject(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws)
"ndmin", NULL};
if (PyTuple_GET_SIZE(args) > 2) {
- PyErr_SetString(PyExc_ValueError,
- "only 2 non-keyword arguments accepted");
+ PyErr_Format(PyExc_TypeError,
+ "array() takes from 1 to 2 positional arguments but "
+ "%zd were given", PyTuple_GET_SIZE(args));
return NULL;
}