summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordy Williams <43758351+jordyjwilliams@users.noreply.github.com>2022-10-11 22:40:27 +1100
committerGitHub <noreply@github.com>2022-10-11 13:40:27 +0200
commit4d19f55dd94eb16e02a605ce963d02c51411057d (patch)
tree86621f53005dad9ba3769229e0bb596ec9ddb665
parent1ed87fa09d5a5887e2ef47b31e6fe2fefff9882a (diff)
downloadnumpy-4d19f55dd94eb16e02a605ce963d02c51411057d.tar.gz
ENH: allow explicit `like=None` in all array creation functions (#22379)
Addresses #22069. As discussed in the existing issue @seberg stated that it would be best to support explicitly defined like=None when creating arange objects. Extended to all other array_creation methods which did not currently support like=None. Closes gh-22069
-rw-r--r--numpy/core/multiarray.pyi100
-rw-r--r--numpy/core/src/multiarray/arrayfunction_override.c4
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c48
-rw-r--r--numpy/core/tests/test_multiarray.py1
-rw-r--r--numpy/core/tests/test_overrides.py18
5 files changed, 94 insertions, 77 deletions
diff --git a/numpy/core/multiarray.pyi b/numpy/core/multiarray.pyi
index e943b110b..45ee17f03 100644
--- a/numpy/core/multiarray.pyi
+++ b/numpy/core/multiarray.pyi
@@ -175,7 +175,7 @@ def array(
order: _OrderKACF = ...,
subok: L[True],
ndmin: int = ...,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> _ArrayType: ...
@overload
def array(
@@ -186,7 +186,7 @@ def array(
order: _OrderKACF = ...,
subok: bool = ...,
ndmin: int = ...,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def array(
@@ -197,7 +197,7 @@ def array(
order: _OrderKACF = ...,
subok: bool = ...,
ndmin: int = ...,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
def array(
@@ -208,7 +208,7 @@ def array(
order: _OrderKACF = ...,
subok: bool = ...,
ndmin: int = ...,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def array(
@@ -219,7 +219,7 @@ def array(
order: _OrderKACF = ...,
subok: bool = ...,
ndmin: int = ...,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
@@ -228,7 +228,7 @@ def zeros(
dtype: None = ...,
order: _OrderCF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[float64]: ...
@overload
def zeros(
@@ -236,7 +236,7 @@ def zeros(
dtype: _DTypeLike[_SCT],
order: _OrderCF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def zeros(
@@ -244,7 +244,7 @@ def zeros(
dtype: DTypeLike,
order: _OrderCF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
@@ -253,7 +253,7 @@ def empty(
dtype: None = ...,
order: _OrderCF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[float64]: ...
@overload
def empty(
@@ -261,7 +261,7 @@ def empty(
dtype: _DTypeLike[_SCT],
order: _OrderCF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def empty(
@@ -269,7 +269,7 @@ def empty(
dtype: DTypeLike,
order: _OrderCF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
@@ -467,7 +467,7 @@ def asarray(
dtype: None = ...,
order: _OrderKACF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def asarray(
@@ -475,7 +475,7 @@ def asarray(
dtype: None = ...,
order: _OrderKACF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
def asarray(
@@ -483,7 +483,7 @@ def asarray(
dtype: _DTypeLike[_SCT],
order: _OrderKACF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def asarray(
@@ -491,7 +491,7 @@ def asarray(
dtype: DTypeLike,
order: _OrderKACF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
@@ -500,7 +500,7 @@ def asanyarray(
dtype: None = ...,
order: _OrderKACF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> _ArrayType: ...
@overload
def asanyarray(
@@ -508,7 +508,7 @@ def asanyarray(
dtype: None = ...,
order: _OrderKACF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def asanyarray(
@@ -516,7 +516,7 @@ def asanyarray(
dtype: None = ...,
order: _OrderKACF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
def asanyarray(
@@ -524,7 +524,7 @@ def asanyarray(
dtype: _DTypeLike[_SCT],
order: _OrderKACF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def asanyarray(
@@ -532,7 +532,7 @@ def asanyarray(
dtype: DTypeLike,
order: _OrderKACF = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
@@ -540,28 +540,28 @@ def ascontiguousarray(
a: _ArrayLike[_SCT],
dtype: None = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def ascontiguousarray(
a: object,
dtype: None = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
def ascontiguousarray(
a: Any,
dtype: _DTypeLike[_SCT],
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def ascontiguousarray(
a: Any,
dtype: DTypeLike,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
@@ -569,28 +569,28 @@ def asfortranarray(
a: _ArrayLike[_SCT],
dtype: None = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def asfortranarray(
a: object,
dtype: None = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
def asfortranarray(
a: Any,
dtype: _DTypeLike[_SCT],
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def asfortranarray(
a: Any,
dtype: DTypeLike,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
# In practice `list[Any]` is list with an int, int and a valid
@@ -608,7 +608,7 @@ def fromstring(
count: SupportsIndex = ...,
*,
sep: str,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[float64]: ...
@overload
def fromstring(
@@ -617,7 +617,7 @@ def fromstring(
count: SupportsIndex = ...,
*,
sep: str,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def fromstring(
@@ -626,7 +626,7 @@ def fromstring(
count: SupportsIndex = ...,
*,
sep: str,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
def frompyfunc(
@@ -645,7 +645,7 @@ def fromfile(
sep: str = ...,
offset: SupportsIndex = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[float64]: ...
@overload
def fromfile(
@@ -655,7 +655,7 @@ def fromfile(
sep: str = ...,
offset: SupportsIndex = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def fromfile(
@@ -665,7 +665,7 @@ def fromfile(
sep: str = ...,
offset: SupportsIndex = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
@@ -674,7 +674,7 @@ def fromiter(
dtype: _DTypeLike[_SCT],
count: SupportsIndex = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def fromiter(
@@ -682,7 +682,7 @@ def fromiter(
dtype: DTypeLike,
count: SupportsIndex = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
@@ -692,7 +692,7 @@ def frombuffer(
count: SupportsIndex = ...,
offset: SupportsIndex = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[float64]: ...
@overload
def frombuffer(
@@ -701,7 +701,7 @@ def frombuffer(
count: SupportsIndex = ...,
offset: SupportsIndex = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def frombuffer(
@@ -710,7 +710,7 @@ def frombuffer(
count: SupportsIndex = ...,
offset: SupportsIndex = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
@@ -718,7 +718,7 @@ def arange( # type: ignore[misc]
stop: _IntLike_co,
/, *,
dtype: None = ...,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[signedinteger[Any]]: ...
@overload
def arange( # type: ignore[misc]
@@ -727,14 +727,14 @@ def arange( # type: ignore[misc]
step: _IntLike_co = ...,
dtype: None = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[signedinteger[Any]]: ...
@overload
def arange( # type: ignore[misc]
stop: _FloatLike_co,
/, *,
dtype: None = ...,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[floating[Any]]: ...
@overload
def arange( # type: ignore[misc]
@@ -743,14 +743,14 @@ def arange( # type: ignore[misc]
step: _FloatLike_co = ...,
dtype: None = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[floating[Any]]: ...
@overload
def arange(
stop: _TD64Like_co,
/, *,
dtype: None = ...,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[timedelta64]: ...
@overload
def arange(
@@ -759,7 +759,7 @@ def arange(
step: _TD64Like_co = ...,
dtype: None = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[timedelta64]: ...
@overload
def arange( # both start and stop must always be specified for datetime64
@@ -768,14 +768,14 @@ def arange( # both start and stop must always be specified for datetime64
step: datetime64 = ...,
dtype: None = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[datetime64]: ...
@overload
def arange(
stop: Any,
/, *,
dtype: _DTypeLike[_SCT],
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def arange(
@@ -784,14 +784,14 @@ def arange(
step: Any = ...,
dtype: _DTypeLike[_SCT] = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[_SCT]: ...
@overload
def arange(
stop: Any, /,
*,
dtype: DTypeLike,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
@overload
def arange(
@@ -800,7 +800,7 @@ def arange(
step: Any = ...,
dtype: DTypeLike = ...,
*,
- like: _SupportsArrayFunc = ...,
+ like: None | _SupportsArrayFunc = ...,
) -> NDArray[Any]: ...
def datetime_data(
diff --git a/numpy/core/src/multiarray/arrayfunction_override.c b/numpy/core/src/multiarray/arrayfunction_override.c
index af53d7821..2bb3fbe28 100644
--- a/numpy/core/src/multiarray/arrayfunction_override.c
+++ b/numpy/core/src/multiarray/arrayfunction_override.c
@@ -63,7 +63,7 @@ pyobject_array_insert(PyObject **array, int length, int index, PyObject *item)
* Collects arguments with __array_function__ and their corresponding methods
* in the order in which they should be tried (i.e., skipping redundant types).
* `relevant_args` is expected to have been produced by PySequence_Fast.
- * Returns the number of arguments, or -1 on failure.
+ * Returns the number of arguments, or -1 on failure.
*/
static int
get_implementing_args_and_methods(PyObject *relevant_args,
@@ -384,7 +384,7 @@ array_implement_array_function(
* only. Added as an extension to NEP-18 in an effort to bring NEP-35 to
* life with minimal dispatch overhead.
*
- * The caller must ensure that `like != NULL`.
+ * The caller must ensure that `like != Py_None` or `like == NULL`.
*/
NPY_NO_EXPORT PyObject *
array_implement_c_array_function_creation(
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index a5b66f5a8..dda8831c5 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -1734,7 +1734,7 @@ array_array(PyObject *NPY_UNUSED(ignored),
int ndmin = 0;
PyArray_Descr *type = NULL;
NPY_ORDER order = NPY_KEEPORDER;
- PyObject *like = NULL;
+ PyObject *like = Py_None;
NPY_PREPARE_ARGPARSER;
if (len_args != 1 || (kwnames != NULL)) {
@@ -1750,7 +1750,7 @@ array_array(PyObject *NPY_UNUSED(ignored),
Py_XDECREF(type);
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"array", like, NULL, NULL, args, len_args, kwnames);
if (deferred != Py_NotImplemented) {
@@ -1777,7 +1777,7 @@ array_asarray(PyObject *NPY_UNUSED(ignored),
PyObject *op;
PyArray_Descr *type = NULL;
NPY_ORDER order = NPY_KEEPORDER;
- PyObject *like = NULL;
+ PyObject *like = Py_None;
NPY_PREPARE_ARGPARSER;
if (len_args != 1 || (kwnames != NULL)) {
@@ -1790,7 +1790,7 @@ array_asarray(PyObject *NPY_UNUSED(ignored),
Py_XDECREF(type);
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"asarray", like, NULL, NULL, args, len_args, kwnames);
if (deferred != Py_NotImplemented) {
@@ -1816,7 +1816,7 @@ array_asanyarray(PyObject *NPY_UNUSED(ignored),
PyObject *op;
PyArray_Descr *type = NULL;
NPY_ORDER order = NPY_KEEPORDER;
- PyObject *like = NULL;
+ PyObject *like = Py_None;
NPY_PREPARE_ARGPARSER;
if (len_args != 1 || (kwnames != NULL)) {
@@ -1829,7 +1829,7 @@ array_asanyarray(PyObject *NPY_UNUSED(ignored),
Py_XDECREF(type);
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"asanyarray", like, NULL, NULL, args, len_args, kwnames);
if (deferred != Py_NotImplemented) {
@@ -1855,7 +1855,7 @@ array_ascontiguousarray(PyObject *NPY_UNUSED(ignored),
{
PyObject *op;
PyArray_Descr *type = NULL;
- PyObject *like = NULL;
+ PyObject *like = Py_None;
NPY_PREPARE_ARGPARSER;
if (len_args != 1 || (kwnames != NULL)) {
@@ -1867,7 +1867,7 @@ array_ascontiguousarray(PyObject *NPY_UNUSED(ignored),
Py_XDECREF(type);
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"ascontiguousarray", like, NULL, NULL, args, len_args, kwnames);
if (deferred != Py_NotImplemented) {
@@ -1893,7 +1893,7 @@ array_asfortranarray(PyObject *NPY_UNUSED(ignored),
{
PyObject *op;
PyArray_Descr *type = NULL;
- PyObject *like = NULL;
+ PyObject *like = Py_None;
NPY_PREPARE_ARGPARSER;
if (len_args != 1 || (kwnames != NULL)) {
@@ -1905,7 +1905,7 @@ array_asfortranarray(PyObject *NPY_UNUSED(ignored),
Py_XDECREF(type);
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"asfortranarray", like, NULL, NULL, args, len_args, kwnames);
if (deferred != Py_NotImplemented) {
@@ -1978,7 +1978,7 @@ array_empty(PyObject *NPY_UNUSED(ignored),
NPY_ORDER order = NPY_CORDER;
npy_bool is_f_order;
PyArrayObject *ret = NULL;
- PyObject *like = NULL;
+ PyObject *like = Py_None;
NPY_PREPARE_ARGPARSER;
if (npy_parse_arguments("empty", args, len_args, kwnames,
@@ -1990,7 +1990,7 @@ array_empty(PyObject *NPY_UNUSED(ignored),
goto fail;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"empty", like, NULL, NULL, args, len_args, kwnames);
if (deferred != Py_NotImplemented) {
@@ -2180,7 +2180,7 @@ array_zeros(PyObject *NPY_UNUSED(ignored),
NPY_ORDER order = NPY_CORDER;
npy_bool is_f_order = NPY_FALSE;
PyArrayObject *ret = NULL;
- PyObject *like = NULL;
+ PyObject *like = Py_None;
NPY_PREPARE_ARGPARSER;
if (npy_parse_arguments("zeros", args, len_args, kwnames,
@@ -2193,7 +2193,7 @@ array_zeros(PyObject *NPY_UNUSED(ignored),
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"zeros", like, NULL, NULL, args, len_args, kwnames);
if (deferred != Py_NotImplemented) {
@@ -2256,7 +2256,7 @@ array_fromstring(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds
char *sep = NULL;
Py_ssize_t s;
static char *kwlist[] = {"string", "dtype", "count", "sep", "like", NULL};
- PyObject *like = NULL;
+ PyObject *like = Py_None;
PyArray_Descr *descr = NULL;
if (!PyArg_ParseTupleAndKeywords(args, keywds,
@@ -2265,7 +2265,7 @@ array_fromstring(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds
Py_XDECREF(descr);
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"fromstring", like, args, keywds, NULL, 0, NULL);
if (deferred != Py_NotImplemented) {
@@ -2297,7 +2297,7 @@ array_fromfile(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds)
char *sep = "";
Py_ssize_t nin = -1;
static char *kwlist[] = {"file", "dtype", "count", "sep", "offset", "like", NULL};
- PyObject *like = NULL;
+ PyObject *like = Py_None;
PyArray_Descr *type = NULL;
int own;
npy_off_t orig_pos = 0, offset = 0;
@@ -2310,7 +2310,7 @@ array_fromfile(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds)
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"fromfile", like, args, keywds, NULL, 0, NULL);
if (deferred != Py_NotImplemented) {
@@ -2387,7 +2387,7 @@ array_fromiter(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds)
PyObject *iter;
Py_ssize_t nin = -1;
static char *kwlist[] = {"iter", "dtype", "count", "like", NULL};
- PyObject *like = NULL;
+ PyObject *like = Py_None;
PyArray_Descr *descr = NULL;
if (!PyArg_ParseTupleAndKeywords(args, keywds,
@@ -2396,7 +2396,7 @@ array_fromiter(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds)
Py_XDECREF(descr);
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"fromiter", like, args, keywds, NULL, 0, NULL);
if (deferred != Py_NotImplemented) {
@@ -2414,7 +2414,7 @@ array_frombuffer(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds
PyObject *obj = NULL;
Py_ssize_t nin = -1, offset = 0;
static char *kwlist[] = {"buffer", "dtype", "count", "offset", "like", NULL};
- PyObject *like = NULL;
+ PyObject *like = Py_None;
PyArray_Descr *type = NULL;
if (!PyArg_ParseTupleAndKeywords(args, keywds,
@@ -2424,7 +2424,7 @@ array_frombuffer(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"frombuffer", like, args, keywds, NULL, 0, NULL);
if (deferred != Py_NotImplemented) {
@@ -3043,7 +3043,7 @@ array_arange(PyObject *NPY_UNUSED(ignored),
{
PyObject *o_start = NULL, *o_stop = NULL, *o_step = NULL, *range=NULL;
PyArray_Descr *typecode = NULL;
- PyObject *like = NULL;
+ PyObject *like = Py_None;
NPY_PREPARE_ARGPARSER;
if (npy_parse_arguments("arange", args, len_args, kwnames,
@@ -3056,7 +3056,7 @@ array_arange(PyObject *NPY_UNUSED(ignored),
Py_XDECREF(typecode);
return NULL;
}
- if (like != NULL) {
+ if (like != Py_None) {
PyObject *deferred = array_implement_c_array_function_creation(
"arange", like, NULL, NULL, args, len_args, kwnames);
if (deferred != Py_NotImplemented) {
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index e27a39c3a..773dd5bfd 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -1185,7 +1185,6 @@ class TestCreation:
expected = expected * (arr.nbytes // len(expected))
assert arr.tobytes() == expected
-
class TestStructured:
def test_subarray_field_access(self):
a = np.zeros((3, 5), dtype=[('a', ('i4', (2, 2)))])
diff --git a/numpy/core/tests/test_overrides.py b/numpy/core/tests/test_overrides.py
index e68406ebd..84e864afd 100644
--- a/numpy/core/tests/test_overrides.py
+++ b/numpy/core/tests/test_overrides.py
@@ -622,3 +622,21 @@ class TestArrayLike:
with assert_raises(TypeError):
# Raises the error about `value_error` being invalid first
np.array(1, value_error=True, like=ref)
+
+ @pytest.mark.parametrize('function, args, kwargs', _array_tests)
+ def test_like_as_none(self, function, args, kwargs):
+ self.add_method('array', self.MyArray)
+ self.add_method(function, self.MyArray)
+ np_func = getattr(np, function)
+
+ like_args = tuple(a() if callable(a) else a for a in args)
+ # required for loadtxt and genfromtxt to init w/o error.
+ like_args_exp = tuple(a() if callable(a) else a for a in args)
+
+ array_like = np_func(*like_args, **kwargs, like=None)
+ expected = np_func(*like_args_exp, **kwargs)
+ # Special-case np.empty to ensure values match
+ if function == "empty":
+ array_like.fill(1)
+ expected.fill(1)
+ assert_equal(array_like, expected)