From 496bd1a84a6a62adf5f8cb48cf7abc7795484531 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Sat, 5 Jun 2021 14:23:13 +0530 Subject: Added np.CopyMode --- numpy/__init__.py | 3 ++- numpy/_globals.py | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.py b/numpy/__init__.py index baff5e141..690a8135c 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -110,7 +110,8 @@ import sys import warnings from ._globals import ( - ModuleDeprecationWarning, VisibleDeprecationWarning, _NoValue + ModuleDeprecationWarning, VisibleDeprecationWarning, + _NoValue, CopyMode ) # We first need to detect if we're being called as part of the numpy setup diff --git a/numpy/_globals.py b/numpy/_globals.py index 0b715c870..a8f57f3b6 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -15,8 +15,11 @@ That was not the case when the singleton classes were defined in the numpy motivated this module. """ +import enum + __ALL__ = [ - 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', '_NoValue' + 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', + '_NoValue', 'CopyMode' ] @@ -89,3 +92,19 @@ class _NoValueType: _NoValue = _NoValueType() + +class CopyMode(enum.Enum): + + ALWAYS = 1 + IF_NEEDED = 0 + NEVER = 2 + + def __bool__(self): + # For backwards compatiblity + if self == CopyMode.ALWAYS: + return True + + if self == CopyMode.IF_NEEDED: + return False + + raise TypeError(f"{self} is neither True nor False.") \ No newline at end of file -- cgit v1.2.1 From 3a3d31b338258c099fa4f2d4e4290b19b2a409b0 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Sat, 5 Jun 2021 15:08:40 +0530 Subject: initial work for supporting never_copy --- numpy/__init__.pyi | 3 ++- numpy/core/include/numpy/ndarraytypes.h | 2 ++ numpy/core/src/multiarray/conversion_utils.c | 5 +++++ numpy/core/src/multiarray/conversion_utils.h | 3 +++ numpy/core/src/multiarray/methods.c | 3 +-- 5 files changed, 13 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index ac37eb8ad..e710aee76 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -202,6 +202,7 @@ from numpy import ( rec as rec, testing as testing, version as version, + CopyMode as CopyMode ) from numpy.core.function_base import ( @@ -1232,7 +1233,7 @@ class _ArrayOrScalarCommon: order: _OrderKACF = ..., casting: _Casting = ..., subok: bool = ..., - copy: bool = ..., + copy: Union[bool, CopyMode[Any]] = ..., ) -> _ArraySelf: ... def copy(self: _ArraySelf, order: _OrderKACF = ...) -> _ArraySelf: ... def dump(self, file: str) -> None: ... diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index d1acfdf26..569e6bb34 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -451,6 +451,8 @@ typedef struct { int len; } PyArray_Dims; +typedef enum {IF_NEEDED, ALWAYS, NEVER} PyNpCopyMode_Enum; + typedef struct { /* * Functions to cast to most other standard types diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 3c4c21ded..50ebea0e5 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -162,6 +162,11 @@ PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq) return PyArray_IntpConverter(obj, seq); } +NPY_NO_EXPORT int +PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { + return 0; +} + /*NUMPY_API * Get buffer chunk from object * diff --git a/numpy/core/src/multiarray/conversion_utils.h b/numpy/core/src/multiarray/conversion_utils.h index 7d1871c43..d2874aafa 100644 --- a/numpy/core/src/multiarray/conversion_utils.h +++ b/numpy/core/src/multiarray/conversion_utils.h @@ -9,6 +9,9 @@ PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq); NPY_NO_EXPORT int PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq); +NPY_NO_EXPORT int +PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copyflag); + NPY_NO_EXPORT int PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf); diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 251e527a6..892039508 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -830,13 +830,12 @@ array_astype(PyArrayObject *self, NPY_ORDER order = NPY_KEEPORDER; int forcecopy = 1, subok = 1; NPY_PREPARE_ARGPARSER; - if (npy_parse_arguments("astype", args, len_args, kwnames, "dtype", &PyArray_DescrConverter, &dtype, "|order", &PyArray_OrderConverter, &order, "|casting", &PyArray_CastingConverter, &casting, "|subok", &PyArray_PythonPyIntFromInt, &subok, - "|copy", &PyArray_PythonPyIntFromInt, &forcecopy, + "|copy", &PyArray_CopyConverter, &forcecopy, NULL, NULL, NULL) < 0) { Py_XDECREF(dtype); return NULL; -- cgit v1.2.1 From be5823f603a37a5de2192f9772dca1b31cffb9be Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Mon, 7 Jun 2021 09:15:57 +0530 Subject: Addressed reviews and PyArray_CopyConverter defined --- numpy/__init__.py | 3 ++- numpy/__init__.pyi | 11 +++++++++-- numpy/_globals.py | 6 ++++-- numpy/core/include/numpy/ndarraytypes.h | 2 +- numpy/core/src/multiarray/conversion_utils.c | 25 ++++++++++++++++++++++++- numpy/core/src/multiarray/methods.c | 2 +- 6 files changed, 41 insertions(+), 8 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.py b/numpy/__init__.py index 690a8135c..c5cf6d753 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -133,7 +133,8 @@ else: raise ImportError(msg) from e __all__ = ['ModuleDeprecationWarning', - 'VisibleDeprecationWarning'] + 'VisibleDeprecationWarning', + 'CopyMode'] # get the version using versioneer from ._version import get_versions diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index e710aee76..a2d6ce435 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -2,6 +2,7 @@ import builtins import os import sys import datetime as dt +import enum from abc import abstractmethod from types import TracebackType from contextlib import ContextDecorator @@ -1233,7 +1234,7 @@ class _ArrayOrScalarCommon: order: _OrderKACF = ..., casting: _Casting = ..., subok: bool = ..., - copy: Union[bool, CopyMode[Any]] = ..., + copy: bool | CopyMode = ..., ) -> _ArraySelf: ... def copy(self: _ArraySelf, order: _OrderKACF = ...) -> _ArraySelf: ... def dump(self, file: str) -> None: ... @@ -3547,6 +3548,12 @@ trunc: _UFunc_Nin1_Nout1[L['trunc'], L[7], None] abs = absolute +class CopyMode(enum.IntEnum): + + ALWAYS: L[1] + IF_NEEDED: L[0] + NEVER: L[2] + # Warnings class ModuleDeprecationWarning(DeprecationWarning): ... class VisibleDeprecationWarning(UserWarning): ... @@ -3654,4 +3661,4 @@ class broadcast: def size(self) -> int: ... def __next__(self) -> Tuple[Any, ...]: ... def __iter__(self: _T) -> _T: ... - def reset(self) -> None: ... + def reset(self) -> None: ... \ No newline at end of file diff --git a/numpy/_globals.py b/numpy/_globals.py index a8f57f3b6..a6fbec340 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -93,7 +93,7 @@ class _NoValueType: _NoValue = _NoValueType() -class CopyMode(enum.Enum): +class CopyMode(enum.IntEnum): ALWAYS = 1 IF_NEEDED = 0 @@ -107,4 +107,6 @@ class CopyMode(enum.Enum): if self == CopyMode.IF_NEEDED: return False - raise TypeError(f"{self} is neither True nor False.") \ No newline at end of file + raise TypeError(f"{self} is neither True nor False.") + +CopyMode.__module__ = 'numpy' \ No newline at end of file diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 569e6bb34..86dc19c64 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -451,7 +451,7 @@ typedef struct { int len; } PyArray_Dims; -typedef enum {IF_NEEDED, ALWAYS, NEVER} PyNpCopyMode_Enum; +typedef enum PyNpCopyMode {IF_NEEDED, ALWAYS, NEVER} PyNpCopyMode_Enum; typedef struct { /* diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 50ebea0e5..984b0b948 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -17,6 +17,8 @@ #include "alloc.h" #include "npy_buffer.h" +#include "npy_argparse.h" + static int PyArray_PyIntAsInt_ErrMsg(PyObject *o, const char * msg) NPY_GCC_NONNULL(2); static npy_intp @@ -164,7 +166,28 @@ PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq) NPY_NO_EXPORT int PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { - return 0; + if (obj == Py_None) { + PyErr_SetString(PyExc_ValueError, + "NoneType copy mode not allowed. Please choose one of" + "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER"); + return NPY_FAIL; + } + + int int_copymode = -1; + PyArray_PythonPyIntFromInt(obj, &int_copymode); + + if( int_copymode != ALWAYS && + int_copymode != IF_NEEDED && + int_copymode != NEVER ) { + PyErr_Format(PyExc_ValueError, + "Unrecognized copy mode %d. Please choose one of" + "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER", + int_copymode); + return NPY_FAIL; + } + + *copymode = (PyNpCopyMode_Enum) int_copymode; + return NPY_SUCCEED; } /*NUMPY_API diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 892039508..d4494d6e9 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -828,7 +828,7 @@ array_astype(PyArrayObject *self, */ NPY_CASTING casting = NPY_UNSAFE_CASTING; NPY_ORDER order = NPY_KEEPORDER; - int forcecopy = 1, subok = 1; + PyNpCopyMode_Enum forcecopy = 1, subok = 1; NPY_PREPARE_ARGPARSER; if (npy_parse_arguments("astype", args, len_args, kwnames, "dtype", &PyArray_DescrConverter, &dtype, -- cgit v1.2.1 From e16f3ff524206723cff3517845a9e2d77277ac6d Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Mon, 7 Jun 2021 09:52:00 +0530 Subject: np.CopyMode.NEVER gives error if copy cannot be avoided in np.array.astype --- numpy/core/src/multiarray/methods.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index d4494d6e9..cc161c4fc 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -852,20 +852,29 @@ array_astype(PyArrayObject *self, * and it's not a subtype if subok is False, then we * can skip the copy. */ - if (!forcecopy && (order == NPY_KEEPORDER || - (order == NPY_ANYORDER && - (PyArray_IS_C_CONTIGUOUS(self) || - PyArray_IS_F_CONTIGUOUS(self))) || - (order == NPY_CORDER && - PyArray_IS_C_CONTIGUOUS(self)) || - (order == NPY_FORTRANORDER && - PyArray_IS_F_CONTIGUOUS(self))) && - (subok || PyArray_CheckExact(self)) && - PyArray_EquivTypes(dtype, PyArray_DESCR(self))) { + if ( (forcecopy == IF_NEEDED || forcecopy == NEVER) && + (order == NPY_KEEPORDER || + (order == NPY_ANYORDER && + (PyArray_IS_C_CONTIGUOUS(self) || + PyArray_IS_F_CONTIGUOUS(self))) || + (order == NPY_CORDER && + PyArray_IS_C_CONTIGUOUS(self)) || + (order == NPY_FORTRANORDER && + PyArray_IS_F_CONTIGUOUS(self))) && + (subok || PyArray_CheckExact(self)) && + PyArray_EquivTypes(dtype, PyArray_DESCR(self))) { Py_DECREF(dtype); Py_INCREF(self); return (PyObject *)self; } + + if( forcecopy == NEVER ) { + PyErr_SetString(PyExc_ValueError, + "Unable to avoid copy while casting in np.CopyMode.NEVER"); + Py_DECREF(dtype); + return NULL; + } + if (!PyArray_CanCastArrayTo(self, dtype, casting)) { PyErr_Clear(); npy_set_invalid_cast_error( -- cgit v1.2.1 From 645fadbe3203da0846a2525f7a0d2080a8355ae9 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Mon, 7 Jun 2021 11:01:38 +0530 Subject: Updated API ready for formal testing --- numpy/__init__.pyi | 3 +-- numpy/core/include/numpy/ndarraytypes.h | 8 +++++++- numpy/core/src/multiarray/conversion_utils.c | 6 +++--- numpy/core/src/multiarray/methods.c | 6 +++--- numpy/core/src/multiarray/multiarraymodule.c | 26 ++++++++++++++++++++------ 5 files changed, 34 insertions(+), 15 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index a2d6ce435..0555b0151 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -203,7 +203,6 @@ from numpy import ( rec as rec, testing as testing, version as version, - CopyMode as CopyMode ) from numpy.core.function_base import ( @@ -3336,7 +3335,7 @@ def array( object: object, dtype: DTypeLike = ..., *, - copy: bool = ..., + copy: bool | CopyMode = ..., order: _OrderKACF = ..., subok: bool = ..., ndmin: int = ..., diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 86dc19c64..58a5aba47 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -451,7 +451,11 @@ typedef struct { int len; } PyArray_Dims; -typedef enum PyNpCopyMode {IF_NEEDED, ALWAYS, NEVER} PyNpCopyMode_Enum; +typedef enum PyNpCopyMode { + NPY_IF_NEEDED, + NPY_ALWAYS, + NPY_NEVER +} PyNpCopyMode_Enum; typedef struct { /* @@ -914,6 +918,8 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *); #define NPY_ARRAY_UPDATEIFCOPY 0x1000 /* Deprecated in 1.14 */ #define NPY_ARRAY_WRITEBACKIFCOPY 0x2000 +#define NPY_ARRAY_ENSURENOCOPY 0x4000 + /* * NOTE: there are also internal flags defined in multiarray/arrayobject.h, * which start at bit 31 and work down. diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 984b0b948..663b9009a 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -176,9 +176,9 @@ PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { int int_copymode = -1; PyArray_PythonPyIntFromInt(obj, &int_copymode); - if( int_copymode != ALWAYS && - int_copymode != IF_NEEDED && - int_copymode != NEVER ) { + if( int_copymode != NPY_ALWAYS && + int_copymode != NPY_IF_NEEDED && + int_copymode != NPY_NEVER ) { PyErr_Format(PyExc_ValueError, "Unrecognized copy mode %d. Please choose one of" "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER", diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index cc161c4fc..d431c6f97 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -852,7 +852,7 @@ array_astype(PyArrayObject *self, * and it's not a subtype if subok is False, then we * can skip the copy. */ - if ( (forcecopy == IF_NEEDED || forcecopy == NEVER) && + if ( (forcecopy == NPY_IF_NEEDED || forcecopy == NPY_NEVER) && (order == NPY_KEEPORDER || (order == NPY_ANYORDER && (PyArray_IS_C_CONTIGUOUS(self) || @@ -868,8 +868,8 @@ array_astype(PyArrayObject *self, return (PyObject *)self; } - if( forcecopy == NEVER ) { - PyErr_SetString(PyExc_ValueError, + if( forcecopy == NPY_NEVER ) { + PyErr_SetString(PyExc_RuntimeError, "Unable to avoid copy while casting in np.CopyMode.NEVER"); Py_DECREF(dtype); return NULL; diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index f7c3ea093..edb200700 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1557,7 +1557,7 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin, NPY_ORDER order) static NPY_INLINE PyObject * _array_fromobject_generic( - PyObject *op, PyArray_Descr *type, npy_bool copy, NPY_ORDER order, + PyObject *op, PyArray_Descr *type, PyNpCopyMode_Enum copy, NPY_ORDER order, npy_bool subok, int ndmin) { PyArrayObject *oparr = NULL, *ret = NULL; @@ -1574,12 +1574,18 @@ _array_fromobject_generic( if (PyArray_CheckExact(op) || (subok && PyArray_Check(op))) { oparr = (PyArrayObject *)op; if (type == NULL) { - if (!copy && STRIDING_OK(oparr, order)) { + if ((copy == NPY_IF_NEEDED || copy == NPY_NEVER) && + STRIDING_OK(oparr, order)) { ret = oparr; Py_INCREF(ret); goto finish; } else { + if( copy == NPY_NEVER ) { + PyErr_SetString(PyExc_RuntimeError, + "Unable to avoid copy while creating a new array."); + return NULL; + } ret = (PyArrayObject *)PyArray_NewCopy(oparr, order); goto finish; } @@ -1587,12 +1593,18 @@ _array_fromobject_generic( /* One more chance */ oldtype = PyArray_DESCR(oparr); if (PyArray_EquivTypes(oldtype, type)) { - if (!copy && STRIDING_OK(oparr, order)) { + if ((copy == NPY_IF_NEEDED || copy == NPY_NEVER) && + STRIDING_OK(oparr, order)) { Py_INCREF(op); ret = oparr; goto finish; } else { + if( copy == NPY_NEVER ) { + PyErr_SetString(PyExc_RuntimeError, + "Unable to avoid copy while creating a new array."); + return NULL; + } ret = (PyArrayObject *)PyArray_NewCopy(oparr, order); if (oldtype == type || ret == NULL) { goto finish; @@ -1605,8 +1617,10 @@ _array_fromobject_generic( } } - if (copy) { + if (copy == NPY_ALWAYS) { flags = NPY_ARRAY_ENSURECOPY; + } else if( copy == NPY_NEVER ) { + flags = NPY_ARRAY_ENSURENOCOPY; } if (order == NPY_CORDER) { flags |= NPY_ARRAY_C_CONTIGUOUS; @@ -1651,7 +1665,7 @@ array_array(PyObject *NPY_UNUSED(ignored), { PyObject *op; npy_bool subok = NPY_FALSE; - npy_bool copy = NPY_TRUE; + PyNpCopyMode_Enum copy = NPY_ALWAYS; int ndmin = 0; PyArray_Descr *type = NULL; NPY_ORDER order = NPY_KEEPORDER; @@ -1662,7 +1676,7 @@ array_array(PyObject *NPY_UNUSED(ignored), if (npy_parse_arguments("array", args, len_args, kwnames, "object", NULL, &op, "|dtype", &PyArray_DescrConverter2, &type, - "$copy", &PyArray_BoolConverter, ©, + "$copy", &PyArray_CopyConverter, ©, "$order", &PyArray_OrderConverter, &order, "$subok", &PyArray_BoolConverter, &subok, "$ndmin", &PyArray_PythonPyIntFromInt, &ndmin, -- cgit v1.2.1 From 8538720ffcbf0ff69e4380e3ec4bfa985197ec7e Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Mon, 7 Jun 2021 11:09:11 +0530 Subject: tests for astype added --- numpy/core/tests/test_api.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'numpy') diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index 9e99e0bc3..38261a8c3 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -585,3 +585,30 @@ def test_broadcast_arrays(): def test_full_from_list(shape, fill_value, expected_output): output = np.full(shape, fill_value) assert_equal(output, expected_output) + +def test_astype_copyflag(): + # test the various copyflag options + arr = np.arange(10, dtype=np.intp) + + res_true = arr.astype(np.intp, copy=True) + assert not np.may_share_memory(arr, res_true) + res_always = arr.astype(np.intp, copy=np.CopyMode.ALWAYS) + assert not np.may_share_memory(arr, res_always) + + res_false = arr.astype(np.intp, copy=False) + # `res_false is arr` currently, but check `may_share_memory`. + assert np.may_share_memory(arr, res_false) + res_if_needed = arr.astype(np.intp, copy=np.CopyMode.IF_NEEDED) + # `res_if_needed is arr` currently, but check `may_share_memory`. + assert np.may_share_memory(arr, res_if_needed) + + res_never = arr.astype(np.intp, copy=np.CopyMode.NEVER) + assert np.may_share_memory(arr, res_never) + + + # Simple tests for when a copy is necessary: + res_false = arr.astype(np.float64, copy=False) + assert_array_equal(res_false, arr) + res_if_needed = arr.astype(np.float64, copy=np.CopyMode.IF_NEEDED) + assert_array_equal(res_if_needed, arr) + assert_raises(RuntimeError, arr.astype, np.float64, copy=np.CopyMode.NEVER) -- cgit v1.2.1 From 8c00223ab944d18a7077c59f4ae0ed010f61a17e Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Mon, 7 Jun 2021 12:03:43 +0530 Subject: Added tests for np.array --- numpy/core/src/multiarray/conversion_utils.c | 8 +- numpy/core/tests/test_multiarray.py | 180 +++++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 663b9009a..fc4904729 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -168,8 +168,8 @@ NPY_NO_EXPORT int PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { if (obj == Py_None) { PyErr_SetString(PyExc_ValueError, - "NoneType copy mode not allowed. Please choose one of" - "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER"); + "NoneType copy mode not allowed. Please choose one of " + "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER."); return NPY_FAIL; } @@ -180,8 +180,8 @@ PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { int_copymode != NPY_IF_NEEDED && int_copymode != NPY_NEVER ) { PyErr_Format(PyExc_ValueError, - "Unrecognized copy mode %d. Please choose one of" - "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER", + "Unrecognized copy mode %d. Please choose one of " + "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER.", int_copymode); return NPY_FAIL; } diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index d567653f5..981e59e20 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7745,6 +7745,186 @@ class TestNewBufferProtocol: _multiarray_tests.corrupt_or_fix_bufferinfo(obj) +class TestArrayCreationCopyArgument(object): + + true_vals = [True, np.CopyMode.ALWAYS, np.True_] + false_vals = [False, np.CopyMode.IF_NEEDED, np.False_] + + def test_scalars(self): + + # Test both numpy and python scalars + for dtype in np.typecodes["All"]: + arr = np.zeros((), dtype=dtype) + scalar = arr[()] + pyscalar = arr.item(0) + + # Test never-copy raises error: + assert_raises(ValueError, np.array, scalar, copy=np.CopyMode.NEVER) + assert_raises(ValueError, np.array, pyscalar, copy=np.CopyMode.NEVER) + + + def test_compatible_cast(self): + + # Some types are compatible even though they are different, no + # copy is necessary for them. This is mostly true for some integers + def int_types(byteswap=False): + int_types = (np.typecodes["Integer"] + + np.typecodes["UnsignedInteger"]) + for int_type in int_types: + yield np.dtype(int_type) + if byteswap: + yield np.dtype(int_type).newbyteorder() + + for int1 in int_types(): + for int2 in int_types(True): + arr = np.arange(10, dtype=int1) + + for copy in self.true_vals: + res = np.array(arr, copy=copy, dtype=int2) + assert res is not arr and res.flags.owndata + assert_array_equal(res, arr) + + + + if int1 == int2: + # Casting is not necessary, base check is sufficient here + for copy in self.false_vals: + res = np.array(arr, copy=copy, dtype=int2) + assert res is arr or res.base is arr + + + res = np.array(arr, copy=np.CopyMode.NEVER, dtype=int2) + assert res is arr or res.base is arr + + + else: + # Casting is necessary, assert copy works: + for copy in self.false_vals: + res = np.array(arr, copy=copy, dtype=int2) + assert res is not arr and res.flags.owndata + assert_array_equal(res, arr) + + + assert_raises(ValueError, np.array, + arr, copy=np.CopyMode.NEVER, dtype=int2) + + + def test_buffer_interface(self): + + # Buffer interface gives direct memory access (no copy) + arr = np.arange(10) + view = memoryview(arr) + + # Checking bases is a bit tricky since numpy creates another + # memoryview, so use may_share_memory. + for copy in self.true_vals: + res = np.array(view, copy=copy) + assert not np.may_share_memory(arr, res) + for copy in self.false_vals: + res = np.array(view, copy=copy) + assert np.may_share_memory(arr, res) + res = np.array(view, copy=np.CopyMode.NEVER) + assert np.may_share_memory(arr, res) + + def test_array_interfaces(self): + # Array interface gives direct memory access (much like a memoryview) + base_arr = np.arange(10) + + + class ArrayLike: + __array_interface__ = base_arr.__array_interface__ + + arr = ArrayLike() + + for copy, val in [(True, None), (np.CopyMode.ALWAYS, None), + (False, arr), (np.CopyMode.IF_NEEDED, arr), + (np.CopyMode.NEVER, arr)]: + res = np.array(arr, copy=copy) + assert res.base is val + + def test___array__(self): + base_arr = np.arange(10) + + class ArrayLike: + def __array__(self): + # __array__ should return a copy, numpy cannot know this + # however. + return base_arr + + arr = ArrayLike() + + for copy in self.true_vals: + res = np.array(arr, copy=copy) + assert_array_equal(res, base_arr) + # An additional copy is currently forced by numpy in this case, + # you could argue, numpy does not trust the ArrayLike. This + # may be open for change: + assert res is not base_arr + + for copy in self.false_vals: + res = np.array(arr, copy=False) + assert_array_equal(res, base_arr) + assert res is base_arr # numpy trusts the ArrayLike + + assert_raises(RuntimeError, np.array, arr, copy=np.CopyMode.NEVER) + + + @pytest.mark.parametrize( + "arr", [np.ones(()), np.arange(81).reshape((9, 9))]) + @pytest.mark.parametrize("order1", ["C", "F", None]) + @pytest.mark.parametrize("order2", ["C", "F", "A", "K"]) + def test_order_mismatch(self, arr, order1, order2): + # The order is the main (python side) reason that can cause + # a never-copy to fail. + # Prepare C-order, F-order and non-contiguous arrays: + arr = arr.copy(order1) + if order1 == "C": + assert arr.flags.c_contiguous + elif order1 == "F": + assert arr.flags.f_contiguous + elif arr.ndim != 0: + # Make array non-contiguous + arr = arr[::2, ::2] + assert not arr.flags.forc + + + # Whether a copy is necessary depends on the order of arr: + if order2 == "C": + no_copy_necessary = arr.flags.c_contiguous + elif order2 == "F": + no_copy_necessary = arr.flags.f_contiguous + else: + # Keeporder and Anyorder are OK with non-contiguous output. + # This is not consistent with the `astype` behaviour which + # enforces contiguity for "A". It is probably historic from when + # "K" did not exist. + no_copy_necessary = True + + + # Test it for both the array and a memoryview + for view in [arr, memoryview(arr)]: + for copy in self.true_vals: + res = np.array(view, copy=copy, order=order2) + assert res is not arr and res.flags.owndata + assert_array_equal(arr, res) + + + if no_copy_necessary: + for copy in self.false_vals: + res = np.array(view, copy=copy, order=order2) + # res.base.obj refers to the memoryview + assert res is arr or res.base.obj is arr + + res = np.array(view, copy=np.CopyMode.NEVER, order=order2) + assert res is arr or res.base.obj is arr + else: + for copy in self.false_vals: + res = np.array(arr, copy=copy, order=order2) + assert_array_equal(arr, res) + assert_raises(ValueError, np.array, + view, copy=np.CopyMode.NEVER, order=order2) + + class TestArrayAttributeDeletion: def test_multiarray_writable_attributes_deletion(self): -- cgit v1.2.1 From 4e59da126b522dd5a894041a4c9570e130fe7ae4 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Mon, 7 Jun 2021 15:02:50 +0530 Subject: Testing for copy argument for existing APIs complete --- numpy/core/src/multiarray/ctors.c | 14 +++++++++++++- numpy/core/tests/test_multiarray.py | 19 +++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index ef28d7797..670a51077 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1842,6 +1842,11 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, if ((requires & NPY_ARRAY_ELEMENTSTRIDES) && !PyArray_ElementStrides(obj)) { PyObject *ret; + if( requires & NPY_ARRAY_ENSURENOCOPY ) { + PyErr_SetString(PyExc_RuntimeError, + "Unable to avoid copy while creating a new array."); + return NULL; + } ret = PyArray_NewCopy((PyArrayObject *)obj, NPY_ANYORDER); Py_DECREF(obj); obj = ret; @@ -1916,6 +1921,14 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) !PyArray_EquivTypes(oldtype, newtype); if (copy) { + + if( flags & NPY_ARRAY_ENSURENOCOPY ) { + PyErr_SetString(PyExc_RuntimeError, + "Unable to avoid copy while creating " + "an array from given array."); + return NULL; + } + NPY_ORDER order = NPY_KEEPORDER; int subok = 1; @@ -1988,7 +2001,6 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) if (flags & NPY_ARRAY_ENSUREARRAY) { subtype = &PyArray_Type; } - ret = (PyArrayObject *)PyArray_View(arr, NULL, subtype); if (ret == NULL) { return NULL; diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 981e59e20..734a7cea3 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7750,19 +7750,6 @@ class TestArrayCreationCopyArgument(object): true_vals = [True, np.CopyMode.ALWAYS, np.True_] false_vals = [False, np.CopyMode.IF_NEEDED, np.False_] - def test_scalars(self): - - # Test both numpy and python scalars - for dtype in np.typecodes["All"]: - arr = np.zeros((), dtype=dtype) - scalar = arr[()] - pyscalar = arr.item(0) - - # Test never-copy raises error: - assert_raises(ValueError, np.array, scalar, copy=np.CopyMode.NEVER) - assert_raises(ValueError, np.array, pyscalar, copy=np.CopyMode.NEVER) - - def test_compatible_cast(self): # Some types are compatible even though they are different, no @@ -7805,7 +7792,7 @@ class TestArrayCreationCopyArgument(object): assert_array_equal(res, arr) - assert_raises(ValueError, np.array, + assert_raises(RuntimeError, np.array, arr, copy=np.CopyMode.NEVER, dtype=int2) @@ -7866,7 +7853,7 @@ class TestArrayCreationCopyArgument(object): assert_array_equal(res, base_arr) assert res is base_arr # numpy trusts the ArrayLike - assert_raises(RuntimeError, np.array, arr, copy=np.CopyMode.NEVER) + assert np.array(arr, copy=np.CopyMode.NEVER) is base_arr @pytest.mark.parametrize( @@ -7921,7 +7908,7 @@ class TestArrayCreationCopyArgument(object): for copy in self.false_vals: res = np.array(arr, copy=copy, order=order2) assert_array_equal(arr, res) - assert_raises(ValueError, np.array, + assert_raises(RuntimeError, np.array, view, copy=np.CopyMode.NEVER, order=order2) -- cgit v1.2.1 From 6bff3dd72e36e009dd4417054d49b64f0458a821 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Tue, 8 Jun 2021 08:35:48 +0530 Subject: Added some notes for future commits --- numpy/core/src/multiarray/array_coercion.c | 1 + numpy/core/src/multiarray/ctors.c | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index 22050a56f..d567ee0a4 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -1163,6 +1163,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( * It might be nice to deprecate this? But it allows things such as * `arr1d[...] = np.array([[1,2,3,4]])` */ +// Here we check whether a copy is being made or not. Check this function. NPY_NO_EXPORT int PyArray_DiscoverDTypeAndShape( PyObject *obj, int max_dims, diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 670a51077..a125ce81f 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1282,7 +1282,7 @@ _array_from_array_like(PyObject *op, PyErr_Clear(); } else { - tmp = _array_from_buffer_3118(memoryview); + tmp = _array_from_buffer_3118(memoryview); // Assume: never creates a copy Py_DECREF(memoryview); if (tmp == NULL) { return NULL; @@ -1324,7 +1324,7 @@ _array_from_array_like(PyObject *op, * this should be changed! */ if (!writeable && tmp == Py_NotImplemented) { - tmp = PyArray_FromArrayAttr(op, requested_dtype, context); + tmp = PyArray_FromArrayAttr(op, requested_dtype, context); // Assume: array was copied. if (tmp == NULL) { return NULL; } -- cgit v1.2.1 From a90880a0a61cb32a23c7eb4bab463941714c59d9 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Tue, 8 Jun 2021 09:55:23 +0530 Subject: RuntimeError -> ValueError --- numpy/core/src/multiarray/ctors.c | 4 ++-- numpy/core/src/multiarray/methods.c | 2 +- numpy/core/src/multiarray/multiarraymodule.c | 4 ++-- numpy/core/tests/test_api.py | 2 +- numpy/core/tests/test_multiarray.py | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index a125ce81f..87406bdbc 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1843,7 +1843,7 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, !PyArray_ElementStrides(obj)) { PyObject *ret; if( requires & NPY_ARRAY_ENSURENOCOPY ) { - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating a new array."); return NULL; } @@ -1923,7 +1923,7 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) if (copy) { if( flags & NPY_ARRAY_ENSURENOCOPY ) { - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating " "an array from given array."); return NULL; diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index d431c6f97..9613bf04e 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -869,7 +869,7 @@ array_astype(PyArrayObject *self, } if( forcecopy == NPY_NEVER ) { - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while casting in np.CopyMode.NEVER"); Py_DECREF(dtype); return NULL; diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index edb200700..489c51df3 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1582,7 +1582,7 @@ _array_fromobject_generic( } else { if( copy == NPY_NEVER ) { - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating a new array."); return NULL; } @@ -1601,7 +1601,7 @@ _array_fromobject_generic( } else { if( copy == NPY_NEVER ) { - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating a new array."); return NULL; } diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index 38261a8c3..3bd01e5b4 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -611,4 +611,4 @@ def test_astype_copyflag(): assert_array_equal(res_false, arr) res_if_needed = arr.astype(np.float64, copy=np.CopyMode.IF_NEEDED) assert_array_equal(res_if_needed, arr) - assert_raises(RuntimeError, arr.astype, np.float64, copy=np.CopyMode.NEVER) + assert_raises(ValueError, arr.astype, np.float64, copy=np.CopyMode.NEVER) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 734a7cea3..ba8caddda 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7792,7 +7792,7 @@ class TestArrayCreationCopyArgument(object): assert_array_equal(res, arr) - assert_raises(RuntimeError, np.array, + assert_raises(ValueError, np.array, arr, copy=np.CopyMode.NEVER, dtype=int2) @@ -7908,7 +7908,7 @@ class TestArrayCreationCopyArgument(object): for copy in self.false_vals: res = np.array(arr, copy=copy, order=order2) assert_array_equal(arr, res) - assert_raises(RuntimeError, np.array, + assert_raises(ValueError, np.array, view, copy=np.CopyMode.NEVER, order=order2) -- cgit v1.2.1 From 5481a8ac389962f855d15c91ba0fa365b3f06c90 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Tue, 8 Jun 2021 13:40:41 +0530 Subject: enum.IntEnum -> enum.Enum --- numpy/__init__.pyi | 2 +- numpy/_globals.py | 2 +- numpy/core/src/multiarray/conversion_utils.c | 24 +++++++++++++++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 6bf9d49a8..f086a7a0f 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -3642,7 +3642,7 @@ trunc: _UFunc_Nin1_Nout1[L['trunc'], L[7], None] abs = absolute -class CopyMode(enum.IntEnum): +class CopyMode(enum.Enum): ALWAYS: L[1] IF_NEEDED: L[0] diff --git a/numpy/_globals.py b/numpy/_globals.py index a6fbec340..e40694e1a 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -93,7 +93,7 @@ class _NoValueType: _NoValue = _NoValueType() -class CopyMode(enum.IntEnum): +class CopyMode(enum.Enum): ALWAYS = 1 IF_NEEDED = 0 diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index fc4904729..e16c03e31 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -174,15 +174,29 @@ PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { } int int_copymode = -1; - PyArray_PythonPyIntFromInt(obj, &int_copymode); + PyObject* numpy_CopyMode = NULL; + npy_cache_import("numpy._globals", "CopyMode", &numpy_CopyMode); + if( numpy_CopyMode != NULL ) { + if(PyObject_IsInstance(obj, numpy_CopyMode)) { + PyObject* mode_value = PyObject_GetAttrString(obj, "value"); + PyArray_PythonPyIntFromInt(mode_value, &int_copymode); + } + } + + // If obj is not an instance of numpy.CopyMode then follow + // the conventional assumption that it must be value that + // can be converted to an integer. + if( int_copymode < 0 ) { + PyArray_PythonPyIntFromInt(obj, &int_copymode); + } if( int_copymode != NPY_ALWAYS && int_copymode != NPY_IF_NEEDED && int_copymode != NPY_NEVER ) { - PyErr_Format(PyExc_ValueError, - "Unrecognized copy mode %d. Please choose one of " - "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER.", - int_copymode); + PyErr_SetString(PyExc_ValueError, + "Unrecognized copy mode. Please choose one of " + "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER, " + "True/np.True_, False/np.False_"); return NPY_FAIL; } -- cgit v1.2.1 From f2e7a818561c59e014d0627a187ae359dafe7a37 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Tue, 8 Jun 2021 13:43:04 +0530 Subject: aligned astype with main branch --- numpy/__init__.pyi | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index f086a7a0f..6cbc05782 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1227,14 +1227,6 @@ class _ArrayOrScalarCommon: def __deepcopy__(self: _ArraySelf, __memo: Optional[dict] = ...) -> _ArraySelf: ... def __eq__(self, other): ... def __ne__(self, other): ... - def astype( - self: _ArraySelf, - dtype: DTypeLike, - order: _OrderKACF = ..., - casting: _Casting = ..., - subok: bool = ..., - copy: bool | CopyMode = ..., - ) -> _ArraySelf: ... def copy(self: _ArraySelf, order: _OrderKACF = ...) -> _ArraySelf: ... def dump(self, file: str) -> None: ... def dumps(self) -> bytes: ... @@ -1877,7 +1869,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): order: _OrderKACF = ..., casting: _Casting = ..., subok: bool = ..., - copy: bool = ..., + copy: bool | CopyMode = ..., ) -> NDArray[_ScalarType]: ... @overload def astype( @@ -1886,7 +1878,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): order: _OrderKACF = ..., casting: _Casting = ..., subok: bool = ..., - copy: bool = ..., + copy: bool | CopyMode = ..., ) -> NDArray[Any]: ... @overload @@ -2897,7 +2889,7 @@ class generic(_ArrayOrScalarCommon): order: _OrderKACF = ..., casting: _Casting = ..., subok: bool = ..., - copy: bool = ..., + copy: bool | CopyMode = ..., ) -> _ScalarType: ... @overload def astype( @@ -2906,7 +2898,7 @@ class generic(_ArrayOrScalarCommon): order: _OrderKACF = ..., casting: _Casting = ..., subok: bool = ..., - copy: bool = ..., + copy: bool | CopyMode = ..., ) -> Any: ... # NOTE: `view` will perform a 0D->scalar cast, -- cgit v1.2.1 From e828f1569dd65cb8a93a619fc89edec582c744cc Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Tue, 8 Jun 2021 14:56:41 +0530 Subject: Add tests for scalars --- numpy/core/src/multiarray/ctors.c | 7 +++++++ numpy/core/tests/test_multiarray.py | 11 +++++++++++ 2 files changed, 18 insertions(+) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 87406bdbc..c2abc299c 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1715,6 +1715,12 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, /* Create a new array and copy the data */ Py_INCREF(dtype); /* hold on in case of a subarray that is replaced */ + if( flags & NPY_ARRAY_ENSURENOCOPY ) { + PyErr_SetString(PyExc_ValueError, + "Unable to avoid copy while creating " + "an array from descriptor."); + return NULL; + } ret = (PyArrayObject *)PyArray_NewFromDescr( &PyArray_Type, dtype, ndim, dims, NULL, NULL, flags&NPY_ARRAY_F_CONTIGUOUS, NULL); @@ -1839,6 +1845,7 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, if (obj == NULL) { return NULL; } + if ((requires & NPY_ARRAY_ELEMENTSTRIDES) && !PyArray_ElementStrides(obj)) { PyObject *ret; diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index ba8caddda..8fd7ccf4e 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7750,6 +7750,17 @@ class TestArrayCreationCopyArgument(object): true_vals = [True, np.CopyMode.ALWAYS, np.True_] false_vals = [False, np.CopyMode.IF_NEEDED, np.False_] + def test_scalars(self): + # Test both numpy and python scalars + for dtype in np.typecodes["All"]: + arr = np.zeros((), dtype=dtype) + scalar = arr[()] + pyscalar = arr.item(0) + + # Test never-copy raises error: + assert_raises(ValueError, np.array, scalar, copy=np.CopyMode.NEVER) + assert_raises(ValueError, np.array, pyscalar, copy=np.CopyMode.NEVER) + def test_compatible_cast(self): # Some types are compatible even though they are different, no -- cgit v1.2.1 From 91d669396a7f73abecd511f1c49076f2bc441e5c Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Tue, 8 Jun 2021 15:05:20 +0530 Subject: resolved linting issues --- numpy/core/tests/test_multiarray.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 70e4f1044..22816643a 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7765,8 +7765,10 @@ class TestArrayCreationCopyArgument(object): pyscalar = arr.item(0) # Test never-copy raises error: - assert_raises(ValueError, np.array, scalar, copy=np.CopyMode.NEVER) - assert_raises(ValueError, np.array, pyscalar, copy=np.CopyMode.NEVER) + assert_raises(ValueError, np.array, scalar, + copy=np.CopyMode.NEVER) + assert_raises(ValueError, np.array, pyscalar, + copy=np.CopyMode.NEVER) def test_compatible_cast(self): @@ -7809,7 +7811,6 @@ class TestArrayCreationCopyArgument(object): assert res is not arr and res.flags.owndata assert_array_equal(res, arr) - assert_raises(ValueError, np.array, arr, copy=np.CopyMode.NEVER, dtype=int2) -- cgit v1.2.1 From 7b53a02c647eb9b1c627799e7ce1b7fecb0bf928 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Tue, 8 Jun 2021 15:09:29 +0530 Subject: Fixed blank line linting issue --- numpy/_globals.py | 3 ++- numpy/core/tests/test_api.py | 1 - numpy/core/tests/test_multiarray.py | 10 ---------- 3 files changed, 2 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/_globals.py b/numpy/_globals.py index e40694e1a..9a99acde3 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -109,4 +109,5 @@ class CopyMode(enum.Enum): raise TypeError(f"{self} is neither True nor False.") -CopyMode.__module__ = 'numpy' \ No newline at end of file + +CopyMode.__module__ = 'numpy' diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index 0204d79c9..d48b20800 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -618,7 +618,6 @@ def test_astype_copyflag(): res_never = arr.astype(np.intp, copy=np.CopyMode.NEVER) assert np.may_share_memory(arr, res_never) - # Simple tests for when a copy is necessary: res_false = arr.astype(np.float64, copy=False) assert_array_equal(res_false, arr) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 22816643a..d1246f11c 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7791,19 +7791,15 @@ class TestArrayCreationCopyArgument(object): assert res is not arr and res.flags.owndata assert_array_equal(res, arr) - - if int1 == int2: # Casting is not necessary, base check is sufficient here for copy in self.false_vals: res = np.array(arr, copy=copy, dtype=int2) assert res is arr or res.base is arr - res = np.array(arr, copy=np.CopyMode.NEVER, dtype=int2) assert res is arr or res.base is arr - else: # Casting is necessary, assert copy works: for copy in self.false_vals: @@ -7814,7 +7810,6 @@ class TestArrayCreationCopyArgument(object): assert_raises(ValueError, np.array, arr, copy=np.CopyMode.NEVER, dtype=int2) - def test_buffer_interface(self): # Buffer interface gives direct memory access (no copy) @@ -7836,7 +7831,6 @@ class TestArrayCreationCopyArgument(object): # Array interface gives direct memory access (much like a memoryview) base_arr = np.arange(10) - class ArrayLike: __array_interface__ = base_arr.__array_interface__ @@ -7874,7 +7868,6 @@ class TestArrayCreationCopyArgument(object): assert np.array(arr, copy=np.CopyMode.NEVER) is base_arr - @pytest.mark.parametrize( "arr", [np.ones(()), np.arange(81).reshape((9, 9))]) @pytest.mark.parametrize("order1", ["C", "F", None]) @@ -7893,7 +7886,6 @@ class TestArrayCreationCopyArgument(object): arr = arr[::2, ::2] assert not arr.flags.forc - # Whether a copy is necessary depends on the order of arr: if order2 == "C": no_copy_necessary = arr.flags.c_contiguous @@ -7906,7 +7898,6 @@ class TestArrayCreationCopyArgument(object): # "K" did not exist. no_copy_necessary = True - # Test it for both the array and a memoryview for view in [arr, memoryview(arr)]: for copy in self.true_vals: @@ -7914,7 +7905,6 @@ class TestArrayCreationCopyArgument(object): assert res is not arr and res.flags.owndata assert_array_equal(arr, res) - if no_copy_necessary: for copy in self.false_vals: res = np.array(view, copy=copy, order=order2) -- cgit v1.2.1 From eedb30087d2c1de95b2a65aef074f9aade1b9658 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 9 Jun 2021 08:52:57 +0530 Subject: Apply suggestions from code review Co-authored-by: Eric Wieser --- numpy/_globals.py | 4 ++-- numpy/core/include/numpy/ndarraytypes.h | 8 ++++---- numpy/core/src/multiarray/conversion_utils.c | 16 +++++++++++----- numpy/core/src/multiarray/methods.c | 5 +++-- 4 files changed, 20 insertions(+), 13 deletions(-) (limited to 'numpy') diff --git a/numpy/_globals.py b/numpy/_globals.py index 9a99acde3..b7a399a79 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -95,8 +95,8 @@ _NoValue = _NoValueType() class CopyMode(enum.Enum): - ALWAYS = 1 - IF_NEEDED = 0 + ALWAYS = True + IF_NEEDED = False NEVER = 2 def __bool__(self): diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 6626da1e9..6a861dc1d 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -452,10 +452,10 @@ typedef struct { } PyArray_Dims; typedef enum PyNpCopyMode { - NPY_IF_NEEDED, - NPY_ALWAYS, - NPY_NEVER -} PyNpCopyMode_Enum; + NPY_COPY_IF_NEEDED, + NPY_COPY_ALWAYS, + NPY_COPY_NEVER +} PyArray_CopyMode; typedef struct { /* diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index e16c03e31..8f608ea3e 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -176,17 +176,23 @@ PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { int int_copymode = -1; PyObject* numpy_CopyMode = NULL; npy_cache_import("numpy._globals", "CopyMode", &numpy_CopyMode); - if( numpy_CopyMode != NULL ) { - if(PyObject_IsInstance(obj, numpy_CopyMode)) { - PyObject* mode_value = PyObject_GetAttrString(obj, "value"); - PyArray_PythonPyIntFromInt(mode_value, &int_copymode); + if (numpy_CopyMode == NULL) { + return NPY_FAIL; + } + if (PyObject_IsInstance(obj, numpy_CopyMode)) { + PyObject* mode_value = PyObject_GetAttrString(obj, "value"); + if (mode_value == NULL) { + return NPY_FAIL; + } + if (!PyArray_PythonPyIntFromInt(mode_value, &int_copymode)) { + return NPY_FAIL; } } // If obj is not an instance of numpy.CopyMode then follow // the conventional assumption that it must be value that // can be converted to an integer. - if( int_copymode < 0 ) { + else { PyArray_PythonPyIntFromInt(obj, &int_copymode); } diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 9613bf04e..a3b97ea7c 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -828,7 +828,8 @@ array_astype(PyArrayObject *self, */ NPY_CASTING casting = NPY_UNSAFE_CASTING; NPY_ORDER order = NPY_KEEPORDER; - PyNpCopyMode_Enum forcecopy = 1, subok = 1; + PyNpCopyMode_Enum forcecopy = 1; + int subok = 1; NPY_PREPARE_ARGPARSER; if (npy_parse_arguments("astype", args, len_args, kwnames, "dtype", &PyArray_DescrConverter, &dtype, @@ -852,7 +853,7 @@ array_astype(PyArrayObject *self, * and it's not a subtype if subok is False, then we * can skip the copy. */ - if ( (forcecopy == NPY_IF_NEEDED || forcecopy == NPY_NEVER) && + if (forcecopy != NPY_ALWAYS && (order == NPY_KEEPORDER || (order == NPY_ANYORDER && (PyArray_IS_C_CONTIGUOUS(self) || -- cgit v1.2.1 From bc8903d5fe64b705d03011a472357175d6aadce4 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Wed, 9 Jun 2021 09:20:57 +0530 Subject: renaming complete --- numpy/core/src/multiarray/conversion_utils.c | 10 +++++----- numpy/core/src/multiarray/conversion_utils.h | 2 +- numpy/core/src/multiarray/methods.c | 6 +++--- numpy/core/src/multiarray/multiarraymodule.c | 16 ++++++++-------- 4 files changed, 17 insertions(+), 17 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 8f608ea3e..22b77070c 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -165,7 +165,7 @@ PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq) } NPY_NO_EXPORT int -PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { +PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { if (obj == Py_None) { PyErr_SetString(PyExc_ValueError, "NoneType copy mode not allowed. Please choose one of " @@ -196,9 +196,9 @@ PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { PyArray_PythonPyIntFromInt(obj, &int_copymode); } - if( int_copymode != NPY_ALWAYS && - int_copymode != NPY_IF_NEEDED && - int_copymode != NPY_NEVER ) { + if( int_copymode != NPY_COPY_ALWAYS && + int_copymode != NPY_COPY_IF_NEEDED && + int_copymode != NPY_COPY_NEVER ) { PyErr_SetString(PyExc_ValueError, "Unrecognized copy mode. Please choose one of " "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER, " @@ -206,7 +206,7 @@ PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copymode) { return NPY_FAIL; } - *copymode = (PyNpCopyMode_Enum) int_copymode; + *copymode = (PyArray_CopyMode) int_copymode; return NPY_SUCCEED; } diff --git a/numpy/core/src/multiarray/conversion_utils.h b/numpy/core/src/multiarray/conversion_utils.h index d2874aafa..d1f17c58e 100644 --- a/numpy/core/src/multiarray/conversion_utils.h +++ b/numpy/core/src/multiarray/conversion_utils.h @@ -10,7 +10,7 @@ NPY_NO_EXPORT int PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq); NPY_NO_EXPORT int -PyArray_CopyConverter(PyObject *obj, PyNpCopyMode_Enum *copyflag); +PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copyflag); NPY_NO_EXPORT int PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf); diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index a3b97ea7c..5e1a8b0e5 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -828,7 +828,7 @@ array_astype(PyArrayObject *self, */ NPY_CASTING casting = NPY_UNSAFE_CASTING; NPY_ORDER order = NPY_KEEPORDER; - PyNpCopyMode_Enum forcecopy = 1; + PyArray_CopyMode forcecopy = 1; int subok = 1; NPY_PREPARE_ARGPARSER; if (npy_parse_arguments("astype", args, len_args, kwnames, @@ -853,7 +853,7 @@ array_astype(PyArrayObject *self, * and it's not a subtype if subok is False, then we * can skip the copy. */ - if (forcecopy != NPY_ALWAYS && + if (forcecopy != NPY_COPY_ALWAYS && (order == NPY_KEEPORDER || (order == NPY_ANYORDER && (PyArray_IS_C_CONTIGUOUS(self) || @@ -869,7 +869,7 @@ array_astype(PyArrayObject *self, return (PyObject *)self; } - if( forcecopy == NPY_NEVER ) { + if( forcecopy == NPY_COPY_NEVER ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while casting in np.CopyMode.NEVER"); Py_DECREF(dtype); diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 489c51df3..7e1311ea3 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1557,7 +1557,7 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin, NPY_ORDER order) static NPY_INLINE PyObject * _array_fromobject_generic( - PyObject *op, PyArray_Descr *type, PyNpCopyMode_Enum copy, NPY_ORDER order, + PyObject *op, PyArray_Descr *type, PyArray_CopyMode copy, NPY_ORDER order, npy_bool subok, int ndmin) { PyArrayObject *oparr = NULL, *ret = NULL; @@ -1574,14 +1574,14 @@ _array_fromobject_generic( if (PyArray_CheckExact(op) || (subok && PyArray_Check(op))) { oparr = (PyArrayObject *)op; if (type == NULL) { - if ((copy == NPY_IF_NEEDED || copy == NPY_NEVER) && + if ((copy == NPY_COPY_IF_NEEDED || copy == NPY_COPY_NEVER) && STRIDING_OK(oparr, order)) { ret = oparr; Py_INCREF(ret); goto finish; } else { - if( copy == NPY_NEVER ) { + if( copy == NPY_COPY_NEVER ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating a new array."); return NULL; @@ -1593,14 +1593,14 @@ _array_fromobject_generic( /* One more chance */ oldtype = PyArray_DESCR(oparr); if (PyArray_EquivTypes(oldtype, type)) { - if ((copy == NPY_IF_NEEDED || copy == NPY_NEVER) && + if ((copy == NPY_COPY_IF_NEEDED || copy == NPY_COPY_NEVER) && STRIDING_OK(oparr, order)) { Py_INCREF(op); ret = oparr; goto finish; } else { - if( copy == NPY_NEVER ) { + if( copy == NPY_COPY_NEVER ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating a new array."); return NULL; @@ -1617,9 +1617,9 @@ _array_fromobject_generic( } } - if (copy == NPY_ALWAYS) { + if (copy == NPY_COPY_ALWAYS) { flags = NPY_ARRAY_ENSURECOPY; - } else if( copy == NPY_NEVER ) { + } else if( copy == NPY_COPY_NEVER ) { flags = NPY_ARRAY_ENSURENOCOPY; } if (order == NPY_CORDER) { @@ -1665,7 +1665,7 @@ array_array(PyObject *NPY_UNUSED(ignored), { PyObject *op; npy_bool subok = NPY_FALSE; - PyNpCopyMode_Enum copy = NPY_ALWAYS; + PyArray_CopyMode copy = NPY_COPY_ALWAYS; int ndmin = 0; PyArray_Descr *type = NULL; NPY_ORDER order = NPY_KEEPORDER; -- cgit v1.2.1 From 3268a48ba4c0e7ae97dc358fa85e7c1b09d7cb21 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Wed, 9 Jun 2021 12:08:08 +0530 Subject: fixed failures due to Python 3.8.2 --- numpy/core/src/multiarray/conversion_utils.c | 52 ++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 11 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 22b77070c..c5e1ef808 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -175,11 +175,11 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { int int_copymode = -1; PyObject* numpy_CopyMode = NULL; + PyObject* numpy_bool_ = NULL; npy_cache_import("numpy._globals", "CopyMode", &numpy_CopyMode); - if (numpy_CopyMode == NULL) { - return NPY_FAIL; - } - if (PyObject_IsInstance(obj, numpy_CopyMode)) { + npy_cache_import("numpy", "bool_", &numpy_bool_); + + if (numpy_CopyMode != NULL && PyObject_IsInstance(obj, numpy_CopyMode)) { PyObject* mode_value = PyObject_GetAttrString(obj, "value"); if (mode_value == NULL) { return NPY_FAIL; @@ -187,22 +187,52 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { if (!PyArray_PythonPyIntFromInt(mode_value, &int_copymode)) { return NPY_FAIL; } + } else if(numpy_bool_ != NULL && PyObject_IsInstance(obj, numpy_bool_)) { + /* + In Python 3.8.2, numpy.bool_ objects are not converted + to integers using PyLong_AsLong. Hence, the following + if condition checks whether the input obj is a numpy.bool_ object + and extracts the values by perfroming comparisions between + obj and numpy.True_/numpy.False_. + */ + PyObject *numpy_True_ = NULL, *numpy_False_ = NULL; + npy_cache_import("numpy", "True_", &numpy_True_); + npy_cache_import("numpy", "False_", &numpy_False_); + if( numpy_True_ != NULL && + PyObject_RichCompareBool(obj, numpy_True_, Py_EQ) ) { + int_copymode = 1; + } else if( numpy_False_ != NULL && + PyObject_RichCompareBool(obj, numpy_False_, Py_EQ) ) { + int_copymode = 0; + } + } else if( PyBool_Check(obj) ) { + /* + In Python 3.8.2, Boolean objects are not converted + to integers using PyLong_AsLong. Hence, the following + if condition checks whether the input obj is a Boolean object + and extracts the values by perfroming comparisions between + obj and Py_True/Py_False. + */ + if( PyObject_RichCompareBool(obj, Py_True, Py_EQ) ) { + int_copymode = 1; + } else if( PyObject_RichCompareBool(obj, Py_False, Py_EQ) ) { + int_copymode = 0; + } } - - // If obj is not an instance of numpy.CopyMode then follow - // the conventional assumption that it must be value that - // can be converted to an integer. else { + // If obj is not an instance of numpy.CopyMode then follow + // the conventional assumption that it must be value that + // can be converted to an integer. PyArray_PythonPyIntFromInt(obj, &int_copymode); } if( int_copymode != NPY_COPY_ALWAYS && int_copymode != NPY_COPY_IF_NEEDED && int_copymode != NPY_COPY_NEVER ) { - PyErr_SetString(PyExc_ValueError, - "Unrecognized copy mode. Please choose one of " + PyErr_Format(PyExc_ValueError, + "Unrecognized copy mode %d. Please choose one of " "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER, " - "True/np.True_, False/np.False_"); + "True/np.True_, False/np.False_", int_copymode); return NPY_FAIL; } -- cgit v1.2.1 From 782d823301ac932ce0a41b816403b83cce54d20b Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Thu, 10 Jun 2021 09:04:44 +0530 Subject: fixed PYPY errors --- numpy/core/tests/test_multiarray.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index d1246f11c..54386de95 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7909,10 +7909,12 @@ class TestArrayCreationCopyArgument(object): for copy in self.false_vals: res = np.array(view, copy=copy, order=order2) # res.base.obj refers to the memoryview - assert res is arr or res.base.obj is arr + if not IS_PYPY: + assert res is arr or res.base.obj is arr res = np.array(view, copy=np.CopyMode.NEVER, order=order2) - assert res is arr or res.base.obj is arr + if not IS_PYPY: + assert res is arr or res.base.obj is arr else: for copy in self.false_vals: res = np.array(arr, copy=copy, order=order2) -- cgit v1.2.1 From 342e0c55d97a49749ed685d7f0f4ab03f5c8551b Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Thu, 10 Jun 2021 09:18:17 +0530 Subject: removed complicated checks for copy_mode --- numpy/core/src/multiarray/conversion_utils.c | 43 ++++------------------------ 1 file changed, 5 insertions(+), 38 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index c5e1ef808..8208bd067 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -175,9 +175,7 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { int int_copymode = -1; PyObject* numpy_CopyMode = NULL; - PyObject* numpy_bool_ = NULL; npy_cache_import("numpy._globals", "CopyMode", &numpy_CopyMode); - npy_cache_import("numpy", "bool_", &numpy_bool_); if (numpy_CopyMode != NULL && PyObject_IsInstance(obj, numpy_CopyMode)) { PyObject* mode_value = PyObject_GetAttrString(obj, "value"); @@ -187,43 +185,12 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { if (!PyArray_PythonPyIntFromInt(mode_value, &int_copymode)) { return NPY_FAIL; } - } else if(numpy_bool_ != NULL && PyObject_IsInstance(obj, numpy_bool_)) { - /* - In Python 3.8.2, numpy.bool_ objects are not converted - to integers using PyLong_AsLong. Hence, the following - if condition checks whether the input obj is a numpy.bool_ object - and extracts the values by perfroming comparisions between - obj and numpy.True_/numpy.False_. - */ - PyObject *numpy_True_ = NULL, *numpy_False_ = NULL; - npy_cache_import("numpy", "True_", &numpy_True_); - npy_cache_import("numpy", "False_", &numpy_False_); - if( numpy_True_ != NULL && - PyObject_RichCompareBool(obj, numpy_True_, Py_EQ) ) { - int_copymode = 1; - } else if( numpy_False_ != NULL && - PyObject_RichCompareBool(obj, numpy_False_, Py_EQ) ) { - int_copymode = 0; - } - } else if( PyBool_Check(obj) ) { - /* - In Python 3.8.2, Boolean objects are not converted - to integers using PyLong_AsLong. Hence, the following - if condition checks whether the input obj is a Boolean object - and extracts the values by perfroming comparisions between - obj and Py_True/Py_False. - */ - if( PyObject_RichCompareBool(obj, Py_True, Py_EQ) ) { - int_copymode = 1; - } else if( PyObject_RichCompareBool(obj, Py_False, Py_EQ) ) { - int_copymode = 0; + } else { + npy_bool bool_copymode; + if( !PyArray_BoolConverter(obj, &bool_copymode) ) { + return NPY_FAIL; } - } - else { - // If obj is not an instance of numpy.CopyMode then follow - // the conventional assumption that it must be value that - // can be converted to an integer. - PyArray_PythonPyIntFromInt(obj, &int_copymode); + int_copymode = (int) bool_copymode; } if( int_copymode != NPY_COPY_ALWAYS && -- cgit v1.2.1 From 30e34729c68288514a27211dd4475bc74441aa49 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Tue, 15 Jun 2021 08:52:55 +0530 Subject: interface shifted --- numpy/core/multiarray.pyi | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'numpy') diff --git a/numpy/core/multiarray.pyi b/numpy/core/multiarray.pyi index bc33165be..6d6d90da2 100644 --- a/numpy/core/multiarray.pyi +++ b/numpy/core/multiarray.pyi @@ -20,6 +20,7 @@ from typing import ( from numpy import ( # Re-exports + CopyMode, busdaycalendar as busdaycalendar, broadcast as broadcast, dtype as dtype, @@ -177,7 +178,7 @@ def array( object: _ArrayType, dtype: None = ..., *, - copy: bool = ..., + copy: bool | CopyMode = ..., order: _OrderKACF = ..., subok: L[True], ndmin: int = ..., @@ -188,7 +189,7 @@ def array( object: _ArrayLike[_SCT], dtype: None = ..., *, - copy: bool = ..., + copy: bool | CopyMode = ..., order: _OrderKACF = ..., subok: bool = ..., ndmin: int = ..., @@ -199,7 +200,7 @@ def array( object: object, dtype: None = ..., *, - copy: bool = ..., + copy: bool | CopyMode = ..., order: _OrderKACF = ..., subok: bool = ..., ndmin: int = ..., @@ -210,7 +211,7 @@ def array( object: Any, dtype: _DTypeLike[_SCT], *, - copy: bool = ..., + copy: bool | CopyMode = ..., order: _OrderKACF = ..., subok: bool = ..., ndmin: int = ..., @@ -221,7 +222,7 @@ def array( object: Any, dtype: DTypeLike, *, - copy: bool = ..., + copy: bool | CopyMode = ..., order: _OrderKACF = ..., subok: bool = ..., ndmin: int = ..., @@ -948,3 +949,9 @@ def compare_chararrays( ) -> NDArray[bool_]: ... def add_docstring(__obj: Callable[..., Any], __docstring: str) -> None: ... + +class CopyMode(enum.Enum): + + ALWAYS: L[1] + IF_NEEDED: L[0] + NEVER: L[2] -- cgit v1.2.1 From 321e028b90526ec40df281ae543f18aa4434bcd3 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Sat, 7 Aug 2021 11:05:40 +0530 Subject: Shifted to CopyMode to np.array_api --- numpy/__init__.py | 5 ++--- numpy/__init__.pyi | 2 +- numpy/_globals.py | 25 ++----------------------- numpy/array_api.py | 24 ++++++++++++++++++++++++ numpy/core/multiarray.pyi | 5 ++++- numpy/core/src/multiarray/conversion_utils.c | 9 ++++++--- numpy/core/src/multiarray/methods.c | 3 ++- numpy/core/tests/test_api.py | 10 +++++----- numpy/core/tests/test_multiarray.py | 26 +++++++++++++------------- numpy/tests/test_public_api.py | 2 ++ 10 files changed, 61 insertions(+), 50 deletions(-) create mode 100644 numpy/array_api.py (limited to 'numpy') diff --git a/numpy/__init__.py b/numpy/__init__.py index 476815520..83d2c279e 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -111,7 +111,7 @@ import warnings from ._globals import ( ModuleDeprecationWarning, VisibleDeprecationWarning, - _NoValue, CopyMode + _NoValue ) # We first need to detect if we're being called as part of the numpy setup @@ -133,8 +133,7 @@ else: raise ImportError(msg) from e __all__ = ['ModuleDeprecationWarning', - 'VisibleDeprecationWarning', - 'CopyMode'] + 'VisibleDeprecationWarning'] # get the version using versioneer from ._version import get_versions diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index e036a32fe..c8bdae5f0 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -3876,4 +3876,4 @@ class iinfo(Generic[_IntType]): @overload def __new__(cls, dtype: int | Type[int]) -> iinfo[int_]: ... @overload - def __new__(cls, dtype: str) -> iinfo[Any]: ... + def __new__(cls, dtype: str) -> iinfo[Any]: ... \ No newline at end of file diff --git a/numpy/_globals.py b/numpy/_globals.py index b7a399a79..97007b51e 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -15,11 +15,9 @@ That was not the case when the singleton classes were defined in the numpy motivated this module. """ -import enum - __ALL__ = [ 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', - '_NoValue', 'CopyMode' + '_NoValue' ] @@ -91,23 +89,4 @@ class _NoValueType: return "" -_NoValue = _NoValueType() - -class CopyMode(enum.Enum): - - ALWAYS = True - IF_NEEDED = False - NEVER = 2 - - def __bool__(self): - # For backwards compatiblity - if self == CopyMode.ALWAYS: - return True - - if self == CopyMode.IF_NEEDED: - return False - - raise TypeError(f"{self} is neither True nor False.") - - -CopyMode.__module__ = 'numpy' +_NoValue = _NoValueType() \ No newline at end of file diff --git a/numpy/array_api.py b/numpy/array_api.py new file mode 100644 index 000000000..527ccc3fa --- /dev/null +++ b/numpy/array_api.py @@ -0,0 +1,24 @@ +import enum + +__all__ = [ + 'CopyMode' + ] + +class CopyMode(enum.Enum): + + ALWAYS = True + IF_NEEDED = False + NEVER = 2 + + def __bool__(self): + # For backwards compatiblity + if self == CopyMode.ALWAYS: + return True + + if self == CopyMode.IF_NEEDED: + return False + + raise TypeError(f"{self} is neither True nor False.") + + +CopyMode.__module__ = 'numpy.array_api' diff --git a/numpy/core/multiarray.pyi b/numpy/core/multiarray.pyi index b02f7f391..8c4526a5d 100644 --- a/numpy/core/multiarray.pyi +++ b/numpy/core/multiarray.pyi @@ -20,7 +20,6 @@ from typing import ( from numpy import ( # Re-exports - CopyMode, busdaycalendar as busdaycalendar, broadcast as broadcast, dtype as dtype, @@ -79,6 +78,10 @@ from numpy.typing import ( _TD64Like_co, ) +from numpy.array_api import ( + CopyMode +) + if sys.version_info >= (3, 8): from typing import SupportsIndex, final, Final, Literal as L else: diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index a1f3dafb8..7aa1e9abe 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -169,13 +169,15 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { if (obj == Py_None) { PyErr_SetString(PyExc_ValueError, "NoneType copy mode not allowed. Please choose one of " - "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER."); + "np.array_api.CopyMode.ALWAYS, " + "np.array_api.CopyMode.IF_NEEDED, " + "np.array_api.CopyMode.NEVER."); return NPY_FAIL; } int int_copymode = -1; PyObject* numpy_CopyMode = NULL; - npy_cache_import("numpy._globals", "CopyMode", &numpy_CopyMode); + npy_cache_import("numpy.array_api", "CopyMode", &numpy_CopyMode); if (numpy_CopyMode != NULL && PyObject_IsInstance(obj, numpy_CopyMode)) { PyObject* mode_value = PyObject_GetAttrString(obj, "value"); @@ -198,7 +200,8 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { int_copymode != NPY_COPY_NEVER ) { PyErr_Format(PyExc_ValueError, "Unrecognized copy mode %d. Please choose one of " - "np.CopyMode.ALWAYS, np.CopyMode.IF_NEEDED, np.CopyMode.NEVER, " + "np.array_api.CopyMode.ALWAYS, np.array_api.CopyMode.IF_NEEDED, " + "np.array_api.CopyMode.NEVER, " "True/np.True_, False/np.False_", int_copymode); return NPY_FAIL; } diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index f20082c40..3e66e55f3 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -874,7 +874,8 @@ array_astype(PyArrayObject *self, if( forcecopy == NPY_COPY_NEVER ) { PyErr_SetString(PyExc_ValueError, - "Unable to avoid copy while casting in np.CopyMode.NEVER"); + "Unable to avoid copy while casting in " + "np.array_api.CopyMode.NEVER"); Py_DECREF(dtype); return NULL; } diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index d48b20800..e3e07138d 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -605,22 +605,22 @@ def test_astype_copyflag(): res_true = arr.astype(np.intp, copy=True) assert not np.may_share_memory(arr, res_true) - res_always = arr.astype(np.intp, copy=np.CopyMode.ALWAYS) + res_always = arr.astype(np.intp, copy=np.array_api.CopyMode.ALWAYS) assert not np.may_share_memory(arr, res_always) res_false = arr.astype(np.intp, copy=False) # `res_false is arr` currently, but check `may_share_memory`. assert np.may_share_memory(arr, res_false) - res_if_needed = arr.astype(np.intp, copy=np.CopyMode.IF_NEEDED) + res_if_needed = arr.astype(np.intp, copy=np.array_api.CopyMode.IF_NEEDED) # `res_if_needed is arr` currently, but check `may_share_memory`. assert np.may_share_memory(arr, res_if_needed) - res_never = arr.astype(np.intp, copy=np.CopyMode.NEVER) + res_never = arr.astype(np.intp, copy=np.array_api.CopyMode.NEVER) assert np.may_share_memory(arr, res_never) # Simple tests for when a copy is necessary: res_false = arr.astype(np.float64, copy=False) assert_array_equal(res_false, arr) - res_if_needed = arr.astype(np.float64, copy=np.CopyMode.IF_NEEDED) + res_if_needed = arr.astype(np.float64, copy=np.array_api.CopyMode.IF_NEEDED) assert_array_equal(res_if_needed, arr) - assert_raises(ValueError, arr.astype, np.float64, copy=np.CopyMode.NEVER) + assert_raises(ValueError, arr.astype, np.float64, copy=np.array_api.CopyMode.NEVER) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index b9dbda43b..c51d2766e 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7818,8 +7818,8 @@ class TestNewBufferProtocol: class TestArrayCreationCopyArgument(object): - true_vals = [True, np.CopyMode.ALWAYS, np.True_] - false_vals = [False, np.CopyMode.IF_NEEDED, np.False_] + true_vals = [True, np.array_api.CopyMode.ALWAYS, np.True_] + false_vals = [False, np.array_api.CopyMode.IF_NEEDED, np.False_] def test_scalars(self): # Test both numpy and python scalars @@ -7830,9 +7830,9 @@ class TestArrayCreationCopyArgument(object): # Test never-copy raises error: assert_raises(ValueError, np.array, scalar, - copy=np.CopyMode.NEVER) + copy=np.array_api.CopyMode.NEVER) assert_raises(ValueError, np.array, pyscalar, - copy=np.CopyMode.NEVER) + copy=np.array_api.CopyMode.NEVER) def test_compatible_cast(self): @@ -7861,7 +7861,7 @@ class TestArrayCreationCopyArgument(object): res = np.array(arr, copy=copy, dtype=int2) assert res is arr or res.base is arr - res = np.array(arr, copy=np.CopyMode.NEVER, dtype=int2) + res = np.array(arr, copy=np.array_api.CopyMode.NEVER, dtype=int2) assert res is arr or res.base is arr else: @@ -7872,7 +7872,7 @@ class TestArrayCreationCopyArgument(object): assert_array_equal(res, arr) assert_raises(ValueError, np.array, - arr, copy=np.CopyMode.NEVER, dtype=int2) + arr, copy=np.array_api.CopyMode.NEVER, dtype=int2) def test_buffer_interface(self): @@ -7888,7 +7888,7 @@ class TestArrayCreationCopyArgument(object): for copy in self.false_vals: res = np.array(view, copy=copy) assert np.may_share_memory(arr, res) - res = np.array(view, copy=np.CopyMode.NEVER) + res = np.array(view, copy=np.array_api.CopyMode.NEVER) assert np.may_share_memory(arr, res) def test_array_interfaces(self): @@ -7900,9 +7900,9 @@ class TestArrayCreationCopyArgument(object): arr = ArrayLike() - for copy, val in [(True, None), (np.CopyMode.ALWAYS, None), - (False, arr), (np.CopyMode.IF_NEEDED, arr), - (np.CopyMode.NEVER, arr)]: + for copy, val in [(True, None), (np.array_api.CopyMode.ALWAYS, None), + (False, arr), (np.array_api.CopyMode.IF_NEEDED, arr), + (np.array_api.CopyMode.NEVER, arr)]: res = np.array(arr, copy=copy) assert res.base is val @@ -7930,7 +7930,7 @@ class TestArrayCreationCopyArgument(object): assert_array_equal(res, base_arr) assert res is base_arr # numpy trusts the ArrayLike - assert np.array(arr, copy=np.CopyMode.NEVER) is base_arr + assert np.array(arr, copy=np.array_api.CopyMode.NEVER) is base_arr @pytest.mark.parametrize( "arr", [np.ones(()), np.arange(81).reshape((9, 9))]) @@ -7976,7 +7976,7 @@ class TestArrayCreationCopyArgument(object): if not IS_PYPY: assert res is arr or res.base.obj is arr - res = np.array(view, copy=np.CopyMode.NEVER, order=order2) + res = np.array(view, copy=np.array_api.CopyMode.NEVER, order=order2) if not IS_PYPY: assert res is arr or res.base.obj is arr else: @@ -7984,7 +7984,7 @@ class TestArrayCreationCopyArgument(object): res = np.array(arr, copy=copy, order=order2) assert_array_equal(arr, res) assert_raises(ValueError, np.array, - view, copy=np.CopyMode.NEVER, order=order2) + view, copy=np.array_api.CopyMode.NEVER, order=order2) class TestArrayAttributeDeletion: diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index 6e4a8dee0..5fb06f71f 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -137,6 +137,7 @@ def test_NPY_NO_EXPORT(): # current status is fine. For others it may make sense to work on making them # private, to clean up our public API and avoid confusion. PUBLIC_MODULES = ['numpy.' + s for s in [ + "array_api", "ctypeslib", "distutils", "distutils.cpuinfo", @@ -321,6 +322,7 @@ def is_unexpected(name): # These are present in a directory with an __init__.py but cannot be imported # code_generators/ isn't installed, but present for an inplace build SKIP_LIST = [ + "numpy.array_api.CopyMode", "numpy.core.code_generators", "numpy.core.code_generators.genapi", "numpy.core.code_generators.generate_umath", -- cgit v1.2.1 From 844883cff35996edeeeebc294e00c4aaf4fe9144 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Sat, 7 Aug 2021 11:07:45 +0530 Subject: corrected linting issues --- numpy/_globals.py | 2 +- numpy/core/tests/test_api.py | 3 ++- numpy/core/tests/test_multiarray.py | 10 +++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) (limited to 'numpy') diff --git a/numpy/_globals.py b/numpy/_globals.py index 97007b51e..5e8a4557a 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -89,4 +89,4 @@ class _NoValueType: return "" -_NoValue = _NoValueType() \ No newline at end of file +_NoValue = _NoValueType() diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index e3e07138d..c2a2e712c 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -621,6 +621,7 @@ def test_astype_copyflag(): # Simple tests for when a copy is necessary: res_false = arr.astype(np.float64, copy=False) assert_array_equal(res_false, arr) - res_if_needed = arr.astype(np.float64, copy=np.array_api.CopyMode.IF_NEEDED) + res_if_needed = arr.astype(np.float64, + copy=np.array_api.CopyMode.IF_NEEDED) assert_array_equal(res_if_needed, arr) assert_raises(ValueError, arr.astype, np.float64, copy=np.array_api.CopyMode.NEVER) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index c51d2766e..4c598da1f 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7861,7 +7861,9 @@ class TestArrayCreationCopyArgument(object): res = np.array(arr, copy=copy, dtype=int2) assert res is arr or res.base is arr - res = np.array(arr, copy=np.array_api.CopyMode.NEVER, dtype=int2) + res = np.array(arr, + copy=np.array_api.CopyMode.NEVER, + dtype=int2) assert res is arr or res.base is arr else: @@ -7872,7 +7874,8 @@ class TestArrayCreationCopyArgument(object): assert_array_equal(res, arr) assert_raises(ValueError, np.array, - arr, copy=np.array_api.CopyMode.NEVER, dtype=int2) + arr, copy=np.array_api.CopyMode.NEVER, + dtype=int2) def test_buffer_interface(self): @@ -7976,7 +7979,8 @@ class TestArrayCreationCopyArgument(object): if not IS_PYPY: assert res is arr or res.base.obj is arr - res = np.array(view, copy=np.array_api.CopyMode.NEVER, order=order2) + res = np.array(view, copy=np.array_api.CopyMode.NEVER, + order=order2) if not IS_PYPY: assert res is arr or res.base.obj is arr else: -- cgit v1.2.1 From 0f8d4c5f23a5fa55efba96034c0215cb683f9e02 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Sat, 7 Aug 2021 11:23:48 +0530 Subject: fixed linting issues --- numpy/core/tests/test_api.py | 3 ++- numpy/core/tests/test_multiarray.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index c2a2e712c..ea2bc8e3d 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -624,4 +624,5 @@ def test_astype_copyflag(): res_if_needed = arr.astype(np.float64, copy=np.array_api.CopyMode.IF_NEEDED) assert_array_equal(res_if_needed, arr) - assert_raises(ValueError, arr.astype, np.float64, copy=np.array_api.CopyMode.NEVER) + assert_raises(ValueError, arr.astype, np.float64, + copy=np.array_api.CopyMode.NEVER) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 4c598da1f..9621b700c 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7988,7 +7988,8 @@ class TestArrayCreationCopyArgument(object): res = np.array(arr, copy=copy, order=order2) assert_array_equal(arr, res) assert_raises(ValueError, np.array, - view, copy=np.array_api.CopyMode.NEVER, order=order2) + view, copy=np.array_api.CopyMode.NEVER, + order=order2) class TestArrayAttributeDeletion: -- cgit v1.2.1 From 698fb6db917e63621df9e9d50335245dd0ab5a2e Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Wed, 18 Aug 2021 12:15:00 +0530 Subject: Made _CopyMode private --- numpy/__init__.py | 2 +- numpy/__init__.pyi | 12 ++++++------ numpy/_globals.py | 24 +++++++++++++++++++++++- numpy/array_api.py | 24 ------------------------ numpy/core/multiarray.pyi | 14 +++++++------- numpy/core/src/multiarray/conversion_utils.c | 12 ++++++------ numpy/core/src/multiarray/methods.c | 2 +- numpy/core/tests/test_api.py | 10 +++++----- numpy/core/tests/test_multiarray.py | 26 +++++++++++++------------- numpy/tests/test_public_api.py | 2 -- 10 files changed, 62 insertions(+), 66 deletions(-) delete mode 100644 numpy/array_api.py (limited to 'numpy') diff --git a/numpy/__init__.py b/numpy/__init__.py index 83d2c279e..5e7f74059 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -111,7 +111,7 @@ import warnings from ._globals import ( ModuleDeprecationWarning, VisibleDeprecationWarning, - _NoValue + _NoValue, _CopyMode ) # We first need to detect if we're being called as part of the numpy setup diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index c8bdae5f0..49c17a015 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1912,7 +1912,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): order: _OrderKACF = ..., casting: _CastingKind = ..., subok: bool = ..., - copy: bool | CopyMode = ..., + copy: bool | _CopyMode = ..., ) -> NDArray[_ScalarType]: ... @overload def astype( @@ -1921,7 +1921,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): order: _OrderKACF = ..., casting: _CastingKind = ..., subok: bool = ..., - copy: bool | CopyMode = ..., + copy: bool | _CopyMode = ..., ) -> NDArray[Any]: ... @overload @@ -2928,7 +2928,7 @@ class generic(_ArrayOrScalarCommon): order: _OrderKACF = ..., casting: _CastingKind = ..., subok: bool = ..., - copy: bool | CopyMode = ..., + copy: bool | _CopyMode = ..., ) -> _ScalarType: ... @overload def astype( @@ -2937,7 +2937,7 @@ class generic(_ArrayOrScalarCommon): order: _OrderKACF = ..., casting: _CastingKind = ..., subok: bool = ..., - copy: bool | CopyMode = ..., + copy: bool | _CopyMode = ..., ) -> Any: ... # NOTE: `view` will perform a 0D->scalar cast, @@ -3690,7 +3690,7 @@ trunc: _UFunc_Nin1_Nout1[L['trunc'], L[7], None] abs = absolute -class CopyMode(enum.Enum): +class _CopyMode(enum.Enum): ALWAYS: L[1] IF_NEEDED: L[0] @@ -3876,4 +3876,4 @@ class iinfo(Generic[_IntType]): @overload def __new__(cls, dtype: int | Type[int]) -> iinfo[int_]: ... @overload - def __new__(cls, dtype: str) -> iinfo[Any]: ... \ No newline at end of file + def __new__(cls, dtype: str) -> iinfo[Any]: ... diff --git a/numpy/_globals.py b/numpy/_globals.py index 5e8a4557a..fd8025443 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -15,9 +15,11 @@ That was not the case when the singleton classes were defined in the numpy motivated this module. """ +import enum + __ALL__ = [ 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', - '_NoValue' + '_NoValue', '_CopyMode' ] @@ -90,3 +92,23 @@ class _NoValueType: _NoValue = _NoValueType() + + +class _CopyMode(enum.Enum): + + ALWAYS = True + IF_NEEDED = False + NEVER = 2 + + def __bool__(self): + # For backwards compatiblity + if self == _CopyMode.ALWAYS: + return True + + if self == _CopyMode.IF_NEEDED: + return False + + raise TypeError(f"{self} is neither True nor False.") + + +_CopyMode.__module__ = 'numpy' \ No newline at end of file diff --git a/numpy/array_api.py b/numpy/array_api.py deleted file mode 100644 index 527ccc3fa..000000000 --- a/numpy/array_api.py +++ /dev/null @@ -1,24 +0,0 @@ -import enum - -__all__ = [ - 'CopyMode' - ] - -class CopyMode(enum.Enum): - - ALWAYS = True - IF_NEEDED = False - NEVER = 2 - - def __bool__(self): - # For backwards compatiblity - if self == CopyMode.ALWAYS: - return True - - if self == CopyMode.IF_NEEDED: - return False - - raise TypeError(f"{self} is neither True nor False.") - - -CopyMode.__module__ = 'numpy.array_api' diff --git a/numpy/core/multiarray.pyi b/numpy/core/multiarray.pyi index 8c4526a5d..7ae831b53 100644 --- a/numpy/core/multiarray.pyi +++ b/numpy/core/multiarray.pyi @@ -79,7 +79,7 @@ from numpy.typing import ( ) from numpy.array_api import ( - CopyMode + _CopyMode ) if sys.version_info >= (3, 8): @@ -181,7 +181,7 @@ def array( object: _ArrayType, dtype: None = ..., *, - copy: bool | CopyMode = ..., + copy: bool | _CopyMode = ..., order: _OrderKACF = ..., subok: L[True], ndmin: int = ..., @@ -192,7 +192,7 @@ def array( object: _ArrayLike[_SCT], dtype: None = ..., *, - copy: bool | CopyMode = ..., + copy: bool | _CopyMode = ..., order: _OrderKACF = ..., subok: bool = ..., ndmin: int = ..., @@ -203,7 +203,7 @@ def array( object: object, dtype: None = ..., *, - copy: bool | CopyMode = ..., + copy: bool | _CopyMode = ..., order: _OrderKACF = ..., subok: bool = ..., ndmin: int = ..., @@ -214,7 +214,7 @@ def array( object: Any, dtype: _DTypeLike[_SCT], *, - copy: bool | CopyMode = ..., + copy: bool | _CopyMode = ..., order: _OrderKACF = ..., subok: bool = ..., ndmin: int = ..., @@ -225,7 +225,7 @@ def array( object: Any, dtype: DTypeLike, *, - copy: bool | CopyMode = ..., + copy: bool | _CopyMode = ..., order: _OrderKACF = ..., subok: bool = ..., ndmin: int = ..., @@ -1005,7 +1005,7 @@ class flagsobj: def __getitem__(self, key: _GetItemKeys) -> bool: ... def __setitem__(self, key: _SetItemKeys, value: bool) -> None: ... -class CopyMode(enum.Enum): +class _CopyMode(enum.Enum): ALWAYS: L[1] IF_NEEDED: L[0] diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 7aa1e9abe..15fe4bde2 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -169,15 +169,15 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { if (obj == Py_None) { PyErr_SetString(PyExc_ValueError, "NoneType copy mode not allowed. Please choose one of " - "np.array_api.CopyMode.ALWAYS, " - "np.array_api.CopyMode.IF_NEEDED, " - "np.array_api.CopyMode.NEVER."); + "np.array_api._CopyMode.ALWAYS, " + "np.array_api._CopyMode.IF_NEEDED, " + "np.array_api._CopyMode.NEVER."); return NPY_FAIL; } int int_copymode = -1; PyObject* numpy_CopyMode = NULL; - npy_cache_import("numpy.array_api", "CopyMode", &numpy_CopyMode); + npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode); if (numpy_CopyMode != NULL && PyObject_IsInstance(obj, numpy_CopyMode)) { PyObject* mode_value = PyObject_GetAttrString(obj, "value"); @@ -200,8 +200,8 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { int_copymode != NPY_COPY_NEVER ) { PyErr_Format(PyExc_ValueError, "Unrecognized copy mode %d. Please choose one of " - "np.array_api.CopyMode.ALWAYS, np.array_api.CopyMode.IF_NEEDED, " - "np.array_api.CopyMode.NEVER, " + "np._CopyMode.ALWAYS, np._CopyMode.IF_NEEDED, " + "np._CopyMode.NEVER, " "True/np.True_, False/np.False_", int_copymode); return NPY_FAIL; } diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 3e66e55f3..ffa735b38 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -875,7 +875,7 @@ array_astype(PyArrayObject *self, if( forcecopy == NPY_COPY_NEVER ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while casting in " - "np.array_api.CopyMode.NEVER"); + "np._CopyMode.NEVER"); Py_DECREF(dtype); return NULL; } diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index ea2bc8e3d..d3c7211cd 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -605,24 +605,24 @@ def test_astype_copyflag(): res_true = arr.astype(np.intp, copy=True) assert not np.may_share_memory(arr, res_true) - res_always = arr.astype(np.intp, copy=np.array_api.CopyMode.ALWAYS) + res_always = arr.astype(np.intp, copy=np._CopyMode.ALWAYS) assert not np.may_share_memory(arr, res_always) res_false = arr.astype(np.intp, copy=False) # `res_false is arr` currently, but check `may_share_memory`. assert np.may_share_memory(arr, res_false) - res_if_needed = arr.astype(np.intp, copy=np.array_api.CopyMode.IF_NEEDED) + res_if_needed = arr.astype(np.intp, copy=np._CopyMode.IF_NEEDED) # `res_if_needed is arr` currently, but check `may_share_memory`. assert np.may_share_memory(arr, res_if_needed) - res_never = arr.astype(np.intp, copy=np.array_api.CopyMode.NEVER) + res_never = arr.astype(np.intp, copy=np._CopyMode.NEVER) assert np.may_share_memory(arr, res_never) # Simple tests for when a copy is necessary: res_false = arr.astype(np.float64, copy=False) assert_array_equal(res_false, arr) res_if_needed = arr.astype(np.float64, - copy=np.array_api.CopyMode.IF_NEEDED) + copy=np._CopyMode.IF_NEEDED) assert_array_equal(res_if_needed, arr) assert_raises(ValueError, arr.astype, np.float64, - copy=np.array_api.CopyMode.NEVER) + copy=np._CopyMode.NEVER) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 9621b700c..9c56df2ba 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7818,8 +7818,8 @@ class TestNewBufferProtocol: class TestArrayCreationCopyArgument(object): - true_vals = [True, np.array_api.CopyMode.ALWAYS, np.True_] - false_vals = [False, np.array_api.CopyMode.IF_NEEDED, np.False_] + true_vals = [True, np._CopyMode.ALWAYS, np.True_] + false_vals = [False, np._CopyMode.IF_NEEDED, np.False_] def test_scalars(self): # Test both numpy and python scalars @@ -7830,9 +7830,9 @@ class TestArrayCreationCopyArgument(object): # Test never-copy raises error: assert_raises(ValueError, np.array, scalar, - copy=np.array_api.CopyMode.NEVER) + copy=np._CopyMode.NEVER) assert_raises(ValueError, np.array, pyscalar, - copy=np.array_api.CopyMode.NEVER) + copy=np._CopyMode.NEVER) def test_compatible_cast(self): @@ -7862,7 +7862,7 @@ class TestArrayCreationCopyArgument(object): assert res is arr or res.base is arr res = np.array(arr, - copy=np.array_api.CopyMode.NEVER, + copy=np._CopyMode.NEVER, dtype=int2) assert res is arr or res.base is arr @@ -7874,7 +7874,7 @@ class TestArrayCreationCopyArgument(object): assert_array_equal(res, arr) assert_raises(ValueError, np.array, - arr, copy=np.array_api.CopyMode.NEVER, + arr, copy=np._CopyMode.NEVER, dtype=int2) def test_buffer_interface(self): @@ -7891,7 +7891,7 @@ class TestArrayCreationCopyArgument(object): for copy in self.false_vals: res = np.array(view, copy=copy) assert np.may_share_memory(arr, res) - res = np.array(view, copy=np.array_api.CopyMode.NEVER) + res = np.array(view, copy=np._CopyMode.NEVER) assert np.may_share_memory(arr, res) def test_array_interfaces(self): @@ -7903,9 +7903,9 @@ class TestArrayCreationCopyArgument(object): arr = ArrayLike() - for copy, val in [(True, None), (np.array_api.CopyMode.ALWAYS, None), - (False, arr), (np.array_api.CopyMode.IF_NEEDED, arr), - (np.array_api.CopyMode.NEVER, arr)]: + for copy, val in [(True, None), (np._CopyMode.ALWAYS, None), + (False, arr), (np._CopyMode.IF_NEEDED, arr), + (np._CopyMode.NEVER, arr)]: res = np.array(arr, copy=copy) assert res.base is val @@ -7933,7 +7933,7 @@ class TestArrayCreationCopyArgument(object): assert_array_equal(res, base_arr) assert res is base_arr # numpy trusts the ArrayLike - assert np.array(arr, copy=np.array_api.CopyMode.NEVER) is base_arr + assert np.array(arr, copy=np._CopyMode.NEVER) is base_arr @pytest.mark.parametrize( "arr", [np.ones(()), np.arange(81).reshape((9, 9))]) @@ -7979,7 +7979,7 @@ class TestArrayCreationCopyArgument(object): if not IS_PYPY: assert res is arr or res.base.obj is arr - res = np.array(view, copy=np.array_api.CopyMode.NEVER, + res = np.array(view, copy=np._CopyMode.NEVER, order=order2) if not IS_PYPY: assert res is arr or res.base.obj is arr @@ -7988,7 +7988,7 @@ class TestArrayCreationCopyArgument(object): res = np.array(arr, copy=copy, order=order2) assert_array_equal(arr, res) assert_raises(ValueError, np.array, - view, copy=np.array_api.CopyMode.NEVER, + view, copy=np._CopyMode.NEVER, order=order2) diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index 5fb06f71f..6e4a8dee0 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -137,7 +137,6 @@ def test_NPY_NO_EXPORT(): # current status is fine. For others it may make sense to work on making them # private, to clean up our public API and avoid confusion. PUBLIC_MODULES = ['numpy.' + s for s in [ - "array_api", "ctypeslib", "distutils", "distutils.cpuinfo", @@ -322,7 +321,6 @@ def is_unexpected(name): # These are present in a directory with an __init__.py but cannot be imported # code_generators/ isn't installed, but present for an inplace build SKIP_LIST = [ - "numpy.array_api.CopyMode", "numpy.core.code_generators", "numpy.core.code_generators.genapi", "numpy.core.code_generators.generate_umath", -- cgit v1.2.1 From b341e4c3249817d2e14ddf71aa850a8a896b9303 Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Wed, 18 Aug 2021 12:15:23 +0530 Subject: Fixed linting issues --- numpy/_globals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/_globals.py b/numpy/_globals.py index fd8025443..133ab11cc 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -111,4 +111,4 @@ class _CopyMode(enum.Enum): raise TypeError(f"{self} is neither True nor False.") -_CopyMode.__module__ = 'numpy' \ No newline at end of file +_CopyMode.__module__ = 'numpy' -- cgit v1.2.1 From 45dbdc9d8fd3fa7fbabbddf690f6c892cc24aa1d Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Fri, 3 Sep 2021 15:10:23 +0530 Subject: CopyMode added to np.array_api --- numpy/array_api/_creation_functions.py | 2 +- numpy/array_api/tests/test_creation_functions.py | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'numpy') diff --git a/numpy/array_api/_creation_functions.py b/numpy/array_api/_creation_functions.py index e9c01e7e6..c15d54db1 100644 --- a/numpy/array_api/_creation_functions.py +++ b/numpy/array_api/_creation_functions.py @@ -43,7 +43,7 @@ def asarray( *, dtype: Optional[Dtype] = None, device: Optional[Device] = None, - copy: Optional[bool] = None, + copy: Optional[Union[bool | np._CopyMode]] = None, ) -> Array: """ Array API compatible wrapper for :py:func:`np.asarray `. diff --git a/numpy/array_api/tests/test_creation_functions.py b/numpy/array_api/tests/test_creation_functions.py index 3cb8865cd..b0c99cd45 100644 --- a/numpy/array_api/tests/test_creation_functions.py +++ b/numpy/array_api/tests/test_creation_functions.py @@ -56,12 +56,17 @@ def test_asarray_copy(): a[0] = 0 assert all(b[0] == 1) assert all(a[0] == 0) - # Once copy=False is implemented, replace this with - # a = asarray([1]) - # b = asarray(a, copy=False) - # a[0] = 0 - # assert all(b[0] == 0) + a = asarray([1]) + b = asarray(a, copy=np._CopyMode.ALWAYS) + a[0] = 0 + assert all(b[0] == 1) + assert all(a[0] == 0) + a = asarray([1]) + b = asarray(a, copy=np._CopyMode.NEVER) + a[0] = 0 + assert all(b[0] == 0) assert_raises(NotImplementedError, lambda: asarray(a, copy=False)) + assert_raises(NotImplementedError, lambda: asarray(a, copy=np._CopyMode.NEVER)) def test_arange_errors(): -- cgit v1.2.1 From a39312cf4eca9dc9573737a01f16fee24efb6c0a Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Fri, 3 Sep 2021 15:15:18 +0530 Subject: fixed linting issues --- numpy/array_api/tests/test_creation_functions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/array_api/tests/test_creation_functions.py b/numpy/array_api/tests/test_creation_functions.py index b0c99cd45..9d2909e91 100644 --- a/numpy/array_api/tests/test_creation_functions.py +++ b/numpy/array_api/tests/test_creation_functions.py @@ -66,7 +66,8 @@ def test_asarray_copy(): a[0] = 0 assert all(b[0] == 0) assert_raises(NotImplementedError, lambda: asarray(a, copy=False)) - assert_raises(NotImplementedError, lambda: asarray(a, copy=np._CopyMode.NEVER)) + assert_raises(NotImplementedError, lambda: asarray(a, + copy=np._CopyMode.NEVER)) def test_arange_errors(): -- cgit v1.2.1 From c2acd5b25a04783fbbe3ba32426039e4dbe9207e Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Fri, 3 Sep 2021 15:17:58 +0530 Subject: fixed linting issues --- numpy/array_api/tests/test_creation_functions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/array_api/tests/test_creation_functions.py b/numpy/array_api/tests/test_creation_functions.py index 9d2909e91..7182209dc 100644 --- a/numpy/array_api/tests/test_creation_functions.py +++ b/numpy/array_api/tests/test_creation_functions.py @@ -66,8 +66,8 @@ def test_asarray_copy(): a[0] = 0 assert all(b[0] == 0) assert_raises(NotImplementedError, lambda: asarray(a, copy=False)) - assert_raises(NotImplementedError, lambda: asarray(a, - copy=np._CopyMode.NEVER)) + assert_raises(NotImplementedError, + lambda: asarray(a, copy=np._CopyMode.NEVER)) def test_arange_errors(): -- cgit v1.2.1 From 56647dd47345a7fd24b4ee8d9d52025fcdc3b9ae Mon Sep 17 00:00:00 2001 From: czgdp1807 Date: Sat, 4 Sep 2021 22:33:52 +0530 Subject: Addressed reviews --- numpy/__init__.pyi | 1 - numpy/_globals.py | 2 +- numpy/array_api/_creation_functions.py | 6 +++--- numpy/array_api/tests/test_creation_functions.py | 2 +- numpy/core/multiarray.pyi | 7 +------ 5 files changed, 6 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index dafedeb56..30b0944fd 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -3689,7 +3689,6 @@ trunc: _UFunc_Nin1_Nout1[L['trunc'], L[7], None] abs = absolute class _CopyMode(enum.Enum): - ALWAYS: L[1] IF_NEEDED: L[0] NEVER: L[2] diff --git a/numpy/_globals.py b/numpy/_globals.py index 133ab11cc..d458fc9c4 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -108,7 +108,7 @@ class _CopyMode(enum.Enum): if self == _CopyMode.IF_NEEDED: return False - raise TypeError(f"{self} is neither True nor False.") + raise ValueError(f"{self} is neither True nor False.") _CopyMode.__module__ = 'numpy' diff --git a/numpy/array_api/_creation_functions.py b/numpy/array_api/_creation_functions.py index c15d54db1..2d6cf4414 100644 --- a/numpy/array_api/_creation_functions.py +++ b/numpy/array_api/_creation_functions.py @@ -43,7 +43,7 @@ def asarray( *, dtype: Optional[Dtype] = None, device: Optional[Device] = None, - copy: Optional[Union[bool | np._CopyMode]] = None, + copy: Optional[Union[bool, np._CopyMode]] = None, ) -> Array: """ Array API compatible wrapper for :py:func:`np.asarray `. @@ -57,11 +57,11 @@ def asarray( _check_valid_dtype(dtype) if device not in ["cpu", None]: raise ValueError(f"Unsupported device {device!r}") - if copy is False: + if copy in (False, np._CopyMode.IF_NEEDED): # Note: copy=False is not yet implemented in np.asarray raise NotImplementedError("copy=False is not yet implemented") if isinstance(obj, Array) and (dtype is None or obj.dtype == dtype): - if copy is True: + if copy in (True, np._CopyMode.ALWAYS): return Array._new(np.array(obj._array, copy=True, dtype=dtype)) return obj if dtype is None and isinstance(obj, int) and (obj > 2 ** 64 or obj < -(2 ** 63)): diff --git a/numpy/array_api/tests/test_creation_functions.py b/numpy/array_api/tests/test_creation_functions.py index 7182209dc..2ee23a47b 100644 --- a/numpy/array_api/tests/test_creation_functions.py +++ b/numpy/array_api/tests/test_creation_functions.py @@ -67,7 +67,7 @@ def test_asarray_copy(): assert all(b[0] == 0) assert_raises(NotImplementedError, lambda: asarray(a, copy=False)) assert_raises(NotImplementedError, - lambda: asarray(a, copy=np._CopyMode.NEVER)) + lambda: asarray(a, copy=np._CopyMode.IF_NEEDED)) def test_arange_errors(): diff --git a/numpy/core/multiarray.pyi b/numpy/core/multiarray.pyi index 97e9c3498..501e55634 100644 --- a/numpy/core/multiarray.pyi +++ b/numpy/core/multiarray.pyi @@ -51,6 +51,7 @@ from numpy import ( _ModeKind, _SupportsBuffer, _IOProtocol, + _CopyMode ) from numpy.typing import ( @@ -1012,9 +1013,3 @@ class flagsobj: def owndata(self) -> bool: ... def __getitem__(self, key: _GetItemKeys) -> bool: ... def __setitem__(self, key: _SetItemKeys, value: bool) -> None: ... - -class _CopyMode(enum.Enum): - - ALWAYS: L[1] - IF_NEEDED: L[0] - NEVER: L[2] \ No newline at end of file -- cgit v1.2.1 From d594768ade744d9b5baf46d270dbb83259c906f2 Mon Sep 17 00:00:00 2001 From: Hugo Defois Date: Wed, 7 Jul 2021 23:53:05 +0200 Subject: BUG: fix a PyArray_DiscoverDTypeAndShape_Recursive leak --- numpy/core/src/multiarray/array_coercion.c | 1 + 1 file changed, 1 insertion(+) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index 847bdafc3..8e5183424 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -1089,6 +1089,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( } /* The cache takes ownership of the sequence here. */ if (npy_new_coercion_cache(obj, seq, 1, coercion_cache_tail_ptr, curr_dims) < 0) { + Py_DECREF(seq); return -1; } -- cgit v1.2.1 From 93fd427d5061dbec7684208317e08e887a996043 Mon Sep 17 00:00:00 2001 From: Hugo Defois Date: Wed, 7 Jul 2021 23:53:42 +0200 Subject: BUG: fix a PyInit__multiarray_umath leak --- numpy/core/src/multiarray/multiarraymodule.c | 1 + 1 file changed, 1 insertion(+) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 5ceed1678..5dccaf94c 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4914,5 +4914,6 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { PyErr_SetString(PyExc_RuntimeError, "cannot load multiarray module."); } + Py_XDECREF(m); return NULL; } -- cgit v1.2.1 From c2de2d1fed4a7fd69e439d25bfb1b91f9df6bd1e Mon Sep 17 00:00:00 2001 From: Hugo Defois Date: Wed, 7 Jul 2021 23:54:23 +0200 Subject: BUG: fix a PyArray_CastScalarToCtype leak --- numpy/core/src/multiarray/scalarapi.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c index e409e9874..28c010f8f 100644 --- a/numpy/core/src/multiarray/scalarapi.c +++ b/numpy/core/src/multiarray/scalarapi.c @@ -235,6 +235,7 @@ PyArray_CastScalarToCtype(PyObject *scalar, void *ctypeptr, descr = PyArray_DescrFromScalar(scalar); castfunc = PyArray_GetCastFunc(descr, outcode->type_num); if (castfunc == NULL) { + Py_XDECREF(descr); return -1; } if (PyTypeNum_ISEXTENDED(descr->type_num) || @@ -254,6 +255,7 @@ PyArray_CastScalarToCtype(PyObject *scalar, void *ctypeptr, NPY_ARRAY_CARRAY, NULL); if (aout == NULL) { Py_DECREF(ain); + Py_DECREF(descr); return -1; } castfunc(PyArray_DATA(ain), PyArray_DATA(aout), 1, ain, aout); -- cgit v1.2.1 From 09326ebc9ee8720a00afbd34068a54414d741e94 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Wed, 29 Sep 2021 14:00:22 -0500 Subject: Fixups based on review comments. --- numpy/core/src/multiarray/array_coercion.c | 2 +- numpy/core/src/multiarray/multiarraymodule.c | 12 ++++++------ numpy/core/src/multiarray/scalarapi.c | 5 ++++- 3 files changed, 11 insertions(+), 8 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index 8e5183424..3c44d312c 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -555,6 +555,7 @@ npy_new_coercion_cache( cache = PyMem_Malloc(sizeof(coercion_cache_obj)); } if (cache == NULL) { + Py_DECREF(arr_or_sequence); PyErr_NoMemory(); return -1; } @@ -1089,7 +1090,6 @@ PyArray_DiscoverDTypeAndShape_Recursive( } /* The cache takes ownership of the sequence here. */ if (npy_new_coercion_cache(obj, seq, 1, coercion_cache_tail_ptr, curr_dims) < 0) { - Py_DECREF(seq); return -1; } diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 5dccaf94c..184f9d05f 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4669,14 +4669,14 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { PyObject *m, *d, *s; PyObject *c_api; - /* Initialize CPU features */ - if (npy_cpu_init() < 0) { - goto err; - } - /* Create the module and add the functions */ m = PyModule_Create(&moduledef); if (!m) { + return NULL; + } + + /* Initialize CPU features */ + if (npy_cpu_init() < 0) { goto err; } @@ -4914,6 +4914,6 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { PyErr_SetString(PyExc_RuntimeError, "cannot load multiarray module."); } - Py_XDECREF(m); + Py_DECREF(m); return NULL; } diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c index 28c010f8f..564352f1f 100644 --- a/numpy/core/src/multiarray/scalarapi.c +++ b/numpy/core/src/multiarray/scalarapi.c @@ -233,9 +233,12 @@ PyArray_CastScalarToCtype(PyObject *scalar, void *ctypeptr, PyArray_VectorUnaryFunc* castfunc; descr = PyArray_DescrFromScalar(scalar); + if (descr == NULL) { + return -1; + } castfunc = PyArray_GetCastFunc(descr, outcode->type_num); if (castfunc == NULL) { - Py_XDECREF(descr); + Py_DECREF(descr); return -1; } if (PyTypeNum_ISEXTENDED(descr->type_num) || -- cgit v1.2.1 From 6000e438ea56e7eb622fe99ce28321eb81faec59 Mon Sep 17 00:00:00 2001 From: Jonas Liechti Date: Fri, 1 Oct 2021 18:43:50 +0200 Subject: use an example that also shows default value --- numpy/lib/function_base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 80eaf8acf..0dee7784a 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -657,11 +657,11 @@ def select(condlist, choicelist, default=0): Examples -------- - >>> x = np.arange(10) - >>> condlist = [x<3, x>5] + >>> x = np.arange(6) + >>> condlist = [x<3, x>3] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) - array([ 0, 1, 2, ..., 49, 64, 81]) + array([ 0, 1, 2, 0, 16, 25]) """ # Check the size of condlist and choicelist are the same, or abort. -- cgit v1.2.1 From dd3dff57b097c375858f84ebf57d75ec50150e55 Mon Sep 17 00:00:00 2001 From: Jonas Liechti Date: Tue, 5 Oct 2021 00:46:41 +0200 Subject: adding example: also highlight priority in multiple matches --- numpy/lib/function_base.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 0dee7784a..3c49c227a 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -660,8 +660,17 @@ def select(condlist, choicelist, default=0): >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [x, x**2] - >>> np.select(condlist, choicelist) - array([ 0, 1, 2, 0, 16, 25]) + >>> np.select(condlist, choicelist, 55) + array([ 0, 1, 2, 55, 16, 25]) + + Note that, when multiple conditions are satisfied, the element is drawn + from the first element in `choicelist` with a matching condition: + + >>> x = np.arange(6) + >>> condlist = [x<=4, x>3] + >>> choicelist = [x, x**2] + >>> np.select(condlist, choicelist, 55) + array([ 0, 1, 2, 3, 4, 25]) """ # Check the size of condlist and choicelist are the same, or abort. -- cgit v1.2.1 From f9e712d2b658b6e7b8eff7eeac062b08a726315d Mon Sep 17 00:00:00 2001 From: Jonas Liechti Date: Tue, 5 Oct 2021 00:54:40 +0200 Subject: type-o: used element instead of array --- numpy/lib/function_base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 3c49c227a..2e77c11dc 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -660,11 +660,11 @@ def select(condlist, choicelist, default=0): >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [x, x**2] - >>> np.select(condlist, choicelist, 55) - array([ 0, 1, 2, 55, 16, 25]) + >>> np.select(condlist, choicelist, 42) + array([ 0, 1, 2, 42, 16, 25]) Note that, when multiple conditions are satisfied, the element is drawn - from the first element in `choicelist` with a matching condition: + from the first array in `choicelist` with a matching condition: >>> x = np.arange(6) >>> condlist = [x<=4, x>3] -- cgit v1.2.1 From 4d23ebeb068c8d6ba6edfc11d32ab2af8bb89c74 Mon Sep 17 00:00:00 2001 From: Alessia Marcolini <98marcolini@gmail.com> Date: Fri, 8 Oct 2021 09:49:11 +0000 Subject: MAINT: remove unused imports --- numpy/array_api/tests/test_creation_functions.py | 15 --------------- numpy/core/getlimits.py | 1 - numpy/core/numerictypes.py | 6 ++---- numpy/core/overrides.py | 1 - numpy/core/tests/test_casting_unittests.py | 1 - numpy/core/tests/test_dtype.py | 1 - numpy/core/tests/test_numeric.py | 2 +- numpy/core/tests/test_scalarmath.py | 4 ++-- numpy/core/tests/test_umath.py | 2 +- numpy/core/tests/test_umath_accuracy.py | 1 - numpy/distutils/ccompiler_opt.py | 3 +-- numpy/f2py/tests/test_callback.py | 1 - numpy/f2py/tests/util.py | 3 --- numpy/lib/_datasource.py | 1 - numpy/lib/tests/test_format.py | 2 -- numpy/lib/tests/test_index_tricks.py | 1 - numpy/lib/tests/test_nanfunctions.py | 4 ++-- numpy/lib/tests/test_regression.py | 2 -- numpy/linalg/linalg.py | 2 +- numpy/linalg/setup.py | 3 +-- numpy/linalg/tests/test_linalg.py | 3 --- numpy/random/tests/test_generator_mt19937_regressions.py | 2 +- numpy/testing/_private/noseclasses.py | 2 +- numpy/typing/tests/data/pass/array_constructors.py | 2 +- numpy/typing/tests/data/pass/array_like.py | 2 +- numpy/typing/tests/data/pass/einsumfunc.py | 2 +- numpy/typing/tests/data/pass/lib_utils.py | 1 - numpy/typing/tests/data/pass/multiarray.py | 1 - 28 files changed, 16 insertions(+), 55 deletions(-) (limited to 'numpy') diff --git a/numpy/array_api/tests/test_creation_functions.py b/numpy/array_api/tests/test_creation_functions.py index 3cb8865cd..7b633eaf1 100644 --- a/numpy/array_api/tests/test_creation_functions.py +++ b/numpy/array_api/tests/test_creation_functions.py @@ -8,30 +8,15 @@ from .._creation_functions import ( empty, empty_like, eye, - from_dlpack, full, full_like, linspace, - meshgrid, ones, ones_like, zeros, zeros_like, ) from .._array_object import Array -from .._dtypes import ( - _all_dtypes, - _boolean_dtypes, - _floating_dtypes, - _integer_dtypes, - _integer_or_boolean_dtypes, - _numeric_dtypes, - int8, - int16, - int32, - int64, - uint64, -) def test_asarray_errors(): diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index 0f7031bac..c96e6d5e7 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -11,7 +11,6 @@ from . import numeric from . import numerictypes as ntypes from .numeric import array, inf, NaN from .umath import log10, exp2, nextafter, isnan -from . import umath def _fr0(a): diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 12f424fd4..8e5de852b 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -80,12 +80,10 @@ Exported symbols include: """ import numbers -import warnings from numpy.core.multiarray import ( - typeinfo, ndarray, array, empty, dtype, datetime_data, - datetime_as_string, busday_offset, busday_count, is_busday, - busdaycalendar + ndarray, array, dtype, datetime_data, datetime_as_string, + busday_offset, busday_count, is_busday, busdaycalendar ) from numpy.core.overrides import set_module diff --git a/numpy/core/overrides.py b/numpy/core/overrides.py index e1fdd06f2..840cf38c9 100644 --- a/numpy/core/overrides.py +++ b/numpy/core/overrides.py @@ -2,7 +2,6 @@ import collections import functools import os -import textwrap from numpy.core._multiarray_umath import ( add_docstring, implement_array_function, _get_implementing_args) diff --git a/numpy/core/tests/test_casting_unittests.py b/numpy/core/tests/test_casting_unittests.py index b0d8ff503..cb4792090 100644 --- a/numpy/core/tests/test_casting_unittests.py +++ b/numpy/core/tests/test_casting_unittests.py @@ -9,7 +9,6 @@ than integration tests. import pytest import textwrap import enum -import itertools import random import numpy as np diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 1c25bee00..6eab4c966 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -3,7 +3,6 @@ import operator import pytest import ctypes import gc -import warnings import types from typing import Any diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index e36f76c53..558ded01b 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -16,7 +16,7 @@ from numpy.testing import ( ) from numpy.core._rational_tests import rational -from hypothesis import assume, given, strategies as st +from hypothesis import given, strategies as st from hypothesis.extra import numpy as hynp diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py index becd65b11..90078a2ea 100644 --- a/numpy/core/tests/test_scalarmath.py +++ b/numpy/core/tests/test_scalarmath.py @@ -5,14 +5,14 @@ import itertools import operator import platform import pytest -from hypothesis import given, settings, Verbosity, assume +from hypothesis import given, settings, Verbosity from hypothesis.strategies import sampled_from import numpy as np from numpy.testing import ( assert_, assert_equal, assert_raises, assert_almost_equal, assert_array_equal, IS_PYPY, suppress_warnings, _gen_alignment_data, - assert_warns, assert_raises_regex, + assert_warns, ) types = [np.bool_, np.byte, np.ubyte, np.short, np.ushort, np.intc, np.uintc, diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 4f57c0088..18e43257a 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -15,7 +15,7 @@ from numpy.testing import ( assert_, assert_equal, assert_raises, assert_raises_regex, assert_array_equal, assert_almost_equal, assert_array_almost_equal, assert_array_max_ulp, assert_allclose, assert_no_warnings, suppress_warnings, - _gen_alignment_data, assert_array_almost_equal_nulp, assert_warns + _gen_alignment_data, assert_array_almost_equal_nulp ) def get_glibc_version(): diff --git a/numpy/core/tests/test_umath_accuracy.py b/numpy/core/tests/test_umath_accuracy.py index a703c697a..32e2dca66 100644 --- a/numpy/core/tests/test_umath_accuracy.py +++ b/numpy/core/tests/test_umath_accuracy.py @@ -1,5 +1,4 @@ import numpy as np -import platform import os from os import path import sys diff --git a/numpy/distutils/ccompiler_opt.py b/numpy/distutils/ccompiler_opt.py index e7fd494d3..88e4870d4 100644 --- a/numpy/distutils/ccompiler_opt.py +++ b/numpy/distutils/ccompiler_opt.py @@ -8,7 +8,7 @@ the sources with proper compiler's flags. instead only focuses on the compiler side, but it creates abstract C headers that can be used later for the final runtime dispatching process.""" -import sys, io, os, re, textwrap, pprint, inspect, atexit, subprocess +import os, re, textwrap, pprint, inspect, atexit, subprocess class _Config: """An abstract class holds all configurable attributes of `CCompilerOpt`, @@ -696,7 +696,6 @@ class _Distutils: ) @staticmethod def _dist_test_spawn(cmd, display=None): - from distutils.errors import CompileError try: o = subprocess.check_output(cmd, stderr=subprocess.STDOUT, universal_newlines=True) diff --git a/numpy/f2py/tests/test_callback.py b/numpy/f2py/tests/test_callback.py index d07e98357..5d2aab94d 100644 --- a/numpy/f2py/tests/test_callback.py +++ b/numpy/f2py/tests/test_callback.py @@ -5,7 +5,6 @@ import pytest import threading import traceback import time -import random import numpy as np from numpy.testing import assert_, assert_equal, IS_PYPY diff --git a/numpy/f2py/tests/util.py b/numpy/f2py/tests/util.py index eace3c9fc..1a6805e75 100644 --- a/numpy/f2py/tests/util.py +++ b/numpy/f2py/tests/util.py @@ -242,9 +242,6 @@ def build_module_distutils(source_files, config_code, module_name, **kw): Build a module via distutils and import it. """ - from numpy.distutils.misc_util import Configuration - from numpy.distutils.core import setup - d = get_module_dir() # Copy files diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py index 56b94853d..8201d3772 100644 --- a/numpy/lib/_datasource.py +++ b/numpy/lib/_datasource.py @@ -324,7 +324,6 @@ class DataSource: # a significant fraction of numpy's total import time. import shutil from urllib.request import urlopen - from urllib.error import URLError upath = self.abspath(path) diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py index 10656a233..78e67a89b 100644 --- a/numpy/lib/tests/test_format.py +++ b/numpy/lib/tests/test_format.py @@ -276,8 +276,6 @@ Test the header writing. ''' import sys import os -import shutil -import tempfile import warnings import pytest from io import BytesIO diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index c21aefd1a..26a34be7e 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -4,7 +4,6 @@ import numpy as np from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_almost_equal, assert_array_almost_equal, assert_raises, assert_raises_regex, - assert_warns ) from numpy.lib.index_tricks import ( mgrid, ogrid, ndenumerate, fill_diagonal, diag_indices, diag_indices_from, diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index 0bd68e461..126dba495 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -5,8 +5,8 @@ import inspect import numpy as np from numpy.lib.nanfunctions import _nan_mask, _replace_nan from numpy.testing import ( - assert_, assert_equal, assert_almost_equal, assert_no_warnings, - assert_raises, assert_array_equal, suppress_warnings + assert_, assert_equal, assert_almost_equal, assert_raises, + assert_array_equal, suppress_warnings ) diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 373226277..55df2a675 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -1,5 +1,3 @@ -import pytest - import os import numpy as np diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 95780d19d..d002a34d4 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -30,7 +30,7 @@ from numpy.core.multiarray import normalize_axis_index from numpy.core.overrides import set_module from numpy.core import overrides from numpy.lib.twodim_base import triu, eye -from numpy.linalg import lapack_lite, _umath_linalg +from numpy.linalg import _umath_linalg array_function_dispatch = functools.partial( diff --git a/numpy/linalg/setup.py b/numpy/linalg/setup.py index e2944f38c..94536bb2c 100644 --- a/numpy/linalg/setup.py +++ b/numpy/linalg/setup.py @@ -3,8 +3,7 @@ import sys def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration - from numpy.distutils.system_info import ( - get_info, system_info, lapack_opt_info, blas_opt_info) + from numpy.distutils.system_info import get_info, system_info config = Configuration('linalg', parent_package, top_path) config.add_subpackage('tests') diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py index a45323bb3..c1ba84a8e 100644 --- a/numpy/linalg/tests/test_linalg.py +++ b/numpy/linalg/tests/test_linalg.py @@ -1,7 +1,6 @@ """ Test functions for linalg module """ -from numpy.core.fromnumeric import shape import os import sys import itertools @@ -22,7 +21,6 @@ from numpy.testing import ( assert_almost_equal, assert_allclose, suppress_warnings, assert_raises_regex, HAS_LAPACK64, ) -from numpy.testing._private.utils import requires_memory def consistent_subclass(out, in_): @@ -1072,7 +1070,6 @@ class TestMatrixPower: assert_raises(LinAlgError, matrix_power, mat, -1) - class TestEigvalshCases(HermitianTestCase, HermitianGeneralizedTestCase): def do(self, a, b, tags): diff --git a/numpy/random/tests/test_generator_mt19937_regressions.py b/numpy/random/tests/test_generator_mt19937_regressions.py index 88d2792a6..0227d6502 100644 --- a/numpy/random/tests/test_generator_mt19937_regressions.py +++ b/numpy/random/tests/test_generator_mt19937_regressions.py @@ -1,7 +1,7 @@ from numpy.testing import (assert_, assert_array_equal) import numpy as np import pytest -from numpy.random import Generator, MT19937, RandomState +from numpy.random import Generator, MT19937 mt19937 = Generator(MT19937()) diff --git a/numpy/testing/_private/noseclasses.py b/numpy/testing/_private/noseclasses.py index 48fa4dc1f..a72ddbe46 100644 --- a/numpy/testing/_private/noseclasses.py +++ b/numpy/testing/_private/noseclasses.py @@ -16,7 +16,7 @@ from nose.plugins.errorclass import ErrorClass, ErrorClassPlugin from nose.plugins.base import Plugin from nose.util import src from .nosetester import get_package_name -from .utils import KnownFailureException, KnownFailureTest +from .utils import KnownFailureException # Some of the classes in this module begin with 'Numpy' to clearly distinguish diff --git a/numpy/typing/tests/data/pass/array_constructors.py b/numpy/typing/tests/data/pass/array_constructors.py index 206f70a15..2763d9c92 100644 --- a/numpy/typing/tests/data/pass/array_constructors.py +++ b/numpy/typing/tests/data/pass/array_constructors.py @@ -1,5 +1,5 @@ import sys -from typing import List, Any +from typing import Any import numpy as np diff --git a/numpy/typing/tests/data/pass/array_like.py b/numpy/typing/tests/data/pass/array_like.py index e16d196b6..5bd2fda20 100644 --- a/numpy/typing/tests/data/pass/array_like.py +++ b/numpy/typing/tests/data/pass/array_like.py @@ -1,4 +1,4 @@ -from typing import Any, List, Optional +from typing import Any, Optional import numpy as np from numpy.typing import ArrayLike, _SupportsArray diff --git a/numpy/typing/tests/data/pass/einsumfunc.py b/numpy/typing/tests/data/pass/einsumfunc.py index a2a39fb1c..429764e67 100644 --- a/numpy/typing/tests/data/pass/einsumfunc.py +++ b/numpy/typing/tests/data/pass/einsumfunc.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import List, Any +from typing import Any import numpy as np diff --git a/numpy/typing/tests/data/pass/lib_utils.py b/numpy/typing/tests/data/pass/lib_utils.py index 0a15dad22..65640c288 100644 --- a/numpy/typing/tests/data/pass/lib_utils.py +++ b/numpy/typing/tests/data/pass/lib_utils.py @@ -1,7 +1,6 @@ from __future__ import annotations from io import StringIO -from typing import Any import numpy as np diff --git a/numpy/typing/tests/data/pass/multiarray.py b/numpy/typing/tests/data/pass/multiarray.py index e5d33c673..26cedfd77 100644 --- a/numpy/typing/tests/data/pass/multiarray.py +++ b/numpy/typing/tests/data/pass/multiarray.py @@ -1,4 +1,3 @@ -from typing import Any import numpy as np import numpy.typing as npt -- cgit v1.2.1 From fb6d3928c847058301ff2365fae90916e8850640 Mon Sep 17 00:00:00 2001 From: Alessia Marcolini <98marcolini@gmail.com> Date: Fri, 8 Oct 2021 09:53:30 +0000 Subject: MAINT: Remove duplicated tests --- numpy/core/tests/test_numeric.py | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 558ded01b..a98c7016a 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -932,25 +932,6 @@ class TestTypes: # Promote with object: assert_equal(promote_types('O', S+'30'), np.dtype('O')) - @pytest.mark.parametrize(["dtype1", "dtype2"], - [[np.dtype("V6"), np.dtype("V10")], - [np.dtype([("name1", "i8")]), np.dtype([("name2", "i8")])], - [np.dtype("i8,i8"), np.dtype("i4,i4")], - ]) - def test_invalid_void_promotion(self, dtype1, dtype2): - # Mainly test structured void promotion, which currently allows - # byte-swapping, but nothing else: - with pytest.raises(TypeError): - np.promote_types(dtype1, dtype2) - - @pytest.mark.parametrize(["dtype1", "dtype2"], - [[np.dtype("V10"), np.dtype("V10")], - [np.dtype([("name1", "i8")])], - [np.dtype("i8,i8"), np.dtype("i8,>i8")], - ]) - def test_valid_void_promotion(self, dtype1, dtype2): - assert np.promote_types(dtype1, dtype2) is dtype1 - @pytest.mark.parametrize("dtype", list(np.typecodes["All"]) + ["i,i", "S3", "S100", "U3", "U100", rational]) -- cgit v1.2.1 From af5a3fc361b3870fb413aaad9df1dac13f39d8ac Mon Sep 17 00:00:00 2001 From: Alessia Marcolini <98marcolini@gmail.com> Date: Fri, 8 Oct 2021 09:58:19 +0000 Subject: MAINT: separate multiple imports in multiple lines --- numpy/distutils/ccompiler_opt.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/ccompiler_opt.py b/numpy/distutils/ccompiler_opt.py index 88e4870d4..9fce05c9e 100644 --- a/numpy/distutils/ccompiler_opt.py +++ b/numpy/distutils/ccompiler_opt.py @@ -8,7 +8,14 @@ the sources with proper compiler's flags. instead only focuses on the compiler side, but it creates abstract C headers that can be used later for the final runtime dispatching process.""" -import os, re, textwrap, pprint, inspect, atexit, subprocess +import atexit +import inspect +import os +import pprint +import re +import subprocess +import textwrap + class _Config: """An abstract class holds all configurable attributes of `CCompilerOpt`, @@ -516,7 +523,8 @@ class _Config: def __init__(self): if self.conf_tmp_path is None: - import tempfile, shutil + import shutil + import tempfile tmp = tempfile.mkdtemp() def rm_temp(): try: -- cgit v1.2.1 From e9753fd770aec5248c180c85b6ec79b734feb812 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 14 May 2021 15:31:02 -0700 Subject: MAINT: Refactor reductions to use NEP 43 style dispatching/promotion This also implements new promoters for the logical functions. The reasoning is that any logical function can always be handled by first casting to boolean without changing the meaning. This is true (unless the output is object), and already effectively used for `np.all`/`np.any`. --- numpy/core/src/multiarray/array_method.h | 11 + numpy/core/src/umath/dispatching.c | 172 ++++++- numpy/core/src/umath/dispatching.h | 4 + numpy/core/src/umath/legacy_array_method.c | 19 + numpy/core/src/umath/reduction.c | 57 +- numpy/core/src/umath/reduction.h | 110 +--- numpy/core/src/umath/ufunc_object.c | 745 +++++++++++++-------------- numpy/core/src/umath/ufunc_type_resolution.c | 22 + numpy/core/src/umath/ufunc_type_resolution.h | 4 + numpy/core/src/umath/umathmodule.c | 29 ++ numpy/core/tests/test_ufunc.py | 26 +- numpy/ma/core.py | 2 +- 12 files changed, 689 insertions(+), 512 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_method.h b/numpy/core/src/multiarray/array_method.h index 3017abf25..7f40c2e8e 100644 --- a/numpy/core/src/multiarray/array_method.h +++ b/numpy/core/src/multiarray/array_method.h @@ -21,6 +21,17 @@ typedef enum { NPY_METH_NO_FLOATINGPOINT_ERRORS = 1 << 2, /* Whether the method supports unaligned access (not runtime) */ NPY_METH_SUPPORTS_UNALIGNED = 1 << 3, + /* + * Private flag for now for *logic* functions. The logical functions + * `logical_or` and `logical_and` can always cast the inputs to booleans + * "safely" (because that is how the cast to bool is defined). + * @seberg: I am not sure this is the best way to handle this, so its + * private for now (also it is very limited anyway). + * There is one "exception". NA aware dtypes cannot cast to bool + * (hopefully), so the `??->?` loop should error even with this flag. + * But a second NA fallback loop will be necessary. + */ + _NPY_METH_FORCE_CAST_INPUTS = 1 << 17, /* All flags which can change at runtime */ NPY_METH_RUNTIME_FLAGS = ( diff --git a/numpy/core/src/umath/dispatching.c b/numpy/core/src/umath/dispatching.c index 40de28754..6ad242589 100644 --- a/numpy/core/src/umath/dispatching.c +++ b/numpy/core/src/umath/dispatching.c @@ -254,6 +254,10 @@ resolve_implementation_info(PyUFuncObject *ufunc, for (Py_ssize_t i = 0; i < nargs; i++) { int best; + /* Whether this (normally output) dtype was specified at all */ + npy_bool is_not_specified = ( + op_dtypes[i] == (PyArray_DTypeMeta *)Py_None); + PyObject *prev_dtype = PyTuple_GET_ITEM(best_dtypes, i); PyObject *new_dtype = PyTuple_GET_ITEM(curr_dtypes, i); @@ -267,8 +271,38 @@ resolve_implementation_info(PyUFuncObject *ufunc, * the subclass should be considered a better match * (subclasses are always more specific). */ + if (is_not_specified) { + /* + * When DType is completely unspecified, prefer abstract + * over concrete, assuming it will resolve. + * Furthermore, we cannot decide which abstract/None + * is "better", only concrete ones which are subclasses + * of Abstract ones are defined as worse. + */ + npy_bool prev_is_concrete = NPY_FALSE; + npy_bool new_is_concrete = NPY_FALSE; + if ((prev_dtype != Py_None) && + !NPY_DT_is_abstract((PyArray_DTypeMeta *)prev_dtype)) { + prev_is_concrete = NPY_TRUE; + } + if ((new_dtype != Py_None) && + !NPY_DT_is_abstract((PyArray_DTypeMeta *)new_dtype)) { + new_is_concrete = NPY_TRUE; + } + if (prev_is_concrete == new_is_concrete) { + best = -1; + } + else if (prev_is_concrete) { + unambiguously_equally_good = 0; + best = 1; + } + else { + unambiguously_equally_good = 0; + best = 0; + } + } /* If either is None, the other is strictly more specific */ - if (prev_dtype == Py_None) { + else if (prev_dtype == Py_None) { unambiguously_equally_good = 0; best = 1; } @@ -289,13 +323,29 @@ resolve_implementation_info(PyUFuncObject *ufunc, */ best = -1; } + else if (!NPY_DT_is_abstract((PyArray_DTypeMeta *)prev_dtype)) { + /* old is not abstract, so better (both not possible) */ + unambiguously_equally_good = 0; + best = 0; + } + else if (!NPY_DT_is_abstract((PyArray_DTypeMeta *)new_dtype)) { + /* new is not abstract, so better (both not possible) */ + unambiguously_equally_good = 0; + best = 1; + } /* - * TODO: Unreachable, but we will need logic for abstract - * DTypes to decide if one is a subclass of the other - * (And their subclass relation is well defined.) + * TODO: This will need logic for abstract DTypes to decide if + * one is a subclass of the other (And their subclass + * relation is well defined). For now, we bail out + * in cas someone manages to get here. */ else { - assert(0); + PyErr_SetString(PyExc_NotImplementedError, + "deciding which one of two abstract dtypes is " + "a better match is not yet implemented. This " + "will pick the better (or bail) in the future."); + *out_info = NULL; + return -1; } if ((current_best != -1) && (current_best != best)) { @@ -612,6 +662,35 @@ promote_and_get_info_and_ufuncimpl(PyUFuncObject *ufunc, } return info; } + else if (info == NULL && op_dtypes[0] == NULL) { + /* + * If we have a reduction, fill in the unspecified input/array + * assuming it should have the same dtype as the operand input + * (or the output one if given). + * Then, try again. In some cases, this will choose different + * paths, such as `ll->?` instead of an `??->?` loop for `np.equal` + * when the input is `.l->.` (`.` meaning undefined). This will + * then cause an error. But cast to `?` would always lose + * information, and in many cases important information: + * + * ```python + * from operator import eq + * from functools import reduce + * + * reduce(eq, [1, 2, 3]) != reduce(eq, [True, True, True]) + * ``` + * + * The special cases being `logical_(and|or|xor)` which can always + * cast to boolean ahead of time and still give the right answer + * (unsafe cast to bool is fine here). We special case these at + * the time of this comment (NumPy 1.21). + */ + assert(ufunc->nin == 2 && ufunc->nout == 1); + op_dtypes[0] = op_dtypes[2] != NULL ? op_dtypes[2] : op_dtypes[1]; + Py_INCREF(op_dtypes[0]); + return promote_and_get_info_and_ufuncimpl(ufunc, + ops, signature, op_dtypes, allow_legacy_promotion, 1); + } } /* @@ -743,3 +822,86 @@ promote_and_get_ufuncimpl(PyUFuncObject *ufunc, return method; } + + +/* + * Special promoter for the logical ufuncs. The logical ufuncs can always + * use the ??->? and still get the correct output (as long as the output + * is not supposed to be `object`). + */ +static int +logical_ufunc_promoter(PyUFuncObject *NPY_UNUSED(ufunc), + PyArray_DTypeMeta *op_dtypes[], PyArray_DTypeMeta *signature[], + PyArray_DTypeMeta *new_op_dtypes[]) +{ + /* If we find any object at all, we currently force to object */ + int force_object = 0; + + for (int i = 0; i < 3; i++) { + PyArray_DTypeMeta *item; + if (signature[i] != NULL) { + item = signature[i]; + Py_INCREF(item); + if (item->type_num == NPY_OBJECT) { + force_object = 1; + } + } + else { + /* Always override to boolean */ + item = PyArray_DTypeFromTypeNum(NPY_BOOL); + if (op_dtypes[i] != NULL && op_dtypes[i]->type_num == NPY_OBJECT) { + force_object = 1; + } + } + new_op_dtypes[i] = item; + } + if (!force_object) { + return 0; + } + /* Actually, we have to use the OBJECT loop after all, set all we can + * to object (that might not work out, but just try). + * + * TODO: Change this to check for `op_dtypes[0] == NULL` to STOP + * returning `object` for `np.logical_and.reduce(obj_arr)` + * which will also affect `np.all` and `np.any`! + */ + for (int i = 0; i < 3; i++) { + if (signature[i] != NULL) { + continue; + } + Py_SETREF(new_op_dtypes[i], PyArray_DTypeFromTypeNum(NPY_OBJECT)); + } + return 0; +} + + +NPY_NO_EXPORT int +install_logical_ufunc_promoter(PyObject *ufunc) +{ + if (PyObject_Type(ufunc) != (PyObject *)&PyUFunc_Type) { + PyErr_SetString(PyExc_RuntimeError, + "internal numpy array, logical ufunc was not a ufunc?!"); + return -1; + } + PyObject *dtype_tuple = PyTuple_Pack(3, + &PyArrayDescr_Type, &PyArrayDescr_Type, &PyArrayDescr_Type, NULL); + if (dtype_tuple == NULL) { + return -1; + } + PyObject *promoter = PyCapsule_New(&logical_ufunc_promoter, + "numpy._ufunc_promoter", NULL); + if (promoter == NULL) { + Py_DECREF(dtype_tuple); + return -1; + } + + PyObject *info = PyTuple_Pack(2, dtype_tuple, promoter); + Py_DECREF(dtype_tuple); + Py_DECREF(promoter); + if (info == NULL) { + return -1; + } + + return PyUFunc_AddLoop((PyUFuncObject *)ufunc, info, 0); +} + diff --git a/numpy/core/src/umath/dispatching.h b/numpy/core/src/umath/dispatching.h index 8d116873c..2f314615d 100644 --- a/numpy/core/src/umath/dispatching.h +++ b/numpy/core/src/umath/dispatching.h @@ -26,4 +26,8 @@ NPY_NO_EXPORT PyObject * add_and_return_legacy_wrapping_ufunc_loop(PyUFuncObject *ufunc, PyArray_DTypeMeta *operation_dtypes[], int ignore_duplicate); +NPY_NO_EXPORT int +install_logical_ufunc_promoter(PyObject *ufunc); + + #endif /*_NPY_DISPATCHING_H */ diff --git a/numpy/core/src/umath/legacy_array_method.c b/numpy/core/src/umath/legacy_array_method.c index 77b1b9013..a423823d4 100644 --- a/numpy/core/src/umath/legacy_array_method.c +++ b/numpy/core/src/umath/legacy_array_method.c @@ -217,6 +217,25 @@ PyArray_NewLegacyWrappingArrayMethod(PyUFuncObject *ufunc, */ int any_output_flexible = 0; NPY_ARRAYMETHOD_FLAGS flags = 0; + if (ufunc->nargs == 3 && + signature[0]->type_num == NPY_BOOL && + signature[1]->type_num == NPY_BOOL && + signature[2]->type_num == NPY_BOOL && ( + strcmp(ufunc->name, "logical_or") == 0 || + strcmp(ufunc->name, "logical_and") == 0 || + strcmp(ufunc->name, "logical_xor") == 0)) { + /* + * This is a logical ufunc, and the `??->?` loop`. It is always OK + * to cast any input to bool, because that cast is defined by + * truthiness. + * This allows to ensure two things: + * 1. `np.all`/`np.any` know that force casting the input is OK + * (they must do this since there are no `?l->?`, etc. loops) + * 2. The logical functions automatically work for any DType + * implementing a cast to boolean. + */ + flags = _NPY_METH_FORCE_CAST_INPUTS; + } for (int i = 0; i < ufunc->nin+ufunc->nout; i++) { if (signature[i]->singleton->flags & ( diff --git a/numpy/core/src/umath/reduction.c b/numpy/core/src/umath/reduction.c index d5a251368..c28c8abd8 100644 --- a/numpy/core/src/umath/reduction.c +++ b/numpy/core/src/umath/reduction.c @@ -145,14 +145,12 @@ PyArray_CopyInitialReduceValues( * boilerplate code, just calling the appropriate inner loop function where * necessary. * + * context : The ArrayMethod context (with ufunc, method, and descriptors). * operand : The array to be reduced. * out : NULL, or the array into which to place the result. * wheremask : NOT YET SUPPORTED, but this parameter is placed here * so that support can be added in the future without breaking * API compatibility. Pass in NULL. - * operand_dtype : The dtype the inner loop expects for the operand. - * result_dtype : The dtype the inner loop expects for the result. - * casting : The casting rule to apply to the operands. * axis_flags : Flags indicating the reduction axes of 'operand'. * reorderable : If True, the reduction being done is reorderable, which * means specifying multiple axes of reduction at once is ok, @@ -182,10 +180,8 @@ PyArray_CopyInitialReduceValues( * generalized ufuncs!) */ NPY_NO_EXPORT PyArrayObject * -PyUFunc_ReduceWrapper( +PyUFunc_ReduceWrapper(PyArrayMethod_Context *context, PyArrayObject *operand, PyArrayObject *out, PyArrayObject *wheremask, - PyArray_Descr *operand_dtype, PyArray_Descr *result_dtype, - NPY_CASTING casting, npy_bool *axis_flags, int reorderable, int keepdims, PyObject *identity, PyArray_ReduceLoopFunc *loop, void *data, npy_intp buffersize, const char *funcname, int errormask) @@ -199,6 +195,8 @@ PyUFunc_ReduceWrapper( PyArrayObject *op[3]; PyArray_Descr *op_dtypes[3]; npy_uint32 it_flags, op_flags[3]; + /* Loop auxdata (must be freed on error) */ + NpyAuxData *auxdata = NULL; /* More than one axis means multiple orders are possible */ if (!reorderable && count_axes(PyArray_NDIM(operand), axis_flags) > 1) { @@ -221,8 +219,8 @@ PyUFunc_ReduceWrapper( /* Set up the iterator */ op[0] = out; op[1] = operand; - op_dtypes[0] = result_dtype; - op_dtypes[1] = operand_dtype; + op_dtypes[0] = context->descriptors[0]; + op_dtypes[1] = context->descriptors[1]; it_flags = NPY_ITER_BUFFERED | NPY_ITER_EXTERNAL_LOOP | @@ -291,7 +289,7 @@ PyUFunc_ReduceWrapper( } iter = NpyIter_AdvancedNew(wheremask == NULL ? 2 : 3, op, it_flags, - NPY_KEEPORDER, casting, + NPY_KEEPORDER, NPY_UNSAFE_CASTING, op_flags, op_dtypes, PyArray_NDIM(operand), op_axes, NULL, buffersize); @@ -301,9 +299,29 @@ PyUFunc_ReduceWrapper( result = NpyIter_GetOperandArray(iter)[0]; - int needs_api = NpyIter_IterationNeedsAPI(iter); - /* Start with the floating-point exception flags cleared */ - npy_clear_floatstatus_barrier((char*)&iter); + PyArrayMethod_StridedLoop *strided_loop; + NPY_ARRAYMETHOD_FLAGS flags = 0; + npy_intp fixed_strides[3]; + NpyIter_GetInnerFixedStrideArray(iter, fixed_strides); + if (wheremask != NULL) { + if (PyArrayMethod_GetMaskedStridedLoop(context, + 1, fixed_strides, &strided_loop, &auxdata, &flags) < 0) { + goto fail; + } + } + else { + if (context->method->get_strided_loop(context, + 1, 0, fixed_strides, &strided_loop, &auxdata, &flags) < 0) { + goto fail; + } + } + + int needs_api = (flags & NPY_METH_REQUIRES_PYAPI) != 0; + needs_api |= NpyIter_IterationNeedsAPI(iter); + if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) { + /* Start with the floating-point exception flags cleared */ + npy_clear_floatstatus_barrier((char*)&iter); + } /* * Initialize the result to the reduction unit if possible, @@ -345,16 +363,18 @@ PyUFunc_ReduceWrapper( strideptr = NpyIter_GetInnerStrideArray(iter); countptr = NpyIter_GetInnerLoopSizePtr(iter); - if (loop(iter, dataptr, strideptr, countptr, - iternext, needs_api, skip_first_count, data) < 0) { + if (loop(context, strided_loop, auxdata, + iter, dataptr, strideptr, countptr, iternext, + needs_api, skip_first_count) < 0) { goto fail; } } - /* Check whether any errors occurred during the loop */ - if (PyErr_Occurred() || - _check_ufunc_fperr(errormask, NULL, "reduce") < 0) { - goto fail; + if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) { + /* NOTE: We could check float errors even on error */ + if (_check_ufunc_fperr(errormask, NULL, "reduce") < 0) { + goto fail; + } } if (out != NULL) { @@ -369,6 +389,7 @@ PyUFunc_ReduceWrapper( return result; fail: + NPY_AUXDATA_FREE(auxdata); if (iter != NULL) { NpyIter_Deallocate(iter); } diff --git a/numpy/core/src/umath/reduction.h b/numpy/core/src/umath/reduction.h index 372605dba..bb00dad21 100644 --- a/numpy/core/src/umath/reduction.h +++ b/numpy/core/src/umath/reduction.h @@ -19,93 +19,17 @@ typedef int (PyArray_AssignReduceIdentityFunc)(PyArrayObject *result, void *data); /* - * This is a function for the reduce loop. + * Inner definition of the reduce loop, only used for a static function. + * At some point around NmPy 1.6, there was probably an intention to make + * the reduce loop customizable at this level (per ufunc?). * - * The needs_api parameter indicates whether it's ok to release the GIL during - * the loop, such as when the iternext() function never calls - * a function which could raise a Python exception. - * - * The skip_first_count parameter indicates how many elements need to be - * skipped based on NpyIter_IsFirstVisit checks. This can only be positive - * when the 'assign_identity' parameter was NULL when calling - * PyArray_ReduceWrapper. - * - * The loop gets two data pointers and two strides, and should - * look roughly like this: - * { - * NPY_BEGIN_THREADS_DEF; - * if (!needs_api) { - * NPY_BEGIN_THREADS; - * } - * // This first-visit loop can be skipped if 'assign_identity' was non-NULL - * if (skip_first_count > 0) { - * do { - * char *data0 = dataptr[0], *data1 = dataptr[1]; - * npy_intp stride0 = strideptr[0], stride1 = strideptr[1]; - * npy_intp count = *countptr; - * - * // Skip any first-visit elements - * if (NpyIter_IsFirstVisit(iter, 0)) { - * if (stride0 == 0) { - * --count; - * --skip_first_count; - * data1 += stride1; - * } - * else { - * skip_first_count -= count; - * count = 0; - * } - * } - * - * while (count--) { - * *(result_t *)data0 = my_reduce_op(*(result_t *)data0, - * *(operand_t *)data1); - * data0 += stride0; - * data1 += stride1; - * } - * - * // Jump to the faster loop when skipping is done - * if (skip_first_count == 0) { - * if (iternext(iter)) { - * break; - * } - * else { - * goto finish_loop; - * } - * } - * } while (iternext(iter)); - * } - * do { - * char *data0 = dataptr[0], *data1 = dataptr[1]; - * npy_intp stride0 = strideptr[0], stride1 = strideptr[1]; - * npy_intp count = *countptr; - * - * while (count--) { - * *(result_t *)data0 = my_reduce_op(*(result_t *)data0, - * *(operand_t *)data1); - * data0 += stride0; - * data1 += stride1; - * } - * } while (iternext(iter)); - * finish_loop: - * if (!needs_api) { - * NPY_END_THREADS; - * } - * return (needs_api && PyErr_Occurred()) ? -1 : 0; - * } - * - * If needs_api is True, this function should call PyErr_Occurred() - * to check if an error occurred during processing, and return -1 for - * error, 0 for success. + * TODO: This should be refactored/removed. */ -typedef int (PyArray_ReduceLoopFunc)(NpyIter *iter, - char **dataptr, - npy_intp const *strideptr, - npy_intp const *countptr, - NpyIter_IterNextFunc *iternext, - int needs_api, - npy_intp skip_first_count, - void *data); +typedef int (PyArray_ReduceLoopFunc)(PyArrayMethod_Context *context, + PyArrayMethod_StridedLoop *strided_loop, NpyAuxData *auxdata, + NpyIter *iter, char **dataptrs, npy_intp const *strides, + npy_intp const *countptr, NpyIter_IterNextFunc *iternext, + int needs_api, npy_intp skip_first_count); /* * This function executes all the standard NumPy reduction function @@ -138,16 +62,10 @@ typedef int (PyArray_ReduceLoopFunc)(NpyIter *iter, * errormask : forwarded from _get_bufsize_errmask */ NPY_NO_EXPORT PyArrayObject * -PyUFunc_ReduceWrapper(PyArrayObject *operand, PyArrayObject *out, - PyArrayObject *wheremask, - PyArray_Descr *operand_dtype, - PyArray_Descr *result_dtype, - NPY_CASTING casting, - npy_bool *axis_flags, int reorderable, - int keepdims, - PyObject *identity, - PyArray_ReduceLoopFunc *loop, - void *data, npy_intp buffersize, const char *funcname, - int errormask); +PyUFunc_ReduceWrapper(PyArrayMethod_Context *context, + PyArrayObject *operand, PyArrayObject *out, PyArrayObject *wheremask, + npy_bool *axis_flags, int reorderable, int keepdims, + PyObject *identity, PyArray_ReduceLoopFunc *loop, + void *data, npy_intp buffersize, const char *funcname, int errormask); #endif diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 42290e8c9..06533b127 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -990,6 +990,7 @@ convert_ufunc_arguments(PyUFuncObject *ufunc, } /* Convert and fill in output arguments */ + memset(out_op_DTypes + nin, 0, nout * sizeof(*out_op_DTypes)); if (full_args.out != NULL) { for (int i = 0; i < nout; i++) { obj = PyTuple_GET_ITEM(full_args.out, i); @@ -1047,6 +1048,7 @@ check_for_trivial_loop(PyArrayMethodObject *ufuncimpl, PyArrayObject **op, PyArray_Descr **dtypes, NPY_CASTING casting, npy_intp buffersize) { + int force_cast_input = ufuncimpl->flags & _NPY_METH_FORCE_CAST_INPUTS; int i, nin = ufuncimpl->nin, nop = nin + ufuncimpl->nout; for (i = 0; i < nop; ++i) { @@ -1070,7 +1072,13 @@ check_for_trivial_loop(PyArrayMethodObject *ufuncimpl, must_copy = 1; } - if (PyArray_MinCastSafety(safety, casting) != casting) { + if (force_cast_input && i < nin) { + /* + * ArrayMethod flagged to ignore casting (logical funcs + * can force cast to bool) + */ + } + else if (PyArray_MinCastSafety(safety, casting) != casting) { return 0; /* the cast is not safe enough */ } } @@ -1360,8 +1368,15 @@ validate_casting(PyArrayMethodObject *method, PyUFuncObject *ufunc, */ return 0; } - if (PyUFunc_ValidateCasting(ufunc, casting, ops, descriptors) < 0) { - return -1; + if (method->flags & _NPY_METH_FORCE_CAST_INPUTS) { + if (PyUFunc_ValidateOutCasting(ufunc, casting, ops, descriptors) < 0) { + return -1; + } + } + else { + if (PyUFunc_ValidateCasting(ufunc, casting, ops, descriptors) < 0) { + return -1; + } } return 0; } @@ -2661,195 +2676,117 @@ PyUFunc_GenericFunction(PyUFuncObject *NPY_UNUSED(ufunc), /* - * Given the output type, finds the specified binary op. The - * ufunc must have nin==2 and nout==1. The function may modify - * otype if the given type isn't found. + * Promote and resolve a reduction like operation. * - * Returns 0 on success, -1 on failure. + * @param ufunc + * @param arr The operation array (out was never used) + * @param signature The DType signature, which may already be set due to the + * dtype passed in by the user, or the special cases (add, multiply). + * (Contains strong references and may be modified.) + * @param enforce_uniform_args If `1` fully uniform dtypes/descriptors are + * enforced as required for accumulate and (currently) reduceat. + * @param out_descrs New references to the resolved descriptors (on success). + + * @returns ufuncimpl The `ArrayMethod` implemention to use. Or NULL if an + * error occurred. */ -static int -get_binary_op_function(PyUFuncObject *ufunc, int *otype, - PyUFuncGenericFunction *out_innerloop, - void **out_innerloopdata) +static PyArrayMethodObject * +reducelike_promote_and_resolve(PyUFuncObject *ufunc, + PyArrayObject *arr, PyArrayObject *out, + PyArray_DTypeMeta *signature[3], + int enforce_uniform_args, PyArray_Descr *out_descrs[3], + char *method_type) { - int i; - - NPY_UF_DBG_PRINT1("Getting binary op function for type number %d\n", - *otype); - - /* If the type is custom and there are userloops, search for it here */ - if (ufunc->userloops != NULL && PyTypeNum_ISUSERDEF(*otype)) { - PyObject *key, *obj; - key = PyLong_FromLong(*otype); - if (key == NULL) { - return -1; - } - obj = PyDict_GetItemWithError(ufunc->userloops, key); - Py_DECREF(key); - if (obj == NULL && PyErr_Occurred()) { - return -1; - } - else if (obj != NULL) { - PyUFunc_Loop1d *funcdata = PyCapsule_GetPointer(obj, NULL); - if (funcdata == NULL) { - return -1; - } - while (funcdata != NULL) { - int *types = funcdata->arg_types; - - if (types[0] == *otype && types[1] == *otype && - types[2] == *otype) { - *out_innerloop = funcdata->func; - *out_innerloopdata = funcdata->data; - return 0; - } + /* + * Note that the `ops` is not realy correct. But legacy resolution + * cannot quite handle the correct ops (e.g. a NULL first item if `out` + * is NULL), and it should only matter in very strange cases. + */ + PyArrayObject *ops[3] = {arr, arr, NULL}; + /* + * TODO: If `out` is not provided, arguably `initial` could define + * the first DType (and maybe also the out one), that way + * `np.add.reduce([1, 2, 3], initial=3.4)` would return a float + * value. As of 1.20, it returned an integer, so that should + * probably go to an error/warning first. + */ + PyArray_DTypeMeta *operation_DTypes[3] = { + NULL, NPY_DTYPE(PyArray_DESCR(arr)), NULL}; - funcdata = funcdata->next; - } - } + if (out != NULL) { + operation_DTypes[0] = NPY_DTYPE(PyArray_DESCR(out)); + operation_DTypes[2] = operation_DTypes[0]; } - /* Search for a function with compatible inputs */ - for (i = 0; i < ufunc->ntypes; ++i) { - char *types = ufunc->types + i*ufunc->nargs; - - NPY_UF_DBG_PRINT3("Trying loop with signature %d %d -> %d\n", - types[0], types[1], types[2]); - - if (PyArray_CanCastSafely(*otype, types[0]) && - types[0] == types[1] && - (*otype == NPY_OBJECT || types[0] != NPY_OBJECT)) { - /* If the signature is "xx->x", we found the loop */ - if (types[2] == types[0]) { - *out_innerloop = ufunc->functions[i]; - *out_innerloopdata = ufunc->data[i]; - *otype = types[0]; - return 0; - } - /* - * Otherwise, we found the natural type of the reduction, - * replace otype and search again - */ - else { - *otype = types[2]; - break; - } + PyArrayMethodObject *ufuncimpl = promote_and_get_ufuncimpl(ufunc, + ops, signature, operation_DTypes, NPY_FALSE, NPY_TRUE); + if (ufuncimpl == NULL) { + if (!PyErr_Occurred()) { + PyErr_Format(PyExc_TypeError, + "Could not find a matching loop for %s.reduce.", + method_type); + return NULL; } + return NULL; } - /* Search for the exact function */ - for (i = 0; i < ufunc->ntypes; ++i) { - char *types = ufunc->types + i*ufunc->nargs; - - if (PyArray_CanCastSafely(*otype, types[0]) && - types[0] == types[1] && - types[1] == types[2] && - (*otype == NPY_OBJECT || types[0] != NPY_OBJECT)) { - /* Since the signature is "xx->x", we found the loop */ - *out_innerloop = ufunc->functions[i]; - *out_innerloopdata = ufunc->data[i]; - *otype = types[0]; - return 0; - } + /* Find the correct descriptors for the operation */ + if (resolve_descriptors(3, ufunc, ufuncimpl, + ops, out_descrs, signature, NPY_UNSAFE_CASTING) < 0) { + return NULL; } - return -1; -} - -static int -reduce_type_resolver(PyUFuncObject *ufunc, PyArrayObject *arr, - PyArray_Descr *odtype, PyArray_Descr **out_dtype) -{ - int i, retcode; - PyArrayObject *op[3] = {arr, arr, NULL}; - PyArray_Descr *dtypes[3] = {NULL, NULL, NULL}; - const char *ufunc_name = ufunc_get_name_cstr(ufunc); - PyObject *type_tup = NULL; - - *out_dtype = NULL; - /* - * If odtype is specified, make a type tuple for the type - * resolution. + * The first operand and output be the same array, so they should + * be identical. The second argument can be different for reductions, + * but is checked to be identical for accumulate and reduceat. */ - if (odtype != NULL) { - type_tup = PyTuple_Pack(3, odtype, odtype, Py_None); - if (type_tup == NULL) { - return -1; - } - } - - /* Use the type resolution function to find our loop */ - retcode = ufunc->type_resolver( - ufunc, NPY_UNSAFE_CASTING, - op, type_tup, dtypes); - Py_DECREF(type_tup); - if (retcode == -1) { - return -1; - } - else if (retcode == -2) { - PyErr_Format(PyExc_RuntimeError, - "type resolution returned NotImplemented to " - "reduce ufunc %s", ufunc_name); - return -1; + if (out_descrs[0] != out_descrs[2] || ( + enforce_uniform_args && out_descrs[0] != out_descrs[1])) { + PyErr_Format(PyExc_TypeError, + "The resolved dtypes are not compatible with an accumulate " + "or reduceat loop."); + goto fail; } - - /* - * The first two type should be equivalent. Because of how - * reduce has historically behaved in NumPy, the return type - * could be different, and it is the return type on which the - * reduction occurs. - */ - if (!PyArray_EquivTypes(dtypes[0], dtypes[1])) { - for (i = 0; i < 3; ++i) { - Py_DECREF(dtypes[i]); - } - PyErr_Format(PyExc_RuntimeError, - "could not find a type resolution appropriate for " - "reduce ufunc %s", ufunc_name); - return -1; + /* TODO: This really should _not_ be unsafe casting (same above)! */ + if (validate_casting(ufuncimpl, + ufunc, ops, out_descrs, NPY_UNSAFE_CASTING) < 0) { + goto fail; } - Py_DECREF(dtypes[0]); - Py_DECREF(dtypes[1]); - *out_dtype = dtypes[2]; + return ufuncimpl; - return 0; + fail: + for (int i = 0; i < 3; ++i) { + Py_DECREF(out_descrs[i]); + } + return NULL; } + static int -reduce_loop(NpyIter *iter, char **dataptrs, npy_intp const *strides, - npy_intp const *countptr, NpyIter_IterNextFunc *iternext, - int needs_api, npy_intp skip_first_count, void *data) +reduce_loop(PyArrayMethod_Context *context, + PyArrayMethod_StridedLoop *strided_loop, NpyAuxData *auxdata, + NpyIter *iter, char **dataptrs, npy_intp const *strides, + npy_intp const *countptr, NpyIter_IterNextFunc *iternext, + int needs_api, npy_intp skip_first_count) { - PyArray_Descr *dtypes[3], **iter_dtypes; - PyUFuncObject *ufunc = (PyUFuncObject *)data; - char *dataptrs_copy[3]; - npy_intp strides_copy[3]; + int retval; + char *dataptrs_copy[4]; + npy_intp strides_copy[4]; npy_bool masked; - /* The normal selected inner loop */ - PyUFuncGenericFunction innerloop = NULL; - void *innerloopdata = NULL; - NPY_BEGIN_THREADS_DEF; /* Get the number of operands, to determine whether "where" is used */ masked = (NpyIter_GetNOp(iter) == 3); - /* Get the inner loop */ - iter_dtypes = NpyIter_GetDescrArray(iter); - dtypes[0] = iter_dtypes[0]; - dtypes[1] = iter_dtypes[1]; - dtypes[2] = iter_dtypes[0]; - if (ufunc->legacy_inner_loop_selector(ufunc, dtypes, - &innerloop, &innerloopdata, &needs_api) < 0) { - return -1; + if (!needs_api) { + NPY_BEGIN_THREADS_THRESHOLDED(NpyIter_GetIterSize(iter)); } - NPY_BEGIN_THREADS_NDITER(iter); - if (skip_first_count > 0) { - do { + assert(!masked); /* Path currently not available for masked */ + while (1) { npy_intp count = *countptr; /* Skip any first-visit elements */ @@ -2872,27 +2809,23 @@ reduce_loop(NpyIter *iter, char **dataptrs, npy_intp const *strides, strides_copy[0] = strides[0]; strides_copy[1] = strides[1]; strides_copy[2] = strides[0]; - innerloop(dataptrs_copy, &count, - strides_copy, innerloopdata); - if (needs_api && PyErr_Occurred()) { + retval = strided_loop(context, + dataptrs_copy, &count, strides_copy, auxdata); + if (retval < 0) { goto finish_loop; } - /* Jump to the faster loop when skipping is done */ - if (skip_first_count == 0) { - if (iternext(iter)) { - break; - } - else { - goto finish_loop; - } + /* Advance loop, and abort on error (or finish) */ + if (!iternext(iter)) { + goto finish_loop; } - } while (iternext(iter)); - } - if (needs_api && PyErr_Occurred()) { - goto finish_loop; + /* When skipping is done break and continue with faster loop */ + if (skip_first_count == 0) { + break; + } + } } do { @@ -2903,42 +2836,27 @@ reduce_loop(NpyIter *iter, char **dataptrs, npy_intp const *strides, strides_copy[0] = strides[0]; strides_copy[1] = strides[1]; strides_copy[2] = strides[0]; - - if (!masked) { - innerloop(dataptrs_copy, countptr, - strides_copy, innerloopdata); + if (masked) { + /* + * TODO: This code had a broadcasted-mask optimization that may + * make sense in the array-method masked strided loop code. + */ + dataptrs_copy[3] = dataptrs[2]; + strides_copy[3] = strides[2]; } - else { - npy_intp count = *countptr; - char *maskptr = dataptrs[2]; - npy_intp mask_stride = strides[2]; - /* Optimization for when the mask is broadcast */ - npy_intp n = mask_stride == 0 ? count : 1; - while (count) { - char mask = *maskptr; - maskptr += mask_stride; - while (n < count && mask == *maskptr) { - n++; - maskptr += mask_stride; - } - /* If mask set, apply inner loop on this contiguous region */ - if (mask) { - innerloop(dataptrs_copy, &n, - strides_copy, innerloopdata); - } - dataptrs_copy[0] += n * strides[0]; - dataptrs_copy[1] += n * strides[1]; - dataptrs_copy[2] = dataptrs_copy[0]; - count -= n; - n = 1; - } + + retval = strided_loop(context, + dataptrs_copy, countptr, strides_copy, auxdata); + if (retval < 0) { + goto finish_loop; } - } while (!(needs_api && PyErr_Occurred()) && iternext(iter)); + + } while (iternext(iter)); finish_loop: NPY_END_THREADS; - return (needs_api && PyErr_Occurred()) ? -1 : 0; + return retval; } /* @@ -2959,15 +2877,14 @@ finish_loop: * this function does not validate them. */ static PyArrayObject * -PyUFunc_Reduce(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, - int naxes, int *axes, PyArray_Descr *odtype, int keepdims, +PyUFunc_Reduce(PyUFuncObject *ufunc, + PyArrayObject *arr, PyArrayObject *out, + int naxes, int *axes, PyArray_DTypeMeta *signature[3], int keepdims, PyObject *initial, PyArrayObject *wheremask) { int iaxes, ndim; npy_bool reorderable; npy_bool axis_flags[NPY_MAXDIMS]; - PyArray_Descr *dtype; - PyArrayObject *result; PyObject *identity; const char *ufunc_name = ufunc_get_name_cstr(ufunc); /* These parameters come from a TLS global */ @@ -2994,6 +2911,7 @@ PyUFunc_Reduce(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, } /* Get the identity */ + /* TODO: Both of these must be provided by the ArrayMethod! */ identity = _get_identity(ufunc, &reorderable); if (identity == NULL) { return NULL; @@ -3017,21 +2935,27 @@ PyUFunc_Reduce(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, Py_INCREF(initial); /* match the reference count in the if above */ } - /* Get the reduction dtype */ - if (reduce_type_resolver(ufunc, arr, odtype, &dtype) < 0) { + PyArray_Descr *descrs[3]; + PyArrayMethodObject *ufuncimpl = reducelike_promote_and_resolve(ufunc, + arr, out, signature, 0, descrs, "reduce"); + if (ufuncimpl == NULL) { Py_DECREF(initial); return NULL; } - result = PyUFunc_ReduceWrapper(arr, out, wheremask, dtype, dtype, - NPY_UNSAFE_CASTING, - axis_flags, reorderable, - keepdims, - initial, - reduce_loop, - ufunc, buffersize, ufunc_name, errormask); + PyArrayMethod_Context context = { + .caller = (PyObject *)ufunc, + .method = ufuncimpl, + .descriptors = descrs, + }; + + PyArrayObject *result = PyUFunc_ReduceWrapper(&context, + arr, out, wheremask, axis_flags, reorderable, keepdims, + initial, reduce_loop, ufunc, buffersize, ufunc_name, errormask); - Py_DECREF(dtype); + for (int i = 0; i < 3; i++) { + Py_DECREF(descrs[i]); + } Py_DECREF(initial); return result; } @@ -3039,23 +2963,21 @@ PyUFunc_Reduce(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, static PyObject * PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, - int axis, int otype) + int axis, PyArray_DTypeMeta *signature[3]) { PyArrayObject *op[2]; - PyArray_Descr *op_dtypes[2] = {NULL, NULL}; int op_axes_arrays[2][NPY_MAXDIMS]; int *op_axes[2] = {op_axes_arrays[0], op_axes_arrays[1]}; npy_uint32 op_flags[2]; - int idim, ndim, otype_final; + int idim, ndim; int needs_api, need_outer_iterator; - NpyIter *iter = NULL; + int res = 0; - /* The selected inner loop */ - PyUFuncGenericFunction innerloop = NULL; - void *innerloopdata = NULL; + PyArrayMethod_StridedLoop *strided_loop; + NpyAuxData *auxdata = NULL; - const char *ufunc_name = ufunc_get_name_cstr(ufunc); + NpyIter *iter = NULL; /* These parameters come from extobj= or from a TLS global */ int buffersize = 0, errormask = 0; @@ -3077,42 +2999,32 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, /* Take a reference to out for later returning */ Py_XINCREF(out); - otype_final = otype; - if (get_binary_op_function(ufunc, &otype_final, - &innerloop, &innerloopdata) < 0) { - PyArray_Descr *dtype = PyArray_DescrFromType(otype); - PyErr_Format(PyExc_ValueError, - "could not find a matching type for %s.accumulate, " - "requested type has type code '%c'", - ufunc_name, dtype ? dtype->type : '-'); - Py_XDECREF(dtype); - goto fail; + PyArray_Descr *descrs[3]; + PyArrayMethodObject *ufuncimpl = reducelike_promote_and_resolve(ufunc, + arr, out, signature, 1, descrs, "accumulate"); + if (ufuncimpl == NULL) { + return NULL; } - ndim = PyArray_NDIM(arr); + /* The below code assumes that all descriptors are identical: */ + assert(descrs[0] == descrs[1] && descrs[0] == descrs[2]); - /* - * Set up the output data type, using the input's exact - * data type if the type number didn't change to preserve - * metadata - */ - if (PyArray_DESCR(arr)->type_num == otype_final) { - if (PyArray_ISNBO(PyArray_DESCR(arr)->byteorder)) { - op_dtypes[0] = PyArray_DESCR(arr); - Py_INCREF(op_dtypes[0]); - } - else { - op_dtypes[0] = PyArray_DescrNewByteorder(PyArray_DESCR(arr), - NPY_NATIVE); - } - } - else { - op_dtypes[0] = PyArray_DescrFromType(otype_final); - } - if (op_dtypes[0] == NULL) { + if (PyDataType_REFCHK(descrs[2]) && descrs[2]->type_num != NPY_OBJECT) { + /* This can be removed, but the initial element copy needs fixing */ + PyErr_SetString(PyExc_TypeError, + "accumulation currently only supports `object` dtype with " + "references"); goto fail; } + PyArrayMethod_Context context = { + .caller = (PyObject *)ufunc, + .method = ufuncimpl, + .descriptors = descrs, + }; + + ndim = PyArray_NDIM(arr); + #if NPY_UF_DBG_TRACING printf("Found %s.accumulate inner loop with dtype : ", ufunc_name); PyObject_Print((PyObject *)op_dtypes[0], stdout, 0); @@ -3138,9 +3050,9 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, need_outer_iterator = (ndim > 1); /* We can't buffer, so must do UPDATEIFCOPY */ if (!PyArray_ISALIGNED(arr) || (out && !PyArray_ISALIGNED(out)) || - !PyArray_EquivTypes(op_dtypes[0], PyArray_DESCR(arr)) || + !PyArray_EquivTypes(descrs[1], PyArray_DESCR(arr)) || (out && - !PyArray_EquivTypes(op_dtypes[0], PyArray_DESCR(out)))) { + !PyArray_EquivTypes(descrs[0], PyArray_DESCR(out)))) { need_outer_iterator = 1; } /* If input and output overlap in memory, use iterator to figure it out */ @@ -3153,7 +3065,6 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, npy_uint32 flags = NPY_ITER_ZEROSIZE_OK| NPY_ITER_REFS_OK| NPY_ITER_COPY_IF_OVERLAP; - PyArray_Descr **op_dtypes_param = NULL; /* * The way accumulate is set up, we can't do buffering, @@ -3170,13 +3081,11 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, */ op_flags[0] |= NPY_ITER_UPDATEIFCOPY|NPY_ITER_ALIGNED|NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE; op_flags[1] |= NPY_ITER_COPY|NPY_ITER_ALIGNED|NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE; - op_dtypes_param = op_dtypes; - op_dtypes[1] = op_dtypes[0]; + NPY_UF_DBG_PRINT("Allocating outer iterator\n"); iter = NpyIter_AdvancedNew(2, op, flags, NPY_KEEPORDER, NPY_UNSAFE_CASTING, - op_flags, - op_dtypes_param, + op_flags, descrs, ndim_iter, op_axes, NULL, 0); if (iter == NULL) { goto fail; @@ -3194,14 +3103,14 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, } } - /* Get the output */ + /* Get the output from the iterator if it was allocated */ if (out == NULL) { if (iter) { op[0] = out = NpyIter_GetOperandArray(iter)[0]; Py_INCREF(out); } else { - PyArray_Descr *dtype = op_dtypes[0]; + PyArray_Descr *dtype = descrs[0]; Py_INCREF(dtype); op[0] = out = (PyArrayObject *)PyArray_NewFromDescr( &PyArray_Type, dtype, @@ -3210,10 +3119,31 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, if (out == NULL) { goto fail; } - } } + npy_intp fixed_strides[3]; + if (need_outer_iterator) { + NpyIter_GetInnerFixedStrideArray(iter, fixed_strides); + } + else { + fixed_strides[0] = PyArray_STRIDES(op[0])[axis]; + fixed_strides[1] = PyArray_STRIDES(op[1])[axis]; + fixed_strides[2] = fixed_strides[0]; + } + + + NPY_ARRAYMETHOD_FLAGS flags = 0; + if (ufuncimpl->get_strided_loop(&context, + 1, 0, fixed_strides, &strided_loop, &auxdata, &flags) < 0) { + goto fail; + } + needs_api = (flags & NPY_METH_REQUIRES_PYAPI) != 0; + if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) { + /* Start with the floating-point exception flags cleared */ + npy_clear_floatstatus_barrier((char*)&iter); + } + /* * If the reduction axis has size zero, either return the reduction * unit for UFUNC_REDUCE, or return the zero-sized output array @@ -3234,7 +3164,7 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, NpyIter_IterNextFunc *iternext; char **dataptr; - int itemsize = op_dtypes[0]->elsize; + int itemsize = descrs[0]->elsize; /* Get the variables needed for the loop */ iternext = NpyIter_GetIterNext(iter, NULL); @@ -3242,8 +3172,7 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, goto fail; } dataptr = NpyIter_GetDataPtrArray(iter); - needs_api = NpyIter_IterationNeedsAPI(iter); - + needs_api |= NpyIter_IterationNeedsAPI(iter); /* Execute the loop with just the outer iterator */ count_m1 = PyArray_DIM(op[1], axis)-1; @@ -3257,7 +3186,9 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, stride_copy[1] = stride1; stride_copy[2] = stride0; - NPY_BEGIN_THREADS_NDITER(iter); + if (!needs_api) { + NPY_BEGIN_THREADS_THRESHOLDED(NpyIter_GetIterSize(iter)); + } do { dataptr_copy[0] = dataptr[0]; @@ -3270,7 +3201,7 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, * Output (dataptr[0]) and input (dataptr[1]) may point to * the same memory, e.g. np.add.accumulate(a, out=a). */ - if (otype == NPY_OBJECT) { + if (descrs[2]->type_num == NPY_OBJECT) { /* * Incref before decref to avoid the possibility of the * reference count being zero temporarily. @@ -3290,18 +3221,17 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, dataptr_copy[2] += stride0; NPY_UF_DBG_PRINT1("iterator loop count %d\n", (int)count_m1); - innerloop(dataptr_copy, &count_m1, - stride_copy, innerloopdata); + res = strided_loop(&context, + dataptr_copy, &count_m1, stride_copy, auxdata); } - } while (!(needs_api && PyErr_Occurred()) && iternext(iter)); + } while (res == 0 && iternext(iter)); NPY_END_THREADS; } else if (iter == NULL) { char *dataptr_copy[3]; - npy_intp stride_copy[3]; - int itemsize = op_dtypes[0]->elsize; + int itemsize = descrs[0]->elsize; /* Execute the loop with no iterators */ npy_intp count = PyArray_DIM(op[1], axis); @@ -3315,15 +3245,11 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, PyArray_NDIM(op[0]))) { PyErr_SetString(PyExc_ValueError, "provided out is the wrong size " - "for the reduction"); + "for the accumulation."); goto fail; } stride0 = PyArray_STRIDE(op[0], axis); - stride_copy[0] = stride0; - stride_copy[1] = stride1; - stride_copy[2] = stride0; - /* Turn the two items into three for the inner loop */ dataptr_copy[0] = PyArray_BYTES(op[0]); dataptr_copy[1] = PyArray_BYTES(op[1]); @@ -3335,7 +3261,7 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, * Output (dataptr[0]) and input (dataptr[1]) may point to the * same memory, e.g. np.add.accumulate(a, out=a). */ - if (otype == NPY_OBJECT) { + if (descrs[2]->type_num == NPY_OBJECT) { /* * Incref before decref to avoid the possibility of the * reference count being zero temporarily. @@ -3356,25 +3282,34 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, NPY_UF_DBG_PRINT1("iterator loop count %d\n", (int)count); - needs_api = PyDataType_REFCHK(op_dtypes[0]); + needs_api = PyDataType_REFCHK(descrs[0]); if (!needs_api) { NPY_BEGIN_THREADS_THRESHOLDED(count); } - innerloop(dataptr_copy, &count, - stride_copy, innerloopdata); + res = strided_loop(&context, + dataptr_copy, &count, fixed_strides, auxdata); NPY_END_THREADS; } } finish: - Py_XDECREF(op_dtypes[0]); - int res = 0; + NPY_AUXDATA_FREE(auxdata); + Py_DECREF(descrs[0]); + Py_DECREF(descrs[1]); + Py_DECREF(descrs[2]); + if (!NpyIter_Deallocate(iter)) { res = -1; } + + if (res == 0 && !(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) { + /* NOTE: We could check float errors even when `res < 0` */ + res = _check_ufunc_fperr(errormask, NULL, "accumulate"); + } + if (res < 0) { Py_DECREF(out); return NULL; @@ -3384,7 +3319,11 @@ finish: fail: Py_XDECREF(out); - Py_XDECREF(op_dtypes[0]); + + NPY_AUXDATA_FREE(auxdata); + Py_XDECREF(descrs[0]); + Py_XDECREF(descrs[1]); + Py_XDECREF(descrs[2]); NpyIter_Deallocate(iter); @@ -3409,28 +3348,31 @@ fail: * indices[1::2] = range(1,len(array)) * * output shape is based on the size of indices + * + * TODO: Reduceat duplicates too much code from accumulate! */ static PyObject * PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, - PyArrayObject *out, int axis, int otype) + PyArrayObject *out, int axis, PyArray_DTypeMeta *signature[3]) { PyArrayObject *op[3]; - PyArray_Descr *op_dtypes[3] = {NULL, NULL, NULL}; int op_axes_arrays[3][NPY_MAXDIMS]; int *op_axes[3] = {op_axes_arrays[0], op_axes_arrays[1], op_axes_arrays[2]}; npy_uint32 op_flags[3]; - int idim, ndim, otype_final; - int need_outer_iterator = 0; + int idim, ndim; + int needs_api, need_outer_iterator = 0; + + int res = 0; NpyIter *iter = NULL; + PyArrayMethod_StridedLoop *strided_loop; + NpyAuxData *auxdata = NULL; + /* The reduceat indices - ind must be validated outside this call */ npy_intp *reduceat_ind; npy_intp i, ind_size, red_axis_size; - /* The selected inner loop */ - PyUFuncGenericFunction innerloop = NULL; - void *innerloopdata = NULL; const char *ufunc_name = ufunc_get_name_cstr(ufunc); char *opname = "reduceat"; @@ -3470,42 +3412,32 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, /* Take a reference to out for later returning */ Py_XINCREF(out); - otype_final = otype; - if (get_binary_op_function(ufunc, &otype_final, - &innerloop, &innerloopdata) < 0) { - PyArray_Descr *dtype = PyArray_DescrFromType(otype); - PyErr_Format(PyExc_ValueError, - "could not find a matching type for %s.%s, " - "requested type has type code '%c'", - ufunc_name, opname, dtype ? dtype->type : '-'); - Py_XDECREF(dtype); - goto fail; + PyArray_Descr *descrs[3]; + PyArrayMethodObject *ufuncimpl = reducelike_promote_and_resolve(ufunc, + arr, out, signature, 1, descrs, "accumulate"); + if (ufuncimpl == NULL) { + return NULL; } - ndim = PyArray_NDIM(arr); + /* The below code assumes that all descriptors are identical: */ + assert(descrs[0] == descrs[1] && descrs[0] == descrs[2]); - /* - * Set up the output data type, using the input's exact - * data type if the type number didn't change to preserve - * metadata - */ - if (PyArray_DESCR(arr)->type_num == otype_final) { - if (PyArray_ISNBO(PyArray_DESCR(arr)->byteorder)) { - op_dtypes[0] = PyArray_DESCR(arr); - Py_INCREF(op_dtypes[0]); - } - else { - op_dtypes[0] = PyArray_DescrNewByteorder(PyArray_DESCR(arr), - NPY_NATIVE); - } - } - else { - op_dtypes[0] = PyArray_DescrFromType(otype_final); - } - if (op_dtypes[0] == NULL) { + if (PyDataType_REFCHK(descrs[2]) && descrs[2]->type_num != NPY_OBJECT) { + /* This can be removed, but the initial element copy needs fixing */ + PyErr_SetString(PyExc_TypeError, + "reduceat currently only supports `object` dtype with " + "references"); goto fail; } + PyArrayMethod_Context context = { + .caller = (PyObject *)ufunc, + .method = ufuncimpl, + .descriptors = descrs, + }; + + ndim = PyArray_NDIM(arr); + #if NPY_UF_DBG_TRACING printf("Found %s.%s inner loop with dtype : ", ufunc_name, opname); PyObject_Print((PyObject *)op_dtypes[0], stdout, 0); @@ -3532,11 +3464,16 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, op[2] = ind; if (out != NULL || ndim > 1 || !PyArray_ISALIGNED(arr) || - !PyArray_EquivTypes(op_dtypes[0], PyArray_DESCR(arr))) { + !PyArray_EquivTypes(descrs[0], PyArray_DESCR(arr))) { need_outer_iterator = 1; } if (need_outer_iterator) { + /* indices are already passed in correctly */ + assert(PyArray_DESCR(op[2])->type_num == NPY_INTP); + + PyArray_Descr *op_dtypes[3] = {descrs[0], descrs[1], NULL}; + npy_uint32 flags = NPY_ITER_ZEROSIZE_OK| NPY_ITER_REFS_OK| NPY_ITER_MULTI_INDEX| @@ -3565,8 +3502,7 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, NPY_UF_DBG_PRINT("Allocating outer iterator\n"); iter = NpyIter_AdvancedNew(3, op, flags, NPY_KEEPORDER, NPY_UNSAFE_CASTING, - op_flags, - op_dtypes, + op_flags, op_dtypes, ndim, op_axes, NULL, 0); if (iter == NULL) { goto fail; @@ -3592,9 +3528,9 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, } /* Allocate the output for when there's no outer iterator */ else if (out == NULL) { - Py_INCREF(op_dtypes[0]); + Py_INCREF(descrs[0]); op[0] = out = (PyArrayObject *)PyArray_NewFromDescr( - &PyArray_Type, op_dtypes[0], + &PyArray_Type, descrs[0], 1, &ind_size, NULL, NULL, 0, NULL); if (out == NULL) { @@ -3602,6 +3538,28 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, } } + npy_intp fixed_strides[3]; + if (need_outer_iterator) { + NpyIter_GetInnerFixedStrideArray(iter, fixed_strides); + } + else { + fixed_strides[1] = PyArray_STRIDES(op[1])[axis]; + } + /* The reduce axis does not advance here in the strided-loop */ + fixed_strides[0] = 0; + fixed_strides[2] = 0; + + NPY_ARRAYMETHOD_FLAGS flags = 0; + if (ufuncimpl->get_strided_loop(&context, + 1, 0, fixed_strides, &strided_loop, &auxdata, &flags) < 0) { + goto fail; + } + needs_api = (flags & NPY_METH_REQUIRES_PYAPI) != 0; + if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) { + /* Start with the floating-point exception flags cleared */ + npy_clear_floatstatus_barrier((char*)&iter); + } + /* * If the output has zero elements, return now. */ @@ -3619,8 +3577,8 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, npy_intp stride0, stride1; npy_intp stride0_ind = PyArray_STRIDE(op[0], axis); - int itemsize = op_dtypes[0]->elsize; - int needs_api = NpyIter_IterationNeedsAPI(iter); + int itemsize = descrs[0]->elsize; + needs_api |= NpyIter_IterationNeedsAPI(iter); /* Get the variables needed for the loop */ iternext = NpyIter_GetIterNext(iter, NULL); @@ -3643,7 +3601,6 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, NPY_BEGIN_THREADS_NDITER(iter); do { - for (i = 0; i < ind_size; ++i) { npy_intp start = reduceat_ind[i], end = (i == ind_size-1) ? count_m1+1 : @@ -3661,7 +3618,7 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, * to the same memory, e.g. * np.add.reduceat(a, np.arange(len(a)), out=a). */ - if (otype == NPY_OBJECT) { + if (descrs[2]->type_num == NPY_OBJECT) { /* * Incref before decref to avoid the possibility of * the reference count being zero temporarily. @@ -3681,33 +3638,24 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, dataptr_copy[1] += stride1; NPY_UF_DBG_PRINT1("iterator loop count %d\n", (int)count); - innerloop(dataptr_copy, &count, - stride_copy, innerloopdata); + res = strided_loop(&context, + dataptr_copy, &count, stride_copy, auxdata); } } - } while (!(needs_api && PyErr_Occurred()) && iternext(iter)); + } while (res == 0 && iternext(iter)); NPY_END_THREADS; } else if (iter == NULL) { char *dataptr_copy[3]; - npy_intp stride_copy[3]; - int itemsize = op_dtypes[0]->elsize; + int itemsize = descrs[0]->elsize; npy_intp stride0_ind = PyArray_STRIDE(op[0], axis); - - /* Execute the loop with no iterators */ - npy_intp stride0 = 0, stride1 = PyArray_STRIDE(op[1], axis); - - int needs_api = PyDataType_REFCHK(op_dtypes[0]); + npy_intp stride1 = PyArray_STRIDE(op[1], axis); NPY_UF_DBG_PRINT("UFunc: Reduce loop with no iterators\n"); - stride_copy[0] = stride0; - stride_copy[1] = stride1; - stride_copy[2] = stride0; - if (!needs_api) { NPY_BEGIN_THREADS; } @@ -3729,7 +3677,7 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, * the same memory, e.g. * np.add.reduceat(a, np.arange(len(a)), out=a). */ - if (otype == NPY_OBJECT) { + if (descrs[2]->type_num == NPY_OBJECT) { /* * Incref before decref to avoid the possibility of the * reference count being zero temporarily. @@ -3749,8 +3697,11 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, dataptr_copy[1] += stride1; NPY_UF_DBG_PRINT1("iterator loop count %d\n", (int)count); - innerloop(dataptr_copy, &count, - stride_copy, innerloopdata); + res = strided_loop(&context, + dataptr_copy, &count, fixed_strides, auxdata); + if (res != 0) { + break; + } } } @@ -3758,8 +3709,21 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, } finish: - Py_XDECREF(op_dtypes[0]); + NPY_AUXDATA_FREE(auxdata); + Py_DECREF(descrs[0]); + Py_DECREF(descrs[1]); + Py_DECREF(descrs[2]); + if (!NpyIter_Deallocate(iter)) { + res = -1; + } + + if (res == 0 && !(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) { + /* NOTE: We could check float errors even when `res < 0` */ + res = _check_ufunc_fperr(errormask, NULL, "reduceat"); + } + + if (res < 0) { Py_DECREF(out); return NULL; } @@ -3768,9 +3732,14 @@ finish: fail: Py_XDECREF(out); - Py_XDECREF(op_dtypes[0]); + + NPY_AUXDATA_FREE(auxdata); + Py_XDECREF(descrs[0]); + Py_XDECREF(descrs[1]); + Py_XDECREF(descrs[2]); NpyIter_Deallocate(iter); + return NULL; } @@ -3868,7 +3837,7 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, PyArrayObject *mp = NULL, *wheremask = NULL, *ret = NULL; PyObject *op = NULL; PyArrayObject *indices = NULL; - PyArray_Descr *otype = NULL; + PyArray_DTypeMeta *signature[3] = {NULL, NULL, NULL}; PyArrayObject *out = NULL; int keepdims = 0; PyObject *initial = NULL; @@ -4012,13 +3981,10 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, } if (otype_obj && otype_obj != Py_None) { /* Use `_get_dtype` because `dtype` is a DType and not the instance */ - PyArray_DTypeMeta *dtype = _get_dtype(otype_obj); - if (dtype == NULL) { + signature[0] = _get_dtype(otype_obj); + if (signature[0] == NULL) { goto fail; } - otype = dtype->singleton; - Py_INCREF(otype); - Py_DECREF(dtype); } if (out_obj && !PyArray_OutputConverter(out_obj, &out)) { goto fail; @@ -4038,15 +4004,6 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, ndim = PyArray_NDIM(mp); - /* Check to see that type (and otype) is not FLEXIBLE */ - if (PyArray_ISFLEXIBLE(mp) || - (otype && PyTypeNum_ISFLEXIBLE(otype->type_num))) { - PyErr_Format(PyExc_TypeError, - "cannot perform %s with flexible type", - _reduce_type[operation]); - goto fail; - } - /* Convert the 'axis' parameter into a list of axes */ if (axes_obj == NULL) { /* apply defaults */ @@ -4109,14 +4066,12 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, } /* - * If out is specified it determines otype - * unless otype already specified. + * If no dtype is specified and out is not specified, we override the + * integer and bool dtype used for add and multiply. + * + * TODO: The following should be handled by a promoter! */ - if (otype == NULL && out != NULL) { - otype = PyArray_DESCR(out); - Py_INCREF(otype); - } - if (otype == NULL) { + if (signature[0] == NULL && out == NULL) { /* * For integer types --- make sure at least a long * is used for add and multiply reduction to avoid overflow @@ -4136,16 +4091,18 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, typenum = NPY_LONG; } } + signature[0] = PyArray_DTypeFromTypeNum(typenum); } - otype = PyArray_DescrFromType(typenum); } + Py_XINCREF(signature[0]); + signature[2] = signature[0]; switch(operation) { case UFUNC_REDUCE: - ret = PyUFunc_Reduce(ufunc, mp, out, naxes, axes, - otype, keepdims, initial, wheremask); - Py_XDECREF(wheremask); + ret = PyUFunc_Reduce(ufunc, + mp, out, naxes, axes, signature, keepdims, initial, wheremask); + Py_XSETREF(wheremask, NULL); break; case UFUNC_ACCUMULATE: if (ndim == 0) { @@ -4157,8 +4114,8 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, "accumulate does not allow multiple axes"); goto fail; } - ret = (PyArrayObject *)PyUFunc_Accumulate(ufunc, mp, out, axes[0], - otype->type_num); + ret = (PyArrayObject *)PyUFunc_Accumulate(ufunc, + mp, out, axes[0], signature); break; case UFUNC_REDUCEAT: if (ndim == 0) { @@ -4171,19 +4128,22 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, goto fail; } ret = (PyArrayObject *)PyUFunc_Reduceat(ufunc, - mp, indices, out, axes[0], otype->type_num); + mp, indices, out, axes[0], signature); Py_SETREF(indices, NULL); break; } + if (ret == NULL) { + goto fail; + } + + Py_DECREF(signature[0]); + Py_DECREF(signature[1]); + Py_DECREF(signature[2]); + Py_DECREF(mp); - Py_DECREF(otype); Py_XDECREF(full_args.in); Py_XDECREF(full_args.out); - if (ret == NULL) { - return NULL; - } - /* Wrap and return the output */ { /* Find __array_wrap__ - note that these rules are different to the @@ -4211,7 +4171,10 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, } fail: - Py_XDECREF(otype); + Py_XDECREF(signature[0]); + Py_XDECREF(signature[1]); + Py_XDECREF(signature[2]); + Py_XDECREF(mp); Py_XDECREF(wheremask); Py_XDECREF(indices); diff --git a/numpy/core/src/umath/ufunc_type_resolution.c b/numpy/core/src/umath/ufunc_type_resolution.c index 7e24bc493..aa8d7b982 100644 --- a/numpy/core/src/umath/ufunc_type_resolution.c +++ b/numpy/core/src/umath/ufunc_type_resolution.c @@ -247,6 +247,28 @@ PyUFunc_ValidateCasting(PyUFuncObject *ufunc, } +/* + * Same as `PyUFunc_ValidateCasting` but only checks output casting. + */ +NPY_NO_EXPORT int +PyUFunc_ValidateOutCasting(PyUFuncObject *ufunc, + NPY_CASTING casting, PyArrayObject **operands, PyArray_Descr **dtypes) +{ + int i, nin = ufunc->nin, nop = nin + ufunc->nout; + + for (i = nin; i < nop; ++i) { + if (operands[i] == NULL) { + continue; + } + if (!PyArray_CanCastTypeTo(dtypes[i], + PyArray_DESCR(operands[i]), casting)) { + return raise_output_casting_error( + ufunc, casting, dtypes[i], PyArray_DESCR(operands[i]), i); + } + } + return 0; +} + /*UFUNC_API * * This function applies the default type resolution rules diff --git a/numpy/core/src/umath/ufunc_type_resolution.h b/numpy/core/src/umath/ufunc_type_resolution.h index dd88a081a..84a2593f4 100644 --- a/numpy/core/src/umath/ufunc_type_resolution.h +++ b/numpy/core/src/umath/ufunc_type_resolution.h @@ -99,6 +99,10 @@ PyUFunc_DivmodTypeResolver(PyUFuncObject *ufunc, PyObject *type_tup, PyArray_Descr **out_dtypes); +NPY_NO_EXPORT int +PyUFunc_ValidateOutCasting(PyUFuncObject *ufunc, + NPY_CASTING casting, PyArrayObject **operands, PyArray_Descr **dtypes); + /* * Does a linear search for the best inner loop of the ufunc. * diff --git a/numpy/core/src/umath/umathmodule.c b/numpy/core/src/umath/umathmodule.c index a9954dfc1..272555704 100644 --- a/numpy/core/src/umath/umathmodule.c +++ b/numpy/core/src/umath/umathmodule.c @@ -22,6 +22,7 @@ #include "numpy/npy_math.h" #include "number.h" +#include "dispatching.h" static PyUFuncGenericFunction pyfunc_functions[] = {PyUFunc_On_Om}; @@ -305,5 +306,33 @@ int initumath(PyObject *m) return -1; } + /* + * Set up promoters for logical functions + * TODO: This should probably be done at a better place, or even in the + * code generator directly. + */ + s = _PyDict_GetItemStringWithError(d, "logical_and"); + if (s == NULL) { + return -1; + } + if (install_logical_ufunc_promoter(s) < 0) { + return -1; + } + + s = _PyDict_GetItemStringWithError(d, "logical_or"); + if (s == NULL) { + return -1; + } + if (install_logical_ufunc_promoter(s) < 0) { + return -1; + } + + s = _PyDict_GetItemStringWithError(d, "logical_xor"); + if (s == NULL) { + return -1; + } + if (install_logical_ufunc_promoter(s) < 0) { + return -1; + } return 0; } diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index 30929ce91..3f85be1d4 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -2098,6 +2098,15 @@ class TestUfunc: with pytest.raises(TypeError): ufunc(a, a, signature=signature) + @pytest.mark.parametrize("ufunc", + [np.logical_and, np.logical_or, np.logical_xor]) + def test_logical_ufuncs_support_anything(self, ufunc): + # The logical ufuncs support even input that can't be promoted: + a = np.array('1') + c = np.array([1., 2.]) + assert_array_equal(ufunc(a, c), ufunc([True, True], True)) + assert ufunc.reduce(a) == True + def test_reduce_noncontig_output(self): # Check that reduction deals with non-contiguous output arrays # appropriately. @@ -2331,7 +2340,7 @@ def test_reduce_casterrors(offset): out = np.array(-1, dtype=np.intp) count = sys.getrefcount(value) - with pytest.raises(ValueError): + with pytest.raises(TypeError): # This is an unsafe cast, but we currently always allow that: np.add.reduce(arr, dtype=np.intp, out=out) assert count == sys.getrefcount(value) @@ -2340,3 +2349,18 @@ def test_reduce_casterrors(offset): # if the error happened immediately. # This does not define behaviour, the output is invalid and thus undefined assert out[()] < value * offset + + +@pytest.mark.parametrize("method", + [np.add.accumulate, np.add.reduce, + pytest.param(lambda x: np.add.reduceat(x, [0]), id="reduceat")]) +def test_reducelike_floaterrors(method): + # adding inf and -inf creates an invalid float and should give a warning + arr = np.array([np.inf, 0, -np.inf]) + with np.errstate(all="warn"): + with pytest.warns(RuntimeWarning, match="invalid value"): + method(arr) + + with np.errstate(all="raise"): + with pytest.raises(FloatingPointError): + method(arr) diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 2ff1667ba..036d6312c 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -1065,7 +1065,7 @@ class _MaskedBinaryOperation(_MaskedUFunc): tr = self.f.reduce(t, axis) mr = nomask else: - tr = self.f.reduce(t, axis, dtype=dtype or t.dtype) + tr = self.f.reduce(t, axis, dtype=dtype) mr = umath.logical_and.reduce(m, axis) if not tr.shape: -- cgit v1.2.1 From 9dfc548c36e0422feb8c489695db2b4b855aa7e6 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 9 Aug 2021 15:52:20 -0500 Subject: BUG: Fixup some refcounting and remove unused error path --- numpy/core/src/umath/ufunc_object.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 06533b127..6ea582304 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -2694,8 +2694,7 @@ static PyArrayMethodObject * reducelike_promote_and_resolve(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, PyArray_DTypeMeta *signature[3], - int enforce_uniform_args, PyArray_Descr *out_descrs[3], - char *method_type) + int enforce_uniform_args, PyArray_Descr *out_descrs[3]) { /* * Note that the `ops` is not realy correct. But legacy resolution @@ -2712,21 +2711,23 @@ reducelike_promote_and_resolve(PyUFuncObject *ufunc, */ PyArray_DTypeMeta *operation_DTypes[3] = { NULL, NPY_DTYPE(PyArray_DESCR(arr)), NULL}; + Py_INCREF(operation_DTypes[1]); if (out != NULL) { operation_DTypes[0] = NPY_DTYPE(PyArray_DESCR(out)); + Py_INCREF(operation_DTypes[0]); operation_DTypes[2] = operation_DTypes[0]; + Py_INCREF(operation_DTypes[2]); } PyArrayMethodObject *ufuncimpl = promote_and_get_ufuncimpl(ufunc, ops, signature, operation_DTypes, NPY_FALSE, NPY_TRUE); + Py_DECREF(operation_DTypes[1]); + if (out != NULL) { + Py_DECREF(operation_DTypes[0]); + Py_DECREF(operation_DTypes[2]); + } if (ufuncimpl == NULL) { - if (!PyErr_Occurred()) { - PyErr_Format(PyExc_TypeError, - "Could not find a matching loop for %s.reduce.", - method_type); - return NULL; - } return NULL; } @@ -2937,7 +2938,7 @@ PyUFunc_Reduce(PyUFuncObject *ufunc, PyArray_Descr *descrs[3]; PyArrayMethodObject *ufuncimpl = reducelike_promote_and_resolve(ufunc, - arr, out, signature, 0, descrs, "reduce"); + arr, out, signature, 0, descrs); if (ufuncimpl == NULL) { Py_DECREF(initial); return NULL; @@ -3001,7 +3002,7 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, PyArray_Descr *descrs[3]; PyArrayMethodObject *ufuncimpl = reducelike_promote_and_resolve(ufunc, - arr, out, signature, 1, descrs, "accumulate"); + arr, out, signature, 1, descrs); if (ufuncimpl == NULL) { return NULL; } @@ -3414,7 +3415,7 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, PyArray_Descr *descrs[3]; PyArrayMethodObject *ufuncimpl = reducelike_promote_and_resolve(ufunc, - arr, out, signature, 1, descrs, "accumulate"); + arr, out, signature, 1, descrs); if (ufuncimpl == NULL) { return NULL; } -- cgit v1.2.1 From c43d753be2817099e8cff27a6000d29ca23ba967 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 9 Aug 2021 16:16:54 -0500 Subject: BUG: Add forgotten fix for needs-api for reduceat --- numpy/core/src/umath/ufunc_object.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 6ea582304..8c6d9bd64 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -3599,7 +3599,9 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, stride_copy[1] = stride1; stride_copy[2] = stride0; - NPY_BEGIN_THREADS_NDITER(iter); + if (!needs_api) { + NPY_BEGIN_THREADS_THRESHOLDED(NpyIter_GetIterSize(iter)); + } do { for (i = 0; i < ind_size; ++i) { -- cgit v1.2.1 From 1f27ae7779cfb93cf88052775c422f1b1ea7cc34 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 9 Aug 2021 16:17:33 -0500 Subject: TST: Add test for rare reduce dtype mismatch error path --- numpy/core/tests/test_custom_dtypes.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'numpy') diff --git a/numpy/core/tests/test_custom_dtypes.py b/numpy/core/tests/test_custom_dtypes.py index 5eb82bc93..0114b9005 100644 --- a/numpy/core/tests/test_custom_dtypes.py +++ b/numpy/core/tests/test_custom_dtypes.py @@ -101,6 +101,22 @@ class TestSFloat: expected_view = a.view(np.float64) * b.view(np.float64) assert_array_equal(res.view(np.float64), expected_view) + def test_possible_and_impossible_reduce(self): + # For reductions to work, the first and last operand must have the + # same dtype. For this parametric DType that is not necessarily true. + a = self._get_array(2.) + # Addition reductin works (as of writing requires to pass initial + # because setting a scaled-float from the default `0` fails). + res = np.add.reduce(a, initial=0.) + assert res == a.astype(np.float64).sum() + + # But each multiplication changes the factor, so a reduction is not + # possible (the relaxed version of the old refusal to handle any + # flexible dtype). + with pytest.raises(TypeError, + match="The resolved dtypes are not compatible"): + np.multiply.reduce(a) + def test_basic_multiply_promotion(self): float_a = np.array([1., 2., 3.]) b = self._get_array(2.) -- cgit v1.2.1 From a8262a5a87835fcb8b64334334b3eca93a2ab8d5 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 9 Aug 2021 16:32:52 -0500 Subject: TST: Add additional reduceat/accumulate tests --- numpy/core/tests/test_ufunc.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'numpy') diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index 3f85be1d4..0a94a6dc8 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -1362,6 +1362,14 @@ class TestUfunc: np.array([[2]*i for i in [1, 3, 6, 10]], dtype=object), ) + def test_object_array_accumulate_failure(self): + # Typical accumulation on object works as expected: + res = np.add.accumulate(np.array([1, 0, 2], dtype=object)) + assert_array_equal(res, np.array([1, 1, 3], dtype=object)) + # But errors are propagated from the inner-loop if they occur: + with pytest.raises(TypeError): + np.add.accumulate([1, None, 2]) + def test_object_array_reduceat_inplace(self): # Checks that in-place reduceats work, see also gh-7465 arr = np.empty(4, dtype=object) @@ -1381,6 +1389,15 @@ class TestUfunc: np.add.reduceat(arr, np.arange(4), out=arr, axis=-1) assert_array_equal(arr, out) + def test_object_array_reduceat_failure(self): + # Reduceat works as expected when no invalid operation occurs (None is + # not involved in an operation here) + res = np.add.reduceat(np.array([1, None, 2], dtype=object), [1, 2]) + assert_array_equal(res, np.array([None, 2], dtype=object)) + # But errors when None would be involved in an operation: + with pytest.raises(TypeError): + np.add.reduceat([1, None, 2], [0, 2]) + def test_zerosize_reduction(self): # Test with default dtype and object dtype for a in [[], np.array([], dtype=object)]: -- cgit v1.2.1 From 3ce020dd1465b055555b957b30115912def68e3f Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 9 Aug 2021 16:59:21 -0500 Subject: TST: Add test for ufunc out-cast failure when input cast is ignored --- numpy/core/tests/test_ufunc.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'numpy') diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index 0a94a6dc8..fe0b07c6b 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -2124,6 +2124,16 @@ class TestUfunc: assert_array_equal(ufunc(a, c), ufunc([True, True], True)) assert ufunc.reduce(a) == True + @pytest.mark.parametrize("ufunc", + [np.logical_and, np.logical_or, np.logical_xor]) + def test_logical_ufuncs_out_cast_check(self, ufunc): + a = np.array('1') + c = np.array([1., 2.]) + out = a.copy() + with pytest.raises(TypeError): + # It would be safe, but not equiv casting: + ufunc(a, c, out=out, casting="equiv") + def test_reduce_noncontig_output(self): # Check that reduction deals with non-contiguous output arrays # appropriately. -- cgit v1.2.1 From b2c2d1920cca28e9b8f76d96ff9f2711ec7d5699 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 9 Aug 2021 18:53:18 -0500 Subject: MAINT: Remove unnecessary assert that may trip 32 bit Mainly to see if this is indeed the problem, but the assert is not necessary, and it might be we end up with the wrong dtype when the input is a compatible (but different) integer already? --- numpy/core/src/umath/ufunc_object.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 8c6d9bd64..1bdac0933 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -3470,9 +3470,6 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, } if (need_outer_iterator) { - /* indices are already passed in correctly */ - assert(PyArray_DESCR(op[2])->type_num == NPY_INTP); - PyArray_Descr *op_dtypes[3] = {descrs[0], descrs[1], NULL}; npy_uint32 flags = NPY_ITER_ZEROSIZE_OK| -- cgit v1.2.1 From b5a76ef437b672535f0ab10fbb8342655a33ebe6 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 10 Aug 2021 12:37:23 -0500 Subject: BUG: Fix unsupplied DType path in dispatching This did not actually trigger any problems, probably because the reduce fallback mode later just catches it anyway... On the upside, this probably fixes most of the caching issues ;) --- numpy/core/src/umath/dispatching.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/dispatching.c b/numpy/core/src/umath/dispatching.c index 6ad242589..5252e90a9 100644 --- a/numpy/core/src/umath/dispatching.c +++ b/numpy/core/src/umath/dispatching.c @@ -254,10 +254,6 @@ resolve_implementation_info(PyUFuncObject *ufunc, for (Py_ssize_t i = 0; i < nargs; i++) { int best; - /* Whether this (normally output) dtype was specified at all */ - npy_bool is_not_specified = ( - op_dtypes[i] == (PyArray_DTypeMeta *)Py_None); - PyObject *prev_dtype = PyTuple_GET_ITEM(best_dtypes, i); PyObject *new_dtype = PyTuple_GET_ITEM(curr_dtypes, i); @@ -271,7 +267,8 @@ resolve_implementation_info(PyUFuncObject *ufunc, * the subclass should be considered a better match * (subclasses are always more specific). */ - if (is_not_specified) { + /* Whether this (normally output) dtype was specified at all */ + if (op_dtypes[i] == NULL) { /* * When DType is completely unspecified, prefer abstract * over concrete, assuming it will resolve. -- cgit v1.2.1 From 2aeaa44c454d90e6efa140f539bcfbe5a9ca0a77 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 10 Aug 2021 14:10:40 -0500 Subject: TST: Add test for reduceat/accumulate output shape mismatch At least the reduceat path seems to have been untested before. The slight change in code layout (added assert) is just to make the code slightly easier to read. Since otherwise it looks like there is an additional `else` branch when `out` is given. --- numpy/core/src/umath/ufunc_object.c | 8 ++++++-- numpy/core/tests/test_ufunc.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 1bdac0933..0a919e6f4 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -3524,8 +3524,12 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, Py_INCREF(out); } } - /* Allocate the output for when there's no outer iterator */ - else if (out == NULL) { + else { + /* + * Allocate the output for when there's no outer iterator, we always + * use the outer_iteration path when `out` is passed. + */ + assert(out == NULL); Py_INCREF(descrs[0]); op[0] = out = (PyArrayObject *)PyArray_NewFromDescr( &PyArray_Type, descrs[0], diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index fe0b07c6b..78833a33c 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -2155,6 +2155,22 @@ class TestUfunc: assert_equal(y_base[1,:], y_base_copy[1,:]) assert_equal(y_base[3,:], y_base_copy[3,:]) + @pytest.mark.parametrize("with_cast", [True, False]) + def test_reduceat_and_accumulate_out_shape_mismatch(self, with_cast): + # Should raise an error mentioning "shape" or "size" + arr = np.arange(5) + out = np.arange(3) # definitely wrong shape + if with_cast: + # If a cast is necessary on the output, we can be sure to use + # the generic NpyIter (non-fast) path. + out = out.astype(np.float64) + + with pytest.raises(ValueError, match="(shape|size)"): + np.add.reduceat(arr, [0, 3], out=out) + + with pytest.raises(ValueError, match="(shape|size)"): + np.add.accumulate(arr, out=out) + @pytest.mark.parametrize('out_shape', [(), (1,), (3,), (1, 1), (1, 3), (4, 3)]) @pytest.mark.parametrize('keepdims', [True, False]) -- cgit v1.2.1 From 58421058c39ba753c872291899f4ef44aca39ae3 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 10 Aug 2021 16:23:34 -0500 Subject: BUG: Fixup and add tests for logical ufuncs --- numpy/core/src/umath/dispatching.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/dispatching.c b/numpy/core/src/umath/dispatching.c index 5252e90a9..3be3790dd 100644 --- a/numpy/core/src/umath/dispatching.c +++ b/numpy/core/src/umath/dispatching.c @@ -831,7 +831,12 @@ logical_ufunc_promoter(PyUFuncObject *NPY_UNUSED(ufunc), PyArray_DTypeMeta *op_dtypes[], PyArray_DTypeMeta *signature[], PyArray_DTypeMeta *new_op_dtypes[]) { - /* If we find any object at all, we currently force to object */ + /* + * If we find any object DType at all, we currently force to object. + * However, if the output is specified and not object, there is no point, + * it should be just as well to cast the input rather than doing the + * unsafe out cast. + */ int force_object = 0; for (int i = 0; i < 3; i++) { @@ -852,7 +857,9 @@ logical_ufunc_promoter(PyUFuncObject *NPY_UNUSED(ufunc), } new_op_dtypes[i] = item; } - if (!force_object) { + + if (!force_object || (op_dtypes[2] != NULL + && op_dtypes[2]->type_num != NPY_OBJECT)) { return 0; } /* Actually, we have to use the OBJECT loop after all, set all we can -- cgit v1.2.1 From 13cd98abdf77c7c181da9f883d54a274100a03c8 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 7 Sep 2021 16:20:53 -0500 Subject: MAINT: Fixups and new tests based on Marten's reviews More fixups are coming, the biggest change is that the error message is now improved when a reduction makes no sense such as `np.subtract.reduce(np.array([1, 2, 3], dtype="M8[s]"))` where input and output cannot have the same descriptor. (Some more fixups still to go) --- numpy/core/src/umath/reduction.h | 2 +- numpy/core/src/umath/ufunc_object.c | 60 ++++++++++++++++++++-------------- numpy/core/tests/test_custom_dtypes.py | 2 +- numpy/core/tests/test_datetime.py | 15 +++++++++ 4 files changed, 52 insertions(+), 27 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/reduction.h b/numpy/core/src/umath/reduction.h index bb00dad21..2170e27a7 100644 --- a/numpy/core/src/umath/reduction.h +++ b/numpy/core/src/umath/reduction.h @@ -20,7 +20,7 @@ typedef int (PyArray_AssignReduceIdentityFunc)(PyArrayObject *result, /* * Inner definition of the reduce loop, only used for a static function. - * At some point around NmPy 1.6, there was probably an intention to make + * At some point around NumPy 1.6, there was probably an intention to make * the reduce loop customizable at this level (per ufunc?). * * TODO: This should be refactored/removed. diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 0a919e6f4..01d90793c 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -2485,9 +2485,9 @@ PyUFunc_GeneralizedFunctionInternal(PyUFuncObject *ufunc, /* Final preparation of the arraymethod call */ PyArrayMethod_Context context = { - .caller = (PyObject *)ufunc, - .method = ufuncimpl, - .descriptors = operation_descrs, + .caller = (PyObject *)ufunc, + .method = ufuncimpl, + .descriptors = operation_descrs, }; PyArrayMethod_StridedLoop *strided_loop; NPY_ARRAYMETHOD_FLAGS flags = 0; @@ -2607,9 +2607,9 @@ PyUFunc_GenericFunctionInternal(PyUFuncObject *ufunc, /* Final preparation of the arraymethod call */ PyArrayMethod_Context context = { - .caller = (PyObject *)ufunc, - .method = ufuncimpl, - .descriptors = operation_descrs, + .caller = (PyObject *)ufunc, + .method = ufuncimpl, + .descriptors = operation_descrs, }; /* Do the ufunc loop */ @@ -2679,13 +2679,17 @@ PyUFunc_GenericFunction(PyUFuncObject *NPY_UNUSED(ufunc), * Promote and resolve a reduction like operation. * * @param ufunc - * @param arr The operation array (out was never used) + * @param arr The operation array + * @param out The output array or NULL if not provided. Note that NumPy always + * used out to mean the same as `dtype=out.dtype` and never passed + * the array itself to the type-resolution. * @param signature The DType signature, which may already be set due to the * dtype passed in by the user, or the special cases (add, multiply). * (Contains strong references and may be modified.) - * @param enforce_uniform_args If `1` fully uniform dtypes/descriptors are - * enforced as required for accumulate and (currently) reduceat. + * @param enforce_uniform_args If `NPY_TRUE` fully uniform dtypes/descriptors + * are enforced as required for accumulate and (currently) reduceat. * @param out_descrs New references to the resolved descriptors (on success). + * @param method The ufunc method, "reduce", "reduceat", or "accumulate". * @returns ufuncimpl The `ArrayMethod` implemention to use. Or NULL if an * error occurred. @@ -2694,7 +2698,8 @@ static PyArrayMethodObject * reducelike_promote_and_resolve(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, PyArray_DTypeMeta *signature[3], - int enforce_uniform_args, PyArray_Descr *out_descrs[3]) + npy_bool enforce_uniform_args, PyArray_Descr *out_descrs[3], + char *method) { /* * Note that the `ops` is not realy correct. But legacy resolution @@ -2731,22 +2736,28 @@ reducelike_promote_and_resolve(PyUFuncObject *ufunc, return NULL; } - /* Find the correct descriptors for the operation */ + /* + * Find the correct descriptors for the operation. We use unsafe casting + * for historic reasons: The logic ufuncs required it to cast everything to + * boolean. However, we now special case the logical ufuncs, so that the + * casting safety could in principle be set to the default same-kind. + * (although this should possibly happen through a deprecation) + */ if (resolve_descriptors(3, ufunc, ufuncimpl, ops, out_descrs, signature, NPY_UNSAFE_CASTING) < 0) { return NULL; } /* - * The first operand and output be the same array, so they should + * The first operand and output should be the same array, so they should * be identical. The second argument can be different for reductions, * but is checked to be identical for accumulate and reduceat. */ if (out_descrs[0] != out_descrs[2] || ( enforce_uniform_args && out_descrs[0] != out_descrs[1])) { PyErr_Format(PyExc_TypeError, - "The resolved dtypes are not compatible with an accumulate " - "or reduceat loop."); + "the resolved dtypes are not compatible with %s.%s", + ufunc_get_name_cstr(ufunc), method); goto fail; } /* TODO: This really should _not_ be unsafe casting (same above)! */ @@ -2912,7 +2923,7 @@ PyUFunc_Reduce(PyUFuncObject *ufunc, } /* Get the identity */ - /* TODO: Both of these must be provided by the ArrayMethod! */ + /* TODO: Both of these should be provided by the ArrayMethod! */ identity = _get_identity(ufunc, &reorderable); if (identity == NULL) { return NULL; @@ -2938,7 +2949,7 @@ PyUFunc_Reduce(PyUFuncObject *ufunc, PyArray_Descr *descrs[3]; PyArrayMethodObject *ufuncimpl = reducelike_promote_and_resolve(ufunc, - arr, out, signature, 0, descrs); + arr, out, signature, NPY_FALSE, descrs, "reduce"); if (ufuncimpl == NULL) { Py_DECREF(initial); return NULL; @@ -3002,7 +3013,7 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, PyArray_Descr *descrs[3]; PyArrayMethodObject *ufuncimpl = reducelike_promote_and_resolve(ufunc, - arr, out, signature, 1, descrs); + arr, out, signature, NPY_TRUE, descrs, "accumulate"); if (ufuncimpl == NULL) { return NULL; } @@ -3019,9 +3030,9 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, } PyArrayMethod_Context context = { - .caller = (PyObject *)ufunc, - .method = ufuncimpl, - .descriptors = descrs, + .caller = (PyObject *)ufunc, + .method = ufuncimpl, + .descriptors = descrs, }; ndim = PyArray_NDIM(arr); @@ -3415,7 +3426,7 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, PyArray_Descr *descrs[3]; PyArrayMethodObject *ufuncimpl = reducelike_promote_and_resolve(ufunc, - arr, out, signature, 1, descrs); + arr, out, signature, NPY_TRUE, descrs, "reduceat"); if (ufuncimpl == NULL) { return NULL; } @@ -3432,9 +3443,9 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, } PyArrayMethod_Context context = { - .caller = (PyObject *)ufunc, - .method = ufuncimpl, - .descriptors = descrs, + .caller = (PyObject *)ufunc, + .method = ufuncimpl, + .descriptors = descrs, }; ndim = PyArray_NDIM(arr); @@ -4101,7 +4112,6 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc, Py_XINCREF(signature[0]); signature[2] = signature[0]; - switch(operation) { case UFUNC_REDUCE: ret = PyUFunc_Reduce(ufunc, diff --git a/numpy/core/tests/test_custom_dtypes.py b/numpy/core/tests/test_custom_dtypes.py index 0114b9005..b9311e283 100644 --- a/numpy/core/tests/test_custom_dtypes.py +++ b/numpy/core/tests/test_custom_dtypes.py @@ -114,7 +114,7 @@ class TestSFloat: # possible (the relaxed version of the old refusal to handle any # flexible dtype). with pytest.raises(TypeError, - match="The resolved dtypes are not compatible"): + match="the resolved dtypes are not compatible"): np.multiply.reduce(a) def test_basic_multiply_promotion(self): diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py index 69eba7ba0..b95d669a8 100644 --- a/numpy/core/tests/test_datetime.py +++ b/numpy/core/tests/test_datetime.py @@ -2029,6 +2029,21 @@ class TestDateTime: assert_equal(np.maximum.reduce(a), np.timedelta64(7, 's')) + def test_datetime_no_subtract_reducelike(self): + # subtracting two datetime64 works, but we cannot reduce it, since + # the result of that subtraction will have a different dtype. + arr = np.array(["2021-12-02", "2019-05-12"], dtype="M8[ms]") + msg = r"the resolved dtypes are not compatible with subtract\." + + with pytest.raises(TypeError, match=msg + "reduce"): + np.subtract.reduce(arr) + + with pytest.raises(TypeError, match=msg + "accumulate"): + np.subtract.accumulate(arr) + + with pytest.raises(TypeError, match=msg + "reduceat"): + np.subtract.reduceat(arr, [0]) + def test_datetime_busday_offset(self): # First Monday in June assert_equal( -- cgit v1.2.1 From 1242ba33750c08798adc8d69138576a4ae3b735a Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 7 Sep 2021 17:04:22 -0500 Subject: DOC: Comment fixups based on Marten's review --- numpy/core/src/multiarray/array_method.c | 7 +++++++ numpy/core/src/umath/dispatching.c | 7 ++++--- numpy/core/src/umath/ufunc_object.c | 4 ---- 3 files changed, 11 insertions(+), 7 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_method.c b/numpy/core/src/multiarray/array_method.c index c4db73c3b..e5ff1491f 100644 --- a/numpy/core/src/multiarray/array_method.c +++ b/numpy/core/src/multiarray/array_method.c @@ -787,6 +787,13 @@ _masked_stridedloop_data_free(NpyAuxData *auxdata) * This function wraps a regular unmasked strided-loop as a * masked strided-loop, only calling the function for elements * where the mask is True. + * + * TODO: Reductions also use this code to implement masked reductions. + * Before consolidating them, reductions had a special case for + * broadcasts: when the mask stride was 0 the code does not check all + * elements as `npy_memchr` currently does. + * It may be worthwhile to add such an optimization again if broadcasted + * masks are common enough. */ static int generic_masked_strided_loop(PyArrayMethod_Context *context, diff --git a/numpy/core/src/umath/dispatching.c b/numpy/core/src/umath/dispatching.c index 3be3790dd..9c76b40e0 100644 --- a/numpy/core/src/umath/dispatching.c +++ b/numpy/core/src/umath/dispatching.c @@ -862,10 +862,11 @@ logical_ufunc_promoter(PyUFuncObject *NPY_UNUSED(ufunc), && op_dtypes[2]->type_num != NPY_OBJECT)) { return 0; } - /* Actually, we have to use the OBJECT loop after all, set all we can - * to object (that might not work out, but just try). + /* + * Actually, we have to use the OBJECT loop after all, set all we can + * to object (that might not work out, but try). * - * TODO: Change this to check for `op_dtypes[0] == NULL` to STOP + * NOTE: Change this to check for `op_dtypes[0] == NULL` to STOP * returning `object` for `np.logical_and.reduce(obj_arr)` * which will also affect `np.all` and `np.any`! */ diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 01d90793c..228e67576 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -2849,10 +2849,6 @@ reduce_loop(PyArrayMethod_Context *context, strides_copy[1] = strides[1]; strides_copy[2] = strides[0]; if (masked) { - /* - * TODO: This code had a broadcasted-mask optimization that may - * make sense in the array-method masked strided loop code. - */ dataptrs_copy[3] = dataptrs[2]; strides_copy[3] = strides[2]; } -- cgit v1.2.1 From 27f3b03dcc4fa2570825f9233f2e593c3c320673 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 7 Sep 2021 17:40:13 -0500 Subject: TST: Add a test covering logical ufuncs for custom DTypes In particular, this covers a casting error that currently cannot be hit for normal ufuncs, because they already check casting during the legacy dtype resolution (which is called first). --- numpy/core/src/umath/_scaled_float_dtype.c | 52 ++++++++++++++++++++++++++++++ numpy/core/tests/test_custom_dtypes.py | 20 ++++++++++++ 2 files changed, 72 insertions(+) (limited to 'numpy') diff --git a/numpy/core/src/umath/_scaled_float_dtype.c b/numpy/core/src/umath/_scaled_float_dtype.c index eeef33a3d..b6c19362a 100644 --- a/numpy/core/src/umath/_scaled_float_dtype.c +++ b/numpy/core/src/umath/_scaled_float_dtype.c @@ -398,6 +398,42 @@ float_to_from_sfloat_resolve_descriptors( } +/* + * Cast to boolean (for testing the logical functions a bit better). + */ +static int +cast_sfloat_to_bool(PyArrayMethod_Context *NPY_UNUSED(context), + char *const data[], npy_intp const dimensions[], + npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata)) +{ + npy_intp N = dimensions[0]; + char *in = data[0]; + char *out = data[1]; + for (npy_intp i = 0; i < N; i++) { + *(npy_bool *)out = *(double *)in != 0; + in += strides[0]; + out += strides[1]; + } + return 0; +} + +static NPY_CASTING +sfloat_to_bool_resolve_descriptors( + PyArrayMethodObject *NPY_UNUSED(self), + PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]), + PyArray_Descr *given_descrs[2], + PyArray_Descr *loop_descrs[2]) +{ + Py_INCREF(given_descrs[0]); + loop_descrs[0] = given_descrs[0]; + if (loop_descrs[0] == NULL) { + return -1; + } + loop_descrs[1] = PyArray_DescrFromType(NPY_BOOL); /* cannot fail */ + return NPY_UNSAFE_CASTING; +} + + static int init_casts(void) { @@ -453,6 +489,22 @@ init_casts(void) return -1; } + slots[0].slot = NPY_METH_resolve_descriptors; + slots[0].pfunc = &sfloat_to_bool_resolve_descriptors; + slots[1].slot = NPY_METH_strided_loop; + slots[1].pfunc = &cast_sfloat_to_bool; + slots[2].slot = 0; + slots[2].pfunc = NULL; + + spec.name = "sfloat_to_bool_cast"; + dtypes[0] = &PyArray_SFloatDType; + dtypes[1] = PyArray_DTypeFromTypeNum(NPY_BOOL); + Py_DECREF(dtypes[1]); /* immortal anyway */ + + if (PyArray_AddCastingImplementation_FromSpec(&spec, 0)) { + return -1; + } + return 0; } diff --git a/numpy/core/tests/test_custom_dtypes.py b/numpy/core/tests/test_custom_dtypes.py index b9311e283..e502a87ed 100644 --- a/numpy/core/tests/test_custom_dtypes.py +++ b/numpy/core/tests/test_custom_dtypes.py @@ -161,3 +161,23 @@ class TestSFloat: # Check that casting the output fails also (done by the ufunc here) with pytest.raises(TypeError): np.add(a, a, out=c, casting="safe") + + @pytest.mark.parametrize("ufunc", + [np.logical_and, np.logical_or, np.logical_xor]) + def test_logical_ufuncs_casts_to_bool(self, ufunc): + a = self._get_array(2.) + a[0] = 0. # make sure first element is considered False. + + float_equiv = a.astype(float) + expected = ufunc(float_equiv, float_equiv) + res = ufunc(a, a) + assert_array_equal(res, expected) + + # also check that the same works for reductions: + expected = ufunc.reduce(float_equiv) + res = ufunc.reduce(a) + assert_array_equal(res, expected) + + # The output casting does not match the bool, bool -> bool loop: + with pytest.raises(TypeError): + ufunc(a, a, out=np.empty(a.shape, dtype=int), casting="equiv") -- cgit v1.2.1 From 7dcf62379f41407d8f9583d1c2016e3d8ec48384 Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Thu, 14 Oct 2021 14:58:52 +0900 Subject: MAINT: Fix issue with C compiler args containing spaces Instead of doing a dumb string split, use shlex to make sure args containing spaces are handled properly. --- numpy/distutils/unixccompiler.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/unixccompiler.py b/numpy/distutils/unixccompiler.py index 733a9fc50..4884960fd 100644 --- a/numpy/distutils/unixccompiler.py +++ b/numpy/distutils/unixccompiler.py @@ -5,6 +5,7 @@ unixccompiler - can handle very long argument lists for ar. import os import sys import subprocess +import shlex from distutils.errors import CompileError, DistutilsExecError, LibError from distutils.unixccompiler import UnixCCompiler @@ -30,15 +31,15 @@ def UnixCCompiler__compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts if 'OPT' in os.environ: # XXX who uses this? from sysconfig import get_config_vars - opt = " ".join(os.environ['OPT'].split()) - gcv_opt = " ".join(get_config_vars('OPT')[0].split()) - ccomp_s = " ".join(self.compiler_so) + opt = shlex.join(shlex.split(os.environ['OPT'])) + gcv_opt = shlex.join(shlex.split(get_config_vars('OPT')[0])) + ccomp_s = shlex.join(self.compiler_so) if opt not in ccomp_s: ccomp_s = ccomp_s.replace(gcv_opt, opt) - self.compiler_so = ccomp_s.split() - llink_s = " ".join(self.linker_so) + self.compiler_so = shlex.split(ccomp_s) + llink_s = shlex.join(self.linker_so) if opt not in llink_s: - self.linker_so = llink_s.split() + opt.split() + self.linker_so = self.linker_so + shlex.split(opt) display = '%s: %s' % (os.path.basename(self.compiler_so[0]), src) -- cgit v1.2.1 From f931a434839222bb00282a432d6d6a0c2c52eb7d Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 18 Oct 2021 18:10:47 +0200 Subject: ENH: Replace `NestedSequence` with a proper nested sequence protocol --- numpy/array_api/_typing.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'numpy') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index 519e8463c..5e980b16f 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -6,6 +6,8 @@ annotations in the function signatures. The functions in the module are only valid for inputs that match the given type annotations. """ +from __future__ import annotations + __all__ = [ "Array", "Device", @@ -16,7 +18,16 @@ __all__ = [ ] import sys -from typing import Any, Literal, Sequence, Type, Union, TYPE_CHECKING, TypeVar +from typing import ( + Any, + Literal, + Sequence, + Type, + Union, + TYPE_CHECKING, + TypeVar, + Protocol, +) from ._array_object import Array from numpy import ( @@ -33,10 +44,11 @@ from numpy import ( float64, ) -# This should really be recursive, but that isn't supported yet. See the -# similar comment in numpy/typing/_array_like.py -_T = TypeVar("_T") -NestedSequence = Sequence[Sequence[_T]] +_T_co = TypeVar("_T_co", covariant=True) + +class NestedSequence(Protocol[_T_co]): + def __getitem__(self, key: int, /) -> _T_co | NestedSequence[_T_co]: ... + def __len__(self, /) -> int: ... Device = Literal["cpu"] if TYPE_CHECKING or sys.version_info >= (3, 9): -- cgit v1.2.1 From 3952e8f1390629078fdb229236b3b1ce40140c32 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 18 Oct 2021 18:11:06 +0200 Subject: ENH: Change `SupportsDLPack` into a protocol --- numpy/array_api/_typing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/array_api/_typing.py b/numpy/array_api/_typing.py index 5e980b16f..dfa87b358 100644 --- a/numpy/array_api/_typing.py +++ b/numpy/array_api/_typing.py @@ -67,6 +67,8 @@ if TYPE_CHECKING or sys.version_info >= (3, 9): else: Dtype = dtype -SupportsDLPack = Any SupportsBufferProtocol = Any PyCapsule = Any + +class SupportsDLPack(Protocol): + def __dlpack__(self, /, *, stream: None = ...) -> PyCapsule: ... -- cgit v1.2.1 From d74bea12d19dd92c9cf07cac35e94d45fb331832 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 18 Oct 2021 18:13:39 +0200 Subject: MAINT: Replace the `__array_namespace__` return type with `Any` Replace `object` as it cannot be used for expressing the objects in the array namespace. --- numpy/array_api/_array_object.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/array_api/_array_object.py b/numpy/array_api/_array_object.py index 830319e8c..ef66c5efd 100644 --- a/numpy/array_api/_array_object.py +++ b/numpy/array_api/_array_object.py @@ -29,7 +29,7 @@ from ._dtypes import ( _dtype_categories, ) -from typing import TYPE_CHECKING, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Tuple, Union, Any if TYPE_CHECKING: from ._typing import PyCapsule, Device, Dtype @@ -382,7 +382,7 @@ class Array: def __array_namespace__( self: Array, /, *, api_version: Optional[str] = None - ) -> object: + ) -> Any: if api_version is not None and not api_version.startswith("2021."): raise ValueError(f"Unrecognized array API version: {api_version!r}") return array_api -- cgit v1.2.1 From 52b5935ea1ab9a5f1043e7a4af2ced8311affe01 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Tue, 19 Oct 2021 14:24:51 +0800 Subject: BUG: fix win32 np.clip slowness The use of the macro _NPY_CLIP results in multiple re-evaluations of the input arguments. Thus for floating point types, the check of NaNs is performed multiple times. This manifests itself as a slowness on Win32 builds. See #18673. --- numpy/core/src/umath/clip.c.src | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/clip.c.src b/numpy/core/src/umath/clip.c.src index bc966b7ac..48786d4a2 100644 --- a/numpy/core/src/umath/clip.c.src +++ b/numpy/core/src/umath/clip.c.src @@ -76,9 +76,6 @@ * npy_datetime, npy_timedelta# */ -#define _NPY_CLIP(x, min, max) \ - _NPY_@name@_MIN(_NPY_@name@_MAX((x), (min)), (max)) - NPY_NO_EXPORT void @name@_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { @@ -95,25 +92,33 @@ NPY_NO_EXPORT void /* contiguous, branch to let the compiler optimize */ if (is1 == sizeof(@type@) && os1 == sizeof(@type@)) { for(npy_intp i = 0; i < n; i++, ip1 += is1, op1 += os1) { - *(@type@ *)op1 = _NPY_CLIP(*(@type@ *)ip1, min_val, max_val); + @type@ t = *(@type@ *)ip1; + t = _NPY_@name@_MAX(t, min_val); + t = _NPY_@name@_MIN(t, max_val); + *(@type@ *)op1 = t; } } else { for(npy_intp i = 0; i < n; i++, ip1 += is1, op1 += os1) { - *(@type@ *)op1 = _NPY_CLIP(*(@type@ *)ip1, min_val, max_val); + @type@ t = *(@type@ *)ip1; + t = _NPY_@name@_MAX(t, min_val); + t = _NPY_@name@_MIN(t, max_val); + *(@type@ *)op1 = t; } } } else { TERNARY_LOOP { - *(@type@ *)op1 = _NPY_CLIP(*(@type@ *)ip1, *(@type@ *)ip2, *(@type@ *)ip3); + @type@ t = *(@type@ *)ip1; + t = _NPY_@name@_MAX(t, *(@type@ *)ip2); + t = _NPY_@name@_MIN(t, *(@type@ *)ip3); + *(@type@ *)op1 = t; } } npy_clear_floatstatus_barrier((char*)dimensions); } // clean up the macros we defined above -#undef _NPY_CLIP #undef _NPY_@name@_MAX #undef _NPY_@name@_MIN -- cgit v1.2.1 From 8e3f1ac0d4b44750f9c83ff74ed7f29dc6182196 Mon Sep 17 00:00:00 2001 From: warren Date: Tue, 19 Oct 2021 11:44:05 -0400 Subject: BUG: core: Fix incorrect check of NpyIter_Deallocate return value. NpyIter_Deallocate returns 0 for failure and 1 for success, so the check `NpyIter_Deallocate(iter) < 0` is not correct. (See relevant comments in gh-20069.) --- numpy/core/src/umath/_umath_tests.c.src | 10 ++++++++++ numpy/core/src/umath/ufunc_object.c | 2 +- numpy/core/tests/test_umath.py | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/_umath_tests.c.src b/numpy/core/src/umath/_umath_tests.c.src index 33d8539d5..ce42fc271 100644 --- a/numpy/core/src/umath/_umath_tests.c.src +++ b/numpy/core/src/umath/_umath_tests.c.src @@ -400,6 +400,16 @@ addUfuncs(PyObject *dictionary) { } PyDict_SetItemString(dictionary, "always_error", f); Py_DECREF(f); + f = PyUFunc_FromFuncAndDataAndSignature(always_error_functions, + always_error_data, always_error_signatures, 1, 2, 1, PyUFunc_None, + "always_error_gufunc", + "simply, broken, gufunc that sets an error (but releases the GIL).", + 0, "(i),()->()"); + if (f == NULL) { + return -1; + } + PyDict_SetItemString(dictionary, "always_error_gufunc", f); + Py_DECREF(f); f = PyUFunc_FromFuncAndDataAndSignature(inner1d_functions, inner1d_data, inner1d_signatures, 2, 2, 1, PyUFunc_None, "inner1d", "inner on the last dimension and broadcast on the rest \n" diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 228e67576..7824173e2 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -2542,7 +2542,7 @@ PyUFunc_GeneralizedFunctionInternal(PyUFuncObject *ufunc, PyArray_free(inner_strides); NPY_AUXDATA_FREE(auxdata); - if (NpyIter_Deallocate(iter) < 0) { + if (!NpyIter_Deallocate(iter)) { retval = -1; } diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 8ff81ea51..4366e2257 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -3886,3 +3886,11 @@ def test_bad_legacy_ufunc_silent_errors(): with pytest.raises(RuntimeError, match=r"How unexpected :\)!"): ncu_tests.always_error.at(arr, [0, 1, 2], arr) + + +@pytest.mark.parametrize('x1', [np.arange(3.0), [0.0, 1.0, 2.0]]) +def test_bad_legacy_gufunc_silent_errors(x1): + # Verify that an exception raised in a gufunc loop propagates correctly. + # The signature of always_error_gufunc is '(i),()->()'. + with pytest.raises(RuntimeError, match=r"How unexpected :\)!"): + ncu_tests.always_error_gufunc(x1, 0.0) -- cgit v1.2.1 From ac214d2477a1988c3f07062d8915347d3f6b8c6a Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 19 Oct 2021 16:46:20 -0500 Subject: MAINT,BUG: Fix `ufunc.at` to use new ufunc API This also fixes a small issue that I forgot to include the special case for an unspecified output (or input): In this case matching is OK, so long the loop we pick can cast the operand. Previously, `ufunc.at` failed to check for floating point errors, this further adds the missing checks to match normal ufuncs. --- numpy/core/src/multiarray/dtypemeta.h | 6 +- numpy/core/src/umath/dispatching.c | 4 ++ numpy/core/src/umath/ufunc_object.c | 119 ++++++++++++++++++++++++--------- numpy/core/tests/test_custom_dtypes.py | 18 +++++ numpy/core/tests/test_ufunc.py | 8 ++- 5 files changed, 116 insertions(+), 39 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/dtypemeta.h b/numpy/core/src/multiarray/dtypemeta.h index 05e9e2394..2a61fe39d 100644 --- a/numpy/core/src/multiarray/dtypemeta.h +++ b/numpy/core/src/multiarray/dtypemeta.h @@ -74,9 +74,9 @@ typedef struct { #define NPY_DTYPE(descr) ((PyArray_DTypeMeta *)Py_TYPE(descr)) #define NPY_DT_SLOTS(dtype) ((NPY_DType_Slots *)(dtype)->dt_slots) -#define NPY_DT_is_legacy(dtype) ((dtype)->flags & NPY_DT_LEGACY) -#define NPY_DT_is_abstract(dtype) ((dtype)->flags & NPY_DT_ABSTRACT) -#define NPY_DT_is_parametric(dtype) ((dtype)->flags & NPY_DT_PARAMETRIC) +#define NPY_DT_is_legacy(dtype) (((dtype)->flags & NPY_DT_LEGACY) != 0) +#define NPY_DT_is_abstract(dtype) (((dtype)->flags & NPY_DT_ABSTRACT) != 0) +#define NPY_DT_is_parametric(dtype) (((dtype)->flags & NPY_DT_PARAMETRIC) != 0) /* * Macros for convenient classmethod calls, since these require diff --git a/numpy/core/src/umath/dispatching.c b/numpy/core/src/umath/dispatching.c index 9c76b40e0..8e99c0420 100644 --- a/numpy/core/src/umath/dispatching.c +++ b/numpy/core/src/umath/dispatching.c @@ -193,6 +193,10 @@ resolve_implementation_info(PyUFuncObject *ufunc, /* Unspecified out always matches (see below for inputs) */ continue; } + if (resolver_dtype == (PyArray_DTypeMeta *)Py_None) { + /* always matches */ + continue; + } if (given_dtype == resolver_dtype) { continue; } diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 228e67576..647520f90 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -5865,15 +5865,13 @@ ufunc_at(PyUFuncObject *ufunc, PyObject *args) PyArrayObject *op2_array = NULL; PyArrayMapIterObject *iter = NULL; PyArrayIterObject *iter2 = NULL; - PyArray_Descr *dtypes[3] = {NULL, NULL, NULL}; PyArrayObject *operands[3] = {NULL, NULL, NULL}; PyArrayObject *array_operands[3] = {NULL, NULL, NULL}; - int needs_api = 0; + PyArray_DTypeMeta *signature[3] = {NULL, NULL, NULL}; + PyArray_DTypeMeta *operand_DTypes[3] = {NULL, NULL, NULL}; + PyArray_Descr *operation_descrs[3] = {NULL, NULL, NULL}; - PyUFuncGenericFunction innerloop; - void *innerloopdata; - npy_intp i; int nop; /* override vars */ @@ -5886,6 +5884,10 @@ ufunc_at(PyUFuncObject *ufunc, PyObject *args) int buffersize; int errormask = 0; char * err_msg = NULL; + + PyArrayMethod_StridedLoop *strided_loop; + NpyAuxData *auxdata = NULL; + NPY_BEGIN_THREADS_DEF; if (ufunc->nin > 2) { @@ -5973,26 +5975,51 @@ ufunc_at(PyUFuncObject *ufunc, PyObject *args) /* * Create dtypes array for either one or two input operands. - * The output operand is set to the first input operand + * Compare to the logic in `convert_ufunc_arguments`. + * TODO: It may be good to review some of this behaviour, since the + * operand array is special (it is written to) similar to reductions. + * Using unsafe-casting as done here, is likely not desirable. */ operands[0] = op1_array; + operand_DTypes[0] = NPY_DTYPE(PyArray_DESCR(op1_array)); + Py_INCREF(operand_DTypes[0]); + int force_legacy_promotion = 0; + int allow_legacy_promotion = NPY_DT_is_legacy(operand_DTypes[0]); + if (op2_array != NULL) { operands[1] = op2_array; - operands[2] = op1_array; + operand_DTypes[1] = NPY_DTYPE(PyArray_DESCR(op2_array)); + Py_INCREF(operand_DTypes[1]); + allow_legacy_promotion &= NPY_DT_is_legacy(operand_DTypes[1]); + operands[2] = operands[0]; + operand_DTypes[2] = operand_DTypes[0]; + Py_INCREF(operand_DTypes[2]); + nop = 3; + if (allow_legacy_promotion && ((PyArray_NDIM(op1_array) == 0) + != (PyArray_NDIM(op2_array) == 0))) { + /* both are legacy and only one is 0-D: force legacy */ + force_legacy_promotion = should_use_min_scalar(2, operands, 0, NULL); + } } else { - operands[1] = op1_array; + operands[1] = operands[0]; + operand_DTypes[1] = operand_DTypes[0]; + Py_INCREF(operand_DTypes[1]); operands[2] = NULL; nop = 2; } - if (ufunc->type_resolver(ufunc, NPY_UNSAFE_CASTING, - operands, NULL, dtypes) < 0) { + PyArrayMethodObject *ufuncimpl = promote_and_get_ufuncimpl(ufunc, + operands, signature, operand_DTypes, + force_legacy_promotion, allow_legacy_promotion); + if (ufuncimpl == NULL) { goto fail; } - if (ufunc->legacy_inner_loop_selector(ufunc, dtypes, - &innerloop, &innerloopdata, &needs_api) < 0) { + + /* Find the correct descriptors for the operation */ + if (resolve_descriptors(nop, ufunc, ufuncimpl, + operands, operation_descrs, signature, NPY_UNSAFE_CASTING) < 0) { goto fail; } @@ -6053,21 +6080,44 @@ ufunc_at(PyUFuncObject *ufunc, PyObject *args) NPY_ITER_GROWINNER| NPY_ITER_DELAY_BUFALLOC, NPY_KEEPORDER, NPY_UNSAFE_CASTING, - op_flags, dtypes, + op_flags, operation_descrs, -1, NULL, NULL, buffersize); if (iter_buffer == NULL) { goto fail; } - needs_api = needs_api | NpyIter_IterationNeedsAPI(iter_buffer); - iternext = NpyIter_GetIterNext(iter_buffer, NULL); if (iternext == NULL) { NpyIter_Deallocate(iter_buffer); goto fail; } + PyArrayMethod_Context context = { + .caller = (PyObject *)ufunc, + .method = ufuncimpl, + .descriptors = operation_descrs, + }; + + NPY_ARRAYMETHOD_FLAGS flags; + /* Use contiguous strides; if there is such a loop it may be faster */ + npy_intp strides[3] = { + operation_descrs[0]->elsize, operation_descrs[1]->elsize, 0}; + if (nop == 3) { + strides[2] = operation_descrs[2]->elsize; + } + + if (ufuncimpl->get_strided_loop(&context, 1, 0, strides, + &strided_loop, &auxdata, &flags) < 0) { + goto fail; + } + int needs_api = (flags & NPY_METH_REQUIRES_PYAPI) != 0; + needs_api |= NpyIter_IterationNeedsAPI(iter_buffer); + if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) { + /* Start with the floating-point exception flags cleared */ + npy_clear_floatstatus_barrier((char*)&iter); + } + if (!needs_api) { NPY_BEGIN_THREADS; } @@ -6076,14 +6126,13 @@ ufunc_at(PyUFuncObject *ufunc, PyObject *args) * Iterate over first and second operands and call ufunc * for each pair of inputs */ - i = iter->size; - while (i > 0) + int res = 0; + for (npy_intp i = iter->size; i > 0; i--) { char *dataptr[3]; char **buffer_dataptr; /* one element at a time, no stride required but read by innerloop */ - npy_intp count[3] = {1, 0xDEADBEEF, 0xDEADBEEF}; - npy_intp stride[3] = {0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF}; + npy_intp count = 1; /* * Set up data pointers for either one or two input operands. @@ -6102,14 +6151,14 @@ ufunc_at(PyUFuncObject *ufunc, PyObject *args) /* Reset NpyIter data pointers which will trigger a buffer copy */ NpyIter_ResetBasePointers(iter_buffer, dataptr, &err_msg); if (err_msg) { + res = -1; break; } buffer_dataptr = NpyIter_GetDataPtrArray(iter_buffer); - innerloop(buffer_dataptr, count, stride, innerloopdata); - - if (needs_api && PyErr_Occurred()) { + res = strided_loop(&context, buffer_dataptr, &count, strides, auxdata); + if (res != 0) { break; } @@ -6123,32 +6172,35 @@ ufunc_at(PyUFuncObject *ufunc, PyObject *args) if (iter2 != NULL) { PyArray_ITER_NEXT(iter2); } - - i--; } NPY_END_THREADS; - if (err_msg) { + if (res != 0 && err_msg) { PyErr_SetString(PyExc_ValueError, err_msg); } + if (res == 0 && !(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) { + /* NOTE: We could check float errors even when `res < 0` */ + res = _check_ufunc_fperr(errormask, NULL, "at"); + } + NPY_AUXDATA_FREE(auxdata); NpyIter_Deallocate(iter_buffer); Py_XDECREF(op2_array); Py_XDECREF(iter); Py_XDECREF(iter2); - for (i = 0; i < 3; i++) { - Py_XDECREF(dtypes[i]); + for (int i = 0; i < 3; i++) { + Py_XDECREF(operation_descrs[i]); Py_XDECREF(array_operands[i]); } /* - * An error should only be possible if needs_api is true, but this is not - * strictly correct for old-style ufuncs (e.g. `power` released the GIL - * but manually set an Exception). + * An error should only be possible if needs_api is true or `res != 0`, + * but this is not strictly correct for old-style ufuncs + * (e.g. `power` released the GIL but manually set an Exception). */ - if (PyErr_Occurred()) { + if (res != 0 || PyErr_Occurred()) { return NULL; } else { @@ -6163,10 +6215,11 @@ fail: Py_XDECREF(op2_array); Py_XDECREF(iter); Py_XDECREF(iter2); - for (i = 0; i < 3; i++) { - Py_XDECREF(dtypes[i]); + for (int i = 0; i < 3; i++) { + Py_XDECREF(operation_descrs[i]); Py_XDECREF(array_operands[i]); } + NPY_AUXDATA_FREE(auxdata); return NULL; } diff --git a/numpy/core/tests/test_custom_dtypes.py b/numpy/core/tests/test_custom_dtypes.py index e502a87ed..6bcc45d6b 100644 --- a/numpy/core/tests/test_custom_dtypes.py +++ b/numpy/core/tests/test_custom_dtypes.py @@ -117,18 +117,36 @@ class TestSFloat: match="the resolved dtypes are not compatible"): np.multiply.reduce(a) + def test_basic_ufunc_at(self): + float_a = np.array([1., 2., 3.]) + b = self._get_array(2.) + + float_b = b.view(np.float64).copy() + np.multiply.at(float_b, [1, 1, 1], float_a) + np.multiply.at(b, [1, 1, 1], float_a) + + assert_array_equal(b.view(np.float64), float_b) + def test_basic_multiply_promotion(self): float_a = np.array([1., 2., 3.]) b = self._get_array(2.) res1 = float_a * b res2 = b * float_a + # one factor is one, so we get the factor of b: assert res1.dtype == res2.dtype == b.dtype expected_view = float_a * b.view(np.float64) assert_array_equal(res1.view(np.float64), expected_view) assert_array_equal(res2.view(np.float64), expected_view) + # Check that promotion works when `out` is used: + np.multiply(b, float_a, out=res2) + with pytest.raises(TypeError): + # The promoter accepts this (maybe it should not), but the SFloat + # result cannot be cast to integer: + np.multiply(b, float_a, out=np.arange(3)) + def test_basic_addition(self): a = self._get_array(2.) b = self._get_array(4.) diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index 78833a33c..294e51bdd 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -2396,14 +2396,16 @@ def test_reduce_casterrors(offset): @pytest.mark.parametrize("method", [np.add.accumulate, np.add.reduce, - pytest.param(lambda x: np.add.reduceat(x, [0]), id="reduceat")]) -def test_reducelike_floaterrors(method): - # adding inf and -inf creates an invalid float and should give a warning + pytest.param(lambda x: np.add.reduceat(x, [0]), id="reduceat"), + pytest.param(lambda x: np.log.at(x, [2]), id="at")]) +def test_ufunc_methods_floaterrors(method): + # adding inf and -inf (or log(-inf) creates an invalid float and warns arr = np.array([np.inf, 0, -np.inf]) with np.errstate(all="warn"): with pytest.warns(RuntimeWarning, match="invalid value"): method(arr) + arr = np.array([np.inf, 0, -np.inf]) with np.errstate(all="raise"): with pytest.raises(FloatingPointError): method(arr) -- cgit v1.2.1 From d9f8333fc8fb0b1de88df7864c9f29a47eece3e6 Mon Sep 17 00:00:00 2001 From: Warren Weckesser Date: Tue, 19 Oct 2021 23:27:42 -0400 Subject: MAINT: core: Update the comment about _parse_signature with more complete info. --- numpy/core/src/umath/ufunc_object.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 7824173e2..15385b624 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -616,9 +616,24 @@ _is_same_name(const char* s1, const char* s2) } /* - * Sets core_num_dim_ix, core_num_dims, core_dim_ixs, core_offsets, - * and core_signature in PyUFuncObject "ufunc". Returns 0 unless an - * error occurred. + * Sets the following fields in the PyUFuncObject 'ufunc': + * + * Field Type Array Length + * core_enabled int (effectively bool) N/A + * core_num_dim_ix int N/A + * core_dim_flags npy_uint32 * core_num_dim_ix + * core_dim_sizes npy_intp * core_num_dim_ix + * core_num_dims int * nargs (i.e. nin+nout) + * core_offsets int * nargs + * core_dim_ixs int * sum(core_num_dims) + * core_signature char * strlen(signature) + 1 + * + * The function assumes that the values that are arrays have not + * been set already, and sets these pointers to memory allocated + * with PyArray_malloc. These are freed when the ufunc dealloc + * method is called. + * + * Returns 0 unless an error occurred. */ static int _parse_signature(PyUFuncObject *ufunc, const char *signature) -- cgit v1.2.1 From 0a7a0a28874d0d1900f4b4b0fc2058afff30de76 Mon Sep 17 00:00:00 2001 From: Arushi Sharma <40266350+arushi-08@users.noreply.github.com> Date: Thu, 21 Oct 2021 00:02:15 +0530 Subject: DOC: updated docstring for binary file object --- numpy/lib/npyio.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 6c34e95fe..3ae9b0e63 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -285,7 +285,8 @@ def load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, ---------- file : file-like object, string, or pathlib.Path The file to read. File-like objects must support the - ``seek()`` and ``read()`` methods. Pickled files require that the + ``seek()`` and ``read()`` methods. The file object must always + be opened in binary mode. Pickled files require that the file-like object support the ``readline()`` method as well. mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional If not None, then memory-map the file, using the given mode (see -- cgit v1.2.1 From be11c06de7cf128c364c33dafbdecd2372f322e6 Mon Sep 17 00:00:00 2001 From: warren Date: Wed, 20 Oct 2021 18:00:38 -0400 Subject: BUG: lib: Fix error raised by insert. When `insert` is given a single out-of-bounds index in a list, e.g. np.insert([0, 1, 2], [99], [3, 4]) # 99 is out of bounds a TypeError was being raised because of a bug in the formatting of the message. Before this change, the error is TypeError: %i format: a number is required, not list After, we get the expected IndexError: index [99] is out of bounds for axis 0 with size 3 --- numpy/lib/function_base.py | 5 ++--- numpy/lib/tests/test_function_base.py | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 84128e4f0..232a51cab 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4753,9 +4753,8 @@ def insert(arr, obj, values, axis=None): if indices.size == 1: index = indices.item() if index < -N or index > N: - raise IndexError( - "index %i is out of bounds for axis %i with " - "size %i" % (obj, axis, N)) + raise IndexError(f"index {obj} is out of bounds for axis {axis} " + f"with size {N}") if (index < 0): index += N diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 66110b479..c7dfe5673 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -553,6 +553,11 @@ class TestInsert: with pytest.raises(IndexError): np.insert([0, 1, 2], np.array([], dtype=float), []) + @pytest.mark.parametrize('idx', [4, -4]) + def test_index_out_of_bounds(self, idx): + with pytest.raises(IndexError, match='out of bounds'): + np.insert([0, 1, 2], [idx], [3, 4]) + class TestAmax: -- cgit v1.2.1 From db6e91eca062098af0a150ccb4542eb1be972c99 Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Thu, 21 Oct 2021 15:05:17 +0200 Subject: BUG, SIMD: Fix 64-bit integer divison by a scalar on MSVC <= 2017 The multiplier factor that used for integer division wasn't correctly calculated due inaccurate implementation of the emulated version of intrinsic `_udiv128`. --- numpy/core/src/common/simd/intdiv.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/common/simd/intdiv.h b/numpy/core/src/common/simd/intdiv.h index 5d2ab2906..c787c7a7d 100644 --- a/numpy/core/src/common/simd/intdiv.h +++ b/numpy/core/src/common/simd/intdiv.h @@ -162,11 +162,12 @@ NPY_FINLINE npy_uint64 npyv__divh128_u64(npy_uint64 high, npy_uint64 divisor) npy_uint32 divisor_hi = divisor >> 32; npy_uint32 divisor_lo = divisor & 0xFFFFFFFF; // compute high quotient digit - npy_uint32 quotient_hi = (npy_uint32)(high / divisor_hi); + npy_uint64 quotient_hi = high / divisor_hi; npy_uint64 remainder = high - divisor_hi * quotient_hi; npy_uint64 base32 = 1ULL << 32; while (quotient_hi >= base32 || quotient_hi*divisor_lo > base32*remainder) { - remainder += --divisor_hi; + --quotient_hi; + remainder += divisor_hi; if (remainder >= base32) { break; } -- cgit v1.2.1 From 7d0a0db15ae9b02da5001b40b82e377812acf50e Mon Sep 17 00:00:00 2001 From: melissawm Date: Thu, 21 Oct 2021 16:44:01 +0000 Subject: DOC: Fixed docstring for parameters 2 and -2 on linalg.cond --- numpy/linalg/linalg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 95780d19d..d5bcb1cb4 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -1691,8 +1691,8 @@ def cond(x, p=None): -inf min(sum(abs(x), axis=1)) 1 max(sum(abs(x), axis=0)) -1 min(sum(abs(x), axis=0)) - 2 2-norm (largest sing. value) - -2 smallest singular value + 2 (largest sing. value)/(smallest sing. value) + -2 (smallest sing. value)/(largest sing. value) ===== ============================ inf means the numpy.inf object, and the Frobenius norm is -- cgit v1.2.1 From c72fbd00ffc5439cc7def96456c391ef64237886 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Thu, 21 Oct 2021 17:24:04 +0100 Subject: Patch to allow for 2 as a minor version (!) The float representation of e.g. 14.2 is not exact, leading to a minor version number of int(1.999999) == 1 in the original code. --- numpy/distutils/mingw32ccompiler.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/mingw32ccompiler.py b/numpy/distutils/mingw32ccompiler.py index 541055899..c3a0cb418 100644 --- a/numpy/distutils/mingw32ccompiler.py +++ b/numpy/distutils/mingw32ccompiler.py @@ -647,10 +647,9 @@ def generate_manifest(config): if msver is not None: if msver >= 8: check_embedded_msvcr_match_linked(msver) - ma = int(msver) - mi = int((msver - ma) * 10) + ma_str, mi_str = str(msver).split('.') # Write the manifest file - manxml = msvc_manifest_xml(ma, mi) + manxml = msvc_manifest_xml(int(ma_str), int(mi_str)) with open(manifest_name(config), "w") as man: config.temp_files.append(manifest_name(config)) man.write(manxml) -- cgit v1.2.1 From fb6a4a69526c69d1f995cfa50105a3b6b2b38a9d Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Thu, 21 Oct 2021 16:31:18 -0500 Subject: BUG: Relax homogeneous signature fallback in type resolution Relaxing the fallback means that reductions, which pass `(dtype, None, dtype)` as signature (`type_tup` in the old resolver) can resolve to the homogeneous loop if `dtype=dtype` is passed. This change is important, because the resolution changed from using `(None, None, dtype)` to `(dtype, None, dtype)` since reductions normally are expected to have the first input match the output. Specifying the signature more precisly without also relaxing the "homogeneous" fallback, however, lead to a regression noticed by Pandas. Closes gh-20151 --- numpy/core/src/umath/ufunc_type_resolution.c | 11 +++++++---- numpy/core/tests/test_ufunc.py | 5 +++-- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/ufunc_type_resolution.c b/numpy/core/src/umath/ufunc_type_resolution.c index aa8d7b982..9ed923cf5 100644 --- a/numpy/core/src/umath/ufunc_type_resolution.c +++ b/numpy/core/src/umath/ufunc_type_resolution.c @@ -2164,6 +2164,10 @@ type_tuple_type_resolver(PyUFuncObject *self, * `signature=(None,)*nin + (dtype,)*nout`. If the signature matches that * exactly (could be relaxed but that is not necessary for backcompat), * we also try `signature=(dtype,)*(nin+nout)`. + * Since reduction pass in `(dtype, None, dtype)` we broaden this to + * replacing all unspecified dtypes with the homogeneous output one. + * Note that this can (and often will) lead to unsafe casting. This is + * normally rejected (but not currently for reductions!). * This used to be the main meaning for `dtype=dtype`, but some calls broke * the expectation, and changing it allows for `dtype=dtype` to be useful * for ufuncs like `np.ldexp` in the future while also normalizing it to @@ -2182,13 +2186,12 @@ type_tuple_type_resolver(PyUFuncObject *self, if (homogeneous_type != NPY_NOTYPE) { for (int i = 0; i < nin; i++) { if (specified_types[i] != NPY_NOTYPE) { - homogeneous_type = NPY_NOTYPE; - break; + /* Never replace a specified type! */ + continue; } specified_types[i] = homogeneous_type; } - } - if (homogeneous_type != NPY_NOTYPE) { + /* Try again with the homogeneous specified types. */ res = type_tuple_type_resolver_core(self, op, input_casting, casting, specified_types, any_object, diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index 78833a33c..4b06c8668 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -2383,8 +2383,9 @@ def test_reduce_casterrors(offset): out = np.array(-1, dtype=np.intp) count = sys.getrefcount(value) - with pytest.raises(TypeError): - # This is an unsafe cast, but we currently always allow that: + with pytest.raises(ValueError, match="invalid literal"): + # This is an unsafe cast, but we currently always allow that. + # Note that the double loop is picked, but the cast fails. np.add.reduce(arr, dtype=np.intp, out=out) assert count == sys.getrefcount(value) # If an error occurred during casting, the operation is done at most until -- cgit v1.2.1 From 2ae7aeb3aa909b1a16bc58fd0e40dc4476dff35d Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Thu, 19 Aug 2021 09:28:09 +0200 Subject: [demo] how-to replacing numpy custom generation engine by raw C++ This is just a technical prototype to measure and discuss the impact and implication of moving to C++ for kernel code generation. --- numpy/core/setup.py | 15 +- numpy/core/src/common/npy_sort.h.src | 7 +- numpy/core/src/common/numpy_tag.h | 34 ++++ numpy/core/src/npymath/npy_math_private.h | 12 ++ numpy/core/src/npysort/radixsort.c.src | 231 --------------------------- numpy/core/src/npysort/radixsort.cpp | 254 ++++++++++++++++++++++++++++++ numpy/core/src/umath/clip.c.src | 125 --------------- numpy/core/src/umath/clip.cpp | 131 +++++++++++++++ numpy/core/src/umath/clip.h | 33 ++++ numpy/core/src/umath/clip.h.src | 18 --- numpy/distutils/ccompiler.py | 7 +- numpy/distutils/ccompiler_opt.py | 16 +- numpy/distutils/command/build_clib.py | 49 +++--- numpy/distutils/command/build_ext.py | 57 ++++--- numpy/distutils/extension.py | 4 + numpy/distutils/misc_util.py | 15 +- 16 files changed, 577 insertions(+), 431 deletions(-) create mode 100644 numpy/core/src/common/numpy_tag.h delete mode 100644 numpy/core/src/npysort/radixsort.c.src create mode 100644 numpy/core/src/npysort/radixsort.cpp delete mode 100644 numpy/core/src/umath/clip.c.src create mode 100644 numpy/core/src/umath/clip.cpp create mode 100644 numpy/core/src/umath/clip.h delete mode 100644 numpy/core/src/umath/clip.h.src (limited to 'numpy') diff --git a/numpy/core/setup.py b/numpy/core/setup.py index f2524ae13..6800f65e9 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -909,7 +909,7 @@ def configuration(parent_package='',top_path=None): join('src', 'npysort', 'mergesort.c.src'), join('src', 'npysort', 'timsort.c.src'), join('src', 'npysort', 'heapsort.c.src'), - join('src', 'npysort', 'radixsort.c.src'), + join('src', 'npysort', 'radixsort.cpp'), join('src', 'common', 'npy_partition.h.src'), join('src', 'npysort', 'selection.c.src'), join('src', 'common', 'npy_binsearch.h.src'), @@ -948,8 +948,8 @@ def configuration(parent_package='',top_path=None): join('src', 'umath', 'loops_exponent_log.dispatch.c.src'), join('src', 'umath', 'matmul.h.src'), join('src', 'umath', 'matmul.c.src'), - join('src', 'umath', 'clip.h.src'), - join('src', 'umath', 'clip.c.src'), + join('src', 'umath', 'clip.h'), + join('src', 'umath', 'clip.cpp'), join('src', 'umath', 'dispatching.c'), join('src', 'umath', 'legacy_array_method.c'), join('src', 'umath', 'ufunc_object.c'), @@ -979,6 +979,9 @@ def configuration(parent_package='',top_path=None): svml_objs = glob.glob(svml_path + '/**/*.s', recursive=True) config.add_extension('_multiarray_umath', + # Forcing C language even though we have C++ sources. + # It forces the C linker and don't link C++ runtime. + language = 'c', sources=multiarray_src + umath_src + common_src + [generate_config_h, @@ -993,7 +996,11 @@ def configuration(parent_package='',top_path=None): common_deps, libraries=['npymath'], extra_objects=svml_objs, - extra_info=extra_info) + extra_info=extra_info, + extra_cxx_compile_args=['-std=c++11', + '-D__STDC_VERSION__=0', + '-fno-exceptions', + '-fno-rtti']) ####################################################################### # umath_tests module # diff --git a/numpy/core/src/common/npy_sort.h.src b/numpy/core/src/common/npy_sort.h.src index ddbde0c9b..b4a1e9b0c 100644 --- a/numpy/core/src/common/npy_sort.h.src +++ b/numpy/core/src/common/npy_sort.h.src @@ -49,9 +49,14 @@ NPY_NO_EXPORT int atimsort_@suff@(void *vec, npy_intp *ind, npy_intp cnt, void * * #suff = bool, byte, ubyte, short, ushort, int, uint, long, ulong, * longlong, ulonglong# */ - +#ifdef __cplusplus +extern "C" { +#endif NPY_NO_EXPORT int radixsort_@suff@(void *vec, npy_intp cnt, void *null); NPY_NO_EXPORT int aradixsort_@suff@(void *vec, npy_intp *ind, npy_intp cnt, void *null); +#ifdef __cplusplus +} +#endif /**end repeat**/ diff --git a/numpy/core/src/common/numpy_tag.h b/numpy/core/src/common/numpy_tag.h new file mode 100644 index 000000000..a2269d3c7 --- /dev/null +++ b/numpy/core/src/common/numpy_tag.h @@ -0,0 +1,34 @@ +#ifndef _NPY_COMMON_TAG_H_ +#define _NPY_COMMON_TAG_H_ + +namespace npy { + + struct integral_tag {}; + struct floating_point_tag {}; + struct complex_tag {}; + struct date_tag {}; + + struct bool_tag : integral_tag { using type = npy_bool; }; + struct byte_tag : integral_tag {using type = npy_byte; } ; + struct ubyte_tag : integral_tag {using type = npy_ubyte; } ; + struct short_tag : integral_tag {using type = npy_short; } ; + struct ushort_tag : integral_tag {using type = npy_ushort; } ; + struct int_tag : integral_tag {using type = npy_int; } ; + struct uint_tag : integral_tag {using type = npy_uint; } ; + struct long_tag : integral_tag {using type = npy_long ; } ; + struct ulong_tag : integral_tag {using type = npy_ulong ; } ; + struct longlong_tag : integral_tag {using type = npy_longlong ; } ; + struct ulonglong_tag : integral_tag {using type = npy_ulonglong ; } ; + struct half_tag {using type = npy_half ; } ; + struct float_tag : floating_point_tag {using type = npy_float ; } ; + struct double_tag : floating_point_tag {using type = npy_double ; } ; + struct longdouble_tag : floating_point_tag {using type = npy_longdouble ; } ; + struct cfloat_tag : complex_tag {using type = npy_cfloat ; } ; + struct cdouble_tag : complex_tag {using type = npy_cdouble ; } ; + struct clongdouble_tag : complex_tag {using type = npy_clongdouble ; } ; + struct datetime_tag : date_tag {using type = npy_datetime ; } ; + struct timedelta_tag : date_tag {using type = npy_timedelta ; } ; + +} + +#endif diff --git a/numpy/core/src/npymath/npy_math_private.h b/numpy/core/src/npymath/npy_math_private.h index 212d11a0b..d475919b0 100644 --- a/numpy/core/src/npymath/npy_math_private.h +++ b/numpy/core/src/npymath/npy_math_private.h @@ -507,17 +507,29 @@ typedef union { #else /* !_MSC_VER */ typedef union { npy_cdouble npy_z; +#ifdef __cplusplus + std::complex c99z; +#else complex double c99_z; +#endif } __npy_cdouble_to_c99_cast; typedef union { npy_cfloat npy_z; +#ifdef __cplusplus + std::complex c99z; +#else complex float c99_z; +#endif } __npy_cfloat_to_c99_cast; typedef union { npy_clongdouble npy_z; +#ifdef __cplusplus + std::complex c99_z; +#else complex long double c99_z; +#endif } __npy_clongdouble_to_c99_cast; #endif /* !_MSC_VER */ diff --git a/numpy/core/src/npysort/radixsort.c.src b/numpy/core/src/npysort/radixsort.c.src deleted file mode 100644 index 99d8ed42a..000000000 --- a/numpy/core/src/npysort/radixsort.c.src +++ /dev/null @@ -1,231 +0,0 @@ -#define NPY_NO_DEPRECATED_API NPY_API_VERSION - -#include "npy_sort.h" -#include "npysort_common.h" -#include - -/* - ***************************************************************************** - ** INTEGER SORTS ** - ***************************************************************************** - */ - - -/**begin repeat - * - * #TYPE = BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, - * LONGLONG, ULONGLONG# - * #suff = bool, byte, ubyte, short, ushort, int, uint, long, ulong, - * longlong, ulonglong# - * #type = npy_ubyte, npy_ubyte, npy_ubyte, npy_ushort, npy_ushort, npy_uint, - * npy_uint, npy_ulong, npy_ulong, npy_ulonglong, npy_ulonglong# - * #sign = 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0# - * #floating = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0# - */ - -// Reference: https://github.com/eloj/radix-sorting#-key-derivation -#if @sign@ - // Floating-point is currently disabled. - // Floating-point tests succeed for double and float on macOS but not on Windows/Linux. - // Basic sorting tests succeed but others relying on sort fail. - // Possibly related to floating-point normalisation or multiple NaN reprs? Not sure. - #if @floating@ - // For floats, we invert the key if the sign bit is set, else we invert the sign bit. - #define KEY_OF(x) ((x) ^ (-((x) >> (sizeof(@type@) * 8 - 1)) | ((@type@)1 << (sizeof(@type@) * 8 - 1)))) - #else - // For signed ints, we flip the sign bit so the negatives are below the positives. - #define KEY_OF(x) ((x) ^ ((@type@)1 << (sizeof(@type@) * 8 - 1))) - #endif -#else - // For unsigned ints, the key is as-is - #define KEY_OF(x) (x) -#endif - -static inline npy_ubyte -nth_byte_@suff@(@type@ key, npy_intp l) { - return (key >> (l << 3)) & 0xFF; -} - -static @type@* -radixsort0_@suff@(@type@ *arr, @type@ *aux, npy_intp num) -{ - npy_intp cnt[sizeof(@type@)][1 << 8] = { { 0 } }; - npy_intp i; - size_t l; - @type@ key0 = KEY_OF(arr[0]); - size_t ncols = 0; - npy_ubyte cols[sizeof(@type@)]; - - for (i = 0; i < num; i++) { - @type@ k = KEY_OF(arr[i]); - - for (l = 0; l < sizeof(@type@); l++) { - cnt[l][nth_byte_@suff@(k, l)]++; - } - } - - for (l = 0; l < sizeof(@type@); l++) { - if (cnt[l][nth_byte_@suff@(key0, l)] != num) { - cols[ncols++] = l; - } - } - - for (l = 0; l < ncols; l++) { - npy_intp a = 0; - for (i = 0; i < 256; i++) { - npy_intp b = cnt[cols[l]][i]; - cnt[cols[l]][i] = a; - a += b; - } - } - - for (l = 0; l < ncols; l++) { - @type@* temp; - for (i = 0; i < num; i++) { - @type@ k = KEY_OF(arr[i]); - npy_intp dst = cnt[cols[l]][nth_byte_@suff@(k, cols[l])]++; - aux[dst] = arr[i]; - } - - temp = aux; - aux = arr; - arr = temp; - } - - return arr; -} - -NPY_NO_EXPORT int -radixsort_@suff@(void *start, npy_intp num, void *NPY_UNUSED(varr)) -{ - void *sorted; - @type@ *aux; - @type@ *arr = start; - @type@ k1, k2; - npy_bool all_sorted = 1; - - if (num < 2) { - return 0; - } - - k1 = KEY_OF(arr[0]); - for (npy_intp i = 1; i < num; i++) { - k2 = KEY_OF(arr[i]); - if (k1 > k2) { - all_sorted = 0; - break; - } - k1 = k2; - } - - if (all_sorted) { - return 0; - } - - aux = malloc(num * sizeof(@type@)); - if (aux == NULL) { - return -NPY_ENOMEM; - } - - sorted = radixsort0_@suff@(start, aux, num); - if (sorted != start) { - memcpy(start, sorted, num * sizeof(@type@)); - } - - free(aux); - return 0; -} - -static npy_intp* -aradixsort0_@suff@(@type@ *arr, npy_intp *aux, npy_intp *tosort, npy_intp num) -{ - npy_intp cnt[sizeof(@type@)][1 << 8] = { { 0 } }; - npy_intp i; - size_t l; - @type@ key0 = KEY_OF(arr[0]); - size_t ncols = 0; - npy_ubyte cols[sizeof(@type@)]; - - for (i = 0; i < num; i++) { - @type@ k = KEY_OF(arr[i]); - - for (l = 0; l < sizeof(@type@); l++) { - cnt[l][nth_byte_@suff@(k, l)]++; - } - } - - for (l = 0; l < sizeof(@type@); l++) { - if (cnt[l][nth_byte_@suff@(key0, l)] != num) { - cols[ncols++] = l; - } - } - - for (l = 0; l < ncols; l++) { - npy_intp a = 0; - for (i = 0; i < 256; i++) { - npy_intp b = cnt[cols[l]][i]; - cnt[cols[l]][i] = a; - a += b; - } - } - - for (l = 0; l < ncols; l++) { - npy_intp* temp; - for (i = 0; i < num; i++) { - @type@ k = KEY_OF(arr[tosort[i]]); - npy_intp dst = cnt[cols[l]][nth_byte_@suff@(k, cols[l])]++; - aux[dst] = tosort[i]; - } - - temp = aux; - aux = tosort; - tosort = temp; - } - - return tosort; -} - -NPY_NO_EXPORT int -aradixsort_@suff@(void *start, npy_intp* tosort, npy_intp num, void *NPY_UNUSED(varr)) -{ - npy_intp *sorted; - npy_intp *aux; - @type@ *arr = start; - @type@ k1, k2; - npy_bool all_sorted = 1; - - if (num < 2) { - return 0; - } - - k1 = KEY_OF(arr[tosort[0]]); - for (npy_intp i = 1; i < num; i++) { - k2 = KEY_OF(arr[tosort[i]]); - if (k1 > k2) { - all_sorted = 0; - break; - } - k1 = k2; - } - - if (all_sorted) { - return 0; - } - - aux = malloc(num * sizeof(npy_intp)); - if (aux == NULL) { - return -NPY_ENOMEM; - } - - sorted = aradixsort0_@suff@(start, aux, tosort, num); - if (sorted != tosort) { - memcpy(tosort, sorted, num * sizeof(npy_intp)); - } - - free(aux); - return 0; -} - -#undef KEY_OF - -/**end repeat**/ diff --git a/numpy/core/src/npysort/radixsort.cpp b/numpy/core/src/npysort/radixsort.cpp new file mode 100644 index 000000000..502613689 --- /dev/null +++ b/numpy/core/src/npysort/radixsort.cpp @@ -0,0 +1,254 @@ +#define NPY_NO_DEPRECATED_API NPY_API_VERSION + +#include "npy_sort.h" +#include "npysort_common.h" +#include "../common/numpy_tag.h" +#include + +#include + +/* + ***************************************************************************** + ** INTEGER SORTS ** + ***************************************************************************** + */ + + +// Reference: https://github.com/eloj/radix-sorting#-key-derivation +template +T KEY_OF(T x) { + // Floating-point is currently disabled. + // Floating-point tests succeed for double and float on macOS but not on Windows/Linux. + // Basic sorting tests succeed but others relying on sort fail. + // Possibly related to floating-point normalisation or multiple NaN reprs? Not sure. + if(std::is_floating_point::value) { + // For floats, we invert the key if the sign bit is set, else we invert the sign bit. + return ((x) ^ (-((x) >> (sizeof(T) * 8 - 1)) | ((T)1 << (sizeof(T) * 8 - 1)))); + } + else if(std::is_signed::value) { + // For signed ints, we flip the sign bit so the negatives are below the positives. + return ((x) ^ ((T)1 << (sizeof(T) * 8 - 1))); + } + else { + return x; + } +} + + +template +static inline npy_ubyte +nth_byte(T key, npy_intp l) { + return (key >> (l << 3)) & 0xFF; +} + +template +static T* +radixsort0(T *start, T *aux, npy_intp num) +{ + npy_intp cnt[sizeof(T)][1 << 8] = { { 0 } }; + T key0 = KEY_OF(start[0]); + + for (npy_intp i = 0; i < num; i++) { + T k = KEY_OF(start[i]); + + for (size_t l = 0; l < sizeof(T); l++) { + cnt[l][nth_byte(k, l)]++; + } + } + + size_t ncols = 0; + npy_ubyte cols[sizeof(T)]; + for (size_t l = 0; l < sizeof(T); l++) { + if (cnt[l][nth_byte(key0, l)] != num) { + cols[ncols++] = l; + } + } + + for (size_t l = 0; l < ncols; l++) { + npy_intp a = 0; + for (npy_intp i = 0; i < 256; i++) { + npy_intp b = cnt[cols[l]][i]; + cnt[cols[l]][i] = a; + a += b; + } + } + + for (size_t l = 0; l < ncols; l++) { + T* temp; + for (npy_intp i = 0; i < num; i++) { + T k = KEY_OF(start[i]); + npy_intp dst = cnt[cols[l]][nth_byte(k, cols[l])]++; + aux[dst] = start[i]; + } + + temp = aux; + aux = start; + start = temp; + } + + return start; +} + +template +static int +radixsort_(T *start, npy_intp num) +{ + + if (num < 2) { + return 0; + } + + npy_bool all_sorted = 1; + T k1 = KEY_OF(start[0]), k2; + for (npy_intp i = 1; i < num; i++) { + k2 = KEY_OF(start[i]); + if (k1 > k2) { + all_sorted = 0; + break; + } + k1 = k2; + } + + if (all_sorted) { + return 0; + } + + T* aux = (T*)malloc(num * sizeof(T)); + if (aux == nullptr) { + return -NPY_ENOMEM; + } + + T *sorted = radixsort0(start, aux, num); + if (sorted != start) { + memcpy(start, sorted, num * sizeof(T)); + } + + free(aux); + return 0; +} + +template +static int +radixsort(void *start, npy_intp num) { + return radixsort_((T*)start, num); +} + +template +static npy_intp* +aradixsort0(T *start, npy_intp *aux, npy_intp *tosort, npy_intp num) +{ + npy_intp cnt[sizeof(T)][1 << 8] = { { 0 } }; + T key0 = KEY_OF(start[0]); + + for (npy_intp i = 0; i < num; i++) { + T k = KEY_OF(start[i]); + + for (size_t l = 0; l < sizeof(T); l++) { + cnt[l][nth_byte(k, l)]++; + } + } + + size_t ncols = 0; + npy_ubyte cols[sizeof(T)]; + for (size_t l = 0; l < sizeof(T); l++) { + if (cnt[l][nth_byte(key0, l)] != num) { + cols[ncols++] = l; + } + } + + for (size_t l = 0; l < ncols; l++) { + npy_intp a = 0; + for (npy_intp i = 0; i < 256; i++) { + npy_intp b = cnt[cols[l]][i]; + cnt[cols[l]][i] = a; + a += b; + } + } + + for (size_t l = 0; l < ncols; l++) { + npy_intp* temp; + for (npy_intp i = 0; i < num; i++) { + T k = KEY_OF(start[tosort[i]]); + npy_intp dst = cnt[cols[l]][nth_byte(k, cols[l])]++; + aux[dst] = tosort[i]; + } + + temp = aux; + aux = tosort; + tosort = temp; + } + + return tosort; +} + +template +static int +aradixsort_(T *start, npy_intp* tosort, npy_intp num) +{ + npy_intp *sorted; + npy_intp *aux; + T k1, k2; + npy_bool all_sorted = 1; + + if (num < 2) { + return 0; + } + + k1 = KEY_OF(start[tosort[0]]); + for (npy_intp i = 1; i < num; i++) { + k2 = KEY_OF(start[tosort[i]]); + if (k1 > k2) { + all_sorted = 0; + break; + } + k1 = k2; + } + + if (all_sorted) { + return 0; + } + + aux = (npy_intp*)malloc(num * sizeof(npy_intp)); + if (aux == NULL) { + return -NPY_ENOMEM; + } + + sorted = aradixsort0(start, aux, tosort, num); + if (sorted != tosort) { + memcpy(tosort, sorted, num * sizeof(npy_intp)); + } + + free(aux); + return 0; +} + +template +static int +aradixsort(void *start, npy_intp* tosort, npy_intp num) { + return aradixsort_((T*)start, tosort, num); +} + +extern "C" { +NPY_NO_EXPORT int radixsort_bool(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_byte(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_ubyte(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_short(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_ushort(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_int(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_uint(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_long(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_ulong(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_longlong(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int radixsort_ulonglong(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } +NPY_NO_EXPORT int aradixsort_bool(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_byte(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_ubyte(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_short(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_ushort(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_int(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_uint(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_long(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_ulong(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_longlong(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int aradixsort_ulonglong(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +} diff --git a/numpy/core/src/umath/clip.c.src b/numpy/core/src/umath/clip.c.src deleted file mode 100644 index 48786d4a2..000000000 --- a/numpy/core/src/umath/clip.c.src +++ /dev/null @@ -1,125 +0,0 @@ -/** - * This module provides the inner loops for the clip ufunc - */ -#define PY_SSIZE_T_CLEAN -#include - -#define _UMATHMODULE -#define _MULTIARRAYMODULE -#define NPY_NO_DEPRECATED_API NPY_API_VERSION - -#include "numpy/halffloat.h" -#include "numpy/npy_math.h" -#include "numpy/ndarraytypes.h" -#include "numpy/npy_common.h" -#include "numpy/utils.h" -#include "fast_loop_macros.h" - -/* - * Produce macros that perform nan/nat-propagating min and max - */ - -/**begin repeat - * #name = BOOL, - * BYTE, UBYTE, SHORT, USHORT, INT, UINT, - * LONG, ULONG, LONGLONG, ULONGLONG# - */ -#define _NPY_@name@_MIN(a, b) PyArray_MIN(a, b) -#define _NPY_@name@_MAX(a, b) PyArray_MAX(a, b) -/**end repeat**/ - -#define _NPY_HALF_MIN(a, b) (npy_half_isnan(a) || npy_half_le(a, b) ? (a) : (b)) -#define _NPY_HALF_MAX(a, b) (npy_half_isnan(a) || npy_half_ge(a, b) ? (a) : (b)) - -/**begin repeat - * #name = FLOAT, DOUBLE, LONGDOUBLE# - */ -#define _NPY_@name@_MIN(a, b) (npy_isnan(a) ? (a) : PyArray_MIN(a, b)) -#define _NPY_@name@_MAX(a, b) (npy_isnan(a) ? (a) : PyArray_MAX(a, b)) -/**end repeat**/ - -/**begin repeat - * #name = CFLOAT, CDOUBLE, CLONGDOUBLE# - */ -#define _NPY_@name@_MIN(a, b) (npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CLT(a, b) ? (a) : (b)) -#define _NPY_@name@_MAX(a, b) (npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CGT(a, b) ? (a) : (b)) -/**end repeat**/ - -/**begin repeat - * #name = DATETIME, TIMEDELTA# - */ -#define _NPY_@name@_MIN(a, b) ( \ - (a) == NPY_DATETIME_NAT ? (a) : \ - (b) == NPY_DATETIME_NAT ? (b) : \ - (a) < (b) ? (a) : (b) \ -) -#define _NPY_@name@_MAX(a, b) ( \ - (a) == NPY_DATETIME_NAT ? (a) : \ - (b) == NPY_DATETIME_NAT ? (b) : \ - (a) > (b) ? (a) : (b) \ -) -/**end repeat**/ - -/**begin repeat - * - * #name = BOOL, - * BYTE, UBYTE, SHORT, USHORT, INT, UINT, - * LONG, ULONG, LONGLONG, ULONGLONG, - * HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE, - * DATETIME, TIMEDELTA# - * #type = npy_bool, - * npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint, - * npy_long, npy_ulong, npy_longlong, npy_ulonglong, - * npy_half, npy_float, npy_double, npy_longdouble, - * npy_cfloat, npy_cdouble, npy_clongdouble, - * npy_datetime, npy_timedelta# - */ - -NPY_NO_EXPORT void -@name@_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) -{ - if (steps[1] == 0 && steps[2] == 0) { - /* min and max are constant throughout the loop, the most common case */ - /* NOTE: it may be possible to optimize these checks for nan */ - @type@ min_val = *(@type@ *)args[1]; - @type@ max_val = *(@type@ *)args[2]; - - char *ip1 = args[0], *op1 = args[3]; - npy_intp is1 = steps[0], os1 = steps[3]; - npy_intp n = dimensions[0]; - - /* contiguous, branch to let the compiler optimize */ - if (is1 == sizeof(@type@) && os1 == sizeof(@type@)) { - for(npy_intp i = 0; i < n; i++, ip1 += is1, op1 += os1) { - @type@ t = *(@type@ *)ip1; - t = _NPY_@name@_MAX(t, min_val); - t = _NPY_@name@_MIN(t, max_val); - *(@type@ *)op1 = t; - } - } - else { - for(npy_intp i = 0; i < n; i++, ip1 += is1, op1 += os1) { - @type@ t = *(@type@ *)ip1; - t = _NPY_@name@_MAX(t, min_val); - t = _NPY_@name@_MIN(t, max_val); - *(@type@ *)op1 = t; - } - } - } - else { - TERNARY_LOOP { - @type@ t = *(@type@ *)ip1; - t = _NPY_@name@_MAX(t, *(@type@ *)ip2); - t = _NPY_@name@_MIN(t, *(@type@ *)ip3); - *(@type@ *)op1 = t; - } - } - npy_clear_floatstatus_barrier((char*)dimensions); -} - -// clean up the macros we defined above -#undef _NPY_@name@_MAX -#undef _NPY_@name@_MIN - -/**end repeat**/ diff --git a/numpy/core/src/umath/clip.cpp b/numpy/core/src/umath/clip.cpp new file mode 100644 index 000000000..830d3a39c --- /dev/null +++ b/numpy/core/src/umath/clip.cpp @@ -0,0 +1,131 @@ +/** + * This module provides the inner loops for the clip ufunc + */ +#define _UMATHMODULE +#define _MULTIARRAYMODULE +#define NPY_NO_DEPRECATED_API NPY_API_VERSION + +#define PY_SSIZE_T_CLEAN +#include + + +#include "numpy/halffloat.h" +#include "numpy/npy_math.h" +#include "numpy/ndarraytypes.h" +#include "numpy/npy_common.h" +#include "numpy/utils.h" +#include "fast_loop_macros.h" +#include "../common/numpy_tag.h" + + +template +T _NPY_MIN(T a, T b, npy::integral_tag const &) { return PyArray_MIN(a, b); } +template +T _NPY_MAX(T a, T b, npy::integral_tag const &) { return PyArray_MAX(a, b); } + +npy_half _NPY_MIN(npy_half a, npy_half b, npy::half_tag const &) { return npy_half_isnan(a) || npy_half_le(a, b) ? (a) : (b); } +npy_half _NPY_MAX(npy_half a, npy_half b, npy::half_tag const &) { return npy_half_isnan(a) || npy_half_ge(a, b) ? (a) : (b); } + +template +T _NPY_MIN(T a, T b, npy::floating_point_tag const &) { return npy_isnan(a) ? (a) : PyArray_MIN(a, b); } +template +T _NPY_MAX(T a, T b, npy::floating_point_tag const &) { return npy_isnan(a) ? (a) : PyArray_MAX(a, b); } + +template +T _NPY_MIN(T a, T b, npy::complex_tag const &) { return npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CLT(a, b) ? (a) : (b); } +template +T _NPY_MAX(T a, T b, npy::complex_tag const &) { return npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CGT(a, b) ? (a) : (b); } + +template +T _NPY_MIN(T a, T b, npy::date_tag const &) { + return (a) == NPY_DATETIME_NAT + ? (a) + : (b) == NPY_DATETIME_NAT ? (b) : (a) < (b) ? (a) : (b); +} +template +T _NPY_MAX(T a, T b, npy::date_tag const &) { + return (a) == NPY_DATETIME_NAT + ? (a) + : (b) == NPY_DATETIME_NAT ? (b) : (a) > (b) ? (a) : (b); +} + +/* generic dispatcher */ +template +T _NPY_MIN(T const& a, T const& b) { + return _NPY_MIN(a, b, Tag{}); +} +template +T _NPY_MAX(T const& a, T const& b) { + return _NPY_MAX(a, b, Tag{}); +} + + +template +T _NPY_CLIP(T x, T min, T max) { + return _NPY_MIN(_NPY_MAX((x), (min)), (max)); +} + +template +static void +_npy_clip_(T **args, npy_intp const *dimensions, npy_intp const *steps) +{ + npy_intp n = dimensions[0]; + if (steps[1] == 0 && steps[2] == 0) { + /* min and max are constant throughout the loop, the most common case */ + /* NOTE: it may be possible to optimize these checks for nan */ + T min_val = *args[1]; + T max_val = *args[2]; + + T *ip1 = args[0], *op1 = args[3]; + npy_intp is1 = steps[0]/sizeof(T), os1 = steps[3]/sizeof(T); + + /* contiguous, branch to let the compiler optimize */ + if (is1 == 1 && os1 == 1) { + for(npy_intp i = 0; i < n; i++, ip1++, op1++) { + *op1 = _NPY_CLIP(*ip1, min_val, max_val); + } + } + else { + for(npy_intp i = 0; i < n; i++, ip1 += is1, op1 += os1) { + *op1 = _NPY_CLIP(*ip1, min_val, max_val); + } + } + } + else { + T *ip1 = args[0], *ip2 = args[1], *ip3 = args[2], *op1 = args[3]; + npy_intp is1 = steps[0]/sizeof(T), is2 = steps[1]/sizeof(T), is3 = steps[2]/sizeof(T), os1 = steps[3]/sizeof(T); + for(npy_intp i = 0; i < n; i++, ip1 += is1, ip2 += is2, ip3 += is3, op1 += os1) + *op1 = _NPY_CLIP(*ip1, *ip2, *ip3); + } + npy_clear_floatstatus_barrier((char*)dimensions); +} + +template +static void +_npy_clip(char **args, npy_intp const *dimensions, npy_intp const *steps) { + using T = typename Tag::type; + return _npy_clip_((T**)args, dimensions, steps); +} + +extern "C" { +NPY_NO_EXPORT void BOOL_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void BYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void UBYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void SHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void USHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void INT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void UINT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void LONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void ULONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void LONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void ULONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void HALF_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void FLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void DOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void LONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void CFLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void CDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void CLONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void DATETIME_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void TIMEDELTA_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +} diff --git a/numpy/core/src/umath/clip.h b/numpy/core/src/umath/clip.h new file mode 100644 index 000000000..66b3f5c39 --- /dev/null +++ b/numpy/core/src/umath/clip.h @@ -0,0 +1,33 @@ +#ifndef _NPY_UMATH_CLIP_H_ +#define _NPY_UMATH_CLIP_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +NPY_NO_EXPORT void BOOL_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void BYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void UBYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void SHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void USHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void INT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void UINT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void LONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void ULONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void LONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void ULONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void HALF_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void FLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void DOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void LONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void CFLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void CDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void CLONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void DATETIME_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void TIMEDELTA_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/numpy/core/src/umath/clip.h.src b/numpy/core/src/umath/clip.h.src deleted file mode 100644 index f16856cdf..000000000 --- a/numpy/core/src/umath/clip.h.src +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _NPY_UMATH_CLIP_H_ -#define _NPY_UMATH_CLIP_H_ - - -/**begin repeat - * - * #name = BOOL, - * BYTE, UBYTE, SHORT, USHORT, INT, UINT, - * LONG, ULONG, LONGLONG, ULONGLONG, - * HALF, FLOAT, DOUBLE, LONGDOUBLE, - * CFLOAT, CDOUBLE, CLONGDOUBLE, - * DATETIME, TIMEDELTA# - */ -NPY_NO_EXPORT void -@name@_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -/**end repeat**/ - -#endif diff --git a/numpy/distutils/ccompiler.py b/numpy/distutils/ccompiler.py index 4495c8fee..9c85d28b9 100644 --- a/numpy/distutils/ccompiler.py +++ b/numpy/distutils/ccompiler.py @@ -23,7 +23,8 @@ from numpy.distutils.exec_command import ( ) from numpy.distutils.misc_util import cyg2win32, is_sequence, mingw32, \ get_num_build_jobs, \ - _commandline_dep_string + _commandline_dep_string, \ + sanitize_cxx_flags # globals for parallel build management import threading @@ -674,7 +675,9 @@ def CCompiler_cxx_compiler(self): return self cxx = copy(self) - cxx.compiler_so = [cxx.compiler_cxx[0]] + cxx.compiler_so[1:] + cxx.compiler_cxx = cxx.compiler_cxx + cxx.compiler_so = [cxx.compiler_cxx[0]] + \ + sanitize_cxx_flags(cxx.compiler_so[1:]) if sys.platform.startswith('aix') and 'ld_so_aix' in cxx.linker_so[0]: # AIX needs the ld_so_aix script included with Python cxx.linker_so = [cxx.linker_so[0], cxx.compiler_cxx[0]] \ diff --git a/numpy/distutils/ccompiler_opt.py b/numpy/distutils/ccompiler_opt.py index e7fd494d3..9c386b3ac 100644 --- a/numpy/distutils/ccompiler_opt.py +++ b/numpy/distutils/ccompiler_opt.py @@ -188,7 +188,8 @@ class _Config: # native usually works only with x86 native = '-march=native', opt = '-O3', - werror = '-Werror' + werror = '-Werror', + cxx = '-std=c++11', ), clang = dict( native = '-march=native', @@ -198,22 +199,26 @@ class _Config: # cases `-Werror` gets skipped during the availability test due to # "unused arguments" warnings. # see https://github.com/numpy/numpy/issues/19624 - werror = '-Werror-implicit-function-declaration -Werror' + werror = '-Werror=switch -Werror', + cxx = '-std=c++11', ), icc = dict( native = '-xHost', opt = '-O3', - werror = '-Werror' + werror = '-Werror', + cxx = '-std=c++11', ), iccw = dict( native = '/QxHost', opt = '/O3', - werror = '/Werror' + werror = '/Werror', + cxx = '-std=c++11', ), msvc = dict( native = None, opt = '/O2', - werror = '/WX' + werror = '/WX', + cxx = '-std=c++11', ) ) conf_min_features = dict( @@ -555,6 +560,7 @@ class _Distutils: flags = kwargs.pop("extra_postargs", []) + flags if not ccompiler: ccompiler = self._ccompiler + return ccompiler.compile(sources, extra_postargs=flags, **kwargs) def dist_test(self, source, flags, macros=[]): diff --git a/numpy/distutils/command/build_clib.py b/numpy/distutils/command/build_clib.py index 0e31a7dee..a481758c1 100644 --- a/numpy/distutils/command/build_clib.py +++ b/numpy/distutils/command/build_clib.py @@ -264,6 +264,8 @@ class build_clib(old_build_clib): if include_dirs is None: include_dirs = [] extra_postargs = build_info.get('extra_compiler_args') or [] + extra_cflags = build_info.get('extra_cflags') or [] + extra_cxxflags = build_info.get('extra_cxxflags') or [] include_dirs.extend(get_numpy_include_dirs()) # where compiled F90 module files are: @@ -315,38 +317,45 @@ class build_clib(old_build_clib): macros=macros + copt_macros, include_dirs=include_dirs, debug=self.debug, - extra_postargs=extra_postargs, + extra_postargs=extra_postargs + extra_cxxflags, ccompiler=cxx_compiler ) if copt_c_sources: log.info("compiling C dispatch-able sources") - objects += self.compiler_opt.try_dispatch(copt_c_sources, - output_dir=self.build_temp, - src_dir=copt_build_src, - macros=macros + copt_macros, - include_dirs=include_dirs, - debug=self.debug, - extra_postargs=extra_postargs) + objects += self.compiler_opt.try_dispatch( + copt_c_sources, + output_dir=self.build_temp, + src_dir=copt_build_src, + macros=macros + copt_macros, + include_dirs=include_dirs, + debug=self.debug, + extra_postargs=extra_postargs + extra_cflags) if c_sources: log.info("compiling C sources") - objects += compiler.compile(c_sources, - output_dir=self.build_temp, - macros=macros + copt_macros, - include_dirs=include_dirs, - debug=self.debug, - extra_postargs=extra_postargs + copt_baseline_flags) + objects += compiler.compile( + c_sources, + output_dir=self.build_temp, + macros=macros + copt_macros, + include_dirs=include_dirs, + debug=self.debug, + extra_postargs=(extra_postargs + + copt_baseline_flags + + extra_cflags)) if cxx_sources: log.info("compiling C++ sources") cxx_compiler = compiler.cxx_compiler() - cxx_objects = cxx_compiler.compile(cxx_sources, - output_dir=self.build_temp, - macros=macros + copt_macros, - include_dirs=include_dirs, - debug=self.debug, - extra_postargs=extra_postargs + copt_baseline_flags) + cxx_objects = cxx_compiler.compile( + cxx_sources, + output_dir=self.build_temp, + macros=macros + copt_macros, + include_dirs=include_dirs, + debug=self.debug, + extra_postargs=(extra_postargs + + copt_baseline_flags + + extra_cxxflags)) objects.extend(cxx_objects) if f_sources or fmodule_sources: diff --git a/numpy/distutils/command/build_ext.py b/numpy/distutils/command/build_ext.py index b8378d473..7040a2411 100644 --- a/numpy/distutils/command/build_ext.py +++ b/numpy/distutils/command/build_ext.py @@ -243,7 +243,8 @@ class build_ext (old_build_ext): if l and l != ext_language and ext.language: log.warn('resetting extension %r language from %r to %r.' % (ext.name, l, ext_language)) - ext.language = ext_language + if not ext.language: + ext.language = ext_language # global language all_languages.update(ext_languages) @@ -376,6 +377,9 @@ class build_ext (old_build_ext): log.info("building '%s' extension", ext.name) extra_args = ext.extra_compile_args or [] + extra_cflags = ext.extra_c_compile_args or [] + extra_cxxflags = ext.extra_cxx_compile_args or [] + macros = ext.define_macros[:] for undef in ext.undef_macros: macros.append((undef,)) @@ -462,38 +466,43 @@ class build_ext (old_build_ext): macros=macros + copt_macros, include_dirs=include_dirs, debug=self.debug, - extra_postargs=extra_args, + extra_postargs=extra_args + extra_cxxflags, ccompiler=cxx_compiler, **kws ) if copt_c_sources: log.info("compiling C dispatch-able sources") - c_objects += self.compiler_opt.try_dispatch(copt_c_sources, - output_dir=output_dir, - src_dir=copt_build_src, - macros=macros + copt_macros, - include_dirs=include_dirs, - debug=self.debug, - extra_postargs=extra_args, - **kws) + c_objects += self.compiler_opt.try_dispatch( + copt_c_sources, + output_dir=output_dir, + src_dir=copt_build_src, + macros=macros + copt_macros, + include_dirs=include_dirs, + debug=self.debug, + extra_postargs=extra_args + extra_cflags, + **kws) if c_sources: log.info("compiling C sources") - c_objects += self.compiler.compile(c_sources, - output_dir=output_dir, - macros=macros + copt_macros, - include_dirs=include_dirs, - debug=self.debug, - extra_postargs=extra_args + copt_baseline_flags, - **kws) + c_objects += self.compiler.compile( + c_sources, + output_dir=output_dir, + macros=macros + copt_macros, + include_dirs=include_dirs, + debug=self.debug, + extra_postargs=(extra_args + copt_baseline_flags + + extra_cflags), + **kws) if cxx_sources: log.info("compiling C++ sources") - c_objects += cxx_compiler.compile(cxx_sources, - output_dir=output_dir, - macros=macros + copt_macros, - include_dirs=include_dirs, - debug=self.debug, - extra_postargs=extra_args + copt_baseline_flags, - **kws) + c_objects += cxx_compiler.compile( + cxx_sources, + output_dir=output_dir, + macros=macros + copt_macros, + include_dirs=include_dirs, + debug=self.debug, + extra_postargs=(extra_args + copt_baseline_flags + + extra_cxxflags), + **kws) extra_postargs = [] f_objects = [] diff --git a/numpy/distutils/extension.py b/numpy/distutils/extension.py index c90b5d725..3ede013e0 100644 --- a/numpy/distutils/extension.py +++ b/numpy/distutils/extension.py @@ -47,6 +47,8 @@ class Extension(old_Extension): language=None, f2py_options=None, module_dirs=None, + extra_c_compile_args=None, + extra_cxx_compile_args=None, extra_f77_compile_args=None, extra_f90_compile_args=None,): @@ -83,6 +85,8 @@ class Extension(old_Extension): # numpy_distutils features self.f2py_options = f2py_options or [] self.module_dirs = module_dirs or [] + self.extra_c_compile_args = extra_c_compile_args or [] + self.extra_cxx_compile_args = extra_cxx_compile_args or [] self.extra_f77_compile_args = extra_f77_compile_args or [] self.extra_f90_compile_args = extra_f90_compile_args or [] diff --git a/numpy/distutils/misc_util.py b/numpy/distutils/misc_util.py index c9e051237..f0f9b4bd7 100644 --- a/numpy/distutils/misc_util.py +++ b/numpy/distutils/misc_util.py @@ -11,6 +11,7 @@ import multiprocessing import textwrap import importlib.util from threading import local as tlocal +from functools import reduce import distutils from distutils.errors import DistutilsError @@ -43,7 +44,7 @@ __all__ = ['Configuration', 'get_numpy_include_dirs', 'default_config_dict', 'dot_join', 'get_frame', 'minrelpath', 'njoin', 'is_sequence', 'is_string', 'as_list', 'gpaths', 'get_language', 'get_build_architecture', 'get_info', 'get_pkg_info', - 'get_num_build_jobs'] + 'get_num_build_jobs', 'sanitize_cxx_flags'] class InstallableLib: """ @@ -2478,3 +2479,15 @@ def get_build_architecture(): # systems, so delay the import to here. from distutils.msvccompiler import get_build_architecture return get_build_architecture() + + +_cxx_ignore_flags = {'-Werror=implicit-function-declaration'} + + +def sanitize_cxx_flags(cxxflags): + ''' + Some flags are valid for C but not C++. Prune them. + ''' + return [flag for flag in cxxflags if flag not in _cxx_ignore_flags] + + -- cgit v1.2.1 From 1012141a15a432c1c7a3f3d964c38ca04970bd1b Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Fri, 22 Oct 2021 12:34:45 +0100 Subject: Fix automatic detection for MSVCRT version Original algorithm appended both character of minor version > 9, but the string against which it is being matched, only uses the first character. --- numpy/distutils/mingw32ccompiler.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/mingw32ccompiler.py b/numpy/distutils/mingw32ccompiler.py index c3a0cb418..430a3a3e7 100644 --- a/numpy/distutils/mingw32ccompiler.py +++ b/numpy/distutils/mingw32ccompiler.py @@ -549,10 +549,12 @@ if sys.platform == 'win32': _MSVCRVER_TO_FULLVER['100'] = "10.0.30319.460" # Python 3.7 uses 1415, but get_build_version returns 140 ?? _MSVCRVER_TO_FULLVER['140'] = "14.15.26726.0" - if hasattr(msvcrt, "CRT_ASSEMBLY_VERSION"): - major, minor, rest = msvcrt.CRT_ASSEMBLY_VERSION.split(".", 2) - _MSVCRVER_TO_FULLVER[major + minor] = msvcrt.CRT_ASSEMBLY_VERSION - del major, minor, rest + crt_ver = getattr(msvcrt, 'CRT_ASSEMBLY_VERSION', None) + if crt_ver is not None: # Available at least back to Python 3.3 + maj, min = re.match(r'(\d+)\.(\d)', crt_ver).groups() + _MSVCRVER_TO_FULLVER[maj + min] = crt_ver + del maj, min + del crt_ver except ImportError: # If we are here, means python was not built with MSVC. Not sure what # to do in that case: manifest building will fail, but it should not be -- cgit v1.2.1 From 02f602b0fe4c5a67fe82f0cd7cb9c820392445f9 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Fri, 22 Oct 2021 12:40:14 +0100 Subject: Remove fix for odd MSVCRT version on Windows 3.7 The comment in the original code came about because of the bug fixed in gh-20155. The fixed subsequent clause should now give the correct assembly version for the given Python. In fact, the original clause matches the MSVC for Python 3.7.1: ``` PS C:\> .\Python37\python.exe .\io\check_versions.py sys.version 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] distutils build version 14.1 msvcrt.CRT_ASSEMBLY_VERSION 14.15.26726.0 ``` --- numpy/distutils/mingw32ccompiler.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/mingw32ccompiler.py b/numpy/distutils/mingw32ccompiler.py index 430a3a3e7..82d296434 100644 --- a/numpy/distutils/mingw32ccompiler.py +++ b/numpy/distutils/mingw32ccompiler.py @@ -547,8 +547,6 @@ if sys.platform == 'win32': # Value from msvcrt.CRT_ASSEMBLY_VERSION under Python 3.3.0 # on Windows XP: _MSVCRVER_TO_FULLVER['100'] = "10.0.30319.460" - # Python 3.7 uses 1415, but get_build_version returns 140 ?? - _MSVCRVER_TO_FULLVER['140'] = "14.15.26726.0" crt_ver = getattr(msvcrt, 'CRT_ASSEMBLY_VERSION', None) if crt_ver is not None: # Available at least back to Python 3.3 maj, min = re.match(r'(\d+)\.(\d)', crt_ver).groups() -- cgit v1.2.1 From adea1d4fd4e1638f74bf1c8bc48b06992d569c7b Mon Sep 17 00:00:00 2001 From: melissawm Date: Fri, 22 Oct 2021 12:11:18 -0300 Subject: DOC: Clarify table contents in cond docstring --- numpy/linalg/linalg.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index d5bcb1cb4..e7196524e 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -1680,7 +1680,7 @@ def cond(x, p=None): x : (..., M, N) array_like The matrix whose condition number is sought. p : {None, 1, -1, 2, -2, inf, -inf, 'fro'}, optional - Order of the norm: + Order of the norm used in the condition number computation: ===== ============================ p norm for matrices @@ -1691,11 +1691,11 @@ def cond(x, p=None): -inf min(sum(abs(x), axis=1)) 1 max(sum(abs(x), axis=0)) -1 min(sum(abs(x), axis=0)) - 2 (largest sing. value)/(smallest sing. value) - -2 (smallest sing. value)/(largest sing. value) + 2 2-norm (largest sing. value) + -2 smallest singular value ===== ============================ - inf means the numpy.inf object, and the Frobenius norm is + inf means the `numpy.inf` object, and the Frobenius norm is the root-of-sum-of-squares norm. Returns -- cgit v1.2.1 From 5f9cd964581950d671d2e17a724cdd6984dcd29d Mon Sep 17 00:00:00 2001 From: Jonas Liechti Date: Fri, 22 Oct 2021 17:41:32 +0200 Subject: removed redundant comment --- numpy/lib/function_base.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 2e77c11dc..392a059b5 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -663,9 +663,6 @@ def select(condlist, choicelist, default=0): >>> np.select(condlist, choicelist, 42) array([ 0, 1, 2, 42, 16, 25]) - Note that, when multiple conditions are satisfied, the element is drawn - from the first array in `choicelist` with a matching condition: - >>> x = np.arange(6) >>> condlist = [x<=4, x>3] >>> choicelist = [x, x**2] -- cgit v1.2.1 From aa232f68943ef23be298e2be824e97ad7e174ed4 Mon Sep 17 00:00:00 2001 From: Ronan Lamy Date: Fri, 22 Oct 2021 19:58:24 +0100 Subject: MAINT: Remove useless custom tp_alloc and tp_free on ndarray array_alloc is equivalent to the default object allocator PyType_GenericAlloc. array_free was added "just in case" in e9ebefec but doesn't seem to serve any actual purpose. --- numpy/core/src/multiarray/arrayobject.c | 18 ------------------ 1 file changed, 18 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index 9b9df08f2..28aff5d65 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -1705,22 +1705,6 @@ array_iter(PyArrayObject *arr) return PySeqIter_New((PyObject *)arr); } -static PyObject * -array_alloc(PyTypeObject *type, Py_ssize_t NPY_UNUSED(nitems)) -{ - /* nitems will always be 0 */ - PyObject *obj = PyObject_Malloc(type->tp_basicsize); - PyObject_Init(obj, type); - return obj; -} - -static void -array_free(PyObject * v) -{ - /* avoid same deallocator as PyBaseObject, see gentype_free */ - PyObject_Free(v); -} - NPY_NO_EXPORT PyTypeObject PyArray_Type = { PyVarObject_HEAD_INIT(NULL, 0) @@ -1741,7 +1725,5 @@ NPY_NO_EXPORT PyTypeObject PyArray_Type = { .tp_iter = (getiterfunc)array_iter, .tp_methods = array_methods, .tp_getset = array_getsetlist, - .tp_alloc = (allocfunc)array_alloc, .tp_new = (newfunc)array_new, - .tp_free = (freefunc)array_free, }; -- cgit v1.2.1 From ae96099c6f2c7f91c4875d55f981d877c25a0fd9 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 22 Oct 2021 23:20:09 +0200 Subject: ENH: Add annotations for `np.chararray` --- numpy/__init__.pyi | 475 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 422 insertions(+), 53 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 097773c5d..9a788baa7 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -33,6 +33,8 @@ from numpy.typing import ( _ArrayLikeTD64_co, _ArrayLikeDT64_co, _ArrayLikeObject_co, + _ArrayLikeStr_co, + _ArrayLikeBytes_co, # DTypes DTypeLike, @@ -671,59 +673,6 @@ class MachAr: ) -> None: ... def __getattr__(self, key: str) -> Any: ... -class chararray(ndarray[_ShapeType, _DType_co]): - def __new__( - subtype, - shape: Any, - itemsize: Any = ..., - unicode: Any = ..., - buffer: Any = ..., - offset: Any = ..., - strides: Any = ..., - order: Any = ..., - ) -> Any: ... - def __array_finalize__(self, obj): ... - def argsort(self, axis=..., kind=..., order=...): ... - def capitalize(self): ... - def center(self, width, fillchar=...): ... - def count(self, sub, start=..., end=...): ... - def decode(self, encoding=..., errors=...): ... - def encode(self, encoding=..., errors=...): ... - def endswith(self, suffix, start=..., end=...): ... - def expandtabs(self, tabsize=...): ... - def find(self, sub, start=..., end=...): ... - def index(self, sub, start=..., end=...): ... - def isalnum(self): ... - def isalpha(self): ... - def isdigit(self): ... - def islower(self): ... - def isspace(self): ... - def istitle(self): ... - def isupper(self): ... - def join(self, seq): ... - def ljust(self, width, fillchar=...): ... - def lower(self): ... - def lstrip(self, chars=...): ... - def partition(self, sep): ... - def replace(self, old, new, count=...): ... - def rfind(self, sub, start=..., end=...): ... - def rindex(self, sub, start=..., end=...): ... - def rjust(self, width, fillchar=...): ... - def rpartition(self, sep): ... - def rsplit(self, sep=..., maxsplit=...): ... - def rstrip(self, chars=...): ... - def split(self, sep=..., maxsplit=...): ... - def splitlines(self, keepends=...): ... - def startswith(self, prefix, start=..., end=...): ... - def strip(self, chars=...): ... - def swapcase(self): ... - def title(self): ... - def translate(self, table, deletechars=...): ... - def upper(self): ... - def zfill(self, width): ... - def isnumeric(self): ... - def isdecimal(self): ... - # Some of these are aliases; others are wrappers with an identical signature round = around round_ = around @@ -3989,3 +3938,423 @@ class matrix(ndarray[_ShapeType, _DType_co]): def getA(self) -> ndarray[_ShapeType, _DType_co]: ... def getA1(self) -> ndarray[Any, _DType_co]: ... def getH(self) -> matrix[Any, _DType_co]: ... + +_CharType = TypeVar("_CharType", str_, bytes_) +_CharDType = TypeVar("_CharDType", dtype[str_], dtype[bytes_]) +_CharArray = chararray[Any, dtype[_CharType]] + +class chararray(ndarray[_ShapeType, _CharDType]): + @overload + def __new__( + subtype, + shape: _ShapeLike, + itemsize: SupportsIndex | SupportsInt = ..., + unicode: L[False] = ..., + buffer: _SupportsBuffer = ..., + offset: SupportsIndex = ..., + strides: _ShapeLike = ..., + order: _OrderKACF = ..., + ) -> chararray[Any, dtype[bytes_]]: ... + @overload + def __new__( + subtype, + shape: _ShapeLike, + itemsize: SupportsIndex | SupportsInt = ..., + unicode: L[True] = ..., + buffer: _SupportsBuffer = ..., + offset: SupportsIndex = ..., + strides: _ShapeLike = ..., + order: _OrderKACF = ..., + ) -> chararray[Any, dtype[str_]]: ... + + def __array_finalize__(self, obj: NDArray[str_ | bytes_]) -> None: ... + def __mul__(self, other: _ArrayLikeInt_co) -> chararray[Any, _CharDType]: ... + def __rmul__(self, other: _ArrayLikeInt_co) -> chararray[Any, _CharDType]: ... + def __mod__(self, i: Any) -> chararray[Any, _CharDType]: ... + + @overload + def __eq__( + self: _CharArray[str_], + other: _ArrayLikeStr_co, + ) -> NDArray[bool_]: ... + @overload + def __eq__( + self: _CharArray[bytes_], + other: _ArrayLikeBytes_co, + ) -> NDArray[bool_]: ... + + @overload + def __ne__( + self: _CharArray[str_], + other: _ArrayLikeStr_co, + ) -> NDArray[bool_]: ... + @overload + def __ne__( + self: _CharArray[bytes_], + other: _ArrayLikeBytes_co, + ) -> NDArray[bool_]: ... + + @overload + def __ge__( + self: _CharArray[str_], + other: _ArrayLikeStr_co, + ) -> NDArray[bool_]: ... + @overload + def __ge__( + self: _CharArray[bytes_], + other: _ArrayLikeBytes_co, + ) -> NDArray[bool_]: ... + + @overload + def __le__( + self: _CharArray[str_], + other: _ArrayLikeStr_co, + ) -> NDArray[bool_]: ... + @overload + def __le__( + self: _CharArray[bytes_], + other: _ArrayLikeBytes_co, + ) -> NDArray[bool_]: ... + + @overload + def __gt__( + self: _CharArray[str_], + other: _ArrayLikeStr_co, + ) -> NDArray[bool_]: ... + @overload + def __gt__( + self: _CharArray[bytes_], + other: _ArrayLikeBytes_co, + ) -> NDArray[bool_]: ... + + @overload + def __lt__( + self: _CharArray[str_], + other: _ArrayLikeStr_co, + ) -> NDArray[bool_]: ... + @overload + def __lt__( + self: _CharArray[bytes_], + other: _ArrayLikeBytes_co, + ) -> NDArray[bool_]: ... + + @overload + def __add__( + self: _CharArray[str_], + other: _ArrayLikeStr_co, + ) -> _CharArray[str_]: ... + @overload + def __add__( + self: _CharArray[bytes_], + other: _ArrayLikeBytes_co, + ) -> _CharArray[bytes_]: ... + + @overload + def __radd__( + self: _CharArray[str_], + other: _ArrayLikeStr_co, + ) -> _CharArray[str_]: ... + @overload + def __radd__( + self: _CharArray[bytes_], + other: _ArrayLikeBytes_co, + ) -> _CharArray[bytes_]: ... + + @overload + def center( + self: _CharArray[str_], + width: _ArrayLikeInt_co, + fillchar: _ArrayLikeStr_co = ..., + ) -> _CharArray[str_]: ... + @overload + def center( + self: _CharArray[bytes_], + width: _ArrayLikeInt_co, + fillchar: _ArrayLikeBytes_co = ..., + ) -> _CharArray[bytes_]: ... + + @overload + def count( + self: _CharArray[str_], + sub: _ArrayLikeStr_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + @overload + def count( + self: _CharArray[bytes_], + sub: _ArrayLikeBytes_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + + def decode( + self: _CharArray[bytes_], + encoding: None | str = ..., + errors: None | str = ..., + ) -> _CharArray[str_]: ... + + def encode( + self: _CharArray[str_], + encoding: None | str = ..., + errors: None | str = ..., + ) -> _CharArray[bytes_]: ... + + @overload + def endswith( + self: _CharArray[str_], + suffix: _ArrayLikeStr_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[bool_]: ... + @overload + def endswith( + self: _CharArray[bytes_], + suffix: _ArrayLikeBytes_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[bool_]: ... + + def expandtabs( + self, + tabsize: _ArrayLikeInt_co = ..., + ) -> chararray[Any, _CharDType]: ... + + @overload + def find( + self: _CharArray[str_], + sub: _ArrayLikeStr_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + @overload + def find( + self: _CharArray[bytes_], + sub: _ArrayLikeBytes_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + + @overload + def index( + self: _CharArray[str_], + sub: _ArrayLikeStr_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + @overload + def index( + self: _CharArray[bytes_], + sub: _ArrayLikeBytes_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + + @overload + def join( + self: _CharArray[str_], + seq: _ArrayLikeStr_co, + ) -> _CharArray[str_]: ... + @overload + def join( + self: _CharArray[bytes_], + seq: _ArrayLikeBytes_co, + ) -> _CharArray[bytes_]: ... + + @overload + def ljust( + self: _CharArray[str_], + width: _ArrayLikeInt_co, + fillchar: _ArrayLikeStr_co = ..., + ) -> _CharArray[str_]: ... + @overload + def ljust( + self: _CharArray[bytes_], + width: _ArrayLikeInt_co, + fillchar: _ArrayLikeBytes_co = ..., + ) -> _CharArray[bytes_]: ... + + @overload + def lstrip( + self: _CharArray[str_], + chars: None | _ArrayLikeStr_co = ..., + ) -> _CharArray[str_]: ... + @overload + def lstrip( + self: _CharArray[bytes_], + chars: None | _ArrayLikeBytes_co = ..., + ) -> _CharArray[bytes_]: ... + + @overload + def partition( + self: _CharArray[str_], + sep: _ArrayLikeStr_co, + ) -> _CharArray[str_]: ... + @overload + def partition( + self: _CharArray[bytes_], + sep: _ArrayLikeBytes_co, + ) -> _CharArray[bytes_]: ... + + @overload + def replace( + self: _CharArray[str_], + old: _ArrayLikeStr_co, + new: _ArrayLikeStr_co, + count: None | _ArrayLikeInt_co = ..., + ) -> _CharArray[str_]: ... + @overload + def replace( + self: _CharArray[bytes_], + old: _ArrayLikeBytes_co, + new: _ArrayLikeBytes_co, + count: None | _ArrayLikeInt_co = ..., + ) -> _CharArray[bytes_]: ... + + @overload + def rfind( + self: _CharArray[str_], + sub: _ArrayLikeStr_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + @overload + def rfind( + self: _CharArray[bytes_], + sub: _ArrayLikeBytes_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + + @overload + def rindex( + self: _CharArray[str_], + sub: _ArrayLikeStr_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + @overload + def rindex( + self: _CharArray[bytes_], + sub: _ArrayLikeBytes_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[int_]: ... + + @overload + def rjust( + self: _CharArray[str_], + width: _ArrayLikeInt_co, + fillchar: _ArrayLikeStr_co = ..., + ) -> _CharArray[str_]: ... + @overload + def rjust( + self: _CharArray[bytes_], + width: _ArrayLikeInt_co, + fillchar: _ArrayLikeBytes_co = ..., + ) -> _CharArray[bytes_]: ... + + @overload + def rpartition( + self: _CharArray[str_], + sep: _ArrayLikeStr_co, + ) -> _CharArray[str_]: ... + @overload + def rpartition( + self: _CharArray[bytes_], + sep: _ArrayLikeBytes_co, + ) -> _CharArray[bytes_]: ... + + @overload + def rsplit( + self: _CharArray[str_], + sep: None | _ArrayLikeStr_co = ..., + maxsplit: None | _ArrayLikeInt_co = ..., + ) -> NDArray[object_]: ... + @overload + def rsplit( + self: _CharArray[bytes_], + sep: None | _ArrayLikeBytes_co = ..., + maxsplit: None | _ArrayLikeInt_co = ..., + ) -> NDArray[object_]: ... + + @overload + def rstrip( + self: _CharArray[str_], + chars: None | _ArrayLikeStr_co = ..., + ) -> _CharArray[str_]: ... + @overload + def rstrip( + self: _CharArray[bytes_], + chars: None | _ArrayLikeBytes_co = ..., + ) -> _CharArray[bytes_]: ... + + @overload + def split( + self: _CharArray[str_], + sep: None | _ArrayLikeStr_co = ..., + maxsplit: None | _ArrayLikeInt_co = ..., + ) -> NDArray[object_]: ... + @overload + def split( + self: _CharArray[bytes_], + sep: None | _ArrayLikeBytes_co = ..., + maxsplit: None | _ArrayLikeInt_co = ..., + ) -> NDArray[object_]: ... + + def splitlines(self, keepends: None | _ArrayLikeBool_co = ...) -> NDArray[object_]: ... + + @overload + def startswith( + self: _CharArray[str_], + prefix: _ArrayLikeStr_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[bool_]: ... + @overload + def startswith( + self: _CharArray[bytes_], + prefix: _ArrayLikeBytes_co, + start: _ArrayLikeInt_co = ..., + end: None | _ArrayLikeInt_co = ..., + ) -> NDArray[bool_]: ... + + @overload + def strip( + self: _CharArray[str_], + chars: None | _ArrayLikeStr_co = ..., + ) -> _CharArray[str_]: ... + @overload + def strip( + self: _CharArray[bytes_], + chars: None | _ArrayLikeBytes_co = ..., + ) -> _CharArray[bytes_]: ... + + @overload + def translate( + self: _CharArray[str_], + table: _ArrayLikeStr_co, + deletechars: None | _ArrayLikeStr_co = ..., + ) -> _CharArray[str_]: ... + @overload + def translate( + self: _CharArray[bytes_], + table: _ArrayLikeBytes_co, + deletechars: None | _ArrayLikeBytes_co = ..., + ) -> _CharArray[bytes_]: ... + + def zfill(self, width: _ArrayLikeInt_co) -> chararray[Any, _CharDType]: ... + def capitalize(self) -> chararray[_ShapeType, _CharDType]: ... + def title(self) -> chararray[_ShapeType, _CharDType]: ... + def swapcase(self) -> chararray[_ShapeType, _CharDType]: ... + def lower(self) -> chararray[_ShapeType, _CharDType]: ... + def upper(self) -> chararray[_ShapeType, _CharDType]: ... + def isalnum(self) -> ndarray[_ShapeType, dtype[bool_]]: ... + def isalpha(self) -> ndarray[_ShapeType, dtype[bool_]]: ... + def isdigit(self) -> ndarray[_ShapeType, dtype[bool_]]: ... + def islower(self) -> ndarray[_ShapeType, dtype[bool_]]: ... + def isspace(self) -> ndarray[_ShapeType, dtype[bool_]]: ... + def istitle(self) -> ndarray[_ShapeType, dtype[bool_]]: ... + def isupper(self) -> ndarray[_ShapeType, dtype[bool_]]: ... + def isnumeric(self) -> ndarray[_ShapeType, dtype[bool_]]: ... + def isdecimal(self) -> ndarray[_ShapeType, dtype[bool_]]: ... -- cgit v1.2.1 From f7ddf9ccf45ed25f26854378cc99e0dd8d95ad42 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 22 Oct 2021 23:20:41 +0200 Subject: TST: Add typing tests for `np.chararray` --- numpy/typing/tests/data/fail/chararray.pyi | 62 +++++++++++++ numpy/typing/tests/data/reveal/chararray.pyi | 129 +++++++++++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 numpy/typing/tests/data/fail/chararray.pyi create mode 100644 numpy/typing/tests/data/reveal/chararray.pyi (limited to 'numpy') diff --git a/numpy/typing/tests/data/fail/chararray.pyi b/numpy/typing/tests/data/fail/chararray.pyi new file mode 100644 index 000000000..ebc182ec2 --- /dev/null +++ b/numpy/typing/tests/data/fail/chararray.pyi @@ -0,0 +1,62 @@ +import numpy as np +from typing import Any + +AR_U: np.chararray[Any, np.dtype[np.str_]] +AR_S: np.chararray[Any, np.dtype[np.bytes_]] + +AR_S.encode() # E: Invalid self argument +AR_U.decode() # E: Invalid self argument + +AR_U.join(b"_") # E: incompatible type +AR_S.join("_") # E: incompatible type + +AR_U.ljust(5, fillchar=b"a") # E: incompatible type +AR_S.ljust(5, fillchar="a") # E: incompatible type +AR_U.rjust(5, fillchar=b"a") # E: incompatible type +AR_S.rjust(5, fillchar="a") # E: incompatible type + +AR_U.lstrip(chars=b"a") # E: incompatible type +AR_S.lstrip(chars="a") # E: incompatible type +AR_U.strip(chars=b"a") # E: incompatible type +AR_S.strip(chars="a") # E: incompatible type +AR_U.rstrip(chars=b"a") # E: incompatible type +AR_S.rstrip(chars="a") # E: incompatible type + +AR_U.partition(b"a") # E: incompatible type +AR_S.partition("a") # E: incompatible type +AR_U.rpartition(b"a") # E: incompatible type +AR_S.rpartition("a") # E: incompatible type + +AR_U.replace(b"_", b"-") # E: incompatible type +AR_S.replace("_", "-") # E: incompatible type + +AR_U.split(b"_") # E: incompatible type +AR_S.split("_") # E: incompatible type +AR_S.split(1) # E: incompatible type +AR_U.rsplit(b"_") # E: incompatible type +AR_S.rsplit("_") # E: incompatible type + +AR_U.count(b"a", start=[1, 2, 3]) # E: incompatible type +AR_S.count("a", end=9) # E: incompatible type + +AR_U.endswith(b"a", start=[1, 2, 3]) # E: incompatible type +AR_S.endswith("a", end=9) # E: incompatible type +AR_U.startswith(b"a", start=[1, 2, 3]) # E: incompatible type +AR_S.startswith("a", end=9) # E: incompatible type + +AR_U.find(b"a", start=[1, 2, 3]) # E: incompatible type +AR_S.find("a", end=9) # E: incompatible type +AR_U.rfind(b"a", start=[1, 2, 3]) # E: incompatible type +AR_S.rfind("a", end=9) # E: incompatible type + +AR_U.index(b"a", start=[1, 2, 3]) # E: incompatible type +AR_S.index("a", end=9) # E: incompatible type +AR_U.rindex(b"a", start=[1, 2, 3]) # E: incompatible type +AR_S.rindex("a", end=9) # E: incompatible type + +AR_U == AR_S # E: Unsupported operand types +AR_U != AR_S # E: Unsupported operand types +AR_U >= AR_S # E: Unsupported operand types +AR_U <= AR_S # E: Unsupported operand types +AR_U > AR_S # E: Unsupported operand types +AR_U < AR_S # E: Unsupported operand types diff --git a/numpy/typing/tests/data/reveal/chararray.pyi b/numpy/typing/tests/data/reveal/chararray.pyi new file mode 100644 index 000000000..c0a39c92b --- /dev/null +++ b/numpy/typing/tests/data/reveal/chararray.pyi @@ -0,0 +1,129 @@ +import numpy as np +from typing import Any + +AR_U: np.chararray[Any, np.dtype[np.str_]] +AR_S: np.chararray[Any, np.dtype[np.bytes_]] + +reveal_type(AR_U == AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S == AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U != AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S != AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U >= AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S >= AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U <= AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S <= AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U > AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S > AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U < AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S < AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U * 5) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S * [5]) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U % "test") # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S % b"test") # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.capitalize()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.capitalize()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.center(5)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.center([2, 3, 4], b"a")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.encode()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_S.decode()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] + +reveal_type(AR_U.expandtabs()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.expandtabs(tabsize=4)) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.join("_")) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.join([b"_", b""])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.ljust(5)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.ljust([4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.rjust(5)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.rjust([4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.lstrip()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.lstrip(chars=b"_")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.rstrip()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.rstrip(chars=b"_")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.strip()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.strip(chars=b"_")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.partition("\n")) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.partition([b"a", b"b", b"c"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.rpartition("\n")) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.rpartition([b"a", b"b", b"c"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.replace("_", "-")) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.replace([b"_", b""], [b"a", b"b"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.split("_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(AR_S.split(maxsplit=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(AR_U.rsplit("_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(AR_S.rsplit(maxsplit=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] + +reveal_type(AR_U.splitlines()) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(AR_S.splitlines(keepends=[True, True, False])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] + +reveal_type(AR_U.swapcase()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.swapcase()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.title()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.title()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.upper()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.upper()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.zfill(5)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_S.zfill([2, 3, 4])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(AR_U.count("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_S.count([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] + +reveal_type(AR_U.endswith("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.endswith([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.startswith("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.startswith([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U.find("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_S.find([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_U.rfind("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_S.rfind([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] + +reveal_type(AR_U.index("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_S.index([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_U.rindex("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_S.rindex([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] + +reveal_type(AR_U.isalpha()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.isalpha()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U.isalnum()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.isalnum()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U.isdecimal()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.isdecimal()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U.isdigit()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.isdigit()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U.islower()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.islower()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U.isnumeric()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.isnumeric()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U.isspace()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.isspace()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U.istitle()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.istitle()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] + +reveal_type(AR_U.isupper()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_S.isupper()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -- cgit v1.2.1 From 92e89be3e3e2bd2cb4b8cd0eb2568d7331649480 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Fri, 22 Oct 2021 17:29:40 -0600 Subject: MAINT: Restore KnowFailureTest This is for backwards compatibility in the soon to be removed nose support, so leave it in for a bit. --- numpy/testing/_private/noseclasses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/testing/_private/noseclasses.py b/numpy/testing/_private/noseclasses.py index a72ddbe46..48fa4dc1f 100644 --- a/numpy/testing/_private/noseclasses.py +++ b/numpy/testing/_private/noseclasses.py @@ -16,7 +16,7 @@ from nose.plugins.errorclass import ErrorClass, ErrorClassPlugin from nose.plugins.base import Plugin from nose.util import src from .nosetester import get_package_name -from .utils import KnownFailureException +from .utils import KnownFailureException, KnownFailureTest # Some of the classes in this module begin with 'Numpy' to clearly distinguish -- cgit v1.2.1 From 917ec51e69053a29ccb30999ee0a6e2712582395 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Fri, 22 Oct 2021 21:29:50 -0600 Subject: STY: Run clang-format on cpp files and headers. This reformats the new files that came in with the use of templates together with one already present file. The relevant files are - numpy/core/src/common/numpy_tag.h - numpy/core/src/npysort/radixsort.cpp - numpy/core/src/umath/clip.cpp - numpy/core/src/umath/clip.h - numpy/core/tests/data/generate_umath_validation_data.cpp --- numpy/core/src/common/numpy_tag.h | 94 +++++-- numpy/core/src/npysort/radixsort.cpp | 236 ++++++++++++----- numpy/core/src/umath/clip.cpp | 291 ++++++++++++++++----- numpy/core/src/umath/clip.h | 80 ++++-- .../tests/data/generate_umath_validation_data.cpp | 137 +++++----- 5 files changed, 593 insertions(+), 245 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/common/numpy_tag.h b/numpy/core/src/common/numpy_tag.h index a2269d3c7..dc8d5286b 100644 --- a/numpy/core/src/common/numpy_tag.h +++ b/numpy/core/src/common/numpy_tag.h @@ -3,32 +3,76 @@ namespace npy { - struct integral_tag {}; - struct floating_point_tag {}; - struct complex_tag {}; - struct date_tag {}; +struct integral_tag { +}; +struct floating_point_tag { +}; +struct complex_tag { +}; +struct date_tag { +}; - struct bool_tag : integral_tag { using type = npy_bool; }; - struct byte_tag : integral_tag {using type = npy_byte; } ; - struct ubyte_tag : integral_tag {using type = npy_ubyte; } ; - struct short_tag : integral_tag {using type = npy_short; } ; - struct ushort_tag : integral_tag {using type = npy_ushort; } ; - struct int_tag : integral_tag {using type = npy_int; } ; - struct uint_tag : integral_tag {using type = npy_uint; } ; - struct long_tag : integral_tag {using type = npy_long ; } ; - struct ulong_tag : integral_tag {using type = npy_ulong ; } ; - struct longlong_tag : integral_tag {using type = npy_longlong ; } ; - struct ulonglong_tag : integral_tag {using type = npy_ulonglong ; } ; - struct half_tag {using type = npy_half ; } ; - struct float_tag : floating_point_tag {using type = npy_float ; } ; - struct double_tag : floating_point_tag {using type = npy_double ; } ; - struct longdouble_tag : floating_point_tag {using type = npy_longdouble ; } ; - struct cfloat_tag : complex_tag {using type = npy_cfloat ; } ; - struct cdouble_tag : complex_tag {using type = npy_cdouble ; } ; - struct clongdouble_tag : complex_tag {using type = npy_clongdouble ; } ; - struct datetime_tag : date_tag {using type = npy_datetime ; } ; - struct timedelta_tag : date_tag {using type = npy_timedelta ; } ; +struct bool_tag : integral_tag { + using type = npy_bool; +}; +struct byte_tag : integral_tag { + using type = npy_byte; +}; +struct ubyte_tag : integral_tag { + using type = npy_ubyte; +}; +struct short_tag : integral_tag { + using type = npy_short; +}; +struct ushort_tag : integral_tag { + using type = npy_ushort; +}; +struct int_tag : integral_tag { + using type = npy_int; +}; +struct uint_tag : integral_tag { + using type = npy_uint; +}; +struct long_tag : integral_tag { + using type = npy_long; +}; +struct ulong_tag : integral_tag { + using type = npy_ulong; +}; +struct longlong_tag : integral_tag { + using type = npy_longlong; +}; +struct ulonglong_tag : integral_tag { + using type = npy_ulonglong; +}; +struct half_tag { + using type = npy_half; +}; +struct float_tag : floating_point_tag { + using type = npy_float; +}; +struct double_tag : floating_point_tag { + using type = npy_double; +}; +struct longdouble_tag : floating_point_tag { + using type = npy_longdouble; +}; +struct cfloat_tag : complex_tag { + using type = npy_cfloat; +}; +struct cdouble_tag : complex_tag { + using type = npy_cdouble; +}; +struct clongdouble_tag : complex_tag { + using type = npy_clongdouble; +}; +struct datetime_tag : date_tag { + using type = npy_datetime; +}; +struct timedelta_tag : date_tag { + using type = npy_timedelta; +}; -} +} // namespace npy #endif diff --git a/numpy/core/src/npysort/radixsort.cpp b/numpy/core/src/npysort/radixsort.cpp index 502613689..017ea43b6 100644 --- a/numpy/core/src/npysort/radixsort.cpp +++ b/numpy/core/src/npysort/radixsort.cpp @@ -2,9 +2,9 @@ #include "npy_sort.h" #include "npysort_common.h" + #include "../common/numpy_tag.h" #include - #include /* @@ -13,39 +13,44 @@ ***************************************************************************** */ - // Reference: https://github.com/eloj/radix-sorting#-key-derivation -template -T KEY_OF(T x) { - // Floating-point is currently disabled. - // Floating-point tests succeed for double and float on macOS but not on Windows/Linux. - // Basic sorting tests succeed but others relying on sort fail. - // Possibly related to floating-point normalisation or multiple NaN reprs? Not sure. - if(std::is_floating_point::value) { - // For floats, we invert the key if the sign bit is set, else we invert the sign bit. - return ((x) ^ (-((x) >> (sizeof(T) * 8 - 1)) | ((T)1 << (sizeof(T) * 8 - 1)))); - } - else if(std::is_signed::value) { - // For signed ints, we flip the sign bit so the negatives are below the positives. - return ((x) ^ ((T)1 << (sizeof(T) * 8 - 1))); - } - else { - return x; - } -} - - -template +template +T +KEY_OF(T x) +{ + // Floating-point is currently disabled. + // Floating-point tests succeed for double and float on macOS but not on + // Windows/Linux. Basic sorting tests succeed but others relying on sort + // fail. Possibly related to floating-point normalisation or multiple NaN + // reprs? Not sure. + if (std::is_floating_point::value) { + // For floats, we invert the key if the sign bit is set, else we invert + // the sign bit. + return ((x) ^ (-((x) >> (sizeof(T) * 8 - 1)) | + ((T)1 << (sizeof(T) * 8 - 1)))); + } + else if (std::is_signed::value) { + // For signed ints, we flip the sign bit so the negatives are below the + // positives. + return ((x) ^ ((T)1 << (sizeof(T) * 8 - 1))); + } + else { + return x; + } +} + +template static inline npy_ubyte -nth_byte(T key, npy_intp l) { +nth_byte(T key, npy_intp l) +{ return (key >> (l << 3)) & 0xFF; } -template -static T* +template +static T * radixsort0(T *start, T *aux, npy_intp num) { - npy_intp cnt[sizeof(T)][1 << 8] = { { 0 } }; + npy_intp cnt[sizeof(T)][1 << 8] = {{0}}; T key0 = KEY_OF(start[0]); for (npy_intp i = 0; i < num; i++) { @@ -59,8 +64,8 @@ radixsort0(T *start, T *aux, npy_intp num) size_t ncols = 0; npy_ubyte cols[sizeof(T)]; for (size_t l = 0; l < sizeof(T); l++) { - if (cnt[l][nth_byte(key0, l)] != num) { - cols[ncols++] = l; + if (cnt[l][nth_byte(key0, l)] != num) { + cols[ncols++] = l; } } @@ -74,7 +79,7 @@ radixsort0(T *start, T *aux, npy_intp num) } for (size_t l = 0; l < ncols; l++) { - T* temp; + T *temp; for (npy_intp i = 0; i < num; i++) { T k = KEY_OF(start[i]); npy_intp dst = cnt[cols[l]][nth_byte(k, cols[l])]++; @@ -89,11 +94,10 @@ radixsort0(T *start, T *aux, npy_intp num) return start; } -template +template static int radixsort_(T *start, npy_intp num) { - if (num < 2) { return 0; } @@ -113,7 +117,7 @@ radixsort_(T *start, npy_intp num) return 0; } - T* aux = (T*)malloc(num * sizeof(T)); + T *aux = (T *)malloc(num * sizeof(T)); if (aux == nullptr) { return -NPY_ENOMEM; } @@ -127,17 +131,18 @@ radixsort_(T *start, npy_intp num) return 0; } -template +template static int -radixsort(void *start, npy_intp num) { - return radixsort_((T*)start, num); +radixsort(void *start, npy_intp num) +{ + return radixsort_((T *)start, num); } -template -static npy_intp* +template +static npy_intp * aradixsort0(T *start, npy_intp *aux, npy_intp *tosort, npy_intp num) { - npy_intp cnt[sizeof(T)][1 << 8] = { { 0 } }; + npy_intp cnt[sizeof(T)][1 << 8] = {{0}}; T key0 = KEY_OF(start[0]); for (npy_intp i = 0; i < num; i++) { @@ -166,7 +171,7 @@ aradixsort0(T *start, npy_intp *aux, npy_intp *tosort, npy_intp num) } for (size_t l = 0; l < ncols; l++) { - npy_intp* temp; + npy_intp *temp; for (npy_intp i = 0; i < num; i++) { T k = KEY_OF(start[tosort[i]]); npy_intp dst = cnt[cols[l]][nth_byte(k, cols[l])]++; @@ -181,9 +186,9 @@ aradixsort0(T *start, npy_intp *aux, npy_intp *tosort, npy_intp num) return tosort; } -template +template static int -aradixsort_(T *start, npy_intp* tosort, npy_intp num) +aradixsort_(T *start, npy_intp *tosort, npy_intp num) { npy_intp *sorted; npy_intp *aux; @@ -208,7 +213,7 @@ aradixsort_(T *start, npy_intp* tosort, npy_intp num) return 0; } - aux = (npy_intp*)malloc(num * sizeof(npy_intp)); + aux = (npy_intp *)malloc(num * sizeof(npy_intp)); if (aux == NULL) { return -NPY_ENOMEM; } @@ -222,33 +227,128 @@ aradixsort_(T *start, npy_intp* tosort, npy_intp num) return 0; } -template +template static int -aradixsort(void *start, npy_intp* tosort, npy_intp num) { - return aradixsort_((T*)start, tosort, num); +aradixsort(void *start, npy_intp *tosort, npy_intp num) +{ + return aradixsort_((T *)start, tosort, num); } extern "C" { -NPY_NO_EXPORT int radixsort_bool(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_byte(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_ubyte(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_short(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_ushort(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_int(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_uint(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_long(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_ulong(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_longlong(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int radixsort_ulonglong(void* vec, npy_intp cnt, void *NPY_UNUSED(null)) { return radixsort(vec, cnt); } -NPY_NO_EXPORT int aradixsort_bool(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_byte(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_ubyte(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_short(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_ushort(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_int(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_uint(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_long(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_ulong(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_longlong(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } -NPY_NO_EXPORT int aradixsort_ulonglong(void* vec, npy_intp* ind, npy_intp cnt, void *NPY_UNUSED(null)) { return aradixsort(vec, ind, cnt); } +NPY_NO_EXPORT int +radixsort_bool(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_byte(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_ubyte(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_short(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_ushort(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_int(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_uint(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_long(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_ulong(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_longlong(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +radixsort_ulonglong(void *vec, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return radixsort(vec, cnt); +} +NPY_NO_EXPORT int +aradixsort_bool(void *vec, npy_intp *ind, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_byte(void *vec, npy_intp *ind, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_ubyte(void *vec, npy_intp *ind, npy_intp cnt, + void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_short(void *vec, npy_intp *ind, npy_intp cnt, + void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_ushort(void *vec, npy_intp *ind, npy_intp cnt, + void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_int(void *vec, npy_intp *ind, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_uint(void *vec, npy_intp *ind, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_long(void *vec, npy_intp *ind, npy_intp cnt, void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_ulong(void *vec, npy_intp *ind, npy_intp cnt, + void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_longlong(void *vec, npy_intp *ind, npy_intp cnt, + void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} +NPY_NO_EXPORT int +aradixsort_ulonglong(void *vec, npy_intp *ind, npy_intp cnt, + void *NPY_UNUSED(null)) +{ + return aradixsort(vec, ind, cnt); +} } diff --git a/numpy/core/src/umath/clip.cpp b/numpy/core/src/umath/clip.cpp index 830d3a39c..19d05c848 100644 --- a/numpy/core/src/umath/clip.cpp +++ b/numpy/core/src/umath/clip.cpp @@ -8,124 +8,275 @@ #define PY_SSIZE_T_CLEAN #include - #include "numpy/halffloat.h" -#include "numpy/npy_math.h" #include "numpy/ndarraytypes.h" #include "numpy/npy_common.h" +#include "numpy/npy_math.h" #include "numpy/utils.h" + #include "fast_loop_macros.h" -#include "../common/numpy_tag.h" +#include "../common/numpy_tag.h" -template -T _NPY_MIN(T a, T b, npy::integral_tag const &) { return PyArray_MIN(a, b); } -template -T _NPY_MAX(T a, T b, npy::integral_tag const &) { return PyArray_MAX(a, b); } +template +T +_NPY_MIN(T a, T b, npy::integral_tag const &) +{ + return PyArray_MIN(a, b); +} +template +T +_NPY_MAX(T a, T b, npy::integral_tag const &) +{ + return PyArray_MAX(a, b); +} -npy_half _NPY_MIN(npy_half a, npy_half b, npy::half_tag const &) { return npy_half_isnan(a) || npy_half_le(a, b) ? (a) : (b); } -npy_half _NPY_MAX(npy_half a, npy_half b, npy::half_tag const &) { return npy_half_isnan(a) || npy_half_ge(a, b) ? (a) : (b); } +npy_half +_NPY_MIN(npy_half a, npy_half b, npy::half_tag const &) +{ + return npy_half_isnan(a) || npy_half_le(a, b) ? (a) : (b); +} +npy_half +_NPY_MAX(npy_half a, npy_half b, npy::half_tag const &) +{ + return npy_half_isnan(a) || npy_half_ge(a, b) ? (a) : (b); +} -template -T _NPY_MIN(T a, T b, npy::floating_point_tag const &) { return npy_isnan(a) ? (a) : PyArray_MIN(a, b); } -template -T _NPY_MAX(T a, T b, npy::floating_point_tag const &) { return npy_isnan(a) ? (a) : PyArray_MAX(a, b); } +template +T +_NPY_MIN(T a, T b, npy::floating_point_tag const &) +{ + return npy_isnan(a) ? (a) : PyArray_MIN(a, b); +} +template +T +_NPY_MAX(T a, T b, npy::floating_point_tag const &) +{ + return npy_isnan(a) ? (a) : PyArray_MAX(a, b); +} -template -T _NPY_MIN(T a, T b, npy::complex_tag const &) { return npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CLT(a, b) ? (a) : (b); } -template -T _NPY_MAX(T a, T b, npy::complex_tag const &) { return npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CGT(a, b) ? (a) : (b); } +template +T +_NPY_MIN(T a, T b, npy::complex_tag const &) +{ + return npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CLT(a, b) + ? (a) + : (b); +} +template +T +_NPY_MAX(T a, T b, npy::complex_tag const &) +{ + return npy_isnan((a).real) || npy_isnan((a).imag) || PyArray_CGT(a, b) + ? (a) + : (b); +} -template -T _NPY_MIN(T a, T b, npy::date_tag const &) { - return (a) == NPY_DATETIME_NAT - ? (a) - : (b) == NPY_DATETIME_NAT ? (b) : (a) < (b) ? (a) : (b); +template +T +_NPY_MIN(T a, T b, npy::date_tag const &) +{ + return (a) == NPY_DATETIME_NAT ? (a) + : (b) == NPY_DATETIME_NAT ? (b) + : (a) < (b) ? (a) + : (b); } -template -T _NPY_MAX(T a, T b, npy::date_tag const &) { - return (a) == NPY_DATETIME_NAT - ? (a) - : (b) == NPY_DATETIME_NAT ? (b) : (a) > (b) ? (a) : (b); +template +T +_NPY_MAX(T a, T b, npy::date_tag const &) +{ + return (a) == NPY_DATETIME_NAT ? (a) + : (b) == NPY_DATETIME_NAT ? (b) + : (a) > (b) ? (a) + : (b); } /* generic dispatcher */ -template -T _NPY_MIN(T const& a, T const& b) { - return _NPY_MIN(a, b, Tag{}); +template +T +_NPY_MIN(T const &a, T const &b) +{ + return _NPY_MIN(a, b, Tag{}); } -template -T _NPY_MAX(T const& a, T const& b) { - return _NPY_MAX(a, b, Tag{}); +template +T +_NPY_MAX(T const &a, T const &b) +{ + return _NPY_MAX(a, b, Tag{}); } - -template -T _NPY_CLIP(T x, T min, T max) { +template +T +_NPY_CLIP(T x, T min, T max) +{ return _NPY_MIN(_NPY_MAX((x), (min)), (max)); } -template +template static void _npy_clip_(T **args, npy_intp const *dimensions, npy_intp const *steps) { npy_intp n = dimensions[0]; if (steps[1] == 0 && steps[2] == 0) { - /* min and max are constant throughout the loop, the most common case */ + /* min and max are constant throughout the loop, the most common case + */ /* NOTE: it may be possible to optimize these checks for nan */ T min_val = *args[1]; T max_val = *args[2]; T *ip1 = args[0], *op1 = args[3]; - npy_intp is1 = steps[0]/sizeof(T), os1 = steps[3]/sizeof(T); + npy_intp is1 = steps[0] / sizeof(T), os1 = steps[3] / sizeof(T); /* contiguous, branch to let the compiler optimize */ if (is1 == 1 && os1 == 1) { - for(npy_intp i = 0; i < n; i++, ip1++, op1++) { + for (npy_intp i = 0; i < n; i++, ip1++, op1++) { *op1 = _NPY_CLIP(*ip1, min_val, max_val); } } else { - for(npy_intp i = 0; i < n; i++, ip1 += is1, op1 += os1) { + for (npy_intp i = 0; i < n; i++, ip1 += is1, op1 += os1) { *op1 = _NPY_CLIP(*ip1, min_val, max_val); } } } else { - T *ip1 = args[0], *ip2 = args[1], *ip3 = args[2], *op1 = args[3]; - npy_intp is1 = steps[0]/sizeof(T), is2 = steps[1]/sizeof(T), is3 = steps[2]/sizeof(T), os1 = steps[3]/sizeof(T); - for(npy_intp i = 0; i < n; i++, ip1 += is1, ip2 += is2, ip3 += is3, op1 += os1) - *op1 = _NPY_CLIP(*ip1, *ip2, *ip3); + T *ip1 = args[0], *ip2 = args[1], *ip3 = args[2], *op1 = args[3]; + npy_intp is1 = steps[0] / sizeof(T), is2 = steps[1] / sizeof(T), + is3 = steps[2] / sizeof(T), os1 = steps[3] / sizeof(T); + for (npy_intp i = 0; i < n; + i++, ip1 += is1, ip2 += is2, ip3 += is3, op1 += os1) + *op1 = _NPY_CLIP(*ip1, *ip2, *ip3); } - npy_clear_floatstatus_barrier((char*)dimensions); + npy_clear_floatstatus_barrier((char *)dimensions); } -template +template static void -_npy_clip(char **args, npy_intp const *dimensions, npy_intp const *steps) { - using T = typename Tag::type; - return _npy_clip_((T**)args, dimensions, steps); +_npy_clip(char **args, npy_intp const *dimensions, npy_intp const *steps) +{ + using T = typename Tag::type; + return _npy_clip_((T **)args, dimensions, steps); } extern "C" { -NPY_NO_EXPORT void BOOL_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void BYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void UBYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void SHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void USHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void INT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void UINT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void LONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void ULONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void LONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void ULONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void HALF_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void FLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void DOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void LONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void CFLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void CDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void CLONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void DATETIME_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } -NPY_NO_EXPORT void TIMEDELTA_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) { return _npy_clip(args, dimensions, steps); } +NPY_NO_EXPORT void +BOOL_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +BYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +UBYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +SHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +USHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +INT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +UINT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +LONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +ULONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +LONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +ULONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +HALF_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +FLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +DOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +LONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +CFLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +CDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +CLONGDOUBLE_clip(char **args, npy_intp const *dimensions, + npy_intp const *steps, void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +DATETIME_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} +NPY_NO_EXPORT void +TIMEDELTA_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)) +{ + return _npy_clip(args, dimensions, steps); +} } diff --git a/numpy/core/src/umath/clip.h b/numpy/core/src/umath/clip.h index 66b3f5c39..f69ebd1e3 100644 --- a/numpy/core/src/umath/clip.h +++ b/numpy/core/src/umath/clip.h @@ -5,26 +5,66 @@ extern "C" { #endif -NPY_NO_EXPORT void BOOL_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void BYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void UBYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void SHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void USHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void INT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void UINT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void LONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void ULONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void LONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void ULONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void HALF_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void FLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void DOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void LONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void CFLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void CDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void CLONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void DATETIME_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); -NPY_NO_EXPORT void TIMEDELTA_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +BOOL_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +BYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +UBYTE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +SHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +USHORT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +INT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +UINT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +LONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +ULONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +LONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +ULONGLONG_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +HALF_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +FLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +DOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +LONGDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +CFLOAT_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +CDOUBLE_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +CLONGDOUBLE_clip(char **args, npy_intp const *dimensions, + npy_intp const *steps, void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +DATETIME_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); +NPY_NO_EXPORT void +TIMEDELTA_clip(char **args, npy_intp const *dimensions, npy_intp const *steps, + void *NPY_UNUSED(func)); #ifdef __cplusplus } diff --git a/numpy/core/tests/data/generate_umath_validation_data.cpp b/numpy/core/tests/data/generate_umath_validation_data.cpp index 9d97ff4ab..418eae670 100644 --- a/numpy/core/tests/data/generate_umath_validation_data.cpp +++ b/numpy/core/tests/data/generate_umath_validation_data.cpp @@ -1,41 +1,46 @@ -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include struct ufunc { std::string name; - double (*f32func) (double); - long double (*f64func) (long double); + double (*f32func)(double); + long double (*f64func)(long double); float f32ulp; float f64ulp; }; -template -T RandomFloat(T a, T b) { - T random = ((T) rand()) / (T) RAND_MAX; +template +T +RandomFloat(T a, T b) +{ + T random = ((T)rand()) / (T)RAND_MAX; T diff = b - a; T r = random * diff; return a + r; } -template -void append_random_array(std::vector& arr, T min, T max, size_t N) +template +void +append_random_array(std::vector &arr, T min, T max, size_t N) { for (size_t ii = 0; ii < N; ++ii) arr.emplace_back(RandomFloat(min, max)); } -template -std::vector computeTrueVal(const std::vector& in, T2(*mathfunc)(T2)) { +template +std::vector +computeTrueVal(const std::vector &in, T2 (*mathfunc)(T2)) +{ std::vector out; for (T1 elem : in) { - T2 elem_d = (T2) elem; - T1 out_elem = (T1) mathfunc(elem_d); + T2 elem_d = (T2)elem; + T1 out_elem = (T1)mathfunc(elem_d); out.emplace_back(out_elem); } return out; @@ -49,17 +54,20 @@ std::vector computeTrueVal(const std::vector& in, T2(*mathfunc)(T2)) { #define MINDEN std::numeric_limits::denorm_min() #define MINFLT std::numeric_limits::min() #define MAXFLT std::numeric_limits::max() -#define INF std::numeric_limits::infinity() -#define qNAN std::numeric_limits::quiet_NaN() -#define sNAN std::numeric_limits::signaling_NaN() +#define INF std::numeric_limits::infinity() +#define qNAN std::numeric_limits::quiet_NaN() +#define sNAN std::numeric_limits::signaling_NaN() -template -std::vector generate_input_vector(std::string func) { - std::vector input = {MINDEN, -MINDEN, MINFLT, -MINFLT, MAXFLT, -MAXFLT, - INF, -INF, qNAN, sNAN, -1.0, 1.0, 0.0, -0.0}; +template +std::vector +generate_input_vector(std::string func) +{ + std::vector input = {MINDEN, -MINDEN, MINFLT, -MINFLT, MAXFLT, + -MAXFLT, INF, -INF, qNAN, sNAN, + -1.0, 1.0, 0.0, -0.0}; // [-1.0, 1.0] - if ((func == "arcsin") || (func == "arccos") || (func == "arctanh")){ + if ((func == "arcsin") || (func == "arccos") || (func == "arctanh")) { append_random_array(input, -1.0, 1.0, 700); } // (0.0, INF] @@ -98,57 +106,62 @@ std::vector generate_input_vector(std::string func) { return input; } -int main() { - srand (42); +int +main() +{ + srand(42); std::vector umathfunc = { - {"sin",sin,sin,2.37,3.3}, - {"cos",cos,cos,2.36,3.38}, - {"tan",tan,tan,3.91,3.93}, - {"arcsin",asin,asin,3.12,2.55}, - {"arccos",acos,acos,2.1,1.67}, - {"arctan",atan,atan,2.3,2.52}, - {"sinh",sinh,sinh,1.55,1.89}, - {"cosh",cosh,cosh,2.48,1.97}, - {"tanh",tanh,tanh,1.38,1.19}, - {"arcsinh",asinh,asinh,1.01,1.48}, - {"arccosh",acosh,acosh,1.16,1.05}, - {"arctanh",atanh,atanh,1.45,1.46}, - {"cbrt",cbrt,cbrt,1.94,1.82}, - //{"exp",exp,exp,3.76,1.53}, - {"exp2",exp2,exp2,1.01,1.04}, - {"expm1",expm1,expm1,2.62,2.1}, - //{"log",log,log,1.84,1.67}, - {"log10",log10,log10,3.5,1.92}, - {"log1p",log1p,log1p,1.96,1.93}, - {"log2",log2,log2,2.12,1.84}, + {"sin", sin, sin, 2.37, 3.3}, + {"cos", cos, cos, 2.36, 3.38}, + {"tan", tan, tan, 3.91, 3.93}, + {"arcsin", asin, asin, 3.12, 2.55}, + {"arccos", acos, acos, 2.1, 1.67}, + {"arctan", atan, atan, 2.3, 2.52}, + {"sinh", sinh, sinh, 1.55, 1.89}, + {"cosh", cosh, cosh, 2.48, 1.97}, + {"tanh", tanh, tanh, 1.38, 1.19}, + {"arcsinh", asinh, asinh, 1.01, 1.48}, + {"arccosh", acosh, acosh, 1.16, 1.05}, + {"arctanh", atanh, atanh, 1.45, 1.46}, + {"cbrt", cbrt, cbrt, 1.94, 1.82}, + //{"exp",exp,exp,3.76,1.53}, + {"exp2", exp2, exp2, 1.01, 1.04}, + {"expm1", expm1, expm1, 2.62, 2.1}, + //{"log",log,log,1.84,1.67}, + {"log10", log10, log10, 3.5, 1.92}, + {"log1p", log1p, log1p, 1.96, 1.93}, + {"log2", log2, log2, 2.12, 1.84}, }; for (int ii = 0; ii < umathfunc.size(); ++ii) { - // ignore sin/cos + // ignore sin/cos if ((umathfunc[ii].name != "sin") && (umathfunc[ii].name != "cos")) { - std::string fileName = "umath-validation-set-" + umathfunc[ii].name + ".csv"; + std::string fileName = + "umath-validation-set-" + umathfunc[ii].name + ".csv"; std::ofstream txtOut; - txtOut.open (fileName, std::ofstream::trunc); + txtOut.open(fileName, std::ofstream::trunc); txtOut << "dtype,input,output,ulperrortol" << std::endl; // Single Precision auto f32in = generate_input_vector(umathfunc[ii].name); - auto f32out = computeTrueVal(f32in, umathfunc[ii].f32func); + auto f32out = computeTrueVal(f32in, + umathfunc[ii].f32func); for (int jj = 0; jj < f32in.size(); ++jj) { - txtOut << "np.float32" << std::hex << - ",0x" << *reinterpret_cast(&f32in[jj]) << - ",0x" << *reinterpret_cast(&f32out[jj]) << - "," << ceil(umathfunc[ii].f32ulp) << std::endl; + txtOut << "np.float32" << std::hex << ",0x" + << *reinterpret_cast(&f32in[jj]) << ",0x" + << *reinterpret_cast(&f32out[jj]) << "," + << ceil(umathfunc[ii].f32ulp) << std::endl; } // Double Precision auto f64in = generate_input_vector(umathfunc[ii].name); - auto f64out = computeTrueVal(f64in, umathfunc[ii].f64func); + auto f64out = computeTrueVal( + f64in, umathfunc[ii].f64func); for (int jj = 0; jj < f64in.size(); ++jj) { - txtOut << "np.float64" << std::hex << - ",0x" << *reinterpret_cast(&f64in[jj]) << - ",0x" << *reinterpret_cast(&f64out[jj]) << - "," << ceil(umathfunc[ii].f64ulp) << std::endl; + txtOut << "np.float64" << std::hex << ",0x" + << *reinterpret_cast(&f64in[jj]) << ",0x" + << *reinterpret_cast(&f64out[jj]) << "," + << ceil(umathfunc[ii].f64ulp) << std::endl; } txtOut.close(); } -- cgit v1.2.1 From 437a249250248b2cbf7acd8a2a0f836e3b98a547 Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Sat, 23 Oct 2021 07:55:22 +0200 Subject: TST, SIMD: Enhance the test case for SIMD integer division by adding several data ranges --- numpy/core/tests/test_simd.py | 75 +++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 50 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_simd.py b/numpy/core/tests/test_simd.py index f0c60953b..0270ad901 100644 --- a/numpy/core/tests/test_simd.py +++ b/numpy/core/tests/test_simd.py @@ -329,7 +329,7 @@ class _SIMD_FP(_Test_Utility): data_square = [x*x for x in data] square = self.square(vdata) assert square == data_square - + def test_max(self): """ Test intrinsics: @@ -818,6 +818,7 @@ class _SIMD_ALL(_Test_Utility): if self._is_fp(): return + int_min = self._int_min() def trunc_div(a, d): """ Divide towards zero works with large integers > 2^53, @@ -830,57 +831,31 @@ class _SIMD_ALL(_Test_Utility): return a // d return (a + sign_d - sign_a) // d + 1 - int_min = self._int_min() if self._is_signed() else 1 - int_max = self._int_max() - rdata = ( - 0, 1, self.nlanes, int_max-self.nlanes, - int_min, int_min//2 + 1 - ) - divisors = (1, 2, 9, 13, self.nlanes, int_min, int_max, int_max//2) - - for x, d in itertools.product(rdata, divisors): - data = self._data(x) - vdata = self.load(data) - data_divc = [trunc_div(a, d) for a in data] - divisor = self.divisor(d) - divc = self.divc(vdata, divisor) - assert divc == data_divc - - if not self._is_signed(): - return - - safe_neg = lambda x: -x-1 if -x > int_max else -x - # test round division for signed integers - for x, d in itertools.product(rdata, divisors): - d_neg = safe_neg(d) - data = self._data(x) - data_neg = [safe_neg(a) for a in data] - vdata = self.load(data) - vdata_neg = self.load(data_neg) - divisor = self.divisor(d) - divisor_neg = self.divisor(d_neg) - - # round towards zero - data_divc = [trunc_div(a, d_neg) for a in data] - divc = self.divc(vdata, divisor_neg) - assert divc == data_divc - data_divc = [trunc_div(a, d) for a in data_neg] - divc = self.divc(vdata_neg, divisor) + data = [1, -int_min] # to test overflow + data += range(0, 2**8, 2**5) + data += range(0, 2**8, 2**5-1) + bsize = self._scalar_size() + if bsize > 8: + data += range(2**8, 2**16, 2**13) + data += range(2**8, 2**16, 2**13-1) + if bsize > 16: + data += range(2**16, 2**32, 2**29) + data += range(2**16, 2**32, 2**29-1) + if bsize > 32: + data += range(2**32, 2**64, 2**61) + data += range(2**32, 2**64, 2**61-1) + # negate + data += [-x for x in data] + for dividend, divisor in itertools.product(data, data): + divisor = self.setall(divisor)[0] # cast + if divisor == 0: + continue + dividend = self.load(self._data(dividend)) + data_divc = [trunc_div(a, divisor) for a in dividend] + divisor_parms = self.divisor(divisor) + divc = self.divc(dividend, divisor_parms) assert divc == data_divc - # test truncate sign if the dividend is zero - vzero = self.zero() - for d in (-1, -10, -100, int_min//2, int_min): - divisor = self.divisor(d) - divc = self.divc(vzero, divisor) - assert divc == vzero - - # test overflow - vmin = self.setall(int_min) - divisor = self.divisor(-1) - divc = self.divc(vmin, divisor) - assert divc == vmin - def test_arithmetic_reduce_sum(self): """ Test reduce sum intrinsics: -- cgit v1.2.1 From 4682087b93294a90f0e540ab0d8fd7e6e5afdd09 Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Sat, 23 Oct 2021 07:56:29 +0200 Subject: BUG, SIMD: Fix unsigned 8-bit integer divison by a scalar Same as 64-bit divistion, except quad wide divison leads to inaccurate multiplier in certain cases when the log2 of the divisor overflow to zero. --- numpy/core/src/common/simd/intdiv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/common/simd/intdiv.h b/numpy/core/src/common/simd/intdiv.h index c787c7a7d..a7a461721 100644 --- a/numpy/core/src/common/simd/intdiv.h +++ b/numpy/core/src/common/simd/intdiv.h @@ -201,7 +201,7 @@ NPY_FINLINE npyv_u8x3 npyv_divisor_u8(npy_uint8 d) default: l = npyv__bitscan_revnz_u32(d - 1) + 1; // ceil(log2(d)) l2 = (npy_uint8)(1 << l); // 2^l, overflow to 0 if l = 8 - m = ((l2 - d) << 8) / d + 1; // multiplier + m = ((npy_uint16)((l2 - d) << 8)) / d + 1; // multiplier sh1 = 1; sh2 = l - 1; // shift counts } npyv_u8x3 divisor; -- cgit v1.2.1 From 45ea1408cbb0ff571624e2ccff45300fd39defc8 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Sat, 23 Oct 2021 18:26:12 +0200 Subject: TST: xfail `test_overrides` when numpy is build with MKL support --- numpy/distutils/tests/test_system_info.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'numpy') diff --git a/numpy/distutils/tests/test_system_info.py b/numpy/distutils/tests/test_system_info.py index b722281ad..8c26271af 100644 --- a/numpy/distutils/tests/test_system_info.py +++ b/numpy/distutils/tests/test_system_info.py @@ -254,6 +254,10 @@ class TestSystemInfoReading: finally: os.chdir(previousDir) + HAS_MKL = "mkl_rt" in mkl_info().calc_libraries_info().get("libraries", []) + + @pytest.mark.xfail(HAS_MKL, reason=("`[DEFAULT]` override doesn't work if " + "numpy is built with MKL support")) def test_overrides(self): previousDir = os.getcwd() cfg = os.path.join(self._dir1, 'site.cfg') -- cgit v1.2.1 From 07778e58fbb5bd0e5172a9c1a99c391144ed41de Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Sat, 23 Oct 2021 16:14:43 +0100 Subject: Allow clib callable build flags At the moment, we are guessing whether we have the MSVC compiler, by looking at what Python was originally compiled for. That works only if we are using the same compiler, but this is not the case when we compile with e.g. mingw-w64 using Python.org Python. Unfortunately, at the time we are specifying build flags, we don't know what compiler we are using. Allow build flags to clib to be callables that return lists of strings, instead of strings, where the callables can do work like inspecting the compiler, at build time. Use this to check for MSVC at build time, when specifying the `/GL-` flag. See gh-9977 for a related discussion about these flags. --- numpy/core/setup.py | 17 ++++++++++++----- numpy/distutils/command/build_clib.py | 34 +++++++++++++++++++++++++++++++--- numpy/random/setup.py | 14 +++++++++++++- 3 files changed, 56 insertions(+), 9 deletions(-) (limited to 'numpy') diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 6800f65e9..7bf70ed88 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -696,16 +696,23 @@ def configuration(parent_package='',top_path=None): join('src', 'npymath', 'halffloat.c') ] - # Must be true for CRT compilers but not MinGW/cygwin. See gh-9977. - # Intel and Clang also don't seem happy with /GL - is_msvc = (platform.platform().startswith('Windows') and - platform.python_compiler().startswith('MS')) + def gl_if_msvc(build_cmd): + """ Add flag if we are using MSVC compiler + + We can't see this in our scope, because we have not initialized the + distutils build command, so use this deferred calculation to run when + we are building the library. + """ + if build_cmd.compiler.compiler_type == 'msvc': + return ['/GL-'] + return [] + config.add_installed_library('npymath', sources=npymath_sources + [get_mathlib_info], install_dir='lib', build_info={ 'include_dirs' : [], # empty list required for creating npy_math_internal.h - 'extra_compiler_args' : (['/GL-'] if is_msvc else []), + 'extra_compiler_args': [gl_if_msvc], }) config.add_npy_pkg_config("npymath.ini.in", "lib/npy-pkg-config", subst_dict) diff --git a/numpy/distutils/command/build_clib.py b/numpy/distutils/command/build_clib.py index a481758c1..45201f98f 100644 --- a/numpy/distutils/command/build_clib.py +++ b/numpy/distutils/command/build_clib.py @@ -185,6 +185,30 @@ class build_clib(old_build_clib): for (lib_name, build_info) in libraries: self.build_a_library(build_info, lib_name, libraries) + def assemble_flags(self, in_flags): + """ Assemble flags from flag list + + Parameters + ---------- + in_flags : None or sequence + None corresponds to empty list. Sequence elements can be strings + or callables that return lists of strings. Callable takes `self` as + single parameter. + + Returns + ------- + out_flags : list + """ + if in_flags is None: + return [] + out_flags = [] + for in_flag in in_flags: + if callable(in_flag): + out_flags += in_flag(self) + else: + out_flags.append(in_flag) + return out_flags + def build_a_library(self, build_info, lib_name, libraries): # default compilers compiler = self.compiler @@ -263,9 +287,13 @@ class build_clib(old_build_clib): include_dirs = build_info.get('include_dirs') if include_dirs is None: include_dirs = [] - extra_postargs = build_info.get('extra_compiler_args') or [] - extra_cflags = build_info.get('extra_cflags') or [] - extra_cxxflags = build_info.get('extra_cxxflags') or [] + # Flags can be strings, or callables that return a list of strings. + extra_postargs = self.assemble_flags( + build_info.get('extra_compiler_args')) + extra_cflags = self.assemble_flags( + build_info.get('extra_cflags')) + extra_cxxflags = self.assemble_flags( + build_info.get('extra_cxxflags')) include_dirs.extend(get_numpy_include_dirs()) # where compiled F90 module files are: diff --git a/numpy/random/setup.py b/numpy/random/setup.py index dce9a101e..59a30d2b6 100644 --- a/numpy/random/setup.py +++ b/numpy/random/setup.py @@ -65,12 +65,24 @@ def configuration(parent_package='', top_path=None): 'src/distributions/random_mvhg_marginals.c', 'src/distributions/random_hypergeometric.c', ] + + def gl_if_msvc(build_cmd): + """ Add flag if we are using MSVC compiler + + We can't see this in our scope, because we have not initialized the + distutils build command, so use this deferred calculation to run when + we are building the library. + """ + if build_cmd.compiler.compiler_type == 'msvc': + return ['/GL-'] + return [] + config.add_installed_library('npyrandom', sources=npyrandom_sources, install_dir='lib', build_info={ 'include_dirs' : [], # empty list required for creating npyrandom.h - 'extra_compiler_args' : (['/GL-'] if is_msvc else []), + 'extra_compiler_args': [gl_if_msvc], }) for gen in ['mt19937']: -- cgit v1.2.1 From b05defd1232370a69bce383b3c9cbd6a2a91e76d Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sat, 23 Oct 2021 10:02:31 -0600 Subject: TST: Disable test_partial_iteration_cleanup on 32 bit Windows. The test_partial_iteration_cleanup test fails often with Python 3.9 on windows and it is annoying to always need to check if that is the problem so that it may be ignored when tests fail on CI. --- numpy/core/tests/test_nditer.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'numpy') diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index fbf6da0e1..79f44ef80 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -3128,6 +3128,8 @@ def test_warn_noclose(): assert len(sup.log) == 1 +@pytest.mark.skipif(sys.version_info[:2] == (3, 9) and sys.platform == "win32", + reason="Errors with Python 3.9 on Windows") @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts") @pytest.mark.parametrize(["in_dtype", "buf_dtype"], [("i", "O"), ("O", "i"), # most simple cases -- cgit v1.2.1 From d9bbd60d0f2896d1b1f865e6035dccb12db4b1a0 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Sat, 23 Oct 2021 22:54:21 -0500 Subject: BUG: Do not use nonzero fastpath on unaligned arrays The fast-path does not handle unalgined access, previously only bools had a fast path (and bools are by definition always aligned since they are stored in a single byte/char). Closes gh-19592 --- numpy/core/src/multiarray/item_selection.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index ee66378a9..33d378c2b 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -2398,19 +2398,14 @@ PyArray_CountNonzero(PyArrayObject *self) npy_intp *strideptr, *innersizeptr; NPY_BEGIN_THREADS_DEF; - // Special low-overhead version specific to the boolean/int types dtype = PyArray_DESCR(self); - switch(dtype->kind) { - case 'u': - case 'i': - case 'b': - if (dtype->elsize > 8) { - break; - } - return count_nonzero_int( - PyArray_NDIM(self), PyArray_BYTES(self), PyArray_DIMS(self), - PyArray_STRIDES(self), dtype->elsize - ); + /* Special low-overhead version specific to the boolean/int types */ + if (PyArray_ISALIGNED(self) && ( + PyDataType_ISBOOL(dtype) || PyDataType_ISINTEGER(dtype))) { + return count_nonzero_int( + PyArray_NDIM(self), PyArray_BYTES(self), PyArray_DIMS(self), + PyArray_STRIDES(self), dtype->elsize + ); } nonzero = PyArray_DESCR(self)->f->nonzero; -- cgit v1.2.1 From 8fb3d6508e84d7471600b091e815cc6d19fcf1fe Mon Sep 17 00:00:00 2001 From: "Jonas I. Liechti" Date: Sun, 24 Oct 2021 19:47:59 +0000 Subject: incorporated suggested edit Co-authored-by: Mukulika <60316606+Mukulikaa@users.noreply.github.com> --- numpy/lib/function_base.py | 1 - 1 file changed, 1 deletion(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 392a059b5..e0c056d88 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -663,7 +663,6 @@ def select(condlist, choicelist, default=0): >>> np.select(condlist, choicelist, 42) array([ 0, 1, 2, 42, 16, 25]) - >>> x = np.arange(6) >>> condlist = [x<=4, x>3] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist, 55) -- cgit v1.2.1 From 7c857c850f8502b637fb5fa91120156ff6795593 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Mon, 25 Oct 2021 09:49:55 +0200 Subject: Thin compatibility layer for C/C++ math header Should fix #20180 --- numpy/core/src/npymath/npy_math_private.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'numpy') diff --git a/numpy/core/src/npymath/npy_math_private.h b/numpy/core/src/npymath/npy_math_private.h index d475919b0..7ca0c5ba0 100644 --- a/numpy/core/src/npymath/npy_math_private.h +++ b/numpy/core/src/npymath/npy_math_private.h @@ -19,7 +19,13 @@ #define _NPY_MATH_PRIVATE_H_ #include +#ifdef __cplusplus +#include +using std::isgreater; +using std::isless; +#else #include +#endif #include "npy_config.h" #include "npy_fpmath.h" -- cgit v1.2.1 From 7522da64dd7158169c06f7a6151509e56479e1ea Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 25 Oct 2021 11:41:03 +0200 Subject: MAINT: Move `_SupportsWrite` to the main numpy namespace & parametrize it --- numpy/__init__.pyi | 10 +++++++--- numpy/core/_ufunc_config.pyi | 13 ++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 9a788baa7..451ec87ed 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -283,7 +283,6 @@ from numpy.core._ufunc_config import ( getbufsize as getbufsize, seterrcall as seterrcall, geterrcall as geterrcall, - _SupportsWrite, _ErrKind, _ErrFunc, _ErrDictOptional, @@ -632,6 +631,8 @@ from numpy.matrixlib import ( bmat as bmat, ) +_AnyStr_contra = TypeVar("_AnyStr_contra", str, bytes, contravariant=True) + # Protocol for representing file-like-objects accepted # by `ndarray.tofile` and `fromfile` class _IOProtocol(Protocol): @@ -651,6 +652,9 @@ class _MemMapIOProtocol(Protocol): @property def read(self) -> object: ... +class _SupportsWrite(Protocol[_AnyStr_contra]): + def write(self, s: _AnyStr_contra, /) -> object: ... + __all__: List[str] __path__: List[str] __version__: str @@ -994,7 +998,7 @@ class _ArrayOrScalarCommon: def __eq__(self, other): ... def __ne__(self, other): ... def copy(self: _ArraySelf, order: _OrderKACF = ...) -> _ArraySelf: ... - def dump(self, file: str) -> None: ... + def dump(self, file: str | bytes | os.PathLike[str] | os.PathLike[bytes] | _SupportsWrite[bytes]) -> None: ... def dumps(self) -> bytes: ... def tobytes(self, order: _OrderKACF = ...) -> bytes: ... # NOTE: `tostring()` is deprecated and therefore excluded @@ -3337,7 +3341,7 @@ class AxisError(ValueError, IndexError): @overload def __init__(self, axis: int, ndim: int, msg_prefix: None | str = ...) -> None: ... -_CallType = TypeVar("_CallType", bound=Union[_ErrFunc, _SupportsWrite]) +_CallType = TypeVar("_CallType", bound=Union[_ErrFunc, _SupportsWrite[str]]) class errstate(Generic[_CallType], ContextDecorator): call: _CallType diff --git a/numpy/core/_ufunc_config.pyi b/numpy/core/_ufunc_config.pyi index 9c8cc8ab6..cd7129bcb 100644 --- a/numpy/core/_ufunc_config.pyi +++ b/numpy/core/_ufunc_config.pyi @@ -1,11 +1,10 @@ -from typing import Optional, Union, Callable, Any, Literal, Protocol, TypedDict +from typing import Optional, Union, Callable, Any, Literal, TypedDict + +from numpy import _SupportsWrite _ErrKind = Literal["ignore", "warn", "raise", "call", "print", "log"] _ErrFunc = Callable[[str, int], Any] -class _SupportsWrite(Protocol): - def write(self, msg: str, /) -> Any: ... - class _ErrDict(TypedDict): divide: _ErrKind over: _ErrKind @@ -30,8 +29,8 @@ def geterr() -> _ErrDict: ... def setbufsize(size: int) -> int: ... def getbufsize() -> int: ... def seterrcall( - func: Union[None, _ErrFunc, _SupportsWrite] -) -> Union[None, _ErrFunc, _SupportsWrite]: ... -def geterrcall() -> Union[None, _ErrFunc, _SupportsWrite]: ... + func: Union[None, _ErrFunc, _SupportsWrite[str]] +) -> Union[None, _ErrFunc, _SupportsWrite[str]]: ... +def geterrcall() -> Union[None, _ErrFunc, _SupportsWrite[str]]: ... # See `numpy/__init__.pyi` for the `errstate` class -- cgit v1.2.1 From 6b02a0e637aa7db4f48e9c711310e732bab3813d Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 25 Oct 2021 11:47:19 +0200 Subject: MAINT: Remove redundant `NoReturn` overloads Previously these overloads had the (rather niche) purpose of preventing operations between `bytes`- and `int`-based array-likes, as `bytes` is a subtype of `Sequence[int]`. Interestingly, these overloads have been made redundant by the introduction of the `npt._NestedSequence` protocol for the same reason as numpy/numpy#20037: `bytes` (and `str`) do not fully implement the `collections.abc.Sequence` interface. This "problem" could be fixed by changing the signature of `__contains__`, but in this it seems far more advantageous to leave things "broken" as it is. --- numpy/__init__.pyi | 88 ------------------------------------------------------ 1 file changed, 88 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 451ec87ed..347a66d27 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1803,8 +1803,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): # `Sequence[int]`) and `str`. As `str` is a recursive sequence of # strings, it will pass through the final overload otherwise - @overload - def __lt__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __lt__(self: _ArrayNumber_co, other: _ArrayLikeNumber_co) -> NDArray[bool_]: ... @overload @@ -1816,8 +1814,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __lt__(self: NDArray[Any], other: _ArrayLikeObject_co) -> NDArray[bool_]: ... - @overload - def __le__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __le__(self: _ArrayNumber_co, other: _ArrayLikeNumber_co) -> NDArray[bool_]: ... @overload @@ -1829,8 +1825,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __le__(self: NDArray[Any], other: _ArrayLikeObject_co) -> NDArray[bool_]: ... - @overload - def __gt__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __gt__(self: _ArrayNumber_co, other: _ArrayLikeNumber_co) -> NDArray[bool_]: ... @overload @@ -1842,8 +1836,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __gt__(self: NDArray[Any], other: _ArrayLikeObject_co) -> NDArray[bool_]: ... - @overload - def __ge__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ge__(self: _ArrayNumber_co, other: _ArrayLikeNumber_co) -> NDArray[bool_]: ... @overload @@ -1891,8 +1883,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): # Binary ops # NOTE: `ndarray` does not implement `__imatmul__` @overload - def __matmul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... - @overload def __matmul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload def __matmul__(self: _ArrayUInt_co, other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[Any]]: ... # type: ignore[misc] @@ -1907,8 +1897,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __matmul__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rmatmul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rmatmul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -1924,8 +1912,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rmatmul__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __mod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __mod__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -1941,8 +1927,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __mod__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rmod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rmod__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -1958,8 +1942,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rmod__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __divmod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __divmod__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> _2Tuple[NDArray[int8]]: ... # type: ignore[misc] @overload @@ -1971,8 +1953,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __divmod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... - @overload - def __rdivmod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rdivmod__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> _2Tuple[NDArray[int8]]: ... # type: ignore[misc] @overload @@ -1984,8 +1964,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rdivmod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... - @overload - def __add__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __add__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2007,8 +1985,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __add__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __radd__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __radd__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2030,8 +2006,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __radd__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __sub__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __sub__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NoReturn: ... @overload @@ -2053,8 +2027,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __sub__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rsub__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rsub__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NoReturn: ... @overload @@ -2076,8 +2048,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rsub__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __mul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __mul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2097,8 +2067,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __mul__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rmul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rmul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2118,8 +2086,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rmul__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __floordiv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __floordiv__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2139,8 +2105,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __floordiv__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rfloordiv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rfloordiv__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2160,8 +2124,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rfloordiv__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __pow__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __pow__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2177,8 +2139,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __pow__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rpow__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rpow__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2194,8 +2154,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rpow__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __truediv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __truediv__(self: _ArrayInt_co, other: _ArrayInt_co) -> NDArray[float64]: ... # type: ignore[misc] @overload @@ -2213,8 +2171,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __truediv__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rtruediv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rtruediv__(self: _ArrayInt_co, other: _ArrayInt_co) -> NDArray[float64]: ... # type: ignore[misc] @overload @@ -2232,8 +2188,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rtruediv__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __lshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __lshift__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2245,8 +2199,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __lshift__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rlshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rlshift__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2258,8 +2210,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rlshift__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rshift__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2271,8 +2221,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rshift__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rrshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rrshift__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2284,8 +2232,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rrshift__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __and__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __and__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2297,8 +2243,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __and__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rand__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rand__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2310,8 +2254,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rand__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __xor__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __xor__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2323,8 +2265,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __xor__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rxor__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rxor__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2336,8 +2276,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rxor__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __or__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __or__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2349,8 +2287,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __or__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __ror__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ror__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2363,8 +2299,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __ror__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... # `np.generic` does not support inplace operations - @overload # type: ignore[misc] - def __iadd__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __iadd__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload @@ -2382,8 +2316,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __iadd__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __isub__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __isub__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2399,8 +2331,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __isub__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __imul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __imul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload @@ -2416,8 +2346,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __imul__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __itruediv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __itruediv__(self: NDArray[floating[_NBit1]], other: _ArrayLikeFloat_co) -> NDArray[floating[_NBit1]]: ... @overload @@ -2429,8 +2357,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __itruediv__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __ifloordiv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ifloordiv__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2446,8 +2372,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __ifloordiv__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __ipow__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ipow__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2459,8 +2383,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __ipow__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __imod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __imod__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2472,8 +2394,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __imod__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __ilshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ilshift__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2481,8 +2401,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __ilshift__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __irshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __irshift__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2490,8 +2408,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __irshift__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __iand__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __iand__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload @@ -2501,8 +2417,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __iand__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __ixor__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ixor__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload @@ -2512,8 +2426,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __ixor__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload # type: ignore[misc] - def __ior__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ior__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload -- cgit v1.2.1 From 1b3a7cd32bcfd4d644760e5ffd2aafb73fc3448d Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 25 Oct 2021 11:48:02 +0200 Subject: MAINT: Remove the default `memo` value of `ndarray.__deepcopy__` --- numpy/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 347a66d27..4c78179de 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -994,9 +994,9 @@ class _ArrayOrScalarCommon: def __str__(self) -> str: ... def __repr__(self) -> str: ... def __copy__(self: _ArraySelf) -> _ArraySelf: ... - def __deepcopy__(self: _ArraySelf, memo: None | dict = ..., /) -> _ArraySelf: ... def __eq__(self, other): ... def __ne__(self, other): ... + def __deepcopy__(self: _ArraySelf, memo: None | Dict[int, Any], /) -> _ArraySelf: ... def copy(self: _ArraySelf, order: _OrderKACF = ...) -> _ArraySelf: ... def dump(self, file: str | bytes | os.PathLike[str] | os.PathLike[bytes] | _SupportsWrite[bytes]) -> None: ... def dumps(self) -> bytes: ... -- cgit v1.2.1 From 4da0d52c4fb1a9e979a5c4f9f3d60de766269b9b Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 25 Oct 2021 11:55:08 +0200 Subject: MAINT: Add annotations for remaining unannotated methods --- numpy/__init__.pyi | 81 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 10 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 4c78179de..7abf9c62e 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -884,6 +884,13 @@ class dtype(Generic[_DTypeScalar_co]): def __ge__(self, other: DTypeLike) -> bool: ... def __lt__(self, other: DTypeLike) -> bool: ... def __le__(self, other: DTypeLike) -> bool: ... + + # Explicitly defined `__eq__` and `__ne__` to get around mypy's + # `strict_equality` option; even though their signatures are + # identical to their `object`-based counterpart + def __eq__(self, other: Any) -> bool: ... + def __ne__(self, other: Any) -> bool: ... + @property def alignment(self) -> int: ... @property @@ -994,9 +1001,12 @@ class _ArrayOrScalarCommon: def __str__(self) -> str: ... def __repr__(self) -> str: ... def __copy__(self: _ArraySelf) -> _ArraySelf: ... - def __eq__(self, other): ... - def __ne__(self, other): ... def __deepcopy__(self: _ArraySelf, memo: None | Dict[int, Any], /) -> _ArraySelf: ... + + # TODO: How to deal with the non-commutative nature of `==` and `!=`? + # xref numpy/numpy#17368 + def __eq__(self, other: Any) -> Any: ... + def __ne__(self, other: Any) -> Any: ... def copy(self: _ArraySelf, order: _OrderKACF = ...) -> _ArraySelf: ... def dump(self, file: str | bytes | os.PathLike[str] | os.PathLike[bytes] | _SupportsWrite[bytes]) -> None: ... def dumps(self) -> bytes: ... @@ -1013,12 +1023,18 @@ class _ArrayOrScalarCommon: def tolist(self) -> Any: ... @property - def __array_interface__(self): ... + def __array_interface__(self) -> Dict[str, Any]: ... @property def __array_priority__(self) -> float: ... @property - def __array_struct__(self): ... - def __setstate__(self, state, /): ... + def __array_struct__(self) -> Any: ... # builtins.PyCapsule + def __setstate__(self, state: Tuple[ + SupportsIndex, # version + _ShapeLike, # Shape + _DType_co, # DType + bool, # F-continuous + bytes | List[Any], # Data + ], /) -> None: ... # a `bool_` is returned when `keepdims=True` and `self` is a 0d array @overload @@ -3428,8 +3444,6 @@ class format_parser: byteorder: None | _ByteOrder = ..., ) -> None: ... -# TODO: field-lookup returns either a `recarray` or a `ndarray` -# depending on the field dtype class recarray(ndarray[_ShapeType, _DType_co]): # NOTE: While not strictly mandatory, we're demanding here that arguments # for the `format_parser`- and `dtype`-based dtype constructors are @@ -3468,7 +3482,34 @@ class recarray(ndarray[_ShapeType, _DType_co]): def __array_finalize__(self, obj: object) -> None: ... def __getattribute__(self, attr: str) -> Any: ... def __setattr__(self, attr: str, val: ArrayLike) -> None: ... - def __getitem__(self, indx): ... # TODO + @overload + def __getitem__(self, indx: Union[ + SupportsIndex, + _ArrayLikeInt_co, + Tuple[SupportsIndex | _ArrayLikeInt_co, ...], + ]) -> Any: ... + @overload + def __getitem__(self: recarray[Any, dtype[void]], indx: Union[ + None, + slice, + ellipsis, + SupportsIndex, + _ArrayLikeInt_co, + Tuple[None | slice | ellipsis | _ArrayLikeInt_co | SupportsIndex, ...], + ]) -> recarray[Any, _DType_co]: ... + @overload + def __getitem__(self, indx: Union[ + None, + slice, + ellipsis, + SupportsIndex, + _ArrayLikeInt_co, + Tuple[None | slice | ellipsis | _ArrayLikeInt_co | SupportsIndex, ...], + ]) -> ndarray[Any, _DType_co]: ... + @overload + def __getitem__(self, indx: str) -> NDArray[Any]: ... + @overload + def __getitem__(self, indx: list[str]) -> recarray[_ShapeType, dtype[record]]: ... @overload def field(self, attr: int | str, val: None = ...) -> Any: ... @overload @@ -3638,7 +3679,6 @@ class memmap(ndarray[_ShapeType, _DType_co]): array: memmap[_ShapeType, _DType_co], context: None | Tuple[ufunc, Tuple[Any, ...], int] = ..., ) -> Any: ... - def __getitem__(self, index): ... # TODO def flush(self) -> None: ... class vectorize: @@ -3736,6 +3776,7 @@ class poly1d: ) -> poly1d: ... class matrix(ndarray[_ShapeType, _DType_co]): + __array_priority__: ClassVar[float] def __new__( subtype, data: ArrayLike, @@ -3743,7 +3784,27 @@ class matrix(ndarray[_ShapeType, _DType_co]): copy: bool = ..., ) -> matrix[Any, Any]: ... def __array_finalize__(self, obj: NDArray[Any]) -> None: ... - def __getitem__(self, index): ... # TODO + + @overload + def __getitem__(self, key: Union[ + SupportsIndex, + _ArrayLikeInt_co, + Tuple[SupportsIndex | _ArrayLikeInt_co, ...], + ]) -> Any: ... + @overload + def __getitem__(self, key: Union[ + None, + slice, + ellipsis, + SupportsIndex, + _ArrayLikeInt_co, + Tuple[None | slice | ellipsis | _ArrayLikeInt_co | SupportsIndex, ...], + ]) -> matrix[Any, _DType_co]: ... + @overload + def __getitem__(self: NDArray[void], key: str) -> matrix[Any, dtype[Any]]: ... + @overload + def __getitem__(self: NDArray[void], key: list[str]) -> matrix[_ShapeType, dtype[void]]: ... + def __mul__(self, other: ArrayLike) -> matrix[Any, Any]: ... def __rmul__(self, other: ArrayLike) -> matrix[Any, Any]: ... def __imul__(self, other: ArrayLike) -> matrix[_ShapeType, _DType_co]: ... -- cgit v1.2.1 From 0f6feaa9c619251a0c62fb7baa49c369069340d9 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 25 Oct 2021 12:18:07 +0200 Subject: MAINT: Simplify the annotations of `np.version` --- numpy/version.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'numpy') diff --git a/numpy/version.py b/numpy/version.py index 2b076349d..d5657d0d0 100644 --- a/numpy/version.py +++ b/numpy/version.py @@ -1,13 +1,15 @@ +from __future__ import annotations + from ._version import get_versions __ALL__ = ['version', '__version__', 'full_version', 'git_revision', 'release'] -vinfo = get_versions() -version: str = vinfo["version"] +vinfo: dict[str, str] = get_versions() +version = vinfo["version"] __version__ = vinfo.get("closest-tag", vinfo["version"]) -full_version: str = vinfo['version'] -git_revision: str = vinfo['full-revisionid'] +full_version = vinfo['version'] +git_revision = vinfo['full-revisionid'] release = 'dev0' not in version and '+' not in version -short_version: str = vinfo['version'].split("+")[0] +short_version = vinfo['version'].split("+")[0] del get_versions, vinfo -- cgit v1.2.1 From fe90af01d080b285edfcd2d9f62d055286ec72fb Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 25 Oct 2021 12:02:35 +0200 Subject: TST: Update the typing tests --- numpy/typing/tests/data/fail/arithmetic.pyi | 11 ++++++----- numpy/typing/tests/data/fail/comparisons.pyi | 9 ++++----- numpy/typing/tests/data/reveal/ndarray_misc.pyi | 5 +++++ numpy/typing/tests/data/reveal/ufunc_config.pyi | 6 +++--- numpy/typing/tests/data/reveal/version.pyi | 8 ++++++++ 5 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 numpy/typing/tests/data/reveal/version.pyi (limited to 'numpy') diff --git a/numpy/typing/tests/data/fail/arithmetic.pyi b/numpy/typing/tests/data/fail/arithmetic.pyi index 02bbffa53..b99b24c1f 100644 --- a/numpy/typing/tests/data/fail/arithmetic.pyi +++ b/numpy/typing/tests/data/fail/arithmetic.pyi @@ -28,6 +28,7 @@ AR_LIKE_M: List[np.datetime64] # NOTE: mypys `NoReturn` errors are, unfortunately, not that great _1 = AR_b - AR_LIKE_b # E: Need type annotation _2 = AR_LIKE_b - AR_b # E: Need type annotation +AR_i - bytes() # E: No overload variant AR_f - AR_LIKE_m # E: Unsupported operand types AR_f - AR_LIKE_M # E: Unsupported operand types @@ -91,11 +92,11 @@ AR_f *= AR_LIKE_m # E: incompatible type # Array power -AR_b **= AR_LIKE_b # E: incompatible type -AR_b **= AR_LIKE_u # E: incompatible type -AR_b **= AR_LIKE_i # E: incompatible type -AR_b **= AR_LIKE_f # E: incompatible type -AR_b **= AR_LIKE_c # E: incompatible type +AR_b **= AR_LIKE_b # E: Invalid self argument +AR_b **= AR_LIKE_u # E: Invalid self argument +AR_b **= AR_LIKE_i # E: Invalid self argument +AR_b **= AR_LIKE_f # E: Invalid self argument +AR_b **= AR_LIKE_c # E: Invalid self argument AR_u **= AR_LIKE_i # E: incompatible type AR_u **= AR_LIKE_f # E: incompatible type diff --git a/numpy/typing/tests/data/fail/comparisons.pyi b/numpy/typing/tests/data/fail/comparisons.pyi index 0432177e2..febd0a18c 100644 --- a/numpy/typing/tests/data/fail/comparisons.pyi +++ b/numpy/typing/tests/data/fail/comparisons.pyi @@ -21,8 +21,7 @@ AR_M > AR_i # E: Unsupported operand types AR_M > AR_f # E: Unsupported operand types AR_M > AR_m # E: Unsupported operand types -# Unfortunately `NoReturn` errors are not the most descriptive -_1 = AR_i > str() # E: No overload variant -_2 = AR_i > bytes() # E: Need type annotation -_3 = str() > AR_M # E: Unsupported operand types -_4 = bytes() > AR_M # E: Need type annotation +AR_i > str() # E: No overload variant +AR_i > bytes() # E: No overload variant +str() > AR_M # E: Unsupported operand types +bytes() > AR_M # E: Unsupported operand types diff --git a/numpy/typing/tests/data/reveal/ndarray_misc.pyi b/numpy/typing/tests/data/reveal/ndarray_misc.pyi index e384b5388..2d900c53d 100644 --- a/numpy/typing/tests/data/reveal/ndarray_misc.pyi +++ b/numpy/typing/tests/data/reveal/ndarray_misc.pyi @@ -204,3 +204,8 @@ reveal_type(AR_V[0, ...]) # E: numpy.ndarray[Any, numpy.dtype[numpy.void]] reveal_type(AR_V[:]) # E: numpy.ndarray[Any, numpy.dtype[numpy.void]] reveal_type(AR_V["a"]) # E: numpy.ndarray[Any, numpy.dtype[Any]] reveal_type(AR_V[["a", "b"]]) # E: numpy.ndarray[Any, numpy.dtype[numpy.void]] + +reveal_type(AR_f8.dump("test_file")) # E: None +reveal_type(AR_f8.dump(b"test_file")) # E: None +with open("test_file", "wb") as f: + reveal_type(AR_f8.dump(f)) # E: None diff --git a/numpy/typing/tests/data/reveal/ufunc_config.pyi b/numpy/typing/tests/data/reveal/ufunc_config.pyi index 26be80314..6848a3cb5 100644 --- a/numpy/typing/tests/data/reveal/ufunc_config.pyi +++ b/numpy/typing/tests/data/reveal/ufunc_config.pyi @@ -17,9 +17,9 @@ reveal_type(np.geterr()) # E: TypedDict('numpy.core._ufunc_config._ErrDict' reveal_type(np.setbufsize(4096)) # E: int reveal_type(np.getbufsize()) # E: int -reveal_type(np.seterrcall(func)) # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy.core._ufunc_config._SupportsWrite] -reveal_type(np.seterrcall(Write())) # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy.core._ufunc_config._SupportsWrite] -reveal_type(np.geterrcall()) # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy.core._ufunc_config._SupportsWrite] +reveal_type(np.seterrcall(func)) # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy._SupportsWrite[builtins.str]] +reveal_type(np.seterrcall(Write())) # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy._SupportsWrite[builtins.str]] +reveal_type(np.geterrcall()) # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy._SupportsWrite[builtins.str]] reveal_type(np.errstate(call=func, all="call")) # E: numpy.errstate[def (a: builtins.str, b: builtins.int)] reveal_type(np.errstate(call=Write(), divide="log", over="log")) # E: numpy.errstate[ufunc_config.Write] diff --git a/numpy/typing/tests/data/reveal/version.pyi b/numpy/typing/tests/data/reveal/version.pyi new file mode 100644 index 000000000..e53837647 --- /dev/null +++ b/numpy/typing/tests/data/reveal/version.pyi @@ -0,0 +1,8 @@ +import numpy.version + +reveal_type(numpy.version.version) # E: str +reveal_type(numpy.version.__version__) # E: str +reveal_type(numpy.version.full_version) # E: str +reveal_type(numpy.version.git_revision) # E: str +reveal_type(numpy.version.release) # E: bool +reveal_type(numpy.version.short_version) # E: str -- cgit v1.2.1 From 48e6ac6e120c6d408d85d4fdd3c4867e0195a758 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 25 Oct 2021 19:55:15 +0200 Subject: BUG,DOC: Resolve a refguide failure for `ndarray.__class_getitem__` (#20187) --- numpy/core/_add_newdocs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index 7467be80f..50291c3e7 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -2819,7 +2819,7 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('__class_getitem__', >>> import numpy as np >>> np.ndarray[Any, np.dtype[Any]] - numpy.ndarray[typing.Any, numpy.dtype[Any]] + numpy.ndarray[typing.Any, numpy.dtype[typing.Any]] Notes ----- -- cgit v1.2.1 From 84e0707afa587e7655410561324ac36085db2b95 Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Mon, 25 Oct 2021 21:53:48 +0300 Subject: ENH: Configurable allocator (#17582) Fixes gh-17467. Adds a public struct to hold memory manipulation routines PyDataMem_Handler and two new API functions PyDataMem_SetHandler to replace the current routines with the new ones, and PyDataMem_GetHandlerName to get the string name of the current routines (either globally or for a specific ndarray object). This also changes the size of the ndarray object to hold the PyDataMem_Handler active when it was created so subsequent actions on its data memory will remain consistent. Tests and documentation are included. Along the way, I found some places in the code where the current policy is inconsistent (all data memory handling should have gone through npy_*_cache not PyDataMem_*) so even if this is rejected it might improve the cache handling. The PyDataMem_Handler has fields to override memcpy, these are currently not implemented: memcpy in the code base is untouched. I think this PR is invasive enough as-is, if desired memcpy can be handled in a follow-up PR. * ENH: add and use global configurable memory routines * ENH: add tests and a way to compile c-extensions from tests * fix allocation/free exposed by tests * DOC: document the new APIs (and some old ones too) * BUG: return void from FREE, also some cleanup * MAINT: changes from review * fixes from linter * setting ndarray->descr on 0d or scalars mess with FREE * make scalar allocation more consistent wrt np_alloc_cache * change formatting for sphinx * remove memcpy variants * update to match NEP 49 * ENH: add a python-level get_handler_name * ENH: add core.multiarray.get_handler_name * Allow closure-like definition of the data mem routines * Fix incompatible pointer warnings * Note PyDataMemAllocator and PyMemAllocatorEx differentiation Co-authored-by: Matti Picus * Redefine default allocator handling * Always allocate new arrays using the current_handler * Search for the mem_handler name of the data owner * Sub-comparisons don't need a local mem_handler * Make the default_handler a valid PyDataMem_Handler * Fix PyDataMem_SetHandler description (NEP discussion) * Pass the allocators by reference * Implement allocator context-locality * Fix documentation, make PyDataMem_GetHandler return const * remove import of setuptools==49.1.3, doesn't work on python3.10 * Fix refcount leaks * fix function signatures in test * Return early on PyDataMem_GetHandler error (VOID_compare) * Add context/thread-locality tests, allow testing custom policies * ENH: add and use global configurable memory routines * ENH: add tests and a way to compile c-extensions from tests * fix allocation/free exposed by tests * DOC: document the new APIs (and some old ones too) * BUG: return void from FREE, also some cleanup * MAINT: changes from review * fixes from linter * setting ndarray->descr on 0d or scalars mess with FREE * make scalar allocation more consistent wrt np_alloc_cache * change formatting for sphinx * remove memcpy variants * update to match NEP 49 * ENH: add a python-level get_handler_name * ENH: add core.multiarray.get_handler_name * Allow closure-like definition of the data mem routines * Fix incompatible pointer warnings * Note PyDataMemAllocator and PyMemAllocatorEx differentiation Co-authored-by: Matti Picus * Redefine default allocator handling * Always allocate new arrays using the current_handler * Search for the mem_handler name of the data owner * Sub-comparisons don't need a local mem_handler * Make the default_handler a valid PyDataMem_Handler * Fix PyDataMem_SetHandler description (NEP discussion) * Pass the allocators by reference * remove import of setuptools==49.1.3, doesn't work on python3.10 * fix function signatures in test * try to fix cygwin extension building * YAPF mem_policy test * Less empty lines, more comments (tests) * Apply suggestions from code review (set an exception and) Co-authored-by: Matti Picus * skip test on cygwin * update API hash for changed signature * TST: add gc.collect to make sure cycles are broken * Implement thread-locality for PyPy Co-authored-by: Sebastian Berg * Update numpy/core/tests/test_mem_policy.py Co-authored-by: Sebastian Berg * fixes from review * update circleci config * fix test * make the connection between OWNDATA and having a allocator handle more explicit * improve docstring, fix flake8 for tests * update PyDataMem_GetHandler() from review * Implement allocator lifetime management * update NEP and add best-effort handling of error in PyDataMem_UserFREE * ENH: fix and test for blindly taking ownership of data * Update doc/neps/nep-0049.rst Co-authored-by: Elias Koromilas --- numpy/core/_add_newdocs.py | 10 + numpy/core/code_generators/cversions.txt | 4 +- numpy/core/code_generators/numpy_api.py | 9 +- numpy/core/include/numpy/ndarraytypes.h | 38 ++- numpy/core/multiarray.py | 4 +- numpy/core/setup_common.py | 4 +- numpy/core/src/multiarray/alloc.c | 339 +++++++++++++++++++++++++- numpy/core/src/multiarray/alloc.h | 17 +- numpy/core/src/multiarray/arrayobject.c | 20 +- numpy/core/src/multiarray/arraytypes.c.src | 31 ++- numpy/core/src/multiarray/convert_datatype.c | 2 +- numpy/core/src/multiarray/ctors.c | 41 +++- numpy/core/src/multiarray/getset.c | 18 +- numpy/core/src/multiarray/item_selection.c | 33 ++- numpy/core/src/multiarray/methods.c | 49 +++- numpy/core/src/multiarray/multiarraymodule.c | 17 ++ numpy/core/src/multiarray/scalartypes.c.src | 17 +- numpy/core/src/multiarray/shape.c | 12 +- numpy/core/tests/test_mem_policy.py | 340 +++++++++++++++++++++++++++ numpy/core/tests/test_nditer.py | 6 +- numpy/ma/mrecords.py | 1 - numpy/testing/__init__.py | 2 +- numpy/testing/_private/extbuild.py | 251 ++++++++++++++++++++ 23 files changed, 1200 insertions(+), 65 deletions(-) create mode 100644 numpy/core/tests/test_mem_policy.py create mode 100644 numpy/testing/_private/extbuild.py (limited to 'numpy') diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index 50291c3e7..dbbb43009 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -4727,6 +4727,16 @@ add_newdoc('numpy.core.umath', '_add_newdoc_ufunc', and then throwing away the ufunc. """) +add_newdoc('numpy.core.multiarray', 'get_handler_name', + """ + get_handler_name(a: ndarray) -> str,None + + Return the name of the memory handler used by `a`. If not provided, return + the name of the memory handler that will be used to allocate data for the + next `ndarray` in this context. May return None if `a` does not own its + memory, in which case you can traverse ``a.base`` for a memory handler. + """) + add_newdoc('numpy.core.multiarray', '_set_madvise_hugepage', """ _set_madvise_hugepage(enabled: bool) -> bool diff --git a/numpy/core/code_generators/cversions.txt b/numpy/core/code_generators/cversions.txt index a02c7153a..38ee4dac2 100644 --- a/numpy/core/code_generators/cversions.txt +++ b/numpy/core/code_generators/cversions.txt @@ -56,5 +56,7 @@ # DType related API additions. # A new field was added to the end of PyArrayObject_fields. # Version 14 (NumPy 1.21) No change. -# Version 14 (NumPy 1.22) No change. 0x0000000e = 17a0f366e55ec05e5c5c149123478452 + +# Version 15 (NumPy 1.22) Configurable memory allocations +0x0000000f = 0c420aed67010594eb81f23ddfb02a88 diff --git a/numpy/core/code_generators/numpy_api.py b/numpy/core/code_generators/numpy_api.py index fbd323368..3813c6ad7 100644 --- a/numpy/core/code_generators/numpy_api.py +++ b/numpy/core/code_generators/numpy_api.py @@ -76,9 +76,9 @@ multiarray_types_api = { # End 1.6 API } -#define NPY_NUMUSERTYPES (*(int *)PyArray_API[6]) -#define PyBoolArrType_Type (*(PyTypeObject *)PyArray_API[7]) -#define _PyArrayScalar_BoolValues ((PyBoolScalarObject *)PyArray_API[8]) +# define NPY_NUMUSERTYPES (*(int *)PyArray_API[6]) +# define PyBoolArrType_Type (*(PyTypeObject *)PyArray_API[7]) +# define _PyArrayScalar_BoolValues ((PyBoolScalarObject *)PyArray_API[8]) multiarray_funcs_api = { 'PyArray_GetNDArrayCVersion': (0,), @@ -350,6 +350,9 @@ multiarray_funcs_api = { 'PyArray_ResolveWritebackIfCopy': (302,), 'PyArray_SetWritebackIfCopyBase': (303,), # End 1.14 API + 'PyDataMem_SetHandler': (304,), + 'PyDataMem_GetHandler': (305,), + # End 1.21 API } ufunc_types_api = { diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 8d810fa64..80177e2bb 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -355,12 +355,10 @@ struct NpyAuxData_tag { #define NPY_ERR(str) fprintf(stderr, #str); fflush(stderr); #define NPY_ERR2(str) fprintf(stderr, str); fflush(stderr); - /* - * Macros to define how array, and dimension/strides data is - * allocated. - */ - - /* Data buffer - PyDataMem_NEW/FREE/RENEW are in multiarraymodule.c */ +/* +* Macros to define how array, and dimension/strides data is +* allocated. These should be made private +*/ #define NPY_USE_PYMEM 1 @@ -666,6 +664,24 @@ typedef struct _arr_descr { PyObject *shape; /* a tuple */ } PyArray_ArrayDescr; +/* + * Memory handler structure for array data. + */ +/* The declaration of free differs from PyMemAllocatorEx */ +typedef struct { + void *ctx; + void* (*malloc) (void *ctx, size_t size); + void* (*calloc) (void *ctx, size_t nelem, size_t elsize); + void* (*realloc) (void *ctx, void *ptr, size_t new_size); + void (*free) (void *ctx, void *ptr, size_t size); +} PyDataMemAllocator; + +typedef struct { + char name[128]; /* multiple of 64 to keep the struct aligned */ + PyDataMemAllocator allocator; +} PyDataMem_Handler; + + /* * The main array object structure. * @@ -716,6 +732,10 @@ typedef struct tagPyArrayObject_fields { /* For weak references */ PyObject *weakreflist; void *_buffer_info; /* private buffer info, tagged to allow warning */ + /* + * For malloc/calloc/realloc/free per object + */ + PyObject *mem_handler; } PyArrayObject_fields; /* @@ -1659,6 +1679,12 @@ PyArray_CLEARFLAGS(PyArrayObject *arr, int flags) ((PyArrayObject_fields *)arr)->flags &= ~flags; } +static NPY_INLINE NPY_RETURNS_BORROWED_REF PyObject * +PyArray_HANDLER(PyArrayObject *arr) +{ + return ((PyArrayObject_fields *)arr)->mem_handler; +} + #define PyTypeNum_ISBOOL(type) ((type) == NPY_BOOL) #define PyTypeNum_ISUNSIGNED(type) (((type) == NPY_UBYTE) || \ diff --git a/numpy/core/multiarray.py b/numpy/core/multiarray.py index 154df6f4d..351cd3a1b 100644 --- a/numpy/core/multiarray.py +++ b/numpy/core/multiarray.py @@ -31,8 +31,8 @@ __all__ = [ 'count_nonzero', 'c_einsum', 'datetime_as_string', 'datetime_data', 'dot', 'dragon4_positional', 'dragon4_scientific', 'dtype', 'empty', 'empty_like', 'error', 'flagsobj', 'flatiter', 'format_longfloat', - 'frombuffer', 'fromfile', 'fromiter', 'fromstring', 'inner', - 'interp', 'interp_complex', 'is_busday', 'lexsort', + 'frombuffer', 'fromfile', 'fromiter', 'fromstring', 'get_handler_name', + 'inner', 'interp', 'interp_complex', 'is_busday', 'lexsort', 'matmul', 'may_share_memory', 'min_scalar_type', 'ndarray', 'nditer', 'nested_iters', 'normalize_axis_index', 'packbits', 'promote_types', 'putmask', 'ravel_multi_index', 'result_type', 'scalar', diff --git a/numpy/core/setup_common.py b/numpy/core/setup_common.py index 85c8f16d1..70e8fc897 100644 --- a/numpy/core/setup_common.py +++ b/numpy/core/setup_common.py @@ -43,8 +43,8 @@ C_ABI_VERSION = 0x01000009 # 0x0000000d - 1.19.x # 0x0000000e - 1.20.x # 0x0000000e - 1.21.x -# 0x0000000e - 1.22.x -C_API_VERSION = 0x0000000e +# 0x0000000f - 1.22.x +C_API_VERSION = 0x0000000f class MismatchCAPIWarning(Warning): pass diff --git a/numpy/core/src/multiarray/alloc.c b/numpy/core/src/multiarray/alloc.c index adb4ae128..e4756264d 100644 --- a/numpy/core/src/multiarray/alloc.c +++ b/numpy/core/src/multiarray/alloc.c @@ -133,9 +133,10 @@ npy_alloc_cache(npy_uintp sz) /* zero initialized data, sz is number of bytes to allocate */ NPY_NO_EXPORT void * -npy_alloc_cache_zero(npy_uintp sz) +npy_alloc_cache_zero(size_t nmemb, size_t size) { void * p; + size_t sz = nmemb * size; NPY_BEGIN_THREADS_DEF; if (sz < NBUCKETS) { p = _npy_alloc_cache(sz, 1, NBUCKETS, datacache, &PyDataMem_NEW); @@ -145,7 +146,7 @@ npy_alloc_cache_zero(npy_uintp sz) return p; } NPY_BEGIN_THREADS; - p = PyDataMem_NEW_ZEROED(sz, 1); + p = PyDataMem_NEW_ZEROED(nmemb, size); NPY_END_THREADS; return p; } @@ -187,8 +188,8 @@ npy_free_cache_dim(void * p, npy_uintp sz) /* malloc/free/realloc hook */ -NPY_NO_EXPORT PyDataMem_EventHookFunc *_PyDataMem_eventhook; -NPY_NO_EXPORT void *_PyDataMem_eventhook_user_data; +NPY_NO_EXPORT PyDataMem_EventHookFunc *_PyDataMem_eventhook = NULL; +NPY_NO_EXPORT void *_PyDataMem_eventhook_user_data = NULL; /*NUMPY_API * Sets the allocation event hook for numpy array data. @@ -254,21 +255,21 @@ PyDataMem_NEW(size_t size) * Allocates zeroed memory for array data. */ NPY_NO_EXPORT void * -PyDataMem_NEW_ZEROED(size_t size, size_t elsize) +PyDataMem_NEW_ZEROED(size_t nmemb, size_t size) { void *result; - result = calloc(size, elsize); + result = calloc(nmemb, size); if (_PyDataMem_eventhook != NULL) { NPY_ALLOW_C_API_DEF NPY_ALLOW_C_API if (_PyDataMem_eventhook != NULL) { - (*_PyDataMem_eventhook)(NULL, result, size * elsize, + (*_PyDataMem_eventhook)(NULL, result, nmemb * size, _PyDataMem_eventhook_user_data); } NPY_DISABLE_C_API } - PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size); + PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, nmemb * size); return result; } @@ -316,3 +317,325 @@ PyDataMem_RENEW(void *ptr, size_t size) } return result; } + +// The default data mem allocator malloc routine does not make use of a ctx. +// It should be called only through PyDataMem_UserNEW +// since itself does not handle eventhook and tracemalloc logic. +static NPY_INLINE void * +default_malloc(void *NPY_UNUSED(ctx), size_t size) +{ + return _npy_alloc_cache(size, 1, NBUCKETS, datacache, &malloc); +} + +// The default data mem allocator calloc routine does not make use of a ctx. +// It should be called only through PyDataMem_UserNEW_ZEROED +// since itself does not handle eventhook and tracemalloc logic. +static NPY_INLINE void * +default_calloc(void *NPY_UNUSED(ctx), size_t nelem, size_t elsize) +{ + void * p; + size_t sz = nelem * elsize; + NPY_BEGIN_THREADS_DEF; + if (sz < NBUCKETS) { + p = _npy_alloc_cache(sz, 1, NBUCKETS, datacache, &malloc); + if (p) { + memset(p, 0, sz); + } + return p; + } + NPY_BEGIN_THREADS; + p = calloc(nelem, elsize); + NPY_END_THREADS; + return p; +} + +// The default data mem allocator realloc routine does not make use of a ctx. +// It should be called only through PyDataMem_UserRENEW +// since itself does not handle eventhook and tracemalloc logic. +static NPY_INLINE void * +default_realloc(void *NPY_UNUSED(ctx), void *ptr, size_t new_size) +{ + return realloc(ptr, new_size); +} + +// The default data mem allocator free routine does not make use of a ctx. +// It should be called only through PyDataMem_UserFREE +// since itself does not handle eventhook and tracemalloc logic. +static NPY_INLINE void +default_free(void *NPY_UNUSED(ctx), void *ptr, size_t size) +{ + _npy_free_cache(ptr, size, NBUCKETS, datacache, &free); +} + +/* Memory handler global default */ +PyDataMem_Handler default_handler = { + "default_allocator", + { + NULL, /* ctx */ + default_malloc, /* malloc */ + default_calloc, /* calloc */ + default_realloc, /* realloc */ + default_free /* free */ + } +}; + +#if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) +PyObject *current_handler; +#endif + +int uo_index=0; /* user_override index */ + +/* Wrappers for the default or any user-assigned PyDataMem_Handler */ + +NPY_NO_EXPORT void * +PyDataMem_UserNEW(size_t size, PyObject *mem_handler) +{ + void *result; + PyDataMem_Handler *handler = (PyDataMem_Handler *) PyCapsule_GetPointer(mem_handler, "mem_handler"); + if (handler == NULL) { + return NULL; + } + + assert(size != 0); + result = handler->allocator.malloc(handler->allocator.ctx, size); + if (_PyDataMem_eventhook != NULL) { + NPY_ALLOW_C_API_DEF + NPY_ALLOW_C_API + if (_PyDataMem_eventhook != NULL) { + (*_PyDataMem_eventhook)(NULL, result, size, + _PyDataMem_eventhook_user_data); + } + NPY_DISABLE_C_API + } + PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size); + return result; +} + +NPY_NO_EXPORT void * +PyDataMem_UserNEW_ZEROED(size_t nmemb, size_t size, PyObject *mem_handler) +{ + void *result; + PyDataMem_Handler *handler = (PyDataMem_Handler *) PyCapsule_GetPointer(mem_handler, "mem_handler"); + if (handler == NULL) { + return NULL; + } + result = handler->allocator.calloc(handler->allocator.ctx, nmemb, size); + if (_PyDataMem_eventhook != NULL) { + NPY_ALLOW_C_API_DEF + NPY_ALLOW_C_API + if (_PyDataMem_eventhook != NULL) { + (*_PyDataMem_eventhook)(NULL, result, nmemb * size, + _PyDataMem_eventhook_user_data); + } + NPY_DISABLE_C_API + } + PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, nmemb * size); + return result; +} + +/* Similar to array_dealloc in arrayobject.c */ +static NPY_INLINE void +WARN_IN_FREE(PyObject* warning, const char * msg) { + if (PyErr_WarnEx(warning, msg, 1) < 0) { + PyObject * s; + + s = PyUnicode_FromString("PyDataMem_UserFREE"); + if (s) { + PyErr_WriteUnraisable(s); + Py_DECREF(s); + } + else { + PyErr_WriteUnraisable(Py_None); + } + } +} + + + +NPY_NO_EXPORT void +PyDataMem_UserFREE(void *ptr, size_t size, PyObject *mem_handler) +{ + PyDataMem_Handler *handler = (PyDataMem_Handler *) PyCapsule_GetPointer(mem_handler, "mem_handler"); + if (handler == NULL) { + WARN_IN_FREE(PyExc_RuntimeWarning, + "Could not get pointer to 'mem_handler' from PyCapsule"); + PyErr_Clear(); + return; + } + PyTraceMalloc_Untrack(NPY_TRACE_DOMAIN, (npy_uintp)ptr); + handler->allocator.free(handler->allocator.ctx, ptr, size); + if (_PyDataMem_eventhook != NULL) { + NPY_ALLOW_C_API_DEF + NPY_ALLOW_C_API + if (_PyDataMem_eventhook != NULL) { + (*_PyDataMem_eventhook)(ptr, NULL, 0, + _PyDataMem_eventhook_user_data); + } + NPY_DISABLE_C_API + } +} + +NPY_NO_EXPORT void * +PyDataMem_UserRENEW(void *ptr, size_t size, PyObject *mem_handler) +{ + void *result; + PyDataMem_Handler *handler = (PyDataMem_Handler *) PyCapsule_GetPointer(mem_handler, "mem_handler"); + if (handler == NULL) { + return NULL; + } + + assert(size != 0); + result = handler->allocator.realloc(handler->allocator.ctx, ptr, size); + if (result != ptr) { + PyTraceMalloc_Untrack(NPY_TRACE_DOMAIN, (npy_uintp)ptr); + } + PyTraceMalloc_Track(NPY_TRACE_DOMAIN, (npy_uintp)result, size); + if (_PyDataMem_eventhook != NULL) { + NPY_ALLOW_C_API_DEF + NPY_ALLOW_C_API + if (_PyDataMem_eventhook != NULL) { + (*_PyDataMem_eventhook)(ptr, result, size, + _PyDataMem_eventhook_user_data); + } + NPY_DISABLE_C_API + } + return result; +} + +/*NUMPY_API + * Set a new allocation policy. If the input value is NULL, will reset + * the policy to the default. Return the previous policy, or + * return NULL if an error has occurred. We wrap the user-provided + * functions so they will still call the python and numpy + * memory management callback hooks. + */ +NPY_NO_EXPORT PyObject * +PyDataMem_SetHandler(PyObject *handler) +{ + PyObject *old_handler; +#if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) + PyObject *token; + if (PyContextVar_Get(current_handler, NULL, &old_handler)) { + return NULL; + } + if (handler == NULL) { + handler = PyCapsule_New(&default_handler, "mem_handler", NULL); + if (handler == NULL) { + return NULL; + } + } + else { + Py_INCREF(handler); + } + token = PyContextVar_Set(current_handler, handler); + Py_DECREF(handler); + if (token == NULL) { + Py_DECREF(old_handler); + return NULL; + } + Py_DECREF(token); + return old_handler; +#else + PyObject *p; + p = PyThreadState_GetDict(); + if (p == NULL) { + return NULL; + } + old_handler = PyDict_GetItemString(p, "current_allocator"); + if (old_handler == NULL) { + old_handler = PyCapsule_New(&default_handler, "mem_handler", NULL); + if (old_handler == NULL) { + return NULL; + } + } + else { + Py_INCREF(old_handler); + } + if (handler == NULL) { + handler = PyCapsule_New(&default_handler, "mem_handler", NULL); + if (handler == NULL) { + Py_DECREF(old_handler); + return NULL; + } + } + else { + Py_INCREF(handler); + } + const int error = PyDict_SetItemString(p, "current_allocator", handler); + Py_DECREF(handler); + if (error) { + Py_DECREF(old_handler); + return NULL; + } + return old_handler; +#endif +} + +/*NUMPY_API + * Return the policy that will be used to allocate data + * for the next PyArrayObject. On failure, return NULL. + */ +NPY_NO_EXPORT PyObject * +PyDataMem_GetHandler() +{ + PyObject *handler; +#if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) + if (PyContextVar_Get(current_handler, NULL, &handler)) { + return NULL; + } + return handler; +#else + PyObject *p = PyThreadState_GetDict(); + if (p == NULL) { + return NULL; + } + handler = PyDict_GetItemString(p, "current_allocator"); + if (handler == NULL) { + handler = PyCapsule_New(&default_handler, "mem_handler", NULL); + if (handler == NULL) { + return NULL; + } + } + else { + Py_INCREF(handler); + } + return handler; +#endif +} + +NPY_NO_EXPORT PyObject * +get_handler_name(PyObject *NPY_UNUSED(self), PyObject *args) +{ + PyObject *arr=NULL; + if (!PyArg_ParseTuple(args, "|O:get_handler_name", &arr)) { + return NULL; + } + if (arr != NULL && !PyArray_Check(arr)) { + PyErr_SetString(PyExc_ValueError, "if supplied, argument must be an ndarray"); + return NULL; + } + PyObject *mem_handler; + PyDataMem_Handler *handler; + PyObject *name; + if (arr != NULL) { + mem_handler = PyArray_HANDLER((PyArrayObject *) arr); + if (mem_handler == NULL) { + Py_RETURN_NONE; + } + Py_INCREF(mem_handler); + } + else { + mem_handler = PyDataMem_GetHandler(); + if (mem_handler == NULL) { + return NULL; + } + } + handler = (PyDataMem_Handler *) PyCapsule_GetPointer(mem_handler, "mem_handler"); + if (handler == NULL) { + Py_DECREF(mem_handler); + return NULL; + } + name = PyUnicode_FromString(handler->name); + Py_DECREF(mem_handler); + return name; +} diff --git a/numpy/core/src/multiarray/alloc.h b/numpy/core/src/multiarray/alloc.h index 1259abca5..4f7df1f84 100644 --- a/numpy/core/src/multiarray/alloc.h +++ b/numpy/core/src/multiarray/alloc.h @@ -11,13 +11,16 @@ NPY_NO_EXPORT PyObject * _set_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *enabled_obj); NPY_NO_EXPORT void * -npy_alloc_cache(npy_uintp sz); +PyDataMem_UserNEW(npy_uintp sz, PyObject *mem_handler); NPY_NO_EXPORT void * -npy_alloc_cache_zero(npy_uintp sz); +PyDataMem_UserNEW_ZEROED(size_t nmemb, size_t size, PyObject *mem_handler); NPY_NO_EXPORT void -npy_free_cache(void * p, npy_uintp sd); +PyDataMem_UserFREE(void * p, npy_uintp sd, PyObject *mem_handler); + +NPY_NO_EXPORT void * +PyDataMem_UserRENEW(void *ptr, size_t size, PyObject *mem_handler); NPY_NO_EXPORT void * npy_alloc_cache_dim(npy_uintp sz); @@ -37,4 +40,12 @@ npy_free_cache_dim_array(PyArrayObject * arr) npy_free_cache_dim(PyArray_DIMS(arr), PyArray_NDIM(arr)); } +#if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) +extern PyObject *current_handler; /* PyContextVar/PyCapsule */ +extern PyDataMem_Handler default_handler; +#endif + +NPY_NO_EXPORT PyObject * +get_handler_name(PyObject *NPY_UNUSED(self), PyObject *obj); + #endif /* NUMPY_CORE_SRC_MULTIARRAY_ALLOC_H_ */ diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index 28aff5d65..c3615f95f 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -493,7 +493,25 @@ array_dealloc(PyArrayObject *self) if (PyDataType_FLAGCHK(fa->descr, NPY_ITEM_REFCOUNT)) { PyArray_XDECREF(self); } - npy_free_cache(fa->data, PyArray_NBYTES(self)); + /* + * Allocation will never be 0, see comment in ctors.c + * line 820 + */ + size_t nbytes = PyArray_NBYTES(self); + if (nbytes == 0) { + nbytes = fa->descr->elsize ? fa->descr->elsize : 1; + } + if (fa->mem_handler == NULL) { + char const * msg = "Trying to dealloc data, but a memory policy " + "is not set. If you take ownership of the data, you must " + "also set a memory policy."; + WARN_IN_DEALLOC(PyExc_RuntimeWarning, msg); + // Guess at malloc/free ??? + free(fa->data); + } else { + PyDataMem_UserFREE(fa->data, nbytes, fa->mem_handler); + Py_DECREF(fa->mem_handler); + } } /* must match allocation in PyArray_NewFromDescr */ diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index 15782a91b..9fe76845a 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -3093,6 +3093,10 @@ VOID_compare(char *ip1, char *ip2, PyArrayObject *ap) if (!PyArray_HASFIELDS(ap)) { return STRING_compare(ip1, ip2, ap); } + PyObject *mem_handler = PyDataMem_GetHandler(); + if (mem_handler == NULL) { + goto finish; + } descr = PyArray_DESCR(ap); /* * Compare on the first-field. If equal, then @@ -3107,15 +3111,19 @@ VOID_compare(char *ip1, char *ip2, PyArrayObject *ap) if (_unpack_field(tup, &new, &offset) < 0) { goto finish; } - /* descr is the only field checked by compare or copyswap */ + /* Set the fields needed by compare or copyswap */ dummy_struct.descr = new; + swap = PyArray_ISBYTESWAPPED(dummy); nip1 = ip1 + offset; nip2 = ip2 + offset; if (swap || new->alignment > 1) { if (swap || !npy_is_aligned(nip1, new->alignment)) { - /* create buffer and copy */ - nip1 = npy_alloc_cache(new->elsize); + /* + * create temporary buffer and copy, + * always use the current handler for internal allocations + */ + nip1 = PyDataMem_UserNEW(new->elsize, mem_handler); if (nip1 == NULL) { goto finish; } @@ -3124,11 +3132,15 @@ VOID_compare(char *ip1, char *ip2, PyArrayObject *ap) new->f->copyswap(nip1, NULL, swap, dummy); } if (swap || !npy_is_aligned(nip2, new->alignment)) { - /* create buffer and copy */ - nip2 = npy_alloc_cache(new->elsize); + /* + * create temporary buffer and copy, + * always use the current handler for internal allocations + */ + nip2 = PyDataMem_UserNEW(new->elsize, mem_handler); if (nip2 == NULL) { if (nip1 != ip1 + offset) { - npy_free_cache(nip1, new->elsize); + /* destroy temporary buffer */ + PyDataMem_UserFREE(nip1, new->elsize, mem_handler); } goto finish; } @@ -3140,10 +3152,12 @@ VOID_compare(char *ip1, char *ip2, PyArrayObject *ap) res = new->f->compare(nip1, nip2, dummy); if (swap || new->alignment > 1) { if (nip1 != ip1 + offset) { - npy_free_cache(nip1, new->elsize); + /* destroy temporary buffer */ + PyDataMem_UserFREE(nip1, new->elsize, mem_handler); } if (nip2 != ip2 + offset) { - npy_free_cache(nip2, new->elsize); + /* destroy temporary buffer */ + PyDataMem_UserFREE(nip2, new->elsize, mem_handler); } } if (res != 0) { @@ -3152,6 +3166,7 @@ VOID_compare(char *ip1, char *ip2, PyArrayObject *ap) } finish: + Py_XDECREF(mem_handler); return res; } diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c index eeadad374..3135d6989 100644 --- a/numpy/core/src/multiarray/convert_datatype.c +++ b/numpy/core/src/multiarray/convert_datatype.c @@ -2119,7 +2119,7 @@ PyArray_ObjectType(PyObject *op, int minimum_type) * This function is only used in one place within NumPy and should * generally be avoided. It is provided mainly for backward compatibility. * - * The user of the function has to free the returned array. + * The user of the function has to free the returned array with PyDataMem_FREE. */ NPY_NO_EXPORT PyArrayObject ** PyArray_ConvertToCommonType(PyObject *op, int *retn) diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 9da75fb8a..bb3569918 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -726,6 +726,7 @@ PyArray_NewFromDescr_int( fa->nd = nd; fa->dimensions = NULL; fa->data = NULL; + fa->mem_handler = NULL; if (data == NULL) { fa->flags = NPY_ARRAY_DEFAULT; @@ -805,12 +806,19 @@ PyArray_NewFromDescr_int( fa->flags |= NPY_ARRAY_C_CONTIGUOUS|NPY_ARRAY_F_CONTIGUOUS; } + if (data == NULL) { + /* Store the handler in case the default is modified */ + fa->mem_handler = PyDataMem_GetHandler(); + if (fa->mem_handler == NULL) { + goto fail; + } /* * Allocate something even for zero-space arrays * e.g. shape=(0,) -- otherwise buffer exposure * (a.data) doesn't work as it should. * Could probably just allocate a few bytes here. -- Chuck + * Note: always sync this with calls to PyDataMem_UserFREE */ if (nbytes == 0) { nbytes = descr->elsize ? descr->elsize : 1; @@ -820,21 +828,23 @@ PyArray_NewFromDescr_int( * which could also be sub-fields of a VOID array */ if (zeroed || PyDataType_FLAGCHK(descr, NPY_NEEDS_INIT)) { - data = npy_alloc_cache_zero(nbytes); + data = PyDataMem_UserNEW_ZEROED(nbytes, 1, fa->mem_handler); } else { - data = npy_alloc_cache(nbytes); + data = PyDataMem_UserNEW(nbytes, fa->mem_handler); } if (data == NULL) { raise_memory_error(fa->nd, fa->dimensions, descr); goto fail; } + fa->flags |= NPY_ARRAY_OWNDATA; } else { + /* The handlers should never be called in this case */ + fa->mem_handler = NULL; /* - * If data is passed in, this object won't own it by default. - * Caller must arrange for this to be reset if truly desired + * If data is passed in, this object won't own it. */ fa->flags &= ~NPY_ARRAY_OWNDATA; } @@ -902,6 +912,7 @@ PyArray_NewFromDescr_int( return (PyObject *)fa; fail: + Py_XDECREF(fa->mem_handler); Py_DECREF(fa); return NULL; } @@ -3409,7 +3420,9 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char const *sep, size_t *nre dptr += dtype->elsize; if (num < 0 && thisbuf == size) { totalbytes += bytes; - tmp = PyDataMem_RENEW(PyArray_DATA(r), totalbytes); + /* The handler is always valid */ + tmp = PyDataMem_UserRENEW(PyArray_DATA(r), totalbytes, + PyArray_HANDLER(r)); if (tmp == NULL) { err = 1; break; @@ -3431,7 +3444,9 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char const *sep, size_t *nre const size_t nsize = PyArray_MAX(*nread,1)*dtype->elsize; if (nsize != 0) { - tmp = PyDataMem_RENEW(PyArray_DATA(r), nsize); + /* The handler is always valid */ + tmp = PyDataMem_UserRENEW(PyArray_DATA(r), nsize, + PyArray_HANDLER(r)); if (tmp == NULL) { err = 1; } @@ -3536,7 +3551,9 @@ PyArray_FromFile(FILE *fp, PyArray_Descr *dtype, npy_intp num, char *sep) const size_t nsize = PyArray_MAX(nread,1) * dtype->elsize; char *tmp; - if ((tmp = PyDataMem_RENEW(PyArray_DATA(ret), nsize)) == NULL) { + /* The handler is always valid */ + if((tmp = PyDataMem_UserRENEW(PyArray_DATA(ret), nsize, + PyArray_HANDLER(ret))) == NULL) { Py_DECREF(dtype); Py_DECREF(ret); return PyErr_NoMemory(); @@ -3820,7 +3837,9 @@ PyArray_FromIter(PyObject *obj, PyArray_Descr *dtype, npy_intp count) */ elcount = (i >> 1) + (i < 4 ? 4 : 2) + i; if (!npy_mul_with_overflow_intp(&nbytes, elcount, elsize)) { - new_data = PyDataMem_RENEW(PyArray_DATA(ret), nbytes); + /* The handler is always valid */ + new_data = PyDataMem_UserRENEW(PyArray_DATA(ret), nbytes, + PyArray_HANDLER(ret)); } else { new_data = NULL; @@ -3858,10 +3877,12 @@ PyArray_FromIter(PyObject *obj, PyArray_Descr *dtype, npy_intp count) * (assuming realloc is reasonably good about reusing space...) */ if (i == 0 || elsize == 0) { - /* The size cannot be zero for PyDataMem_RENEW. */ + /* The size cannot be zero for realloc. */ goto done; } - new_data = PyDataMem_RENEW(PyArray_DATA(ret), i * elsize); + /* The handler is always valid */ + new_data = PyDataMem_UserRENEW(PyArray_DATA(ret), i * elsize, + PyArray_HANDLER(ret)); if (new_data == NULL) { PyErr_SetString(PyExc_MemoryError, "cannot allocate array memory"); diff --git a/numpy/core/src/multiarray/getset.c b/numpy/core/src/multiarray/getset.c index 2c8d1b3b4..e81ca2947 100644 --- a/numpy/core/src/multiarray/getset.c +++ b/numpy/core/src/multiarray/getset.c @@ -384,7 +384,23 @@ array_data_set(PyArrayObject *self, PyObject *op, void *NPY_UNUSED(ignored)) } if (PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA) { PyArray_XDECREF(self); - PyDataMem_FREE(PyArray_DATA(self)); + size_t nbytes = PyArray_NBYTES(self); + /* + * Allocation will never be 0, see comment in ctors.c + * line 820 + */ + if (nbytes == 0) { + PyArray_Descr *dtype = PyArray_DESCR(self); + nbytes = dtype->elsize ? dtype->elsize : 1; + } + PyObject *handler = PyArray_HANDLER(self); + if (handler == NULL) { + /* This can happen if someone arbitrarily sets NPY_ARRAY_OWNDATA */ + PyErr_SetString(PyExc_RuntimeError, + "no memory handler found but OWNDATA flag set"); + return -1; + } + PyDataMem_UserFREE(PyArray_DATA(self), nbytes, handler); } if (PyArray_BASE(self)) { if ((PyArray_FLAGS(self) & NPY_ARRAY_WRITEBACKIFCOPY) || diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index 33d378c2b..086b674c8 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -776,6 +776,7 @@ PyArray_Repeat(PyArrayObject *aop, PyObject *op, int axis) return NULL; } + /*NUMPY_API */ NPY_NO_EXPORT PyObject * @@ -907,7 +908,7 @@ PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *out, Py_XDECREF(mps[i]); } Py_DECREF(ap); - npy_free_cache(mps, n * sizeof(mps[0])); + PyDataMem_FREE(mps); if (out != NULL && out != obj) { Py_INCREF(out); PyArray_ResolveWritebackIfCopy(obj); @@ -922,7 +923,7 @@ PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *out, Py_XDECREF(mps[i]); } Py_XDECREF(ap); - npy_free_cache(mps, n * sizeof(mps[0])); + PyDataMem_FREE(mps); PyArray_DiscardWritebackIfCopy(obj); Py_XDECREF(obj); return NULL; @@ -962,14 +963,19 @@ _new_sortlike(PyArrayObject *op, int axis, PyArray_SortFunc *sort, return 0; } + PyObject *mem_handler = PyDataMem_GetHandler(); + if (mem_handler == NULL) { + return -1; + } it = (PyArrayIterObject *)PyArray_IterAllButAxis((PyObject *)op, &axis); if (it == NULL) { + Py_DECREF(mem_handler); return -1; } size = it->size; if (needcopy) { - buffer = npy_alloc_cache(N * elsize); + buffer = PyDataMem_UserNEW(N * elsize, mem_handler); if (buffer == NULL) { ret = -1; goto fail; @@ -1053,12 +1059,14 @@ _new_sortlike(PyArrayObject *op, int axis, PyArray_SortFunc *sort, fail: NPY_END_THREADS_DESCR(PyArray_DESCR(op)); - npy_free_cache(buffer, N * elsize); + /* cleanup internal buffer */ + PyDataMem_UserFREE(buffer, N * elsize, mem_handler); if (ret < 0 && !PyErr_Occurred()) { /* Out of memory during sorting or buffer creation */ PyErr_NoMemory(); } Py_DECREF(it); + Py_DECREF(mem_handler); return ret; } @@ -1090,11 +1098,16 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, NPY_BEGIN_THREADS_DEF; + PyObject *mem_handler = PyDataMem_GetHandler(); + if (mem_handler == NULL) { + return NULL; + } rop = (PyArrayObject *)PyArray_NewFromDescr( Py_TYPE(op), PyArray_DescrFromType(NPY_INTP), PyArray_NDIM(op), PyArray_DIMS(op), NULL, NULL, 0, (PyObject *)op); if (rop == NULL) { + Py_DECREF(mem_handler); return NULL; } rstride = PyArray_STRIDE(rop, axis); @@ -1102,6 +1115,7 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, /* Check if there is any argsorting to do */ if (N <= 1 || PyArray_SIZE(op) == 0) { + Py_DECREF(mem_handler); memset(PyArray_DATA(rop), 0, PyArray_NBYTES(rop)); return (PyObject *)rop; } @@ -1115,7 +1129,7 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, size = it->size; if (needcopy) { - valbuffer = npy_alloc_cache(N * elsize); + valbuffer = PyDataMem_UserNEW(N * elsize, mem_handler); if (valbuffer == NULL) { ret = -1; goto fail; @@ -1123,7 +1137,8 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, } if (needidxbuffer) { - idxbuffer = (npy_intp *)npy_alloc_cache(N * sizeof(npy_intp)); + idxbuffer = (npy_intp *)PyDataMem_UserNEW(N * sizeof(npy_intp), + mem_handler); if (idxbuffer == NULL) { ret = -1; goto fail; @@ -1212,8 +1227,9 @@ _new_argsortlike(PyArrayObject *op, int axis, PyArray_ArgSortFunc *argsort, fail: NPY_END_THREADS_DESCR(PyArray_DESCR(op)); - npy_free_cache(valbuffer, N * elsize); - npy_free_cache(idxbuffer, N * sizeof(npy_intp)); + /* cleanup internal buffers */ + PyDataMem_UserFREE(valbuffer, N * elsize, mem_handler); + PyDataMem_UserFREE(idxbuffer, N * sizeof(npy_intp), mem_handler); if (ret < 0) { if (!PyErr_Occurred()) { /* Out of memory during sorting or buffer creation */ @@ -1224,6 +1240,7 @@ fail: } Py_XDECREF(it); Py_XDECREF(rit); + Py_DECREF(mem_handler); return (PyObject *)rop; } diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 391e65f6a..2d66c77dc 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -1975,6 +1975,16 @@ array_setstate(PyArrayObject *self, PyObject *args) return NULL; } + /* + * Reassigning fa->descr messes with the reallocation strategy, + * since fa could be a 0-d or scalar, and then + * PyDataMem_UserFREE will be confused + */ + size_t n_tofree = PyArray_NBYTES(self); + if (n_tofree == 0) { + PyArray_Descr *dtype = PyArray_DESCR(self); + n_tofree = dtype->elsize ? dtype->elsize : 1; + } Py_XDECREF(PyArray_DESCR(self)); fa->descr = typecode; Py_INCREF(typecode); @@ -2041,7 +2051,18 @@ array_setstate(PyArrayObject *self, PyObject *args) } if ((PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA)) { - PyDataMem_FREE(PyArray_DATA(self)); + /* + * Allocation will never be 0, see comment in ctors.c + * line 820 + */ + PyObject *handler = PyArray_HANDLER(self); + if (handler == NULL) { + /* This can happen if someone arbitrarily sets NPY_ARRAY_OWNDATA */ + PyErr_SetString(PyExc_RuntimeError, + "no memory handler found but OWNDATA flag set"); + return NULL; + } + PyDataMem_UserFREE(PyArray_DATA(self), n_tofree, handler); PyArray_CLEARFLAGS(self, NPY_ARRAY_OWNDATA); } Py_XDECREF(PyArray_BASE(self)); @@ -2077,7 +2098,6 @@ array_setstate(PyArrayObject *self, PyObject *args) if (!PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) { int swap = PyArray_ISBYTESWAPPED(self); - fa->data = datastr; /* Bytes should always be considered immutable, but we just grab the * pointer if they are large, to save memory. */ if (!IsAligned(self) || swap || (len <= 1000)) { @@ -2086,8 +2106,16 @@ array_setstate(PyArrayObject *self, PyObject *args) Py_DECREF(rawdata); Py_RETURN_NONE; } - fa->data = PyDataMem_NEW(num); + /* Store the handler in case the default is modified */ + Py_XDECREF(fa->mem_handler); + fa->mem_handler = PyDataMem_GetHandler(); + if (fa->mem_handler == NULL) { + Py_DECREF(rawdata); + return NULL; + } + fa->data = PyDataMem_UserNEW(num, PyArray_HANDLER(self)); if (PyArray_DATA(self) == NULL) { + Py_DECREF(fa->mem_handler); Py_DECREF(rawdata); return PyErr_NoMemory(); } @@ -2123,7 +2151,12 @@ array_setstate(PyArrayObject *self, PyObject *args) Py_DECREF(rawdata); } else { + /* The handlers should never be called in this case */ + Py_XDECREF(fa->mem_handler); + fa->mem_handler = NULL; + fa->data = datastr; if (PyArray_SetBaseObject(self, rawdata) < 0) { + Py_DECREF(rawdata); return NULL; } } @@ -2134,8 +2167,15 @@ array_setstate(PyArrayObject *self, PyObject *args) if (num == 0 || elsize == 0) { Py_RETURN_NONE; } - fa->data = PyDataMem_NEW(num); + /* Store the functions in case the default handler is modified */ + Py_XDECREF(fa->mem_handler); + fa->mem_handler = PyDataMem_GetHandler(); + if (fa->mem_handler == NULL) { + return NULL; + } + fa->data = PyDataMem_UserNEW(num, PyArray_HANDLER(self)); if (PyArray_DATA(self) == NULL) { + Py_DECREF(fa->mem_handler); return PyErr_NoMemory(); } if (PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_NEEDS_INIT)) { @@ -2144,6 +2184,7 @@ array_setstate(PyArrayObject *self, PyObject *args) PyArray_ENABLEFLAGS(self, NPY_ARRAY_OWNDATA); fa->base = NULL; if (_setlist_pkl(self, rawdata) < 0) { + Py_DECREF(fa->mem_handler); return NULL; } } diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index d211f01bc..dea828ed9 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4433,6 +4433,9 @@ static struct PyMethodDef array_module_methods[] = { {"geterrobj", (PyCFunction) ufunc_geterr, METH_VARARGS, NULL}, + {"get_handler_name", + (PyCFunction) get_handler_name, + METH_VARARGS, NULL}, {"_add_newdoc_ufunc", (PyCFunction)add_newdoc_ufunc, METH_VARARGS, NULL}, {"_get_sfloat_dtype", @@ -4910,6 +4913,20 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { if (initumath(m) != 0) { goto err; } +#if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) + /* + * Initialize the context-local PyDataMem_Handler capsule. + */ + c_api = PyCapsule_New(&default_handler, "mem_handler", NULL); + if (c_api == NULL) { + goto err; + } + current_handler = PyContextVar_New("current_allocator", c_api); + Py_DECREF(c_api); + if (current_handler == NULL) { + goto err; + } +#endif return m; err: diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src index 56f17431a..0d52211a8 100644 --- a/numpy/core/src/multiarray/scalartypes.c.src +++ b/numpy/core/src/multiarray/scalartypes.c.src @@ -34,6 +34,16 @@ #include "binop_override.h" +/* + * used for allocating a single scalar, so use the default numpy + * memory allocators instead of the (maybe) user overrides + */ +NPY_NO_EXPORT void * +npy_alloc_cache_zero(size_t nmemb, size_t size); + +NPY_NO_EXPORT void +npy_free_cache(void * p, npy_uintp sz); + NPY_NO_EXPORT PyBoolScalarObject _PyArrayScalar_BoolValues[] = { {PyObject_HEAD_INIT(&PyBoolArrType_Type) 0}, {PyObject_HEAD_INIT(&PyBoolArrType_Type) 1}, @@ -1321,7 +1331,7 @@ gentype_imag_get(PyObject *self, void *NPY_UNUSED(ignored)) int elsize; typecode = PyArray_DescrFromScalar(self); elsize = typecode->elsize; - temp = npy_alloc_cache_zero(elsize); + temp = npy_alloc_cache_zero(1, elsize); ret = PyArray_Scalar(temp, typecode, NULL); npy_free_cache(temp, elsize); } @@ -3151,7 +3161,10 @@ void_arrtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) (int) NPY_MAX_INT); return NULL; } - destptr = npy_alloc_cache_zero(memu); + if (memu == 0) { + memu = 1; + } + destptr = npy_alloc_cache_zero(memu, 1); if (destptr == NULL) { return PyErr_NoMemory(); } diff --git a/numpy/core/src/multiarray/shape.c b/numpy/core/src/multiarray/shape.c index 5a4e8c0f3..162abd6a4 100644 --- a/numpy/core/src/multiarray/shape.c +++ b/numpy/core/src/multiarray/shape.c @@ -121,8 +121,16 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, } /* Reallocate space if needed - allocating 0 is forbidden */ - new_data = PyDataMem_RENEW( - PyArray_DATA(self), newnbytes == 0 ? elsize : newnbytes); + PyObject *handler = PyArray_HANDLER(self); + if (handler == NULL) { + /* This can happen if someone arbitrarily sets NPY_ARRAY_OWNDATA */ + PyErr_SetString(PyExc_RuntimeError, + "no memory handler found but OWNDATA flag set"); + return NULL; + } + new_data = PyDataMem_UserRENEW(PyArray_DATA(self), + newnbytes == 0 ? elsize : newnbytes, + handler); if (new_data == NULL) { PyErr_SetString(PyExc_MemoryError, "cannot allocate memory for array"); diff --git a/numpy/core/tests/test_mem_policy.py b/numpy/core/tests/test_mem_policy.py new file mode 100644 index 000000000..a2ff98ceb --- /dev/null +++ b/numpy/core/tests/test_mem_policy.py @@ -0,0 +1,340 @@ +import asyncio +import gc +import pytest +import numpy as np +import threading +import warnings +from numpy.testing import extbuild, assert_warns +import sys + + +@pytest.fixture +def get_module(tmp_path): + """ Add a memory policy that returns a false pointer 64 bytes into the + actual allocation, and fill the prefix with some text. Then check at each + memory manipulation that the prefix exists, to make sure all alloc/realloc/ + free/calloc go via the functions here. + """ + if sys.platform.startswith('cygwin'): + pytest.skip('link fails on cygwin') + functions = [ + ("set_secret_data_policy", "METH_NOARGS", """ + PyObject *secret_data = + PyCapsule_New(&secret_data_handler, "mem_handler", NULL); + if (secret_data == NULL) { + return NULL; + } + PyObject *old = PyDataMem_SetHandler(secret_data); + Py_DECREF(secret_data); + return old; + """), + ("set_old_policy", "METH_O", """ + PyObject *old; + if (args != NULL && PyCapsule_CheckExact(args)) { + old = PyDataMem_SetHandler(args); + } + else { + old = PyDataMem_SetHandler(NULL); + } + if (old == NULL) { + return NULL; + } + Py_DECREF(old); + Py_RETURN_NONE; + """), + ("get_array", "METH_NOARGS", """ + char *buf = (char *)malloc(20); + npy_intp dims[1]; + dims[0] = 20; + PyArray_Descr *descr = PyArray_DescrNewFromType(NPY_UINT8); + return PyArray_NewFromDescr(&PyArray_Type, descr, 1, dims, NULL, + buf, NPY_ARRAY_WRITEABLE, NULL); + """), + ("set_own", "METH_O", """ + if (!PyArray_Check(args)) { + PyErr_SetString(PyExc_ValueError, + "need an ndarray"); + return NULL; + } + PyArray_ENABLEFLAGS((PyArrayObject*)args, NPY_ARRAY_OWNDATA); + // Maybe try this too? + // PyArray_BASE(PyArrayObject *)args) = NULL; + Py_RETURN_NONE; + """), + ] + prologue = ''' + #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION + #include + /* + * This struct allows the dynamic configuration of the allocator funcs + * of the `secret_data_allocator`. It is provided here for + * demonstration purposes, as a valid `ctx` use-case scenario. + */ + typedef struct { + void *(*malloc)(size_t); + void *(*calloc)(size_t, size_t); + void *(*realloc)(void *, size_t); + void (*free)(void *); + } SecretDataAllocatorFuncs; + + NPY_NO_EXPORT void * + shift_alloc(void *ctx, size_t sz) { + SecretDataAllocatorFuncs *funcs = (SecretDataAllocatorFuncs *)ctx; + char *real = (char *)funcs->malloc(sz + 64); + if (real == NULL) { + return NULL; + } + snprintf(real, 64, "originally allocated %ld", (unsigned long)sz); + return (void *)(real + 64); + } + NPY_NO_EXPORT void * + shift_zero(void *ctx, size_t sz, size_t cnt) { + SecretDataAllocatorFuncs *funcs = (SecretDataAllocatorFuncs *)ctx; + char *real = (char *)funcs->calloc(sz + 64, cnt); + if (real == NULL) { + return NULL; + } + snprintf(real, 64, "originally allocated %ld via zero", + (unsigned long)sz); + return (void *)(real + 64); + } + NPY_NO_EXPORT void + shift_free(void *ctx, void * p, npy_uintp sz) { + SecretDataAllocatorFuncs *funcs = (SecretDataAllocatorFuncs *)ctx; + if (p == NULL) { + return ; + } + char *real = (char *)p - 64; + if (strncmp(real, "originally allocated", 20) != 0) { + fprintf(stdout, "uh-oh, unmatched shift_free, " + "no appropriate prefix\\n"); + /* Make C runtime crash by calling free on the wrong address */ + funcs->free((char *)p + 10); + /* funcs->free(real); */ + } + else { + npy_uintp i = (npy_uintp)atoi(real +20); + if (i != sz) { + fprintf(stderr, "uh-oh, unmatched shift_free" + "(ptr, %ld) but allocated %ld\\n", sz, i); + /* This happens in some places, only print */ + funcs->free(real); + } + else { + funcs->free(real); + } + } + } + NPY_NO_EXPORT void * + shift_realloc(void *ctx, void * p, npy_uintp sz) { + SecretDataAllocatorFuncs *funcs = (SecretDataAllocatorFuncs *)ctx; + if (p != NULL) { + char *real = (char *)p - 64; + if (strncmp(real, "originally allocated", 20) != 0) { + fprintf(stdout, "uh-oh, unmatched shift_realloc\\n"); + return realloc(p, sz); + } + return (void *)((char *)funcs->realloc(real, sz + 64) + 64); + } + else { + char *real = (char *)funcs->realloc(p, sz + 64); + if (real == NULL) { + return NULL; + } + snprintf(real, 64, "originally allocated " + "%ld via realloc", (unsigned long)sz); + return (void *)(real + 64); + } + } + /* As an example, we use the standard {m|c|re}alloc/free funcs. */ + static SecretDataAllocatorFuncs secret_data_handler_ctx = { + malloc, + calloc, + realloc, + free + }; + static PyDataMem_Handler secret_data_handler = { + "secret_data_allocator", + { + &secret_data_handler_ctx, /* ctx */ + shift_alloc, /* malloc */ + shift_zero, /* calloc */ + shift_realloc, /* realloc */ + shift_free /* free */ + } + }; + ''' + more_init = "import_array();" + try: + import mem_policy + return mem_policy + except ImportError: + pass + # if it does not exist, build and load it + return extbuild.build_and_import_extension('mem_policy', + functions, + prologue=prologue, + include_dirs=[np.get_include()], + build_dir=tmp_path, + more_init=more_init) + + +def test_set_policy(get_module): + + get_handler_name = np.core.multiarray.get_handler_name + orig_policy_name = get_handler_name() + + a = np.arange(10).reshape((2, 5)) # a doesn't own its own data + assert get_handler_name(a) is None + assert get_handler_name(a.base) == orig_policy_name + + orig_policy = get_module.set_secret_data_policy() + + b = np.arange(10).reshape((2, 5)) # b doesn't own its own data + assert get_handler_name(b) is None + assert get_handler_name(b.base) == 'secret_data_allocator' + + if orig_policy_name == 'default_allocator': + get_module.set_old_policy(None) # tests PyDataMem_SetHandler(NULL) + assert get_handler_name() == 'default_allocator' + else: + get_module.set_old_policy(orig_policy) + assert get_handler_name() == orig_policy_name + + +def test_policy_propagation(get_module): + # The memory policy goes hand-in-hand with flags.owndata + + class MyArr(np.ndarray): + pass + + get_handler_name = np.core.multiarray.get_handler_name + orig_policy_name = get_handler_name() + a = np.arange(10).view(MyArr).reshape((2, 5)) + assert get_handler_name(a) is None + assert a.flags.owndata is False + + assert get_handler_name(a.base) is None + assert a.base.flags.owndata is False + + assert get_handler_name(a.base.base) == orig_policy_name + assert a.base.base.flags.owndata is True + + +async def concurrent_context1(get_module, orig_policy_name, event): + if orig_policy_name == 'default_allocator': + get_module.set_secret_data_policy() + assert np.core.multiarray.get_handler_name() == 'secret_data_allocator' + else: + get_module.set_old_policy(None) + assert np.core.multiarray.get_handler_name() == 'default_allocator' + event.set() + + +async def concurrent_context2(get_module, orig_policy_name, event): + await event.wait() + # the policy is not affected by changes in parallel contexts + assert np.core.multiarray.get_handler_name() == orig_policy_name + # change policy in the child context + if orig_policy_name == 'default_allocator': + get_module.set_secret_data_policy() + assert np.core.multiarray.get_handler_name() == 'secret_data_allocator' + else: + get_module.set_old_policy(None) + assert np.core.multiarray.get_handler_name() == 'default_allocator' + + +async def async_test_context_locality(get_module): + orig_policy_name = np.core.multiarray.get_handler_name() + + event = asyncio.Event() + # the child contexts inherit the parent policy + concurrent_task1 = asyncio.create_task( + concurrent_context1(get_module, orig_policy_name, event)) + concurrent_task2 = asyncio.create_task( + concurrent_context2(get_module, orig_policy_name, event)) + await concurrent_task1 + await concurrent_task2 + + # the parent context is not affected by child policy changes + assert np.core.multiarray.get_handler_name() == orig_policy_name + + +def test_context_locality(get_module): + if (sys.implementation.name == 'pypy' + and sys.pypy_version_info[:3] < (7, 3, 6)): + pytest.skip('no context-locality support in PyPy < 7.3.6') + asyncio.run(async_test_context_locality(get_module)) + + +def concurrent_thread1(get_module, event): + get_module.set_secret_data_policy() + assert np.core.multiarray.get_handler_name() == 'secret_data_allocator' + event.set() + + +def concurrent_thread2(get_module, event): + event.wait() + # the policy is not affected by changes in parallel threads + assert np.core.multiarray.get_handler_name() == 'default_allocator' + # change policy in the child thread + get_module.set_secret_data_policy() + + +def test_thread_locality(get_module): + orig_policy_name = np.core.multiarray.get_handler_name() + + event = threading.Event() + # the child threads do not inherit the parent policy + concurrent_task1 = threading.Thread(target=concurrent_thread1, + args=(get_module, event)) + concurrent_task2 = threading.Thread(target=concurrent_thread2, + args=(get_module, event)) + concurrent_task1.start() + concurrent_task2.start() + concurrent_task1.join() + concurrent_task2.join() + + # the parent thread is not affected by child policy changes + assert np.core.multiarray.get_handler_name() == orig_policy_name + + +@pytest.mark.slow +def test_new_policy(get_module): + a = np.arange(10) + orig_policy_name = np.core.multiarray.get_handler_name(a) + + orig_policy = get_module.set_secret_data_policy() + + b = np.arange(10) + assert np.core.multiarray.get_handler_name(b) == 'secret_data_allocator' + + # test array manipulation. This is slow + if orig_policy_name == 'default_allocator': + # when the np.core.test tests recurse into this test, the + # policy will be set so this "if" will be false, preventing + # infinite recursion + # + # if needed, debug this by + # - running tests with -- -s (to not capture stdout/stderr + # - setting extra_argv=['-vv'] here + assert np.core.test('full', verbose=2, extra_argv=['-vv']) + # also try the ma tests, the pickling test is quite tricky + assert np.ma.test('full', verbose=2, extra_argv=['-vv']) + + get_module.set_old_policy(orig_policy) + + c = np.arange(10) + assert np.core.multiarray.get_handler_name(c) == orig_policy_name + +def test_switch_owner(get_module): + a = get_module.get_array() + assert np.core.multiarray.get_handler_name(a) is None + get_module.set_own(a) + with warnings.catch_warnings(): + warnings.filterwarnings('always') + # The policy should be NULL, so we have to assume we can call "free" + with assert_warns(RuntimeWarning) as w: + del a + gc.collect() + print(w) diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index 79f44ef80..ed775cac6 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -9,7 +9,7 @@ import numpy.core._multiarray_tests as _multiarray_tests from numpy import array, arange, nditer, all from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_raises, - HAS_REFCOUNT, suppress_warnings + HAS_REFCOUNT, suppress_warnings, break_cycles ) @@ -3150,6 +3150,8 @@ def test_partial_iteration_cleanup(in_dtype, buf_dtype, steps): # Note that resetting does not free references del it + break_cycles() + break_cycles() assert count == sys.getrefcount(value) # Repeat the test with `iternext` @@ -3159,6 +3161,8 @@ def test_partial_iteration_cleanup(in_dtype, buf_dtype, steps): it.iternext() del it # should ensure cleanup + break_cycles() + break_cycles() assert count == sys.getrefcount(value) diff --git a/numpy/ma/mrecords.py b/numpy/ma/mrecords.py index 2ce1f0a23..1e8103bcf 100644 --- a/numpy/ma/mrecords.py +++ b/numpy/ma/mrecords.py @@ -493,7 +493,6 @@ def _mrreconstruct(subtype, baseclass, baseshape, basetype,): _mask = ndarray.__new__(ndarray, baseshape, 'b1') return subtype.__new__(subtype, _data, mask=_mask, dtype=basetype,) - mrecarray = MaskedRecords diff --git a/numpy/testing/__init__.py b/numpy/testing/__init__.py index bca1d3670..a008f5828 100644 --- a/numpy/testing/__init__.py +++ b/numpy/testing/__init__.py @@ -10,7 +10,7 @@ from unittest import TestCase from ._private.utils import * from ._private.utils import (_assert_valid_refcount, _gen_alignment_data, IS_PYSTON) -from ._private import decorators as dec +from ._private import extbuild, decorators as dec from ._private.nosetester import ( run_module_suite, NoseTester as Tester ) diff --git a/numpy/testing/_private/extbuild.py b/numpy/testing/_private/extbuild.py new file mode 100644 index 000000000..8b3a438dd --- /dev/null +++ b/numpy/testing/_private/extbuild.py @@ -0,0 +1,251 @@ +""" +Build a c-extension module on-the-fly in tests. +See build_and_import_extensions for usage hints + +""" + +import os +import pathlib +import sys +import sysconfig +from numpy.distutils.ccompiler import new_compiler +from distutils.errors import CompileError + +__all__ = ['build_and_import_extension', 'compile_extension_module'] + + +def build_and_import_extension( + modname, functions, *, prologue="", build_dir=None, + include_dirs=[], more_init=""): + """ + Build and imports a c-extension module `modname` from a list of function + fragments `functions`. + + + Parameters + ---------- + functions : list of fragments + Each fragment is a sequence of func_name, calling convention, snippet. + prologue : string + Code to preceed the rest, usually extra ``#include`` or ``#define`` + macros. + build_dir : pathlib.Path + Where to build the module, usually a temporary directory + include_dirs : list + Extra directories to find include files when compiling + more_init : string + Code to appear in the module PyMODINIT_FUNC + + Returns + ------- + out: module + The module will have been loaded and is ready for use + + Examples + -------- + >>> functions = [("test_bytes", "METH_O", \"\"\" + if ( !PyBytesCheck(args)) { + Py_RETURN_FALSE; + } + Py_RETURN_TRUE; + \"\"\")] + >>> mod = build_and_import_extension("testme", functions) + >>> assert not mod.test_bytes(u'abc') + >>> assert mod.test_bytes(b'abc') + """ + + body = prologue + _make_methods(functions, modname) + init = """PyObject *mod = PyModule_Create(&moduledef); + """ + if not build_dir: + build_dir = pathlib.Path('.') + if more_init: + init += """#define INITERROR return NULL + """ + init += more_init + init += "\nreturn mod;" + source_string = _make_source(modname, init, body) + try: + mod_so = compile_extension_module( + modname, build_dir, include_dirs, source_string) + except CompileError as e: + # shorten the exception chain + raise RuntimeError(f"could not compile in {build_dir}:") from e + import importlib.util + spec = importlib.util.spec_from_file_location(modname, mod_so) + foo = importlib.util.module_from_spec(spec) + spec.loader.exec_module(foo) + return foo + + +def compile_extension_module( + name, builddir, include_dirs, + source_string, libraries=[], library_dirs=[]): + """ + Build an extension module and return the filename of the resulting + native code file. + + Parameters + ---------- + name : string + name of the module, possibly including dots if it is a module inside a + package. + builddir : pathlib.Path + Where to build the module, usually a temporary directory + include_dirs : list + Extra directories to find include files when compiling + libraries : list + Libraries to link into the extension module + library_dirs: list + Where to find the libraries, ``-L`` passed to the linker + """ + modname = name.split('.')[-1] + dirname = builddir / name + dirname.mkdir(exist_ok=True) + cfile = _convert_str_to_file(source_string, dirname) + include_dirs = [sysconfig.get_config_var('INCLUDEPY')] + include_dirs + + return _c_compile( + cfile, outputfilename=dirname / modname, + include_dirs=include_dirs, libraries=[], library_dirs=[], + ) + + +def _convert_str_to_file(source, dirname): + """Helper function to create a file ``source.c`` in `dirname` that contains + the string in `source`. Returns the file name + """ + filename = dirname / 'source.c' + with filename.open('w') as f: + f.write(str(source)) + return filename + + +def _make_methods(functions, modname): + """ Turns the name, signature, code in functions into complete functions + and lists them in a methods_table. Then turns the methods_table into a + ``PyMethodDef`` structure and returns the resulting code fragment ready + for compilation + """ + methods_table = [] + codes = [] + for funcname, flags, code in functions: + cfuncname = "%s_%s" % (modname, funcname) + if 'METH_KEYWORDS' in flags: + signature = '(PyObject *self, PyObject *args, PyObject *kwargs)' + else: + signature = '(PyObject *self, PyObject *args)' + methods_table.append( + "{\"%s\", (PyCFunction)%s, %s}," % (funcname, cfuncname, flags)) + func_code = """ + static PyObject* {cfuncname}{signature} + {{ + {code} + }} + """.format(cfuncname=cfuncname, signature=signature, code=code) + codes.append(func_code) + + body = "\n".join(codes) + """ + static PyMethodDef methods[] = { + %(methods)s + { NULL } + }; + static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "%(modname)s", /* m_name */ + NULL, /* m_doc */ + -1, /* m_size */ + methods, /* m_methods */ + }; + """ % dict(methods='\n'.join(methods_table), modname=modname) + return body + + +def _make_source(name, init, body): + """ Combines the code fragments into source code ready to be compiled + """ + code = """ + #include + + %(body)s + + PyMODINIT_FUNC + PyInit_%(name)s(void) { + %(init)s + } + """ % dict( + name=name, init=init, body=body, + ) + return code + + +def _c_compile(cfile, outputfilename, include_dirs=[], libraries=[], + library_dirs=[]): + if sys.platform == 'win32': + compile_extra = ["/we4013"] + link_extra = ["/LIBPATH:" + os.path.join(sys.exec_prefix, 'libs')] + elif sys.platform.startswith('linux'): + compile_extra = [ + "-O0", "-g", "-Werror=implicit-function-declaration", "-fPIC"] + link_extra = None + else: + compile_extra = link_extra = None + pass + if sys.platform == 'win32': + link_extra = link_extra + ['/DEBUG'] # generate .pdb file + if sys.platform == 'darwin': + # support Fink & Darwinports + for s in ('/sw/', '/opt/local/'): + if (s + 'include' not in include_dirs + and os.path.exists(s + 'include')): + include_dirs.append(s + 'include') + if s + 'lib' not in library_dirs and os.path.exists(s + 'lib'): + library_dirs.append(s + 'lib') + + outputfilename = outputfilename.with_suffix(get_so_suffix()) + saved_environ = os.environ.copy() + try: + build( + cfile, outputfilename, + compile_extra, link_extra, + include_dirs, libraries, library_dirs) + finally: + # workaround for a distutils bugs where some env vars can + # become longer and longer every time it is used + for key, value in saved_environ.items(): + if os.environ.get(key) != value: + os.environ[key] = value + return outputfilename + + +def build(cfile, outputfilename, compile_extra, link_extra, + include_dirs, libraries, library_dirs): + "cd into the directory where the cfile is, use distutils to build" + + compiler = new_compiler(force=1, verbose=2) + compiler.customize('') + objects = [] + + old = os.getcwd() + os.chdir(cfile.parent) + try: + res = compiler.compile( + [str(cfile.name)], + include_dirs=include_dirs, + extra_preargs=compile_extra + ) + objects += [str(cfile.parent / r) for r in res] + finally: + os.chdir(old) + + compiler.link_shared_object( + objects, str(outputfilename), + libraries=libraries, + extra_preargs=link_extra, + library_dirs=library_dirs) + + +def get_so_suffix(): + ret = sysconfig.get_config_var('EXT_SUFFIX') + assert ret + return ret -- cgit v1.2.1 From 7624b4ef3560114424abccec8db90385465e9b42 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Mon, 25 Oct 2021 14:07:44 -0700 Subject: BUG: Don't pass /arch:SSE2 to MSVC when targeting x64 (#20190) * Don't pass /arch:SSE2 to MSVC link targeting x64 * Fix test * Fix format * Fix one more test --- numpy/distutils/ccompiler_opt.py | 4 ++-- numpy/distutils/tests/test_ccompiler_opt.py | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/ccompiler_opt.py b/numpy/distutils/ccompiler_opt.py index 8f1768f77..d7df386fe 100644 --- a/numpy/distutils/ccompiler_opt.py +++ b/numpy/distutils/ccompiler_opt.py @@ -418,8 +418,8 @@ class _Config: AVX512_ICL = dict(flags="/Qx:ICELAKE-CLIENT") ) if on_x86 and self.cc_is_msvc: return dict( - SSE = dict(flags="/arch:SSE"), - SSE2 = dict(flags="/arch:SSE2"), + SSE = dict(flags="/arch:SSE") if self.cc_on_x86 else {}, + SSE2 = dict(flags="/arch:SSE2") if self.cc_on_x86 else {}, SSE3 = {}, SSSE3 = {}, SSE41 = {}, diff --git a/numpy/distutils/tests/test_ccompiler_opt.py b/numpy/distutils/tests/test_ccompiler_opt.py index 9c54ed66b..1b27ab07c 100644 --- a/numpy/distutils/tests/test_ccompiler_opt.py +++ b/numpy/distutils/tests/test_ccompiler_opt.py @@ -434,7 +434,8 @@ class _Test_CCompilerOpt: self.expect_flags( "sse sse2 vsx vsx2 neon neon_fp16", x86_gcc="-msse -msse2", x86_icc="-msse -msse2", - x86_iccw="/arch:SSE2", x86_msvc="/arch:SSE2", + x86_iccw="/arch:SSE2", + x86_msvc="/arch:SSE2" if self.march() == "x86" else "", ppc64_gcc= "-mcpu=power8", ppc64_clang="-maltivec -mvsx -mpower8-vector", armhf_gcc="-mfpu=neon-fp16 -mfp16-format=ieee", @@ -636,7 +637,8 @@ class _Test_CCompilerOpt: x86_gcc="avx512f avx2 sse42 sse41 sse2", x86_icc="avx512f avx2 sse42 sse41 sse2", x86_iccw="avx512f avx2 sse42 sse41 sse2", - x86_msvc="avx512f avx2 sse2", + x86_msvc="avx512f avx2 sse2" + if self.march() == 'x86' else "avx512f avx2", ppc64="vsx3 vsx2", armhf="asimddp asimd neon_vfpv4 neon", # neon, neon_vfpv4, asimd implies each other -- cgit v1.2.1 From 3263cc6ef528cdd48bd72651ab735dd75c3339c2 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Tue, 26 Oct 2021 07:19:44 +0100 Subject: Apply suggestions from code review Co-authored-by: Matti Picus --- numpy/core/setup.py | 1 + numpy/random/setup.py | 2 ++ 2 files changed, 3 insertions(+) (limited to 'numpy') diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 7bf70ed88..3e1ed4c9b 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -704,6 +704,7 @@ def configuration(parent_package='',top_path=None): we are building the library. """ if build_cmd.compiler.compiler_type == 'msvc': + # explicitly disable whole-program optimization return ['/GL-'] return [] diff --git a/numpy/random/setup.py b/numpy/random/setup.py index 59a30d2b6..866c0cb2f 100644 --- a/numpy/random/setup.py +++ b/numpy/random/setup.py @@ -73,7 +73,9 @@ def configuration(parent_package='', top_path=None): distutils build command, so use this deferred calculation to run when we are building the library. """ + # Keep in sync with numpy/core/setup.py if build_cmd.compiler.compiler_type == 'msvc': + # explicitly disable whole-program optimization return ['/GL-'] return [] -- cgit v1.2.1 From 76852d9694256625d4b283242c34369139785ea1 Mon Sep 17 00:00:00 2001 From: Arushi Sharma <40266350+arushi-08@users.noreply.github.com> Date: Tue, 26 Oct 2021 16:28:04 +0530 Subject: DOC: updated file object docstring --- numpy/lib/npyio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 3ae9b0e63..85e26f094 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -285,7 +285,7 @@ def load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, ---------- file : file-like object, string, or pathlib.Path The file to read. File-like objects must support the - ``seek()`` and ``read()`` methods. The file object must always + ``seek()`` and ``read()`` methods and must always be opened in binary mode. Pickled files require that the file-like object support the ``readline()`` method as well. mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional -- cgit v1.2.1 From dd70e4eb486fdbd42f78d7f197610f524d4f5ca1 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 25 Oct 2021 17:29:30 +0200 Subject: ENH: Add dtype-support to `np.core.numeric` --- numpy/core/numeric.pyi | 532 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 473 insertions(+), 59 deletions(-) (limited to 'numpy') diff --git a/numpy/core/numeric.pyi b/numpy/core/numeric.pyi index 54ab4b7c8..d7ec30351 100644 --- a/numpy/core/numeric.pyi +++ b/numpy/core/numeric.pyi @@ -1,6 +1,5 @@ from typing import ( Any, - Optional, Union, Sequence, Tuple, @@ -8,18 +7,64 @@ from typing import ( List, overload, TypeVar, - Iterable, Literal, + Type, + SupportsAbs, + SupportsIndex, + NoReturn, ) +from typing_extensions import TypeGuard -from numpy import ndarray, generic, dtype, bool_, signedinteger, _OrderKACF, _OrderCF -from numpy.typing import ArrayLike, DTypeLike, _ShapeLike +from numpy import ( + ComplexWarning as ComplexWarning, + dtype, + generic, + unsignedinteger, + signedinteger, + floating, + complexfloating, + bool_, + int_, + intp, + float64, + timedelta64, + object_, + _OrderKACF, + _OrderCF, +) + +from numpy.typing import ( + ArrayLike, + NDArray, + DTypeLike, + _ShapeLike, + _SupportsDType, + _FiniteNestedSequence, + _SupportsArray, + _ScalarLike_co, + _ArrayLikeBool_co, + _ArrayLikeUInt_co, + _ArrayLikeInt_co, + _ArrayLikeFloat_co, + _ArrayLikeComplex_co, + _ArrayLikeTD64_co, + _ArrayLikeObject_co, +) _T = TypeVar("_T") -_ArrayType = TypeVar("_ArrayType", bound=ndarray) +_SCT = TypeVar("_SCT", bound=generic) +_ArrayType = TypeVar("_ArrayType", bound=NDArray[Any]) +_DTypeLike = Union[ + dtype[_SCT], + Type[_SCT], + _SupportsDType[dtype[_SCT]], +] +_ArrayLike = _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]] _CorrelateMode = Literal["valid", "same", "full"] +__all__: List[str] + @overload def zeros_like( a: _ArrayType, @@ -30,20 +75,61 @@ def zeros_like( ) -> _ArrayType: ... @overload def zeros_like( - a: ArrayLike, - dtype: DTypeLike = ..., + a: _ArrayLike[_SCT], + dtype: None = ..., order: _OrderKACF = ..., subok: bool = ..., - shape: Optional[_ShapeLike] = ..., -) -> ndarray: ... + shape: None | _ShapeLike = ..., +) -> NDArray[_SCT]: ... +@overload +def zeros_like( + a: object, + dtype: None = ..., + order: _OrderKACF = ..., + subok: bool = ..., + shape: None | _ShapeLike= ..., +) -> NDArray[Any]: ... +@overload +def zeros_like( + a: Any, + dtype: _DTypeLike[_SCT], + order: _OrderKACF = ..., + subok: bool = ..., + shape: None | _ShapeLike= ..., +) -> NDArray[_SCT]: ... +@overload +def zeros_like( + a: Any, + dtype: DTypeLike, + order: _OrderKACF = ..., + subok: bool = ..., + shape: None | _ShapeLike= ..., +) -> NDArray[Any]: ... +@overload def ones( shape: _ShapeLike, - dtype: DTypeLike = ..., + dtype: None = ..., + order: _OrderCF = ..., + *, + like: ArrayLike = ..., +) -> NDArray[float64]: ... +@overload +def ones( + shape: _ShapeLike, + dtype: _DTypeLike[_SCT], order: _OrderCF = ..., *, like: ArrayLike = ..., -) -> ndarray: ... +) -> NDArray[_SCT]: ... +@overload +def ones( + shape: _ShapeLike, + dtype: DTypeLike, + order: _OrderCF = ..., + *, + like: ArrayLike = ..., +) -> NDArray[Any]: ... @overload def ones_like( @@ -55,21 +141,64 @@ def ones_like( ) -> _ArrayType: ... @overload def ones_like( - a: ArrayLike, - dtype: DTypeLike = ..., + a: _ArrayLike[_SCT], + dtype: None = ..., order: _OrderKACF = ..., subok: bool = ..., - shape: Optional[_ShapeLike] = ..., -) -> ndarray: ... + shape: None | _ShapeLike = ..., +) -> NDArray[_SCT]: ... +@overload +def ones_like( + a: object, + dtype: None = ..., + order: _OrderKACF = ..., + subok: bool = ..., + shape: None | _ShapeLike= ..., +) -> NDArray[Any]: ... +@overload +def ones_like( + a: Any, + dtype: _DTypeLike[_SCT], + order: _OrderKACF = ..., + subok: bool = ..., + shape: None | _ShapeLike= ..., +) -> NDArray[_SCT]: ... +@overload +def ones_like( + a: Any, + dtype: DTypeLike, + order: _OrderKACF = ..., + subok: bool = ..., + shape: None | _ShapeLike= ..., +) -> NDArray[Any]: ... +@overload def full( shape: _ShapeLike, fill_value: Any, - dtype: DTypeLike = ..., + dtype: None = ..., + order: _OrderCF = ..., + *, + like: ArrayLike = ..., +) -> NDArray[Any]: ... +@overload +def full( + shape: _ShapeLike, + fill_value: Any, + dtype: _DTypeLike[_SCT], order: _OrderCF = ..., *, like: ArrayLike = ..., -) -> ndarray: ... +) -> NDArray[_SCT]: ... +@overload +def full( + shape: _ShapeLike, + fill_value: Any, + dtype: DTypeLike, + order: _OrderCF = ..., + *, + like: ArrayLike = ..., +) -> NDArray[Any]: ... @overload def full_like( @@ -82,13 +211,40 @@ def full_like( ) -> _ArrayType: ... @overload def full_like( - a: ArrayLike, + a: _ArrayLike[_SCT], fill_value: Any, - dtype: DTypeLike = ..., + dtype: None = ..., + order: _OrderKACF = ..., + subok: bool = ..., + shape: None | _ShapeLike = ..., +) -> NDArray[_SCT]: ... +@overload +def full_like( + a: object, + fill_value: Any, + dtype: None = ..., + order: _OrderKACF = ..., + subok: bool = ..., + shape: None | _ShapeLike= ..., +) -> NDArray[Any]: ... +@overload +def full_like( + a: Any, + fill_value: Any, + dtype: _DTypeLike[_SCT], + order: _OrderKACF = ..., + subok: bool = ..., + shape: None | _ShapeLike= ..., +) -> NDArray[_SCT]: ... +@overload +def full_like( + a: Any, + fill_value: Any, + dtype: DTypeLike, order: _OrderKACF = ..., subok: bool = ..., - shape: Optional[_ShapeLike] = ..., -) -> ndarray: ... + shape: None | _ShapeLike= ..., +) -> NDArray[Any]: ... @overload def count_nonzero( @@ -105,78 +261,306 @@ def count_nonzero( keepdims: bool = ..., ) -> Any: ... # TODO: np.intp or ndarray[np.intp] -def isfortran(a: Union[ndarray, generic]) -> bool: ... +def isfortran(a: NDArray[Any] | generic) -> bool: ... -def argwhere(a: ArrayLike) -> ndarray: ... +def argwhere(a: ArrayLike) -> NDArray[intp]: ... -def flatnonzero(a: ArrayLike) -> ndarray: ... +def flatnonzero(a: ArrayLike) -> NDArray[intp]: ... +@overload def correlate( - a: ArrayLike, - v: ArrayLike, + a: _ArrayLikeBool_co, + v: _ArrayLikeBool_co, + mode: _CorrelateMode = ..., +) -> NDArray[bool_]: ... +@overload +def correlate( + a: _ArrayLikeUInt_co, + v: _ArrayLikeUInt_co, + mode: _CorrelateMode = ..., +) -> NDArray[unsignedinteger[Any]]: ... +@overload +def correlate( + a: _ArrayLikeInt_co, + v: _ArrayLikeInt_co, mode: _CorrelateMode = ..., -) -> ndarray: ... +) -> NDArray[signedinteger[Any]]: ... +@overload +def correlate( + a: _ArrayLikeFloat_co, + v: _ArrayLikeFloat_co, + mode: _CorrelateMode = ..., +) -> NDArray[floating[Any]]: ... +@overload +def correlate( + a: _ArrayLikeComplex_co, + v: _ArrayLikeComplex_co, + mode: _CorrelateMode = ..., +) -> NDArray[complexfloating[Any, Any]]: ... +@overload +def correlate( + a: _ArrayLikeTD64_co, + v: _ArrayLikeTD64_co, + mode: _CorrelateMode = ..., +) -> NDArray[timedelta64]: ... +@overload +def correlate( + a: _ArrayLikeObject_co, + v: _ArrayLikeObject_co, + mode: _CorrelateMode = ..., +) -> NDArray[object_]: ... +@overload def convolve( - a: ArrayLike, - v: ArrayLike, + a: _ArrayLikeBool_co, + v: _ArrayLikeBool_co, mode: _CorrelateMode = ..., -) -> ndarray: ... +) -> NDArray[bool_]: ... +@overload +def convolve( + a: _ArrayLikeUInt_co, + v: _ArrayLikeUInt_co, + mode: _CorrelateMode = ..., +) -> NDArray[unsignedinteger[Any]]: ... +@overload +def convolve( + a: _ArrayLikeInt_co, + v: _ArrayLikeInt_co, + mode: _CorrelateMode = ..., +) -> NDArray[signedinteger[Any]]: ... +@overload +def convolve( + a: _ArrayLikeFloat_co, + v: _ArrayLikeFloat_co, + mode: _CorrelateMode = ..., +) -> NDArray[floating[Any]]: ... +@overload +def convolve( + a: _ArrayLikeComplex_co, + v: _ArrayLikeComplex_co, + mode: _CorrelateMode = ..., +) -> NDArray[complexfloating[Any, Any]]: ... +@overload +def convolve( + a: _ArrayLikeTD64_co, + v: _ArrayLikeTD64_co, + mode: _CorrelateMode = ..., +) -> NDArray[timedelta64]: ... +@overload +def convolve( + a: _ArrayLikeObject_co, + v: _ArrayLikeObject_co, + mode: _CorrelateMode = ..., +) -> NDArray[object_]: ... @overload def outer( - a: ArrayLike, - b: ArrayLike, + a: _ArrayLikeBool_co, + b: _ArrayLikeBool_co, out: None = ..., -) -> ndarray: ... +) -> NDArray[bool_]: ... @overload def outer( - a: ArrayLike, - b: ArrayLike, - out: _ArrayType = ..., + a: _ArrayLikeUInt_co, + b: _ArrayLikeUInt_co, + out: None = ..., +) -> NDArray[unsignedinteger[Any]]: ... +@overload +def outer( + a: _ArrayLikeInt_co, + b: _ArrayLikeInt_co, + out: None = ..., +) -> NDArray[signedinteger[Any]]: ... +@overload +def outer( + a: _ArrayLikeFloat_co, + b: _ArrayLikeFloat_co, + out: None = ..., +) -> NDArray[floating[Any]]: ... +@overload +def outer( + a: _ArrayLikeComplex_co, + b: _ArrayLikeComplex_co, + out: None = ..., +) -> NDArray[complexfloating[Any, Any]]: ... +@overload +def outer( + a: _ArrayLikeTD64_co, + b: _ArrayLikeTD64_co, + out: None = ..., +) -> NDArray[timedelta64]: ... +@overload +def outer( + a: _ArrayLikeObject_co, + b: _ArrayLikeObject_co, + out: None = ..., +) -> NDArray[object_]: ... +@overload +def outer( + a: _ArrayLikeComplex_co | _ArrayLikeTD64_co | _ArrayLikeObject_co, + b: _ArrayLikeComplex_co | _ArrayLikeTD64_co | _ArrayLikeObject_co, + out: _ArrayType, ) -> _ArrayType: ... +@overload def tensordot( - a: ArrayLike, - b: ArrayLike, - axes: Union[int, Tuple[_ShapeLike, _ShapeLike]] = ..., -) -> ndarray: ... + a: _ArrayLikeBool_co, + b: _ArrayLikeBool_co, + axes: int | Tuple[_ShapeLike, _ShapeLike] = ..., +) -> NDArray[bool_]: ... +@overload +def tensordot( + a: _ArrayLikeUInt_co, + b: _ArrayLikeUInt_co, + axes: int | Tuple[_ShapeLike, _ShapeLike] = ..., +) -> NDArray[unsignedinteger[Any]]: ... +@overload +def tensordot( + a: _ArrayLikeInt_co, + b: _ArrayLikeInt_co, + axes: int | Tuple[_ShapeLike, _ShapeLike] = ..., +) -> NDArray[signedinteger[Any]]: ... +@overload +def tensordot( + a: _ArrayLikeFloat_co, + b: _ArrayLikeFloat_co, + axes: int | Tuple[_ShapeLike, _ShapeLike] = ..., +) -> NDArray[floating[Any]]: ... +@overload +def tensordot( + a: _ArrayLikeComplex_co, + b: _ArrayLikeComplex_co, + axes: int | Tuple[_ShapeLike, _ShapeLike] = ..., +) -> NDArray[complexfloating[Any, Any]]: ... +@overload +def tensordot( + a: _ArrayLikeTD64_co, + b: _ArrayLikeTD64_co, + axes: int | Tuple[_ShapeLike, _ShapeLike] = ..., +) -> NDArray[timedelta64]: ... +@overload +def tensordot( + a: _ArrayLikeObject_co, + b: _ArrayLikeObject_co, + axes: int | Tuple[_ShapeLike, _ShapeLike] = ..., +) -> NDArray[object_]: ... +@overload +def roll( + a: _ArrayLike[_SCT], + shift: _ShapeLike, + axis: None | _ShapeLike = ..., +) -> NDArray[_SCT]: ... +@overload def roll( a: ArrayLike, shift: _ShapeLike, - axis: Optional[_ShapeLike] = ..., -) -> ndarray: ... + axis: None | _ShapeLike = ..., +) -> NDArray[Any]: ... -def rollaxis(a: ndarray, axis: int, start: int = ...) -> ndarray: ... +def rollaxis( + a: NDArray[_SCT], + axis: int, + start: int = ..., +) -> NDArray[_SCT]: ... def moveaxis( - a: ndarray, + a: NDArray[_SCT], source: _ShapeLike, destination: _ShapeLike, -) -> ndarray: ... +) -> NDArray[_SCT]: ... +@overload def cross( - a: ArrayLike, - b: ArrayLike, + a: _ArrayLikeBool_co, + b: _ArrayLikeBool_co, axisa: int = ..., axisb: int = ..., axisc: int = ..., - axis: Optional[int] = ..., -) -> ndarray: ... + axis: None | int = ..., +) -> NoReturn: ... +@overload +def cross( + a: _ArrayLikeUInt_co, + b: _ArrayLikeUInt_co, + axisa: int = ..., + axisb: int = ..., + axisc: int = ..., + axis: None | int = ..., +) -> NDArray[unsignedinteger[Any]]: ... +@overload +def cross( + a: _ArrayLikeInt_co, + b: _ArrayLikeInt_co, + axisa: int = ..., + axisb: int = ..., + axisc: int = ..., + axis: None | int = ..., +) -> NDArray[signedinteger[Any]]: ... +@overload +def cross( + a: _ArrayLikeFloat_co, + b: _ArrayLikeFloat_co, + axisa: int = ..., + axisb: int = ..., + axisc: int = ..., + axis: None | int = ..., +) -> NDArray[floating[Any]]: ... +@overload +def cross( + a: _ArrayLikeComplex_co, + b: _ArrayLikeComplex_co, + axisa: int = ..., + axisb: int = ..., + axisc: int = ..., + axis: None | int = ..., +) -> NDArray[complexfloating[Any, Any]]: ... +@overload +def cross( + a: _ArrayLikeObject_co, + b: _ArrayLikeObject_co, + axisa: int = ..., + axisb: int = ..., + axisc: int = ..., + axis: None | int = ..., +) -> NDArray[object_]: ... @overload def indices( dimensions: Sequence[int], - dtype: DTypeLike = ..., + dtype: Type[int] = ..., sparse: Literal[False] = ..., -) -> ndarray: ... +) -> NDArray[int_]: ... @overload def indices( dimensions: Sequence[int], - dtype: DTypeLike = ..., + dtype: Type[int] = ..., sparse: Literal[True] = ..., -) -> Tuple[ndarray, ...]: ... +) -> Tuple[NDArray[int_], ...]: ... +@overload +def indices( + dimensions: Sequence[int], + dtype: _DTypeLike[_SCT], + sparse: Literal[False] = ..., +) -> NDArray[_SCT]: ... +@overload +def indices( + dimensions: Sequence[int], + dtype: _DTypeLike[_SCT], + sparse: Literal[True], +) -> Tuple[NDArray[_SCT], ...]: ... +@overload +def indices( + dimensions: Sequence[int], + dtype: DTypeLike, + sparse: Literal[False] = ..., +) -> NDArray[Any]: ... +@overload +def indices( + dimensions: Sequence[int], + dtype: DTypeLike, + sparse: Literal[True], +) -> Tuple[NDArray[Any], ...]: ... def fromfunction( function: Callable[..., _T], @@ -187,18 +571,39 @@ def fromfunction( **kwargs: Any, ) -> _T: ... -def isscalar(element: Any) -> bool: ... +def isscalar(element: object) -> TypeGuard[ + generic | bool | int | float | complex | str | bytes | memoryview +]: ... -def binary_repr(num: int, width: Optional[int] = ...) -> str: ... +def binary_repr(num: int, width: None | int = ...) -> str: ... -def base_repr(number: int, base: int = ..., padding: int = ...) -> str: ... +def base_repr( + number: SupportsAbs[float], + base: float = ..., + padding: SupportsIndex = ..., +) -> str: ... +@overload def identity( n: int, - dtype: DTypeLike = ..., + dtype: None = ..., + *, + like: ArrayLike = ..., +) -> NDArray[float64]: ... +@overload +def identity( + n: int, + dtype: _DTypeLike[_SCT], + *, + like: ArrayLike = ..., +) -> NDArray[_SCT]: ... +@overload +def identity( + n: int, + dtype: DTypeLike, *, like: ArrayLike = ..., -) -> ndarray: ... +) -> NDArray[Any]: ... def allclose( a: ArrayLike, @@ -208,13 +613,22 @@ def allclose( equal_nan: bool = ..., ) -> bool: ... +@overload +def isclose( + a: _ScalarLike_co, + b: _ScalarLike_co, + rtol: float = ..., + atol: float = ..., + equal_nan: bool = ..., +) -> bool_: ... +@overload def isclose( a: ArrayLike, b: ArrayLike, rtol: float = ..., atol: float = ..., equal_nan: bool = ..., -) -> Any: ... +) -> NDArray[bool_]: ... def array_equal(a1: ArrayLike, a2: ArrayLike, equal_nan: bool = ...) -> bool: ... -- cgit v1.2.1 From f23139a036695d2458392733c5c270458bc5bdd4 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Tue, 26 Oct 2021 17:55:11 +0200 Subject: TST: Strip the file-extensions from the parametrize ID --- numpy/typing/tests/test_typing.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'numpy') diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index f303ebea3..4dd6530ff 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -98,16 +98,10 @@ def run_mypy() -> None: def get_test_cases(directory: str) -> Iterator[ParameterSet]: for root, _, files in os.walk(directory): for fname in files: - if os.path.splitext(fname)[-1] in (".pyi", ".py"): + short_fname, ext = os.path.splitext(fname) + if ext in (".pyi", ".py"): fullpath = os.path.join(root, fname) - # Use relative path for nice py.test name - relpath = os.path.relpath(fullpath, start=directory) - - yield pytest.param( - fullpath, - # Manually specify a name for the test - id=relpath, - ) + yield pytest.param(fullpath, id=short_fname) @pytest.mark.slow -- cgit v1.2.1 From 6ea6fcbbd70a935b055975b39b175b747205eaf9 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 25 Oct 2021 17:29:41 +0200 Subject: TST: Update the typing tests of `np.core.numeric` --- .../typing/tests/data/fail/array_constructors.pyi | 2 +- .../tests/data/reveal/array_constructors.pyi | 39 +++--- numpy/typing/tests/data/reveal/numeric.pyi | 149 ++++++++++++++------- 3 files changed, 122 insertions(+), 68 deletions(-) (limited to 'numpy') diff --git a/numpy/typing/tests/data/fail/array_constructors.pyi b/numpy/typing/tests/data/fail/array_constructors.pyi index 0e2250513..4f0a60b5b 100644 --- a/numpy/typing/tests/data/fail/array_constructors.pyi +++ b/numpy/typing/tests/data/fail/array_constructors.pyi @@ -10,7 +10,7 @@ np.zeros("test") # E: incompatible type np.zeros() # E: require at least one argument np.ones("test") # E: incompatible type -np.ones() # E: Missing positional argument +np.ones() # E: require at least one argument np.array(0, float, True) # E: No overload variant diff --git a/numpy/typing/tests/data/reveal/array_constructors.pyi b/numpy/typing/tests/data/reveal/array_constructors.pyi index 44c85e988..0aea4ea96 100644 --- a/numpy/typing/tests/data/reveal/array_constructors.pyi +++ b/numpy/typing/tests/data/reveal/array_constructors.pyi @@ -120,32 +120,41 @@ reveal_type(np.logspace(0, 10)) # E: numpy.ndarray[Any, Any] reveal_type(np.geomspace(1, 10)) # E: numpy.ndarray[Any, Any] reveal_type(np.zeros_like(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.zeros_like(C)) # E: numpy.ndarray[Any, Any] -reveal_type(np.zeros_like(B)) # E: SubClass -reveal_type(np.zeros_like(B, dtype=np.int64)) # E: numpy.ndarray[Any, Any] +reveal_type(np.zeros_like(C)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.zeros_like(A, dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.zeros_like(B)) # E: SubClass[{float64}] +reveal_type(np.zeros_like(B, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] reveal_type(np.ones_like(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.ones_like(C)) # E: numpy.ndarray[Any, Any] -reveal_type(np.ones_like(B)) # E: SubClass -reveal_type(np.ones_like(B, dtype=np.int64)) # E: numpy.ndarray[Any, Any] +reveal_type(np.ones_like(C)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.ones_like(A, dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.ones_like(B)) # E: SubClass[{float64}] +reveal_type(np.ones_like(B, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] reveal_type(np.full_like(A, i8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.full_like(C, i8)) # E: numpy.ndarray[Any, Any] +reveal_type(np.full_like(C, i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.full_like(A, i8, dtype=int)) # E: numpy.ndarray[Any, numpy.dtype[Any]] reveal_type(np.full_like(B, i8)) # E: SubClass[{float64}] -reveal_type(np.full_like(B, i8, dtype=np.int64)) # E: numpy.ndarray[Any, Any] +reveal_type(np.full_like(B, i8, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.ones(1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.ones([1, 1, 1])) # E: numpy.ndarray[Any, Any] +reveal_type(np.ones(1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.ones([1, 1, 1])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.ones(5, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.ones(5, dtype=int)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.full(1, i8)) # E: numpy.ndarray[Any, Any] -reveal_type(np.full([1, 1, 1], i8)) # E: numpy.ndarray[Any, Any] +reveal_type(np.full(1, i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.full([1, 1, 1], i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.full(1, i8, dtype=np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.full(1, i8, dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.indices([1, 2, 3])) # E: numpy.ndarray[Any, Any] -reveal_type(np.indices([1, 2, 3], sparse=True)) # E: tuple[numpy.ndarray[Any, Any]] +reveal_type(np.indices([1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(np.indices([1, 2, 3], sparse=True)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{int_}]]] reveal_type(np.fromfunction(func, (3, 5))) # E: SubClass[{float64}] -reveal_type(np.identity(10)) # E: numpy.ndarray[Any, Any] +reveal_type(np.identity(10)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.identity(10, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.identity(10, dtype=int)) # E: numpy.ndarray[Any, numpy.dtype[Any]] reveal_type(np.atleast_1d(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.atleast_1d(C)) # E: numpy.ndarray[Any, numpy.dtype[Any]] diff --git a/numpy/typing/tests/data/reveal/numeric.pyi b/numpy/typing/tests/data/reveal/numeric.pyi index ec6e47ca0..9b3b1419d 100644 --- a/numpy/typing/tests/data/reveal/numeric.pyi +++ b/numpy/typing/tests/data/reveal/numeric.pyi @@ -7,83 +7,128 @@ Does not include tests which fall under ``array_constructors``. from typing import List import numpy as np +import numpy.typing as npt -class SubClass(np.ndarray): +class SubClass(npt.NDArray[np.int64]): ... i8: np.int64 -A: np.ndarray +AR_b: npt.NDArray[np.bool_] +AR_u8: npt.NDArray[np.uint64] +AR_i8: npt.NDArray[np.int64] +AR_f8: npt.NDArray[np.float64] +AR_c16: npt.NDArray[np.complex128] +AR_m: npt.NDArray[np.timedelta64] +AR_O: npt.NDArray[np.object_] + B: List[int] C: SubClass reveal_type(np.count_nonzero(i8)) # E: int -reveal_type(np.count_nonzero(A)) # E: int +reveal_type(np.count_nonzero(AR_i8)) # E: int reveal_type(np.count_nonzero(B)) # E: int -reveal_type(np.count_nonzero(A, keepdims=True)) # E: Any -reveal_type(np.count_nonzero(A, axis=0)) # E: Any +reveal_type(np.count_nonzero(AR_i8, keepdims=True)) # E: Any +reveal_type(np.count_nonzero(AR_i8, axis=0)) # E: Any reveal_type(np.isfortran(i8)) # E: bool -reveal_type(np.isfortran(A)) # E: bool - -reveal_type(np.argwhere(i8)) # E: numpy.ndarray[Any, Any] -reveal_type(np.argwhere(A)) # E: numpy.ndarray[Any, Any] - -reveal_type(np.flatnonzero(i8)) # E: numpy.ndarray[Any, Any] -reveal_type(np.flatnonzero(A)) # E: numpy.ndarray[Any, Any] - -reveal_type(np.correlate(B, A, mode="valid")) # E: numpy.ndarray[Any, Any] -reveal_type(np.correlate(A, A, mode="same")) # E: numpy.ndarray[Any, Any] - -reveal_type(np.convolve(B, A, mode="valid")) # E: numpy.ndarray[Any, Any] -reveal_type(np.convolve(A, A, mode="same")) # E: numpy.ndarray[Any, Any] - -reveal_type(np.outer(i8, A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.outer(B, A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.outer(A, A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.outer(A, A, out=C)) # E: SubClass - -reveal_type(np.tensordot(B, A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.tensordot(A, A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.tensordot(A, A, axes=0)) # E: numpy.ndarray[Any, Any] -reveal_type(np.tensordot(A, A, axes=(0, 1))) # E: numpy.ndarray[Any, Any] +reveal_type(np.isfortran(AR_i8)) # E: bool + +reveal_type(np.argwhere(i8)) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] +reveal_type(np.argwhere(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] + +reveal_type(np.flatnonzero(i8)) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] +reveal_type(np.flatnonzero(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] + +reveal_type(np.correlate(B, AR_i8, mode="valid")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.correlate(AR_i8, AR_i8, mode="same")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.correlate(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.correlate(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] +reveal_type(np.correlate(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.correlate(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.correlate(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(np.correlate(AR_i8, AR_m)) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(np.correlate(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] + +reveal_type(np.convolve(B, AR_i8, mode="valid")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.convolve(AR_i8, AR_i8, mode="same")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.convolve(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.convolve(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] +reveal_type(np.convolve(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.convolve(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.convolve(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(np.convolve(AR_i8, AR_m)) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(np.convolve(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] + +reveal_type(np.outer(i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.outer(B, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.outer(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.outer(AR_i8, AR_i8, out=C)) # E: SubClass +reveal_type(np.outer(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.outer(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] +reveal_type(np.outer(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.convolve(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.outer(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(np.outer(AR_i8, AR_m)) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(np.outer(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] + +reveal_type(np.tensordot(B, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_i8, axes=0)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_i8, axes=(0, 1))) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.tensordot(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.tensordot(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.tensordot(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(np.tensordot(AR_i8, AR_m)) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(np.tensordot(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] reveal_type(np.isscalar(i8)) # E: bool -reveal_type(np.isscalar(A)) # E: bool +reveal_type(np.isscalar(AR_i8)) # E: bool reveal_type(np.isscalar(B)) # E: bool -reveal_type(np.roll(A, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.roll(A, (1, 2))) # E: numpy.ndarray[Any, Any] -reveal_type(np.roll(B, 1)) # E: numpy.ndarray[Any, Any] +reveal_type(np.roll(AR_i8, 1)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.roll(AR_i8, (1, 2))) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.roll(B, 1)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.rollaxis(A, 0, 1)) # E: numpy.ndarray[Any, Any] +reveal_type(np.rollaxis(AR_i8, 0, 1)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.moveaxis(A, 0, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.moveaxis(A, (0, 1), (1, 2))) # E: numpy.ndarray[Any, Any] +reveal_type(np.moveaxis(AR_i8, 0, 1)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.moveaxis(AR_i8, (0, 1), (1, 2))) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.cross(B, A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.cross(A, A)) # E: numpy.ndarray[Any, Any] +reveal_type(np.cross(B, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.cross(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.cross(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] +reveal_type(np.cross(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.cross(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.cross(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(np.cross(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(np.indices([0, 1, 2])) # E: numpy.ndarray[Any, Any] -reveal_type(np.indices([0, 1, 2], sparse=False)) # E: numpy.ndarray[Any, Any] -reveal_type(np.indices([0, 1, 2], sparse=True)) # E: tuple[numpy.ndarray[Any, Any]] +reveal_type(np.indices([0, 1, 2])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(np.indices([0, 1, 2], sparse=True)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{int_}]]] +reveal_type(np.indices([0, 1, 2], dtype=np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.indices([0, 1, 2], sparse=True, dtype=np.float64)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{float64}]]] +reveal_type(np.indices([0, 1, 2], dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.indices([0, 1, 2], sparse=True, dtype=float)) # E: tuple[numpy.ndarray[Any, numpy.dtype[Any]]] reveal_type(np.binary_repr(1)) # E: str reveal_type(np.base_repr(1)) # E: str -reveal_type(np.allclose(i8, A)) # E: bool -reveal_type(np.allclose(B, A)) # E: bool -reveal_type(np.allclose(A, A)) # E: bool +reveal_type(np.allclose(i8, AR_i8)) # E: bool +reveal_type(np.allclose(B, AR_i8)) # E: bool +reveal_type(np.allclose(AR_i8, AR_i8)) # E: bool -reveal_type(np.isclose(i8, A)) # E: Any -reveal_type(np.isclose(B, A)) # E: Any -reveal_type(np.isclose(A, A)) # E: Any +reveal_type(np.isclose(i8, i8)) # E: numpy.bool_ +reveal_type(np.isclose(i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.isclose(B, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.isclose(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.array_equal(i8, A)) # E: bool -reveal_type(np.array_equal(B, A)) # E: bool -reveal_type(np.array_equal(A, A)) # E: bool +reveal_type(np.array_equal(i8, AR_i8)) # E: bool +reveal_type(np.array_equal(B, AR_i8)) # E: bool +reveal_type(np.array_equal(AR_i8, AR_i8)) # E: bool -reveal_type(np.array_equiv(i8, A)) # E: bool -reveal_type(np.array_equiv(B, A)) # E: bool -reveal_type(np.array_equiv(A, A)) # E: bool +reveal_type(np.array_equiv(i8, AR_i8)) # E: bool +reveal_type(np.array_equiv(B, AR_i8)) # E: bool +reveal_type(np.array_equiv(AR_i8, AR_i8)) # E: bool -- cgit v1.2.1 From 58a9038da7e0e76e423cf56434a179620bc0dcb9 Mon Sep 17 00:00:00 2001 From: Arushi Sharma <40266350+arushi-08@users.noreply.github.com> Date: Tue, 26 Oct 2021 23:03:49 +0530 Subject: DOC: Updated random.uniform docstring parameter description (#20146) Add note to the description of the \"high\" parameter to indicate that it is possible for the value to be included in the returned values due to floating point precision, despite the fact that it represents an open interval. Co-authored-by: Ross Barnowski --- numpy/random/_generator.pyx | 6 ++++-- numpy/random/mtrand.pyx | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index 5bacb9f6f..54973100e 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -876,8 +876,10 @@ cdef class Generator: greater than or equal to low. The default value is 0. high : float or array_like of floats Upper boundary of the output interval. All values generated will be - less than high. high - low must be non-negative. The default value - is 1.0. + less than high. The high limit may be included in the returned array of + floats due to floating-point rounding in the equation + ``low + (high-low) * random_sample()``. high - low must be + non-negative. The default value is 1.0. size : int or tuple of ints, optional Output shape. If the given shape is, e.g., ``(m, n, k)``, then ``m * n * k`` samples are drawn. If size is ``None`` (default), diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index 06e75a698..ca539c00e 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -1033,7 +1033,10 @@ cdef class RandomState: greater than or equal to low. The default value is 0. high : float or array_like of floats Upper boundary of the output interval. All values generated will be - less than or equal to high. The default value is 1.0. + less than or equal to high. The high limit may be included in the + returned array of floats due to floating-point rounding in the + equation ``low + (high-low) * random_sample()``. The default value + is 1.0. size : int or tuple of ints, optional Output shape. If the given shape is, e.g., ``(m, n, k)``, then ``m * n * k`` samples are drawn. If size is ``None`` (default), -- cgit v1.2.1 From ba6cd71b14df1ea2694354ac9ce84e8f2d7aa65a Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 26 Oct 2021 15:22:21 -0500 Subject: ENH: Expose promoters and Common-DType API experimentally (#20163) * ENH: Expose promoters and Common-DType API experimentally This is tested in my fork at: https://github.com/seberg/experimental_user_dtypes/pull/4 Which tries most of the functionality newly exported here, by testing a very basic (basically a no-op) promoter. * BUG: Dereference instead of get address in experimental DType API `PyArrayDTypeMeta_Type` should be the struct, but accidentally it was trying to take the reference. That does not work, but my cython code was (until now!) dynamically using `type(np.dtype)` instead, so that it did not notice. --- numpy/core/include/numpy/experimental_dtype_api.h | 122 +++++++++++++++++---- .../src/multiarray/experimental_public_dtype_api.c | 31 +++++- 2 files changed, 128 insertions(+), 25 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/experimental_dtype_api.h b/numpy/core/include/numpy/experimental_dtype_api.h index 22854a725..554c7fb6c 100644 --- a/numpy/core/include/numpy/experimental_dtype_api.h +++ b/numpy/core/include/numpy/experimental_dtype_api.h @@ -16,13 +16,47 @@ * in your module init. (A version mismatch will be reported, just update * to the correct one, this will alert you of possible changes.) * - * The two main symbols exported are: + * The following lists the main symbols currently exported. Please do not + * hesitate to ask for help or clarification: * - * - PyUFunc_AddLoopFromSpec (Register a new loop for a ufunc) - * - PyArrayInitDTypeMeta_FromSpec (Create a new DType) + * - PyUFunc_AddLoopFromSpec: * - * Please check the in-line documentation for details and do not hesitate to - * ask for help. + * Register a new loop for a ufunc. This uses the `PyArrayMethod_Spec` + * which must be filled in (see in-line comments). + * + * - PyUFunc_AddPromoter: + * + * Register a new promoter for a ufunc. A promoter is a function stored + * in a PyCapsule (see in-line comments). It is passed the operation and + * requested DType signatures and can mutate it to attempt a new search + * for a matching loop/promoter. + * I.e. for Numba a promoter could even add the desired loop. + * + * - PyArrayInitDTypeMeta_FromSpec: + * + * Initialize a new DType. It must currently be a static Python C type + * that is declared as `PyArray_DTypeMeta` and not `PyTypeObject`. + * Further, it must subclass `np.dtype` and set its type to + * `PyArrayDTypeMeta_Type` (before calling `PyType_Read()`). + * + * - PyArray_CommonDType: + * + * Find the common-dtype ("promotion") for two DType classes. Similar + * to `np.result_type`, but works on the classes and not instances. + * + * - PyArray_PromoteDTypeSequence: + * + * Same as CommonDType, but works with an arbitrary number of DTypes. + * This function is smarter and can often return successful and unambiguous + * results when `common_dtype(common_dtype(dt1, dt2), dt3)` would + * depend on the operation order or fail. Nevertheless, DTypes should + * aim to ensure that their common-dtype implementation is associative + * and commutative! (Mainly, unsigned and signed integers are not.) + * + * For guaranteed consistent results DTypes must implement common-Dtype + * "transitively". If A promotes B and B promotes C, than A must generally + * also promote C; where "promotes" means implements the promotion. + * (There are some exceptions for abstract DTypes) * * WARNING * ======= @@ -67,11 +101,28 @@ __not_imported(void) printf("*****\nCritical error, dtype API not imported\n*****\n"); } static void *__uninitialized_table[] = { + &__not_imported, &__not_imported, &__not_imported, &__not_imported, &__not_imported, &__not_imported, &__not_imported, &__not_imported}; static void **__experimental_dtype_api_table = __uninitialized_table; + +/* + * DTypeMeta struct, the content may be made fully opaque (except the size). + * We may also move everything into a single `void *dt_slots`. + */ +typedef struct { + PyHeapTypeObject super; + PyArray_Descr *singleton; + int type_num; + PyTypeObject *scalar_type; + npy_uint64 flags; + void *dt_slots; + void *reserved[3]; +} PyArray_DTypeMeta; + + /* * ****************************************************** * ArrayMethod API (Casting and UFuncs) @@ -127,6 +178,28 @@ typedef PyObject *_ufunc_addloop_fromspec_func( (*(_ufunc_addloop_fromspec_func *)(__experimental_dtype_api_table[0])) +/* + * Type of the C promoter function, which must be wrapped into a + * PyCapsule with name "numpy._ufunc_promoter". + */ +typedef int promoter_function(PyObject *ufunc, + PyArray_DTypeMeta *op_dtypes[], PyArray_DTypeMeta *signature[], + PyArray_DTypeMeta *new_op_dtypes[]); + +/* + * Function to register a promoter. + * + * @param ufunc The ufunc object to register the promoter with. + * @param DType_tuple A Python tuple containing DTypes or None matching the + * number of inputs and outputs of the ufunc. + * @param promoter A PyCapsule with name "numpy._ufunc_promoter" containing + * a pointer to a `promoter_function`. + */ +typedef int _ufunc_addpromoter_func( + PyObject *ufunc, PyObject *DType_tuple, PyObject *promoter); +#define PyUFunc_AddPromoter \ + (*(_ufunc_addpromoter_func *)(__experimental_dtype_api_table[1])) + /* * In addition to the normal casting levels, NPY_CAST_IS_VIEW indicates * that no cast operation is necessary at all (although a copy usually will be) @@ -221,24 +294,8 @@ typedef struct{ } PyArrayDTypeMeta_Spec; -/* - * DTypeMeta struct, the content may be made fully opaque (except the size). - * We may also move everything into a single `void *dt_slots`. - */ -typedef struct { - PyHeapTypeObject super; - PyArray_Descr *singleton; - int type_num; - PyTypeObject *scalar_type; - npy_uint64 flags; - void *dt_slots; - void *reserved[3]; -} PyArray_DTypeMeta; - - #define PyArrayDTypeMeta_Type \ - (&(PyTypeObject *)__experimental_dtype_api_table[1]) - + (*(PyTypeObject *)__experimental_dtype_api_table[2]) typedef int __dtypemeta_fromspec( PyArray_DTypeMeta *DType, PyArrayDTypeMeta_Spec *dtype_spec); /* @@ -250,8 +307,25 @@ typedef int __dtypemeta_fromspec( * uses `PyArray_DTypeMeta` defined above as the C-structure. */ #define PyArrayInitDTypeMeta_FromSpec \ - ((__dtypemeta_fromspec *)(__experimental_dtype_api_table[2])) + ((__dtypemeta_fromspec *)(__experimental_dtype_api_table[3])) + + +/* + * ************************************* + * WORKING WITH DTYPES + * ************************************* + */ + +typedef PyArray_DTypeMeta *__common_dtype( + PyArray_DTypeMeta *DType1, PyArray_DTypeMeta *DType2); +#define PyArray_CommonDType \ + ((__common_dtype *)(__experimental_dtype_api_table[4])) + +typedef PyArray_DTypeMeta *__promote_dtype_sequence( + npy_intp num, PyArray_DTypeMeta *DTypes[]); +#define PyArray_PromoteDTypeSequence \ + ((__promote_dtype_sequence *)(__experimental_dtype_api_table[5])) /* @@ -264,7 +338,7 @@ typedef int __dtypemeta_fromspec( * runtime-check this. * You must call this function to use the symbols defined in this file. */ -#define __EXPERIMENTAL_DTYPE_VERSION 1 +#define __EXPERIMENTAL_DTYPE_VERSION 2 static int import_experimental_dtype_api(int version) diff --git a/numpy/core/src/multiarray/experimental_public_dtype_api.c b/numpy/core/src/multiarray/experimental_public_dtype_api.c index 1e8abe9d6..ef5030471 100644 --- a/numpy/core/src/multiarray/experimental_public_dtype_api.c +++ b/numpy/core/src/multiarray/experimental_public_dtype_api.c @@ -13,9 +13,10 @@ #include "dtypemeta.h" #include "array_coercion.h" #include "convert_datatype.h" +#include "common_dtype.h" -#define EXPERIMENTAL_DTYPE_API_VERSION 1 +#define EXPERIMENTAL_DTYPE_API_VERSION 2 typedef struct{ @@ -324,13 +325,41 @@ PyUFunc_AddLoopFromSpec(PyObject *ufunc, PyArrayMethod_Spec *spec) } +static int +PyUFunc_AddPromoter( + PyObject *ufunc, PyObject *DType_tuple, PyObject *promoter) +{ + if (!PyObject_TypeCheck(ufunc, &PyUFunc_Type)) { + PyErr_SetString(PyExc_TypeError, + "ufunc object passed is not a ufunc!"); + return -1; + } + if (!PyCapsule_CheckExact(promoter)) { + PyErr_SetString(PyExc_TypeError, + "promoter must (currently) be a PyCapsule."); + return -1; + } + if (PyCapsule_GetPointer(promoter, "numpy._ufunc_promoter") == NULL) { + return -1; + } + PyObject *info = PyTuple_Pack(2, DType_tuple, promoter); + if (info == NULL) { + return -1; + } + return PyUFunc_AddLoop((PyUFuncObject *)ufunc, info, 0); +} + + NPY_NO_EXPORT PyObject * _get_experimental_dtype_api(PyObject *NPY_UNUSED(mod), PyObject *arg) { static void *experimental_api_table[] = { &PyUFunc_AddLoopFromSpec, + &PyUFunc_AddPromoter, &PyArrayDTypeMeta_Type, &PyArrayInitDTypeMeta_FromSpec, + &PyArray_CommonDType, + &PyArray_PromoteDTypeSequence, NULL, }; -- cgit v1.2.1 From 85f121e86de914a8f24746e7619454c5b6b0ac2a Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 18 Oct 2021 23:43:41 +0200 Subject: DEP: Deprecate `np.MachAr` f --- numpy/__init__.py | 7 +++++++ numpy/__init__.pyi | 14 +++----------- numpy/core/__init__.py | 1 - numpy/core/machar.py | 1 + numpy/core/tests/test_deprecations.py | 8 ++++++++ 5 files changed, 19 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.py b/numpy/__init__.py index 58adeaeab..780ecef11 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -188,12 +188,19 @@ else: n: (getattr(_builtins, n), _msg.format(n=n, extended_msg=extended_msg)) for n, extended_msg in _type_info }) + # Numpy 1.20.0, 2020-10-19 __deprecated_attrs__["typeDict"] = ( core.numerictypes.typeDict, "`np.typeDict` is a deprecated alias for `np.sctypeDict`." ) + # NumPy 1.22, 2021-10-20 + __deprecated_attrs__["MachAr"] = ( + core.machar.MachAr, + "`np.MachAr` is deprecated (NumPy 1.22)." + ) + _msg = ( "`np.{n}` is a deprecated alias for `np.compat.{n}`. " "To silence this warning, use `np.compat.{n}` by itself. " diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 7abf9c62e..e8b18a615 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -665,17 +665,6 @@ test: PytestTester # their annotations are properly implemented # # Placeholders for classes -# TODO: Remove `__getattr__` once the classes are stubbed out -class MachAr: - def __init__( - self, - float_conv: Any = ..., - int_conv: Any = ..., - float_to_float: Any = ..., - float_to_str: Any = ..., - title: Any = ..., - ) -> None: ... - def __getattr__(self, key: str) -> Any: ... # Some of these are aliases; others are wrappers with an identical signature round = around @@ -4335,3 +4324,6 @@ class chararray(ndarray[_ShapeType, _CharDType]): def isupper(self) -> ndarray[_ShapeType, dtype[bool_]]: ... def isnumeric(self) -> ndarray[_ShapeType, dtype[bool_]]: ... def isdecimal(self) -> ndarray[_ShapeType, dtype[bool_]]: ... + +# NOTE: Deprecated +# class MachAr: ... diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py index dad9293e1..e5ef718a9 100644 --- a/numpy/core/__init__.py +++ b/numpy/core/__init__.py @@ -109,7 +109,6 @@ __all__ += fromnumeric.__all__ __all__ += ['record', 'recarray', 'format_parser'] __all__ += ['chararray'] __all__ += function_base.__all__ -__all__ += machar.__all__ __all__ += getlimits.__all__ __all__ += shape_base.__all__ __all__ += einsumfunc.__all__ diff --git a/numpy/core/machar.py b/numpy/core/machar.py index c77be793f..ace19a429 100644 --- a/numpy/core/machar.py +++ b/numpy/core/machar.py @@ -13,6 +13,7 @@ from numpy.core.overrides import set_module # Need to speed this up...especially for longfloat +# Deprecated 2021-10-20, NumPy 1.22 @set_module('numpy') class MachAr: """ diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 898ff8075..d57032e5f 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -1215,3 +1215,11 @@ class TestPartitionBoolIndex(_DeprecationTestCase): def test_not_deprecated(self, func): self.assert_not_deprecated(lambda: func(1)) self.assert_not_deprecated(lambda: func([0, 1])) + + +class TestMachAr(_DeprecationTestCase): + # Deprecated 2021-10-19, NumPy 1.22 + warning_cls = DeprecationWarning + + def test_deprecated(self): + self.assert_deprecated(lambda: np.MachAr) -- cgit v1.2.1 From ae3c21991a617d60d8093e3e958a6b0b5fb0eeb6 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Wed, 20 Oct 2021 11:38:42 +0200 Subject: DEP: Deprecate the `np.core.machar` module --- numpy/__init__.py | 2 +- numpy/core/__init__.py | 16 +- numpy/core/_machar.py | 355 ++++++++++++++++++++++++++++++++++ numpy/core/getlimits.py | 2 +- numpy/core/machar.py | 355 ---------------------------------- numpy/core/tests/test_deprecations.py | 3 + numpy/core/tests/test_machar.py | 2 +- numpy/tests/test_public_api.py | 1 - 8 files changed, 375 insertions(+), 361 deletions(-) create mode 100644 numpy/core/_machar.py delete mode 100644 numpy/core/machar.py (limited to 'numpy') diff --git a/numpy/__init__.py b/numpy/__init__.py index 780ecef11..c34434e75 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -197,7 +197,7 @@ else: # NumPy 1.22, 2021-10-20 __deprecated_attrs__["MachAr"] = ( - core.machar.MachAr, + core._machar.MachAr, "`np.MachAr` is deprecated (NumPy 1.22)." ) diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py index e5ef718a9..332f9940e 100644 --- a/numpy/core/__init__.py +++ b/numpy/core/__init__.py @@ -9,6 +9,7 @@ are available in the main ``numpy`` namespace - use that instead. from numpy.version import version as __version__ import os +import warnings # disables OpenBLAS affinity setting of the main thread that limits # python threads or processes to one core @@ -80,8 +81,8 @@ from .memmap import * from .defchararray import chararray from . import function_base from .function_base import * -from . import machar -from .machar import * +from . import _machar +from ._machar import * from . import getlimits from .getlimits import * from . import shape_base @@ -150,6 +151,17 @@ def _DType_reduce(DType): return _DType_reconstruct, (scalar_type,) +def __getattr__(name): + # Deprecated 2021-10-20, NumPy 1.22 + if name == "machar": + warnings.warn( + "The `np.core.machar` module is deprecated (NumPy 1.22)", + DeprecationWarning, stacklevel=2, + ) + return _machar + raise AttributeError(f"Module {__name__!r} has no attribute {name!r}") + + import copyreg copyreg.pickle(ufunc, _ufunc_reduce) diff --git a/numpy/core/_machar.py b/numpy/core/_machar.py new file mode 100644 index 000000000..ace19a429 --- /dev/null +++ b/numpy/core/_machar.py @@ -0,0 +1,355 @@ +""" +Machine arithmetic - determine the parameters of the +floating-point arithmetic system + +Author: Pearu Peterson, September 2003 + +""" +__all__ = ['MachAr'] + +from numpy.core.fromnumeric import any +from numpy.core._ufunc_config import errstate +from numpy.core.overrides import set_module + +# Need to speed this up...especially for longfloat + +# Deprecated 2021-10-20, NumPy 1.22 +@set_module('numpy') +class MachAr: + """ + Diagnosing machine parameters. + + Attributes + ---------- + ibeta : int + Radix in which numbers are represented. + it : int + Number of base-`ibeta` digits in the floating point mantissa M. + machep : int + Exponent of the smallest (most negative) power of `ibeta` that, + added to 1.0, gives something different from 1.0 + eps : float + Floating-point number ``beta**machep`` (floating point precision) + negep : int + Exponent of the smallest power of `ibeta` that, subtracted + from 1.0, gives something different from 1.0. + epsneg : float + Floating-point number ``beta**negep``. + iexp : int + Number of bits in the exponent (including its sign and bias). + minexp : int + Smallest (most negative) power of `ibeta` consistent with there + being no leading zeros in the mantissa. + xmin : float + Floating-point number ``beta**minexp`` (the smallest [in + magnitude] positive floating point number with full precision). + maxexp : int + Smallest (positive) power of `ibeta` that causes overflow. + xmax : float + ``(1-epsneg) * beta**maxexp`` (the largest [in magnitude] + usable floating value). + irnd : int + In ``range(6)``, information on what kind of rounding is done + in addition, and on how underflow is handled. + ngrd : int + Number of 'guard digits' used when truncating the product + of two mantissas to fit the representation. + epsilon : float + Same as `eps`. + tiny : float + An alias for `smallest_normal`, kept for backwards compatibility. + huge : float + Same as `xmax`. + precision : float + ``- int(-log10(eps))`` + resolution : float + ``- 10**(-precision)`` + smallest_normal : float + The smallest positive floating point number with 1 as leading bit in + the mantissa following IEEE-754. Same as `xmin`. + smallest_subnormal : float + The smallest positive floating point number with 0 as leading bit in + the mantissa following IEEE-754. + + Parameters + ---------- + float_conv : function, optional + Function that converts an integer or integer array to a float + or float array. Default is `float`. + int_conv : function, optional + Function that converts a float or float array to an integer or + integer array. Default is `int`. + float_to_float : function, optional + Function that converts a float array to float. Default is `float`. + Note that this does not seem to do anything useful in the current + implementation. + float_to_str : function, optional + Function that converts a single float to a string. Default is + ``lambda v:'%24.16e' %v``. + title : str, optional + Title that is printed in the string representation of `MachAr`. + + See Also + -------- + finfo : Machine limits for floating point types. + iinfo : Machine limits for integer types. + + References + ---------- + .. [1] Press, Teukolsky, Vetterling and Flannery, + "Numerical Recipes in C++," 2nd ed, + Cambridge University Press, 2002, p. 31. + + """ + + def __init__(self, float_conv=float,int_conv=int, + float_to_float=float, + float_to_str=lambda v:'%24.16e' % v, + title='Python floating point number'): + """ + + float_conv - convert integer to float (array) + int_conv - convert float (array) to integer + float_to_float - convert float array to float + float_to_str - convert array float to str + title - description of used floating point numbers + + """ + # We ignore all errors here because we are purposely triggering + # underflow to detect the properties of the runninng arch. + with errstate(under='ignore'): + self._do_init(float_conv, int_conv, float_to_float, float_to_str, title) + + def _do_init(self, float_conv, int_conv, float_to_float, float_to_str, title): + max_iterN = 10000 + msg = "Did not converge after %d tries with %s" + one = float_conv(1) + two = one + one + zero = one - one + + # Do we really need to do this? Aren't they 2 and 2.0? + # Determine ibeta and beta + a = one + for _ in range(max_iterN): + a = a + a + temp = a + one + temp1 = temp - a + if any(temp1 - one != zero): + break + else: + raise RuntimeError(msg % (_, one.dtype)) + b = one + for _ in range(max_iterN): + b = b + b + temp = a + b + itemp = int_conv(temp-a) + if any(itemp != 0): + break + else: + raise RuntimeError(msg % (_, one.dtype)) + ibeta = itemp + beta = float_conv(ibeta) + + # Determine it and irnd + it = -1 + b = one + for _ in range(max_iterN): + it = it + 1 + b = b * beta + temp = b + one + temp1 = temp - b + if any(temp1 - one != zero): + break + else: + raise RuntimeError(msg % (_, one.dtype)) + + betah = beta / two + a = one + for _ in range(max_iterN): + a = a + a + temp = a + one + temp1 = temp - a + if any(temp1 - one != zero): + break + else: + raise RuntimeError(msg % (_, one.dtype)) + temp = a + betah + irnd = 0 + if any(temp-a != zero): + irnd = 1 + tempa = a + beta + temp = tempa + betah + if irnd == 0 and any(temp-tempa != zero): + irnd = 2 + + # Determine negep and epsneg + negep = it + 3 + betain = one / beta + a = one + for i in range(negep): + a = a * betain + b = a + for _ in range(max_iterN): + temp = one - a + if any(temp-one != zero): + break + a = a * beta + negep = negep - 1 + # Prevent infinite loop on PPC with gcc 4.0: + if negep < 0: + raise RuntimeError("could not determine machine tolerance " + "for 'negep', locals() -> %s" % (locals())) + else: + raise RuntimeError(msg % (_, one.dtype)) + negep = -negep + epsneg = a + + # Determine machep and eps + machep = - it - 3 + a = b + + for _ in range(max_iterN): + temp = one + a + if any(temp-one != zero): + break + a = a * beta + machep = machep + 1 + else: + raise RuntimeError(msg % (_, one.dtype)) + eps = a + + # Determine ngrd + ngrd = 0 + temp = one + eps + if irnd == 0 and any(temp*one - one != zero): + ngrd = 1 + + # Determine iexp + i = 0 + k = 1 + z = betain + t = one + eps + nxres = 0 + for _ in range(max_iterN): + y = z + z = y*y + a = z*one # Check here for underflow + temp = z*t + if any(a+a == zero) or any(abs(z) >= y): + break + temp1 = temp * betain + if any(temp1*beta == z): + break + i = i + 1 + k = k + k + else: + raise RuntimeError(msg % (_, one.dtype)) + if ibeta != 10: + iexp = i + 1 + mx = k + k + else: + iexp = 2 + iz = ibeta + while k >= iz: + iz = iz * ibeta + iexp = iexp + 1 + mx = iz + iz - 1 + + # Determine minexp and xmin + for _ in range(max_iterN): + xmin = y + y = y * betain + a = y * one + temp = y * t + if any((a + a) != zero) and any(abs(y) < xmin): + k = k + 1 + temp1 = temp * betain + if any(temp1*beta == y) and any(temp != y): + nxres = 3 + xmin = y + break + else: + break + else: + raise RuntimeError(msg % (_, one.dtype)) + minexp = -k + + # Determine maxexp, xmax + if mx <= k + k - 3 and ibeta != 10: + mx = mx + mx + iexp = iexp + 1 + maxexp = mx + minexp + irnd = irnd + nxres + if irnd >= 2: + maxexp = maxexp - 2 + i = maxexp + minexp + if ibeta == 2 and not i: + maxexp = maxexp - 1 + if i > 20: + maxexp = maxexp - 1 + if any(a != y): + maxexp = maxexp - 2 + xmax = one - epsneg + if any(xmax*one != xmax): + xmax = one - beta*epsneg + xmax = xmax / (xmin*beta*beta*beta) + i = maxexp + minexp + 3 + for j in range(i): + if ibeta == 2: + xmax = xmax + xmax + else: + xmax = xmax * beta + + smallest_subnormal = abs(xmin / beta ** (it)) + + self.ibeta = ibeta + self.it = it + self.negep = negep + self.epsneg = float_to_float(epsneg) + self._str_epsneg = float_to_str(epsneg) + self.machep = machep + self.eps = float_to_float(eps) + self._str_eps = float_to_str(eps) + self.ngrd = ngrd + self.iexp = iexp + self.minexp = minexp + self.xmin = float_to_float(xmin) + self._str_xmin = float_to_str(xmin) + self.maxexp = maxexp + self.xmax = float_to_float(xmax) + self._str_xmax = float_to_str(xmax) + self.irnd = irnd + + self.title = title + # Commonly used parameters + self.epsilon = self.eps + self.tiny = self.xmin + self.huge = self.xmax + self.smallest_normal = self.xmin + self.smallest_subnormal = float_to_float(smallest_subnormal) + + import math + self.precision = int(-math.log10(float_to_float(self.eps))) + ten = two + two + two + two + two + resolution = ten ** (-self.precision) + self.resolution = float_to_float(resolution) + self._str_resolution = float_to_str(resolution) + + def __str__(self): + fmt = ( + 'Machine parameters for %(title)s\n' + '---------------------------------------------------------------------\n' + 'ibeta=%(ibeta)s it=%(it)s iexp=%(iexp)s ngrd=%(ngrd)s irnd=%(irnd)s\n' + 'machep=%(machep)s eps=%(_str_eps)s (beta**machep == epsilon)\n' + 'negep =%(negep)s epsneg=%(_str_epsneg)s (beta**epsneg)\n' + 'minexp=%(minexp)s xmin=%(_str_xmin)s (beta**minexp == tiny)\n' + 'maxexp=%(maxexp)s xmax=%(_str_xmax)s ((1-epsneg)*beta**maxexp == huge)\n' + 'smallest_normal=%(smallest_normal)s ' + 'smallest_subnormal=%(smallest_subnormal)s\n' + '---------------------------------------------------------------------\n' + ) + return fmt % self.__dict__ + + +if __name__ == '__main__': + print(MachAr()) diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index c96e6d5e7..d6efa5ac6 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -5,7 +5,7 @@ __all__ = ['finfo', 'iinfo'] import warnings -from .machar import MachAr +from ._machar import MachAr from .overrides import set_module from . import numeric from . import numerictypes as ntypes diff --git a/numpy/core/machar.py b/numpy/core/machar.py deleted file mode 100644 index ace19a429..000000000 --- a/numpy/core/machar.py +++ /dev/null @@ -1,355 +0,0 @@ -""" -Machine arithmetic - determine the parameters of the -floating-point arithmetic system - -Author: Pearu Peterson, September 2003 - -""" -__all__ = ['MachAr'] - -from numpy.core.fromnumeric import any -from numpy.core._ufunc_config import errstate -from numpy.core.overrides import set_module - -# Need to speed this up...especially for longfloat - -# Deprecated 2021-10-20, NumPy 1.22 -@set_module('numpy') -class MachAr: - """ - Diagnosing machine parameters. - - Attributes - ---------- - ibeta : int - Radix in which numbers are represented. - it : int - Number of base-`ibeta` digits in the floating point mantissa M. - machep : int - Exponent of the smallest (most negative) power of `ibeta` that, - added to 1.0, gives something different from 1.0 - eps : float - Floating-point number ``beta**machep`` (floating point precision) - negep : int - Exponent of the smallest power of `ibeta` that, subtracted - from 1.0, gives something different from 1.0. - epsneg : float - Floating-point number ``beta**negep``. - iexp : int - Number of bits in the exponent (including its sign and bias). - minexp : int - Smallest (most negative) power of `ibeta` consistent with there - being no leading zeros in the mantissa. - xmin : float - Floating-point number ``beta**minexp`` (the smallest [in - magnitude] positive floating point number with full precision). - maxexp : int - Smallest (positive) power of `ibeta` that causes overflow. - xmax : float - ``(1-epsneg) * beta**maxexp`` (the largest [in magnitude] - usable floating value). - irnd : int - In ``range(6)``, information on what kind of rounding is done - in addition, and on how underflow is handled. - ngrd : int - Number of 'guard digits' used when truncating the product - of two mantissas to fit the representation. - epsilon : float - Same as `eps`. - tiny : float - An alias for `smallest_normal`, kept for backwards compatibility. - huge : float - Same as `xmax`. - precision : float - ``- int(-log10(eps))`` - resolution : float - ``- 10**(-precision)`` - smallest_normal : float - The smallest positive floating point number with 1 as leading bit in - the mantissa following IEEE-754. Same as `xmin`. - smallest_subnormal : float - The smallest positive floating point number with 0 as leading bit in - the mantissa following IEEE-754. - - Parameters - ---------- - float_conv : function, optional - Function that converts an integer or integer array to a float - or float array. Default is `float`. - int_conv : function, optional - Function that converts a float or float array to an integer or - integer array. Default is `int`. - float_to_float : function, optional - Function that converts a float array to float. Default is `float`. - Note that this does not seem to do anything useful in the current - implementation. - float_to_str : function, optional - Function that converts a single float to a string. Default is - ``lambda v:'%24.16e' %v``. - title : str, optional - Title that is printed in the string representation of `MachAr`. - - See Also - -------- - finfo : Machine limits for floating point types. - iinfo : Machine limits for integer types. - - References - ---------- - .. [1] Press, Teukolsky, Vetterling and Flannery, - "Numerical Recipes in C++," 2nd ed, - Cambridge University Press, 2002, p. 31. - - """ - - def __init__(self, float_conv=float,int_conv=int, - float_to_float=float, - float_to_str=lambda v:'%24.16e' % v, - title='Python floating point number'): - """ - - float_conv - convert integer to float (array) - int_conv - convert float (array) to integer - float_to_float - convert float array to float - float_to_str - convert array float to str - title - description of used floating point numbers - - """ - # We ignore all errors here because we are purposely triggering - # underflow to detect the properties of the runninng arch. - with errstate(under='ignore'): - self._do_init(float_conv, int_conv, float_to_float, float_to_str, title) - - def _do_init(self, float_conv, int_conv, float_to_float, float_to_str, title): - max_iterN = 10000 - msg = "Did not converge after %d tries with %s" - one = float_conv(1) - two = one + one - zero = one - one - - # Do we really need to do this? Aren't they 2 and 2.0? - # Determine ibeta and beta - a = one - for _ in range(max_iterN): - a = a + a - temp = a + one - temp1 = temp - a - if any(temp1 - one != zero): - break - else: - raise RuntimeError(msg % (_, one.dtype)) - b = one - for _ in range(max_iterN): - b = b + b - temp = a + b - itemp = int_conv(temp-a) - if any(itemp != 0): - break - else: - raise RuntimeError(msg % (_, one.dtype)) - ibeta = itemp - beta = float_conv(ibeta) - - # Determine it and irnd - it = -1 - b = one - for _ in range(max_iterN): - it = it + 1 - b = b * beta - temp = b + one - temp1 = temp - b - if any(temp1 - one != zero): - break - else: - raise RuntimeError(msg % (_, one.dtype)) - - betah = beta / two - a = one - for _ in range(max_iterN): - a = a + a - temp = a + one - temp1 = temp - a - if any(temp1 - one != zero): - break - else: - raise RuntimeError(msg % (_, one.dtype)) - temp = a + betah - irnd = 0 - if any(temp-a != zero): - irnd = 1 - tempa = a + beta - temp = tempa + betah - if irnd == 0 and any(temp-tempa != zero): - irnd = 2 - - # Determine negep and epsneg - negep = it + 3 - betain = one / beta - a = one - for i in range(negep): - a = a * betain - b = a - for _ in range(max_iterN): - temp = one - a - if any(temp-one != zero): - break - a = a * beta - negep = negep - 1 - # Prevent infinite loop on PPC with gcc 4.0: - if negep < 0: - raise RuntimeError("could not determine machine tolerance " - "for 'negep', locals() -> %s" % (locals())) - else: - raise RuntimeError(msg % (_, one.dtype)) - negep = -negep - epsneg = a - - # Determine machep and eps - machep = - it - 3 - a = b - - for _ in range(max_iterN): - temp = one + a - if any(temp-one != zero): - break - a = a * beta - machep = machep + 1 - else: - raise RuntimeError(msg % (_, one.dtype)) - eps = a - - # Determine ngrd - ngrd = 0 - temp = one + eps - if irnd == 0 and any(temp*one - one != zero): - ngrd = 1 - - # Determine iexp - i = 0 - k = 1 - z = betain - t = one + eps - nxres = 0 - for _ in range(max_iterN): - y = z - z = y*y - a = z*one # Check here for underflow - temp = z*t - if any(a+a == zero) or any(abs(z) >= y): - break - temp1 = temp * betain - if any(temp1*beta == z): - break - i = i + 1 - k = k + k - else: - raise RuntimeError(msg % (_, one.dtype)) - if ibeta != 10: - iexp = i + 1 - mx = k + k - else: - iexp = 2 - iz = ibeta - while k >= iz: - iz = iz * ibeta - iexp = iexp + 1 - mx = iz + iz - 1 - - # Determine minexp and xmin - for _ in range(max_iterN): - xmin = y - y = y * betain - a = y * one - temp = y * t - if any((a + a) != zero) and any(abs(y) < xmin): - k = k + 1 - temp1 = temp * betain - if any(temp1*beta == y) and any(temp != y): - nxres = 3 - xmin = y - break - else: - break - else: - raise RuntimeError(msg % (_, one.dtype)) - minexp = -k - - # Determine maxexp, xmax - if mx <= k + k - 3 and ibeta != 10: - mx = mx + mx - iexp = iexp + 1 - maxexp = mx + minexp - irnd = irnd + nxres - if irnd >= 2: - maxexp = maxexp - 2 - i = maxexp + minexp - if ibeta == 2 and not i: - maxexp = maxexp - 1 - if i > 20: - maxexp = maxexp - 1 - if any(a != y): - maxexp = maxexp - 2 - xmax = one - epsneg - if any(xmax*one != xmax): - xmax = one - beta*epsneg - xmax = xmax / (xmin*beta*beta*beta) - i = maxexp + minexp + 3 - for j in range(i): - if ibeta == 2: - xmax = xmax + xmax - else: - xmax = xmax * beta - - smallest_subnormal = abs(xmin / beta ** (it)) - - self.ibeta = ibeta - self.it = it - self.negep = negep - self.epsneg = float_to_float(epsneg) - self._str_epsneg = float_to_str(epsneg) - self.machep = machep - self.eps = float_to_float(eps) - self._str_eps = float_to_str(eps) - self.ngrd = ngrd - self.iexp = iexp - self.minexp = minexp - self.xmin = float_to_float(xmin) - self._str_xmin = float_to_str(xmin) - self.maxexp = maxexp - self.xmax = float_to_float(xmax) - self._str_xmax = float_to_str(xmax) - self.irnd = irnd - - self.title = title - # Commonly used parameters - self.epsilon = self.eps - self.tiny = self.xmin - self.huge = self.xmax - self.smallest_normal = self.xmin - self.smallest_subnormal = float_to_float(smallest_subnormal) - - import math - self.precision = int(-math.log10(float_to_float(self.eps))) - ten = two + two + two + two + two - resolution = ten ** (-self.precision) - self.resolution = float_to_float(resolution) - self._str_resolution = float_to_str(resolution) - - def __str__(self): - fmt = ( - 'Machine parameters for %(title)s\n' - '---------------------------------------------------------------------\n' - 'ibeta=%(ibeta)s it=%(it)s iexp=%(iexp)s ngrd=%(ngrd)s irnd=%(irnd)s\n' - 'machep=%(machep)s eps=%(_str_eps)s (beta**machep == epsilon)\n' - 'negep =%(negep)s epsneg=%(_str_epsneg)s (beta**epsneg)\n' - 'minexp=%(minexp)s xmin=%(_str_xmin)s (beta**minexp == tiny)\n' - 'maxexp=%(maxexp)s xmax=%(_str_xmax)s ((1-epsneg)*beta**maxexp == huge)\n' - 'smallest_normal=%(smallest_normal)s ' - 'smallest_subnormal=%(smallest_subnormal)s\n' - '---------------------------------------------------------------------\n' - ) - return fmt % self.__dict__ - - -if __name__ == '__main__': - print(MachAr()) diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index d57032e5f..947064a3f 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -1223,3 +1223,6 @@ class TestMachAr(_DeprecationTestCase): def test_deprecated(self): self.assert_deprecated(lambda: np.MachAr) + + def test_deprecated_module(self): + self.assert_deprecated(lambda: getattr(np.core, "machar")) diff --git a/numpy/core/tests/test_machar.py b/numpy/core/tests/test_machar.py index 673f309f1..3a66ec51f 100644 --- a/numpy/core/tests/test_machar.py +++ b/numpy/core/tests/test_machar.py @@ -3,7 +3,7 @@ Test machar. Given recent changes to hardcode type data, we might want to get rid of both MachAr and this test at some point. """ -from numpy.core.machar import MachAr +from numpy.core._machar import MachAr import numpy.core.numerictypes as ntypes from numpy import errstate, array diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index 73a93f489..1e7d389d9 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -178,7 +178,6 @@ PRIVATE_BUT_PRESENT_MODULES = ['numpy.' + s for s in [ "core.fromnumeric", "core.function_base", "core.getlimits", - "core.machar", "core.memmap", "core.multiarray", "core.numeric", -- cgit v1.2.1 From f66a76ec2d592c4f44c3f44c596b6d6f581626e5 Mon Sep 17 00:00:00 2001 From: jessijzhao <35235453+jessijzhao@users.noreply.github.com> Date: Wed, 27 Oct 2021 10:35:31 -0700 Subject: DOC: Fix random.power Raises description (#20205) Current documentation for random.power and random.Generator.power states that ValueError is raised if a < 1; however the power function distribution only supports strictly positive a > 0 and raises ValueError if a <= 0. Fixed documentation to reflect this. --- numpy/random/_generator.pyx | 2 +- numpy/random/mtrand.pyx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index 54973100e..5347ea125 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -2097,7 +2097,7 @@ cdef class Generator: Raises ------ ValueError - If a < 1. + If a <= 0. Notes ----- diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index ca539c00e..81a526ab4 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -2527,7 +2527,7 @@ cdef class RandomState: Raises ------ ValueError - If a < 1. + If a <= 0. See Also -------- -- cgit v1.2.1 From cbc1d01d4260ff92fa9c3292d8dd67e6137a20fe Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Wed, 27 Oct 2021 20:53:24 +0200 Subject: DEP: Deprecate `finfo.machar` --- numpy/__init__.pyi | 9 ----- numpy/core/getlimits.py | 22 ++++++++++-- numpy/core/getlimits.pyi | 52 +--------------------------- numpy/core/tests/test_deprecations.py | 4 +++ numpy/core/tests/test_getlimits.py | 2 +- numpy/core/tests/test_numeric.py | 4 +-- numpy/typing/tests/data/reveal/getlimits.pyi | 26 -------------- 7 files changed, 27 insertions(+), 92 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index e8b18a615..463d4a713 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -14,7 +14,6 @@ if sys.version_info >= (3, 9): from numpy._pytesttester import PytestTester from numpy.core._internal import _ctypes -from numpy.core.getlimits import MachArLike from numpy.typing import ( # Arrays @@ -3384,14 +3383,6 @@ class finfo(Generic[_FloatType]): def smallest_normal(self) -> _FloatType: ... @property def tiny(self) -> _FloatType: ... - - # NOTE: Not technically a property, but this is the only way we can - # access the precision of the underlying float - @property - def machar(self: finfo[floating[_NBit1]]) -> MachArLike[_NBit1]: ... - @machar.setter - def machar(self: finfo[floating[_NBit1]], value: MachArLike[_NBit1]) -> None: ... - @overload def __new__( cls, dtype: inexact[_NBit1] | _DTypeLike[inexact[_NBit1]] diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index d6efa5ac6..ab4a4d2be 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -385,6 +385,8 @@ class finfo: machar : MachAr The object which calculated these parameters and holds more detailed information. + + .. deprecated:: 1.22 machep : int The exponent that yields `eps`. max : floating point number of the appropriate type @@ -501,7 +503,7 @@ class finfo: self.eps = machar.eps.flat[0] self.nexp = machar.iexp self.nmant = machar.it - self.machar = machar + self._machar = machar self._str_tiny = machar._str_xmin.strip() self._str_max = machar._str_xmax.strip() self._str_epsneg = machar._str_epsneg.strip() @@ -551,11 +553,11 @@ class finfo: """ # This check is necessary because the value for smallest_normal is # platform dependent for longdouble types. - if isnan(self.machar.smallest_normal.flat[0]): + if isnan(self._machar.smallest_normal.flat[0]): warnings.warn( 'The value of smallest normal is undefined for double double', UserWarning, stacklevel=2) - return self.machar.smallest_normal.flat[0] + return self._machar.smallest_normal.flat[0] @property def tiny(self): @@ -574,6 +576,20 @@ class finfo: """ return self.smallest_normal + @property + def machar(self): + """The object which calculated these parameters and holds more + detailed information. + + .. deprecated:: 1.22 + """ + # Deprecated 2021-10-27, NumPy 1.22 + warnings.warn( + "`finfo.machar` is deprecated (NumPy 1.22)", + DeprecationWarning, stacklevel=2, + ) + return self._machar + @set_module('numpy') class iinfo: diff --git a/numpy/core/getlimits.pyi b/numpy/core/getlimits.pyi index ca22e18f7..66d062995 100644 --- a/numpy/core/getlimits.pyi +++ b/numpy/core/getlimits.pyi @@ -1,58 +1,8 @@ -from typing import Any, Generic, List, Type, TypeVar +from typing import List from numpy import ( finfo as finfo, iinfo as iinfo, - floating, - signedinteger, ) -from numpy.typing import NBitBase, NDArray - -_NBit = TypeVar("_NBit", bound=NBitBase) - __all__: List[str] - -class MachArLike(Generic[_NBit]): - def __init__( - self, - ftype: Type[floating[_NBit]], - *, - eps: floating[Any], - epsneg: floating[Any], - huge: floating[Any], - tiny: floating[Any], - ibeta: int, - smallest_subnormal: None | floating[Any] = ..., - # Expand `**kwargs` into keyword-only arguments - machep: int, - negep: int, - minexp: int, - maxexp: int, - it: int, - iexp: int, - irnd: int, - ngrd: int, - ) -> None: ... - @property - def smallest_subnormal(self) -> NDArray[floating[_NBit]]: ... - eps: NDArray[floating[_NBit]] - epsilon: NDArray[floating[_NBit]] - epsneg: NDArray[floating[_NBit]] - huge: NDArray[floating[_NBit]] - ibeta: signedinteger[_NBit] - iexp: int - irnd: int - it: int - machep: int - maxexp: int - minexp: int - negep: int - ngrd: int - precision: int - resolution: NDArray[floating[_NBit]] - smallest_normal: NDArray[floating[_NBit]] - tiny: NDArray[floating[_NBit]] - title: str - xmax: NDArray[floating[_NBit]] - xmin: NDArray[floating[_NBit]] diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 947064a3f..a1b379d92 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -1226,3 +1226,7 @@ class TestMachAr(_DeprecationTestCase): def test_deprecated_module(self): self.assert_deprecated(lambda: getattr(np.core, "machar")) + + def test_deprecated_attr(self): + finfo = np.finfo(float) + self.assert_deprecated(lambda: getattr(finfo, "machar")) diff --git a/numpy/core/tests/test_getlimits.py b/numpy/core/tests/test_getlimits.py index de7b3e769..c5148db2c 100644 --- a/numpy/core/tests/test_getlimits.py +++ b/numpy/core/tests/test_getlimits.py @@ -46,7 +46,7 @@ class TestFinfo: [np.float16, np.float32, np.float64, np.complex64, np.complex128])) for dt1, dt2 in dts: - for attr in ('bits', 'eps', 'epsneg', 'iexp', 'machar', 'machep', + for attr in ('bits', 'eps', 'epsneg', 'iexp', 'machep', 'max', 'maxexp', 'min', 'minexp', 'negep', 'nexp', 'nmant', 'precision', 'resolution', 'tiny', 'smallest_normal', 'smallest_subnormal'): diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index a98c7016a..96173a482 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -646,7 +646,7 @@ class TestFloatExceptions: if np.dtype(ftype).kind == 'f': # Get some extreme values for the type fi = np.finfo(ftype) - ft_tiny = fi.machar.tiny + ft_tiny = fi._machar.tiny ft_max = fi.max ft_eps = fi.eps underflow = 'underflow' @@ -655,7 +655,7 @@ class TestFloatExceptions: # 'c', complex, corresponding real dtype rtype = type(ftype(0).real) fi = np.finfo(rtype) - ft_tiny = ftype(fi.machar.tiny) + ft_tiny = ftype(fi._machar.tiny) ft_max = ftype(fi.max) ft_eps = ftype(fi.eps) # The complex types raise different exceptions diff --git a/numpy/typing/tests/data/reveal/getlimits.pyi b/numpy/typing/tests/data/reveal/getlimits.pyi index e12723bfe..90bcb06c8 100644 --- a/numpy/typing/tests/data/reveal/getlimits.pyi +++ b/numpy/typing/tests/data/reveal/getlimits.pyi @@ -1,6 +1,4 @@ import numpy as np -from numpy.typing import _32Bit - f: float f8: np.float64 c8: np.complex64 @@ -11,7 +9,6 @@ u4: np.uint32 finfo_f8: np.finfo[np.float64] iinfo_i8: np.iinfo[np.int64] -machar_f4: np.core.getlimits.MachArLike[_32Bit] reveal_type(np.finfo(f)) # E: numpy.finfo[{double}] reveal_type(np.finfo(f8)) # E: numpy.finfo[{float64}] @@ -36,7 +33,6 @@ reveal_type(finfo_f8.resolution) # E: {float64} reveal_type(finfo_f8.tiny) # E: {float64} reveal_type(finfo_f8.smallest_normal) # E: {float64} reveal_type(finfo_f8.smallest_subnormal) # E: {float64} -reveal_type(finfo_f8.machar) # E: MachArLike[numpy.typing._64Bit] reveal_type(np.iinfo(i)) # E: iinfo[{int_}] reveal_type(np.iinfo(i8)) # E: iinfo[{int64}] @@ -49,25 +45,3 @@ reveal_type(iinfo_i8.bits) # E: int reveal_type(iinfo_i8.key) # E: str reveal_type(iinfo_i8.min) # E: int reveal_type(iinfo_i8.max) # E: int - -reveal_type(machar_f4.eps) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.epsilon) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.epsneg) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.huge) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.resolution) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.tiny) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.xmax) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.xmin) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.smallest_subnormal) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.smallest_normal) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(machar_f4.iexp) # E: int -reveal_type(machar_f4.irnd) # E: int -reveal_type(machar_f4.it) # E: int -reveal_type(machar_f4.machep) # E: int -reveal_type(machar_f4.maxexp) # E: int -reveal_type(machar_f4.minexp) # E: int -reveal_type(machar_f4.negep) # E: int -reveal_type(machar_f4.ngrd) # E: int -reveal_type(machar_f4.precision) # E: int -reveal_type(machar_f4.ibeta) # E: {int32} -reveal_type(machar_f4.title) # E: str -- cgit v1.2.1 From aebf38662647b328e5ac10c52a24202b3a22cf66 Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Wed, 27 Oct 2021 23:09:51 +0300 Subject: MAINT: Only warn for transferred ownership if env variable is set (#20200) Builds on gh-20194. Fixes breakage of SciPy in https://github.com/scipy/scipy/issues/14917 At some point we could flip the default to "warn" instead of "no warning" * make warning conditional on NUMPY_WARN_IF_NO_MEM_POLICY * add test, fix example code * fixes from review * typo --- numpy/core/src/multiarray/arrayobject.c | 11 +++++--- numpy/core/tests/test_mem_policy.py | 49 ++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index c3615f95f..237fb9b72 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -502,10 +502,13 @@ array_dealloc(PyArrayObject *self) nbytes = fa->descr->elsize ? fa->descr->elsize : 1; } if (fa->mem_handler == NULL) { - char const * msg = "Trying to dealloc data, but a memory policy " - "is not set. If you take ownership of the data, you must " - "also set a memory policy."; - WARN_IN_DEALLOC(PyExc_RuntimeWarning, msg); + char *env = getenv("NUMPY_WARN_IF_NO_MEM_POLICY"); + if ((env == NULL) || (strncmp(env, "1", 1) == 0)) { + char const * msg = "Trying to dealloc data, but a memory policy " + "is not set. If you take ownership of the data, you must " + "set a base owning the data (e.g. a PyCapsule)."; + WARN_IN_DEALLOC(PyExc_RuntimeWarning, msg); + } // Guess at malloc/free ??? free(fa->data); } else { diff --git a/numpy/core/tests/test_mem_policy.py b/numpy/core/tests/test_mem_policy.py index a2ff98ceb..4ec9b3b11 100644 --- a/numpy/core/tests/test_mem_policy.py +++ b/numpy/core/tests/test_mem_policy.py @@ -1,5 +1,6 @@ import asyncio import gc +import os import pytest import numpy as np import threading @@ -61,6 +62,29 @@ def get_module(tmp_path): // PyArray_BASE(PyArrayObject *)args) = NULL; Py_RETURN_NONE; """), + ("get_array_with_base", "METH_NOARGS", """ + char *buf = (char *)malloc(20); + npy_intp dims[1]; + dims[0] = 20; + PyArray_Descr *descr = PyArray_DescrNewFromType(NPY_UINT8); + PyObject *arr = PyArray_NewFromDescr(&PyArray_Type, descr, 1, dims, + NULL, buf, + NPY_ARRAY_WRITEABLE, NULL); + if (arr == NULL) return NULL; + PyObject *obj = PyCapsule_New(buf, "buf capsule", + (PyCapsule_Destructor)&warn_on_free); + if (obj == NULL) { + Py_DECREF(arr); + return NULL; + } + if (PyArray_SetBaseObject((PyArrayObject *)arr, obj) < 0) { + Py_DECREF(arr); + Py_DECREF(obj); + return NULL; + } + return arr; + + """), ] prologue = ''' #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION @@ -163,6 +187,12 @@ def get_module(tmp_path): shift_free /* free */ } }; + void warn_on_free(void *capsule) { + PyErr_WarnEx(PyExc_UserWarning, "in warn_on_free", 1); + void * obj = PyCapsule_GetPointer(capsule, + PyCapsule_GetName(capsule)); + free(obj); + }; ''' more_init = "import_array();" try: @@ -331,10 +361,21 @@ def test_switch_owner(get_module): a = get_module.get_array() assert np.core.multiarray.get_handler_name(a) is None get_module.set_own(a) - with warnings.catch_warnings(): - warnings.filterwarnings('always') - # The policy should be NULL, so we have to assume we can call "free" + oldval = os.environ.get('NUMPY_WARN_IF_NO_MEM_POLICY', None) + os.environ['NUMPY_WARN_IF_NO_MEM_POLICY'] = "1" + try: + # The policy should be NULL, so we have to assume we can call + # "free" with assert_warns(RuntimeWarning) as w: del a gc.collect() - print(w) + finally: + if oldval is None: + os.environ.pop('NUMPY_WARN_IF_NO_MEM_POLICY') + else: + os.environ['NUMPY_WARN_IF_NO_MEM_POLICY'] = oldval + + a = get_module.get_array_with_base() + with pytest.warns(UserWarning, match='warn_on_free'): + del a + gc.collect() -- cgit v1.2.1 From f559951b04b64fa0949ea6010093ad8c5b648764 Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Wed, 27 Oct 2021 14:07:36 +0200 Subject: BUG, BLD: Fix cast long double and double warnings(C)/errors(C++) These errors/warnings occurs when sizeof(long double) == sizeof(double). --- numpy/core/include/numpy/npy_common.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/npy_common.h b/numpy/core/include/numpy/npy_common.h index 12a3e725a..57cc592b9 100644 --- a/numpy/core/include/numpy/npy_common.h +++ b/numpy/core/include/numpy/npy_common.h @@ -359,12 +359,11 @@ typedef unsigned char npy_bool; #if NPY_SIZEOF_LONGDOUBLE == NPY_SIZEOF_DOUBLE - typedef double npy_longdouble; #define NPY_LONGDOUBLE_FMT "g" #else - typedef long double npy_longdouble; #define NPY_LONGDOUBLE_FMT "Lg" #endif +typedef long double npy_longdouble; #ifndef Py_USING_UNICODE #error Must use Python with unicode enabled. -- cgit v1.2.1 From 66bd55046a637e0e52c6c8a7d8972995dbe31f3c Mon Sep 17 00:00:00 2001 From: mattip Date: Thu, 28 Oct 2021 09:53:56 +0300 Subject: BUG: fix test c-extension compilation inside a venv --- numpy/testing/_private/extbuild.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/testing/_private/extbuild.py b/numpy/testing/_private/extbuild.py index 8b3a438dd..20bf3dcea 100644 --- a/numpy/testing/_private/extbuild.py +++ b/numpy/testing/_private/extbuild.py @@ -183,7 +183,7 @@ def _c_compile(cfile, outputfilename, include_dirs=[], libraries=[], library_dirs=[]): if sys.platform == 'win32': compile_extra = ["/we4013"] - link_extra = ["/LIBPATH:" + os.path.join(sys.exec_prefix, 'libs')] + link_extra = ["/LIBPATH:" + os.path.join(sys.base_prefix, 'libs')] elif sys.platform.startswith('linux'): compile_extra = [ "-O0", "-g", "-Werror=implicit-function-declaration", "-fPIC"] -- cgit v1.2.1 From 450e7451fa081001ba7fec4167972306455c22c9 Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Thu, 28 Oct 2021 16:55:55 +0200 Subject: BUG, SIMD: Workaround broadcasting SIMD 64-bit integers on MSVC 32-bit mode --- numpy/core/src/common/simd/avx2/misc.h | 27 +++++++++++++++++++++++++-- numpy/core/src/common/simd/avx512/misc.h | 32 ++++++++++++++++++++++++++++++-- numpy/core/src/common/simd/simd.h | 19 +++++++++++++++++++ numpy/core/src/common/simd/sse/misc.h | 25 +++++++++++++++++++++++-- 4 files changed, 97 insertions(+), 6 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/common/simd/avx2/misc.h b/numpy/core/src/common/simd/avx2/misc.h index e96696dc9..5e91e91b3 100644 --- a/numpy/core/src/common/simd/avx2/misc.h +++ b/numpy/core/src/common/simd/avx2/misc.h @@ -24,11 +24,27 @@ #define npyv_setall_s16(VAL) _mm256_set1_epi16((short)VAL) #define npyv_setall_u32(VAL) _mm256_set1_epi32((int)VAL) #define npyv_setall_s32(VAL) _mm256_set1_epi32(VAL) -#define npyv_setall_u64(VAL) _mm256_set1_epi64x(VAL) -#define npyv_setall_s64(VAL) _mm256_set1_epi64x(VAL) #define npyv_setall_f32(VAL) _mm256_set1_ps(VAL) #define npyv_setall_f64(VAL) _mm256_set1_pd(VAL) +NPY_FINLINE __m256i npyv__setr_epi64(npy_int64, npy_int64, npy_int64, npy_int64); +NPY_FINLINE npyv_u64 npyv_setall_u64(npy_uint64 a) +{ + npy_int64 ai = (npy_int64)a; +#if defined(_MSC_VER) && defined(_M_IX86) + return npyv__setr_epi64(ai, ai, ai, ai); +#else + return _mm256_set1_epi64x(ai); +#endif +} +NPY_FINLINE npyv_s64 npyv_setall_s64(npy_int64 a) +{ +#if defined(_MSC_VER) && defined(_M_IX86) + return npyv__setr_epi64(a, a, a, a); +#else + return _mm256_set1_epi64x(a); +#endif +} /* * vector with specific values set to each lane and * set a specific value to all remained lanes @@ -59,7 +75,14 @@ NPY_FINLINE __m256i npyv__setr_epi32(int i0, int i1, int i2, int i3, int i4, int } NPY_FINLINE __m256i npyv__setr_epi64(npy_int64 i0, npy_int64 i1, npy_int64 i2, npy_int64 i3) { +#if defined(_MSC_VER) && defined(_M_IX86) + return _mm256_setr_epi32( + (int)i0, (int)(i0 >> 32), (int)i1, (int)(i1 >> 32), + (int)i2, (int)(i2 >> 32), (int)i3, (int)(i3 >> 32) + ); +#else return _mm256_setr_epi64x(i0, i1, i2, i3); +#endif } NPY_FINLINE __m256 npyv__setr_ps(float i0, float i1, float i2, float i3, float i4, float i5, diff --git a/numpy/core/src/common/simd/avx512/misc.h b/numpy/core/src/common/simd/avx512/misc.h index 4b6729b05..c3039ecfe 100644 --- a/numpy/core/src/common/simd/avx512/misc.h +++ b/numpy/core/src/common/simd/avx512/misc.h @@ -24,11 +24,30 @@ #define npyv_setall_s16(VAL) _mm512_set1_epi16((short)VAL) #define npyv_setall_u32(VAL) _mm512_set1_epi32((int)VAL) #define npyv_setall_s32(VAL) _mm512_set1_epi32(VAL) -#define npyv_setall_u64(VAL) _mm512_set1_epi64(VAL) -#define npyv_setall_s64(VAL) _mm512_set1_epi64(VAL) #define npyv_setall_f32(VAL) _mm512_set1_ps(VAL) #define npyv_setall_f64(VAL) _mm512_set1_pd(VAL) +NPY_FINLINE __m512i npyv__setr_epi64( + npy_int64, npy_int64, npy_int64, npy_int64, + npy_int64, npy_int64, npy_int64, npy_int64 +); +NPY_FINLINE npyv_u64 npyv_setall_u64(npy_uint64 a) +{ + npy_int64 ai = (npy_int64)a; +#if defined(_MSC_VER) && defined(_M_IX86) + return npyv__setr_epi64(ai, ai, ai, ai, ai, ai, ai, ai); +#else + return _mm512_set1_epi64(ai); +#endif +} +NPY_FINLINE npyv_s64 npyv_setall_s64(npy_int64 a) +{ +#if defined(_MSC_VER) && defined(_M_IX86) + return npyv__setr_epi64(a, a, a, a, a, a, a, a); +#else + return _mm512_set1_epi64(a); +#endif +} /** * vector with specific values set to each lane and * set a specific value to all remained lanes @@ -76,7 +95,16 @@ NPY_FINLINE __m512i npyv__setr_epi32( NPY_FINLINE __m512i npyv__setr_epi64(npy_int64 i0, npy_int64 i1, npy_int64 i2, npy_int64 i3, npy_int64 i4, npy_int64 i5, npy_int64 i6, npy_int64 i7) { +#if defined(_MSC_VER) && defined(_M_IX86) + return _mm512_setr_epi32( + (int)i0, (int)(i0 >> 32), (int)i1, (int)(i1 >> 32), + (int)i2, (int)(i2 >> 32), (int)i3, (int)(i3 >> 32), + (int)i4, (int)(i4 >> 32), (int)i5, (int)(i5 >> 32), + (int)i6, (int)(i6 >> 32), (int)i7, (int)(i7 >> 32) + ); +#else return _mm512_setr_epi64(i0, i1, i2, i3, i4, i5, i6, i7); +#endif } NPY_FINLINE __m512 npyv__setr_ps( diff --git a/numpy/core/src/common/simd/simd.h b/numpy/core/src/common/simd/simd.h index a3e2b95de..08b2a7d00 100644 --- a/numpy/core/src/common/simd/simd.h +++ b/numpy/core/src/common/simd/simd.h @@ -27,6 +27,25 @@ typedef npy_int64 npyv_lanetype_s64; typedef float npyv_lanetype_f32; typedef double npyv_lanetype_f64; +#if defined(_MSC_VER) && defined(_M_IX86) +/* + * Avoid using any of the following intrinsics with MSVC 32-bit, + * even if they are apparently work on newer versions. + * They had bad impact on the generated instructions, + * sometimes the compiler deal with them without the respect + * of 32-bit mode which lead to crush due to execute 64-bit + * instructions and other times generate bad emulated instructions. + */ + #undef _mm512_set1_epi64 + #undef _mm256_set1_epi64x + #undef _mm_set1_epi64x + #undef _mm512_setr_epi64x + #undef _mm256_setr_epi64x + #undef _mm_setr_epi64x + #undef _mm512_set_epi64x + #undef _mm256_set_epi64x + #undef _mm_set_epi64x +#endif #if defined(NPY_HAVE_AVX512F) && !defined(NPY_SIMD_FORCE_256) && !defined(NPY_SIMD_FORCE_128) #include "avx512/avx512.h" #elif defined(NPY_HAVE_AVX2) && !defined(NPY_SIMD_FORCE_128) diff --git a/numpy/core/src/common/simd/sse/misc.h b/numpy/core/src/common/simd/sse/misc.h index 1099c491d..7d13fbf55 100644 --- a/numpy/core/src/common/simd/sse/misc.h +++ b/numpy/core/src/common/simd/sse/misc.h @@ -24,11 +24,28 @@ #define npyv_setall_s16(VAL) _mm_set1_epi16((short)(VAL)) #define npyv_setall_u32(VAL) _mm_set1_epi32((int)(VAL)) #define npyv_setall_s32(VAL) _mm_set1_epi32((int)(VAL)) -#define npyv_setall_u64(VAL) _mm_set1_epi64x((npy_int64)(VAL)) -#define npyv_setall_s64(VAL) _mm_set1_epi64x((npy_int64)(VAL)) #define npyv_setall_f32 _mm_set1_ps #define npyv_setall_f64 _mm_set1_pd +NPY_FINLINE __m128i npyv__setr_epi64(npy_int64 i0, npy_int64 i1); + +NPY_FINLINE npyv_u64 npyv_setall_u64(npy_uint64 a) +{ +#if defined(_MSC_VER) && defined(_M_IX86) + return npyv__setr_epi64((npy_int64)a, (npy_int64)a); +#else + return _mm_set1_epi64x((npy_int64)a); +#endif +} +NPY_FINLINE npyv_s64 npyv_setall_s64(npy_int64 a) +{ +#if defined(_MSC_VER) && defined(_M_IX86) + return npyv__setr_epi64(a, a); +#else + return _mm_set1_epi64x((npy_int64)a); +#endif +} + /** * vector with specific values set to each lane and * set a specific value to all remained lanes @@ -53,7 +70,11 @@ NPY_FINLINE __m128i npyv__setr_epi32(int i0, int i1, int i2, int i3) } NPY_FINLINE __m128i npyv__setr_epi64(npy_int64 i0, npy_int64 i1) { +#if defined(_MSC_VER) && defined(_M_IX86) + return _mm_setr_epi32((int)i0, (int)(i0 >> 32), (int)i1, (int)(i1 >> 32)); +#else return _mm_set_epi64x(i1, i0); +#endif } NPY_FINLINE __m128 npyv__setr_ps(float i0, float i1, float i2, float i3) { -- cgit v1.2.1 From 22e9e69d96cf9e93e77592397a3ec63f09a97484 Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Thu, 28 Oct 2021 17:44:58 +0200 Subject: fix up --- numpy/core/src/common/simd/avx2/memory.h | 16 ++++++++-------- numpy/core/src/common/simd/avx512/math.h | 2 +- numpy/core/src/common/simd/avx512/memory.h | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/common/simd/avx2/memory.h b/numpy/core/src/common/simd/avx2/memory.h index e27bf15fe..86ba05ee5 100644 --- a/numpy/core/src/common/simd/avx2/memory.h +++ b/numpy/core/src/common/simd/avx2/memory.h @@ -170,9 +170,9 @@ NPY_FINLINE npyv_s32 npyv_load_tillz_s32(const npy_int32 *ptr, npy_uintp nlane) NPY_FINLINE npyv_s64 npyv_load_till_s64(const npy_int64 *ptr, npy_uintp nlane, npy_int64 fill) { assert(nlane > 0); - const __m256i vfill = _mm256_set1_epi64x(fill); - const __m256i steps = _mm256_setr_epi64x(0, 1, 2, 3); - __m256i vnlane = _mm256_set1_epi64x(nlane > 4 ? 4 : (int)nlane); + const __m256i vfill = npyv_setall_s64(fill); + const __m256i steps = npyv_set_s64(0, 1, 2, 3); + __m256i vnlane = npyv_setall_s64(nlane > 4 ? 4 : (int)nlane); __m256i mask = _mm256_cmpgt_epi64(vnlane, steps); __m256i payload = _mm256_maskload_epi64((const void*)ptr, mask); return _mm256_blendv_epi8(vfill, payload, mask); @@ -181,8 +181,8 @@ NPY_FINLINE npyv_s64 npyv_load_till_s64(const npy_int64 *ptr, npy_uintp nlane, n NPY_FINLINE npyv_s64 npyv_load_tillz_s64(const npy_int64 *ptr, npy_uintp nlane) { assert(nlane > 0); - const __m256i steps = _mm256_setr_epi64x(0, 1, 2, 3); - __m256i vnlane = _mm256_set1_epi64x(nlane > 4 ? 4 : (int)nlane); + const __m256i steps = npyv_set_s64(0, 1, 2, 3); + __m256i vnlane = npyv_setall_s64(nlane > 4 ? 4 : (int)nlane); __m256i mask = _mm256_cmpgt_epi64(vnlane, steps); return _mm256_maskload_epi64((const void*)ptr, mask); } @@ -211,10 +211,10 @@ NPY_FINLINE npyv_s64 npyv_loadn_till_s64(const npy_int64 *ptr, npy_intp stride, npy_uintp nlane, npy_int64 fill) { assert(nlane > 0); - const __m256i vfill = _mm256_set1_epi64x(fill); + const __m256i vfill = npyv_setall_s64(fill); const __m256i idx = _mm256_setr_epi64x(0, 1*stride, 2*stride, 3*stride); const __m256i steps = _mm256_setr_epi64x(0, 1, 2, 3); - __m256i vnlane = _mm256_set1_epi64x(nlane > 4 ? 4 : (int)nlane); + __m256i vnlane = npyv_setall_s64(nlane > 4 ? 4 : (int)nlane); __m256i mask = _mm256_cmpgt_epi64(vnlane, steps); return _mm256_mask_i64gather_epi64(vfill, (const void*)ptr, idx, mask, 8); } @@ -239,7 +239,7 @@ NPY_FINLINE void npyv_store_till_s64(npy_int64 *ptr, npy_uintp nlane, npyv_s64 a { assert(nlane > 0); const __m256i steps = _mm256_setr_epi64x(0, 1, 2, 3); - __m256i vnlane = _mm256_set1_epi64x(nlane > 8 ? 8 : (int)nlane); + __m256i vnlane = npyv_setall_s64(nlane > 8 ? 8 : (int)nlane); __m256i mask = _mm256_cmpgt_epi64(vnlane, steps); _mm256_maskstore_epi64((void*)ptr, mask, a); } diff --git a/numpy/core/src/common/simd/avx512/math.h b/numpy/core/src/common/simd/avx512/math.h index 0141396d0..0949b2b06 100644 --- a/numpy/core/src/common/simd/avx512/math.h +++ b/numpy/core/src/common/simd/avx512/math.h @@ -35,7 +35,7 @@ NPY_FINLINE npyv_f64 npyv_abs_f64(npyv_f64 a) return _mm512_range_pd(a, a, 8); #else return npyv_and_f64( - a, _mm512_castsi512_pd(_mm512_set1_epi64(0x7fffffffffffffffLL)) + a, _mm512_castsi512_pd(npyv_setall_s64(0x7fffffffffffffffLL)) ); #endif } diff --git a/numpy/core/src/common/simd/avx512/memory.h b/numpy/core/src/common/simd/avx512/memory.h index bffd6e907..47095bf72 100644 --- a/numpy/core/src/common/simd/avx512/memory.h +++ b/numpy/core/src/common/simd/avx512/memory.h @@ -110,7 +110,7 @@ NPY_FINLINE npyv_f32 npyv_loadn_f32(const float *ptr, npy_intp stride) //// 64 NPY_FINLINE npyv_u64 npyv_loadn_u64(const npy_uint64 *ptr, npy_intp stride) { - const __m512i idx = _mm512_setr_epi64( + const __m512i idx = npyv_set_s64( 0*stride, 1*stride, 2*stride, 3*stride, 4*stride, 5*stride, 6*stride, 7*stride ); @@ -140,7 +140,7 @@ NPY_FINLINE void npyv_storen_f32(float *ptr, npy_intp stride, npyv_f32 a) //// 64 NPY_FINLINE void npyv_storen_u64(npy_uint64 *ptr, npy_intp stride, npyv_u64 a) { - const __m512i idx = _mm512_setr_epi64( + const __m512i idx = npyv_set_s64( 0*stride, 1*stride, 2*stride, 3*stride, 4*stride, 5*stride, 6*stride, 7*stride ); @@ -173,7 +173,7 @@ NPY_FINLINE npyv_s32 npyv_load_tillz_s32(const npy_int32 *ptr, npy_uintp nlane) NPY_FINLINE npyv_s64 npyv_load_till_s64(const npy_int64 *ptr, npy_uintp nlane, npy_int64 fill) { assert(nlane > 0); - const __m512i vfill = _mm512_set1_epi64(fill); + const __m512i vfill = npyv_setall_s64(fill); const __mmask8 mask = nlane > 31 ? -1 : (1 << nlane) - 1; return _mm512_mask_loadu_epi64(vfill, mask, (const __m512i*)ptr); } @@ -210,11 +210,11 @@ NPY_FINLINE npyv_s64 npyv_loadn_till_s64(const npy_int64 *ptr, npy_intp stride, npy_uintp nlane, npy_int64 fill) { assert(nlane > 0); - const __m512i idx = _mm512_setr_epi64( + const __m512i idx = npyv_set_s64( 0*stride, 1*stride, 2*stride, 3*stride, 4*stride, 5*stride, 6*stride, 7*stride ); - const __m512i vfill = _mm512_set1_epi64(fill); + const __m512i vfill = npyv_setall_s64(fill); const __mmask8 mask = nlane > 31 ? -1 : (1 << nlane) - 1; return _mm512_mask_i64gather_epi64(vfill, mask, idx, (const __m512i*)ptr, 8); } @@ -258,7 +258,7 @@ NPY_FINLINE void npyv_storen_till_s32(npy_int32 *ptr, npy_intp stride, npy_uintp NPY_FINLINE void npyv_storen_till_s64(npy_int64 *ptr, npy_intp stride, npy_uintp nlane, npyv_s64 a) { assert(nlane > 0); - const __m512i idx = _mm512_setr_epi64( + const __m512i idx = npyv_set_s64( 0*stride, 1*stride, 2*stride, 3*stride, 4*stride, 5*stride, 6*stride, 7*stride ); -- cgit v1.2.1 From 4971fef21f2fb0e7b5f5f4b289b26155a8772224 Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Thu, 28 Oct 2021 17:47:55 +0200 Subject: fix up up --- numpy/core/src/common/simd/avx2/memory.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/common/simd/avx2/memory.h b/numpy/core/src/common/simd/avx2/memory.h index 86ba05ee5..5891a270a 100644 --- a/numpy/core/src/common/simd/avx2/memory.h +++ b/numpy/core/src/common/simd/avx2/memory.h @@ -87,7 +87,7 @@ NPY_FINLINE npyv_f32 npyv_loadn_f32(const float *ptr, npy_intp stride) #if 0 // slower NPY_FINLINE npyv_u64 npyv_loadn_u64(const npy_uint64 *ptr, npy_intp stride) { - const __m256i idx = _mm256_setr_epi64x(0, 1*stride, 2*stride, 3*stride); + const __m256i idx = npyv_set_s64(0, 1*stride, 2*stride, 3*stride); return _mm256_i64gather_epi64((const void*)ptr, idx, 8); } NPY_FINLINE npyv_s64 npyv_loadn_s64(const npy_int64 *ptr, npy_intp stride) @@ -212,8 +212,8 @@ npyv_loadn_till_s64(const npy_int64 *ptr, npy_intp stride, npy_uintp nlane, npy_ { assert(nlane > 0); const __m256i vfill = npyv_setall_s64(fill); - const __m256i idx = _mm256_setr_epi64x(0, 1*stride, 2*stride, 3*stride); - const __m256i steps = _mm256_setr_epi64x(0, 1, 2, 3); + const __m256i idx = npyv_set_s64(0, 1*stride, 2*stride, 3*stride); + const __m256i steps = npyv_set_s64(0, 1, 2, 3); __m256i vnlane = npyv_setall_s64(nlane > 4 ? 4 : (int)nlane); __m256i mask = _mm256_cmpgt_epi64(vnlane, steps); return _mm256_mask_i64gather_epi64(vfill, (const void*)ptr, idx, mask, 8); @@ -238,7 +238,7 @@ NPY_FINLINE void npyv_store_till_s32(npy_int32 *ptr, npy_uintp nlane, npyv_s32 a NPY_FINLINE void npyv_store_till_s64(npy_int64 *ptr, npy_uintp nlane, npyv_s64 a) { assert(nlane > 0); - const __m256i steps = _mm256_setr_epi64x(0, 1, 2, 3); + const __m256i steps = npyv_set_s64(0, 1, 2, 3); __m256i vnlane = npyv_setall_s64(nlane > 8 ? 8 : (int)nlane); __m256i mask = _mm256_cmpgt_epi64(vnlane, steps); _mm256_maskstore_epi64((void*)ptr, mask, a); -- cgit v1.2.1 From 7450ea37a96273cfb1641991f7b6fb0fa3ba9590 Mon Sep 17 00:00:00 2001 From: warren Date: Thu, 28 Oct 2021 00:39:20 -0400 Subject: DOC: Mention `nan` results in `power` and `float_power`. By design, `power` and `float_power` return `nan` when a negative value is raised to a non-integral value. There have been several issues raised about this over the last few years, so let's explicitly mention this in the docstrings, and give an example of explicitly specifying `dtype=complex` to get complex results intead of `nan`. Closes gh-20186. Closes gh-12182. Closes gh-12118. --- numpy/core/code_generators/ufunc_docstrings.py | 44 ++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/code_generators/ufunc_docstrings.py b/numpy/core/code_generators/ufunc_docstrings.py index 4e1182de6..ab5f74df3 100644 --- a/numpy/core/code_generators/ufunc_docstrings.py +++ b/numpy/core/code_generators/ufunc_docstrings.py @@ -3065,8 +3065,14 @@ add_newdoc('numpy.core.umath', 'power', First array elements raised to powers from second array, element-wise. Raise each base in `x1` to the positionally-corresponding power in - `x2`. `x1` and `x2` must be broadcastable to the same shape. Note that an - integer type raised to a negative integer power will raise a ValueError. + `x2`. `x1` and `x2` must be broadcastable to the same shape. + + An integer type raised to a negative integer power will raise a + ``ValueError``. + + Negative values raised to a non-integral value will return ``nan``. + To get complex results, cast the input to complex, or specify the + ``dtype`` to be ``complex`` (see the example below). Parameters ---------- @@ -3121,6 +3127,21 @@ add_newdoc('numpy.core.umath', 'power', >>> x1 ** x2 array([ 0, 1, 8, 27, 16, 5]) + Negative values raised to a non-integral value will result in ``nan`` + (and a warning will be generated). + + >>> x3 = np.array([-1.0, -4.0]) + >>> with np.errstate(invalid='ignore'): + ... p = np.power(x3, 1.5) + ... + >>> p + array([nan, nan]) + + To get complex results, give the argument ``dtype=complex``. + + >>> np.power(x3, 1.5, dtype=complex) + array([-1.83697020e-16-1.j, -1.46957616e-15-8.j]) + """) add_newdoc('numpy.core.umath', 'float_power', @@ -3134,6 +3155,10 @@ add_newdoc('numpy.core.umath', 'float_power', inexact. The intent is that the function will return a usable result for negative powers and seldom overflow for positive powers. + Negative values raised to a non-integral value will return ``nan``. + To get complex results, cast the input to complex, or specify the + ``dtype`` to be ``complex`` (see the example below). + .. versionadded:: 1.12.0 Parameters @@ -3181,6 +3206,21 @@ add_newdoc('numpy.core.umath', 'float_power', array([[ 0., 1., 8., 27., 16., 5.], [ 0., 1., 8., 27., 16., 5.]]) + Negative values raised to a non-integral value will result in ``nan`` + (and a warning will be generated). + + >>> x3 = np.array([-1, -4]) + >>> with np.errstate(invalid='ignore'): + ... p = np.float_power(x3, 1.5) + ... + >>> p + array([nan, nan]) + + To get complex results, give the argument ``dtype=complex``. + + >>> np.float_power(x3, 1.5, dtype=complex) + array([-1.83697020e-16-1.j, -1.46957616e-15-8.j]) + """) add_newdoc('numpy.core.umath', 'radians', -- cgit v1.2.1 From 1d90222e3a10621791a58ffb040e60d9bcc67795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E7=AB=8B=E4=B8=9A=EF=BC=88Chris=20Fu=EF=BC=89?= <17433201@qq.com> Date: Fri, 29 Oct 2021 11:41:10 +0800 Subject: BUG: Fix shadowed reference of `dtype` in type stubs Direct use of `dtype` in `ndarray` or `generic` would be shadowed by `ndarray.dtype` or `generic.dtype`, so an alias is used here. See #20223. --- numpy/__init__.pyi | 65 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 463d4a713..bc95c5d06 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1431,6 +1431,9 @@ _ArrayComplex_co = NDArray[Union[bool_, integer[Any], floating[Any], complexfloa _ArrayNumber_co = NDArray[Union[bool_, number[Any]]] _ArrayTD64_co = NDArray[Union[bool_, integer[Any], timedelta64]] +# Introduce an alias for `dtype` to avoid naming conflicts. +_dtype = dtype + class _SupportsItem(Protocol[_T_co]): def item(self, args: Any, /) -> _T_co: ... @@ -1452,13 +1455,13 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @property def real( self: NDArray[_SupportsReal[_ScalarType]], # type: ignore[type-var] - ) -> ndarray[_ShapeType, dtype[_ScalarType]]: ... + ) -> ndarray[_ShapeType, _dtype[_ScalarType]]: ... @real.setter def real(self, value: ArrayLike) -> None: ... @property def imag( self: NDArray[_SupportsImag[_ScalarType]], # type: ignore[type-var] - ) -> ndarray[_ShapeType, dtype[_ScalarType]]: ... + ) -> ndarray[_ShapeType, _dtype[_ScalarType]]: ... @imag.setter def imag(self, value: ArrayLike) -> None: ... def __new__( @@ -1530,7 +1533,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __getitem__(self: NDArray[void], key: str) -> NDArray[Any]: ... @overload - def __getitem__(self: NDArray[void], key: list[str]) -> ndarray[_ShapeType, dtype[void]]: ... + def __getitem__(self: NDArray[void], key: list[str]) -> ndarray[_ShapeType, _dtype[void]]: ... @property def ctypes(self) -> _ctypes[int]: ... @@ -1550,12 +1553,12 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): # Use the same output type as that of the underlying `generic` @overload def item( - self: ndarray[Any, dtype[_SupportsItem[_T]]], # type: ignore[type-var] + self: ndarray[Any, _dtype[_SupportsItem[_T]]], # type: ignore[type-var] *args: SupportsIndex, ) -> _T: ... @overload def item( - self: ndarray[Any, dtype[_SupportsItem[_T]]], # type: ignore[type-var] + self: ndarray[Any, _dtype[_SupportsItem[_T]]], # type: ignore[type-var] args: Tuple[SupportsIndex, ...], /, ) -> _T: ... @@ -1596,7 +1599,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): axis: Optional[SupportsIndex] = ..., kind: _PartitionKind = ..., order: Union[None, str, Sequence[str]] = ..., - ) -> ndarray[Any, dtype[intp]]: ... + ) -> ndarray[Any, _dtype[intp]]: ... def diagonal( self, @@ -1615,7 +1618,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def dot(self, b: ArrayLike, out: _NdArraySubClass) -> _NdArraySubClass: ... # `nonzero()` is deprecated for 0d arrays/generics - def nonzero(self) -> Tuple[ndarray[Any, dtype[intp]], ...]: ... + def nonzero(self) -> Tuple[ndarray[Any, _dtype[intp]], ...]: ... def partition( self, @@ -1647,7 +1650,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): v: ArrayLike, side: _SortSide = ..., sorter: Optional[_ArrayLikeInt_co] = ..., - ) -> ndarray[Any, dtype[intp]]: ... + ) -> ndarray[Any, _dtype[intp]]: ... def setfield( self, @@ -1684,7 +1687,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def take( # type: ignore[misc] - self: ndarray[Any, dtype[_ScalarType]], + self: ndarray[Any, _dtype[_ScalarType]], indices: _IntLike_co, axis: Optional[SupportsIndex] = ..., out: None = ..., @@ -1781,19 +1784,19 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): # Dispatch to the underlying `generic` via protocols def __int__( - self: ndarray[Any, dtype[SupportsInt]], # type: ignore[type-var] + self: ndarray[Any, _dtype[SupportsInt]], # type: ignore[type-var] ) -> int: ... def __float__( - self: ndarray[Any, dtype[SupportsFloat]], # type: ignore[type-var] + self: ndarray[Any, _dtype[SupportsFloat]], # type: ignore[type-var] ) -> float: ... def __complex__( - self: ndarray[Any, dtype[SupportsComplex]], # type: ignore[type-var] + self: ndarray[Any, _dtype[SupportsComplex]], # type: ignore[type-var] ) -> complex: ... def __index__( - self: ndarray[Any, dtype[SupportsIndex]], # type: ignore[type-var] + self: ndarray[Any, _dtype[SupportsIndex]], # type: ignore[type-var] ) -> int: ... def __len__(self) -> int: ... @@ -1925,7 +1928,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __mod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc] @overload - def __mod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ... + def __mod__(self: _ArrayTD64_co, other: _SupportsArray[_dtype[timedelta64]] | _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[timedelta64]: ... @overload def __mod__(self: NDArray[object_], other: Any) -> Any: ... @overload @@ -1940,7 +1943,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rmod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc] @overload - def __rmod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ... + def __rmod__(self: _ArrayTD64_co, other: _SupportsArray[_dtype[timedelta64]] | _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[timedelta64]: ... @overload def __rmod__(self: NDArray[object_], other: Any) -> Any: ... @overload @@ -1955,7 +1958,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __divmod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> _2Tuple[NDArray[floating[Any]]]: ... # type: ignore[misc] @overload - def __divmod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... + def __divmod__(self: _ArrayTD64_co, other: _SupportsArray[_dtype[timedelta64]] | _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... @overload def __rdivmod__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> _2Tuple[NDArray[int8]]: ... # type: ignore[misc] @@ -1966,7 +1969,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rdivmod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> _2Tuple[NDArray[floating[Any]]]: ... # type: ignore[misc] @overload - def __rdivmod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... + def __rdivmod__(self: _ArrayTD64_co, other: _SupportsArray[_dtype[timedelta64]] | _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... @overload def __add__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @@ -2099,7 +2102,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __floordiv__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc] @overload - def __floordiv__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[int64]: ... + def __floordiv__(self: NDArray[timedelta64], other: _SupportsArray[_dtype[timedelta64]] | _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[int64]: ... @overload def __floordiv__(self: NDArray[timedelta64], other: _ArrayLikeBool_co) -> NoReturn: ... @overload @@ -2118,7 +2121,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rfloordiv__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc] @overload - def __rfloordiv__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[int64]: ... + def __rfloordiv__(self: NDArray[timedelta64], other: _SupportsArray[_dtype[timedelta64]] | _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[int64]: ... @overload def __rfloordiv__(self: NDArray[bool_], other: _ArrayLikeTD64_co) -> NoReturn: ... @overload @@ -2165,7 +2168,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __truediv__(self: _ArrayComplex_co, other: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ... # type: ignore[misc] @overload - def __truediv__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[float64]: ... + def __truediv__(self: NDArray[timedelta64], other: _SupportsArray[_dtype[timedelta64]] | _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[float64]: ... @overload def __truediv__(self: NDArray[timedelta64], other: _ArrayLikeBool_co) -> NoReturn: ... @overload @@ -2182,7 +2185,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rtruediv__(self: _ArrayComplex_co, other: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ... # type: ignore[misc] @overload - def __rtruediv__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[float64]: ... + def __rtruediv__(self: NDArray[timedelta64], other: _SupportsArray[_dtype[timedelta64]] | _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[float64]: ... @overload def __rtruediv__(self: NDArray[bool_], other: _ArrayLikeTD64_co) -> NoReturn: ... @overload @@ -2394,7 +2397,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __imod__(self: NDArray[floating[_NBit1]], other: _ArrayLikeFloat_co) -> NDArray[floating[_NBit1]]: ... @overload - def __imod__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ... + def __imod__(self: NDArray[timedelta64], other: _SupportsArray[_dtype[timedelta64]] | _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[timedelta64]: ... @overload def __imod__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... @@ -2459,7 +2462,7 @@ class generic(_ArrayOrScalarCommon): @abstractmethod def __init__(self, *args: Any, **kwargs: Any) -> None: ... @overload - def __array__(self: _ScalarType, dtype: None = ..., /) -> ndarray[Any, dtype[_ScalarType]]: ... + def __array__(self: _ScalarType, dtype: None = ..., /) -> ndarray[Any, _dtype[_ScalarType]]: ... @overload def __array__(self, dtype: _DType, /) -> ndarray[Any, _DType]: ... @property @@ -2474,7 +2477,7 @@ class generic(_ArrayOrScalarCommon): def strides(self) -> Tuple[()]: ... def byteswap(self: _ScalarType, inplace: L[False] = ...) -> _ScalarType: ... @property - def flat(self: _ScalarType) -> flatiter[ndarray[Any, dtype[_ScalarType]]]: ... + def flat(self: _ScalarType) -> flatiter[ndarray[Any, _dtype[_ScalarType]]]: ... @overload def astype( @@ -2547,7 +2550,7 @@ class generic(_ArrayOrScalarCommon): axis: Optional[SupportsIndex] = ..., out: None = ..., mode: _ModeKind = ..., - ) -> ndarray[Any, dtype[_ScalarType]]: ... + ) -> ndarray[Any, _dtype[_ScalarType]]: ... @overload def take( self, @@ -2561,26 +2564,26 @@ class generic(_ArrayOrScalarCommon): self: _ScalarType, repeats: _ArrayLikeInt_co, axis: Optional[SupportsIndex] = ..., - ) -> ndarray[Any, dtype[_ScalarType]]: ... + ) -> ndarray[Any, _dtype[_ScalarType]]: ... def flatten( self: _ScalarType, order: _OrderKACF = ..., - ) -> ndarray[Any, dtype[_ScalarType]]: ... + ) -> ndarray[Any, _dtype[_ScalarType]]: ... def ravel( self: _ScalarType, order: _OrderKACF = ..., - ) -> ndarray[Any, dtype[_ScalarType]]: ... + ) -> ndarray[Any, _dtype[_ScalarType]]: ... @overload def reshape( self: _ScalarType, shape: _ShapeLike, /, *, order: _OrderACF = ... - ) -> ndarray[Any, dtype[_ScalarType]]: ... + ) -> ndarray[Any, _dtype[_ScalarType]]: ... @overload def reshape( self: _ScalarType, *shape: SupportsIndex, order: _OrderACF = ... - ) -> ndarray[Any, dtype[_ScalarType]]: ... + ) -> ndarray[Any, _dtype[_ScalarType]]: ... def squeeze( self: _ScalarType, axis: Union[L[0], Tuple[()]] = ... @@ -2588,7 +2591,7 @@ class generic(_ArrayOrScalarCommon): def transpose(self: _ScalarType, axes: Tuple[()] = ..., /) -> _ScalarType: ... # Keep `dtype` at the bottom to avoid name conflicts with `np.dtype` @property - def dtype(self: _ScalarType) -> dtype[_ScalarType]: ... + def dtype(self: _ScalarType) -> _dtype[_ScalarType]: ... class number(generic, Generic[_NBit1]): # type: ignore @property -- cgit v1.2.1 From b65f9180118775cfd255737246f2029a81cf3761 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 5 Feb 2021 18:16:51 +0100 Subject: ENH: Add annotations for `np.ctypeslib` --- numpy/ctypeslib.pyi | 267 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 260 insertions(+), 7 deletions(-) (limited to 'numpy') diff --git a/numpy/ctypeslib.pyi b/numpy/ctypeslib.pyi index 642017ba7..1c396d240 100644 --- a/numpy/ctypeslib.pyi +++ b/numpy/ctypeslib.pyi @@ -1,15 +1,268 @@ -from typing import List, Type - # NOTE: Numpy's mypy plugin is used for importing the correct # platform-specific `ctypes._SimpleCData[int]` sub-type from ctypes import c_int64 as _c_intp +import os +import sys +import ctypes +from typing import ( + Literal as L, + Any, + List, + Union, + TypeVar, + Type, + Generic, + Optional, + overload, + Iterable, + ClassVar, + Tuple, + Sequence, + Dict, +) + +from numpy import ( + ndarray, + dtype, + generic, + bool_, + byte, + short, + intc, + int_, + longlong, + ubyte, + ushort, + uintc, + uint, + ulonglong, + single, + double, + float_, + longdouble, + void, +) +from numpy.core._internal import _ctypes +from numpy.core.multiarray import flagsobj +from numpy.typing import ( + # Arrays + ArrayLike, + NDArray, + _FiniteNestedSequence, + _SupportsArray, + + # Shapes + _ShapeLike, + + # DTypes + DTypeLike, + _SupportsDType, + _VoidDTypeLike, + _BoolCodes, + _UByteCodes, + _UShortCodes, + _UIntCCodes, + _UIntCodes, + _ULongLongCodes, + _ByteCodes, + _ShortCodes, + _IntCCodes, + _IntCodes, + _LongLongCodes, + _SingleCodes, + _DoubleCodes, + _LongDoubleCodes, +) + +# TODO: Add a proper `_Shape` bound once we've got variadic typevars +_DType = TypeVar("_DType", bound=dtype[Any]) +_DTypeOptional = TypeVar("_DTypeOptional", bound=Optional[dtype[Any]]) +_SCT = TypeVar("_SCT", bound=generic) + +_DTypeLike = Union[ + dtype[_SCT], + Type[_SCT], + _SupportsDType[dtype[_SCT]], +] +_ArrayLike = _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]] + +_FlagsKind = L[ + 'C_CONTIGUOUS', 'CONTIGUOUS', 'C', + 'F_CONTIGUOUS', 'FORTRAN', 'F', + 'ALIGNED', 'A', + 'WRITEABLE', 'W', + 'OWNDATA', 'O', + 'UPDATEIFCOPY', 'U', + 'WRITEBACKIFCOPY', 'X', +] + +# TODO: Add a shape typevar once we have variadic typevars (PEP 646) +class _ndptr(ctypes.c_void_p, Generic[_DTypeOptional]): + # In practice these 4 classvars are defined in the dynamic class + # returned by `ndpointer` + _dtype_: ClassVar[_DTypeOptional] + _shape_: ClassVar[None] + _ndim_: ClassVar[None | int] + _flags_: ClassVar[None | List[_FlagsKind]] + + @overload + @classmethod + def from_param(cls: Type[_ndptr[None]], obj: ndarray[Any, Any]) -> _ctypes: ... + @overload + @classmethod + def from_param(cls: Type[_ndptr[_DType]], obj: ndarray[Any, _DType]) -> _ctypes: ... + +class _concrete_ndptr(_ndptr[_DType]): + _dtype_: ClassVar[_DType] + _shape_: ClassVar[Tuple[int, ...]] + @property + def contents(self) -> ndarray[Any, _DType]: ... + +def load_library( + libname: str | bytes | os.PathLike[str] | os.PathLike[bytes], + loader_path: str | bytes | os.PathLike[str] | os.PathLike[bytes], +) -> ctypes.CDLL: ... + __all__: List[str] c_intp = _c_intp -def load_library(libname, loader_path): ... -def ndpointer(dtype=..., ndim=..., shape=..., flags=...): ... -def as_ctypes(obj): ... -def as_array(obj, shape=...): ... -def as_ctypes_type(dtype): ... +@overload +def ndpointer( + dtype: None = ..., + ndim: int = ..., + shape: None | _ShapeLike = ..., + flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ..., +) -> Type[_ndptr[None]]: ... +@overload +def ndpointer( + dtype: _DTypeLike[_SCT], + ndim: int = ..., + *, + shape: _ShapeLike, + flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ..., +) -> Type[_concrete_ndptr[dtype[_SCT]]]: ... +@overload +def ndpointer( + dtype: DTypeLike, + ndim: int = ..., + *, + shape: _ShapeLike, + flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ..., +) -> Type[_concrete_ndptr[dtype[Any]]]: ... +@overload +def ndpointer( + dtype: _DTypeLike[_SCT], + ndim: int = ..., + shape: None = ..., + flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ..., +) -> Type[_ndptr[dtype[_SCT]]]: ... +@overload +def ndpointer( + dtype: DTypeLike, + ndim: int = ..., + shape: None = ..., + flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ..., +) -> Type[_ndptr[dtype[Any]]]: ... + +@overload +def as_ctypes_type(dtype: _BoolCodes | _DTypeLike[bool_] | Type[ctypes.c_bool]) -> Type[ctypes.c_bool]: ... +@overload +def as_ctypes_type(dtype: _ByteCodes | _DTypeLike[byte] | Type[ctypes.c_byte]) -> Type[ctypes.c_byte]: ... +@overload +def as_ctypes_type(dtype: _ShortCodes | _DTypeLike[short] | Type[ctypes.c_short]) -> Type[ctypes.c_short]: ... +@overload +def as_ctypes_type(dtype: _IntCCodes | _DTypeLike[intc] | Type[ctypes.c_int]) -> Type[ctypes.c_int]: ... +@overload +def as_ctypes_type(dtype: _IntCodes | _DTypeLike[int_] | Type[int | ctypes.c_long]) -> Type[ctypes.c_long]: ... +@overload +def as_ctypes_type(dtype: _LongLongCodes | _DTypeLike[longlong] | Type[ctypes.c_longlong]) -> Type[ctypes.c_longlong]: ... +@overload +def as_ctypes_type(dtype: _UByteCodes | _DTypeLike[ubyte] | Type[ctypes.c_ubyte]) -> Type[ctypes.c_ubyte]: ... +@overload +def as_ctypes_type(dtype: _UShortCodes | _DTypeLike[ushort] | Type[ctypes.c_ushort]) -> Type[ctypes.c_ushort]: ... +@overload +def as_ctypes_type(dtype: _UIntCCodes | _DTypeLike[uintc] | Type[ctypes.c_uint]) -> Type[ctypes.c_uint]: ... +@overload +def as_ctypes_type(dtype: _UIntCodes | _DTypeLike[uint] | Type[ctypes.c_ulong]) -> Type[ctypes.c_ulong]: ... +@overload +def as_ctypes_type(dtype: _ULongLongCodes | _DTypeLike[ulonglong] | Type[ctypes.c_ulonglong]) -> Type[ctypes.c_ulonglong]: ... +@overload +def as_ctypes_type(dtype: _SingleCodes | _DTypeLike[single] | Type[ctypes.c_float]) -> Type[ctypes.c_float]: ... +@overload +def as_ctypes_type(dtype: _DoubleCodes | _DTypeLike[double] | Type[float | ctypes.c_double]) -> Type[ctypes.c_double]: ... +@overload +def as_ctypes_type(dtype: _LongDoubleCodes | _DTypeLike[longdouble] | Type[ctypes.c_longdouble]) -> Type[ctypes.c_longdouble]: ... +@overload +def as_ctypes_type(dtype: _VoidDTypeLike) -> Type[Any]: ... # `ctypes.Union` or `ctypes.Structure` +@overload +def as_ctypes_type(dtype: str) -> Type[Any]: ... + +@overload +def as_array(obj: ctypes._PointerLike, shape: Sequence[int]) -> NDArray[Any]: ... +@overload +def as_array(obj: _ArrayLike[_SCT], shape: None | _ShapeLike = ...) -> NDArray[_SCT]: ... +@overload +def as_array(obj: object, shape: None | _ShapeLike = ...) -> NDArray[Any]: ... + +@overload +def as_ctypes(obj: bool_) -> ctypes.c_bool: ... +@overload +def as_ctypes(obj: byte) -> ctypes.c_byte: ... +@overload +def as_ctypes(obj: short) -> ctypes.c_short: ... +@overload +def as_ctypes(obj: intc) -> ctypes.c_int: ... +@overload +def as_ctypes(obj: int_) -> ctypes.c_long: ... +@overload +def as_ctypes(obj: longlong) -> ctypes.c_longlong: ... +@overload +def as_ctypes(obj: ubyte) -> ctypes.c_ubyte: ... +@overload +def as_ctypes(obj: ushort) -> ctypes.c_ushort: ... +@overload +def as_ctypes(obj: uintc) -> ctypes.c_uint: ... +@overload +def as_ctypes(obj: uint) -> ctypes.c_ulong: ... +@overload +def as_ctypes(obj: ulonglong) -> ctypes.c_ulonglong: ... +@overload +def as_ctypes(obj: single) -> ctypes.c_float: ... +@overload +def as_ctypes(obj: double) -> ctypes.c_double: ... +@overload +def as_ctypes(obj: longdouble) -> ctypes.c_longdouble: ... +@overload +def as_ctypes(obj: void) -> Any: ... # `ctypes.Union` or `ctypes.Structure` +@overload +def as_ctypes(obj: NDArray[bool_]) -> ctypes.Array[ctypes.c_bool]: ... +@overload +def as_ctypes(obj: NDArray[byte]) -> ctypes.Array[ctypes.c_byte]: ... +@overload +def as_ctypes(obj: NDArray[short]) -> ctypes.Array[ctypes.c_short]: ... +@overload +def as_ctypes(obj: NDArray[intc]) -> ctypes.Array[ctypes.c_int]: ... +@overload +def as_ctypes(obj: NDArray[int_]) -> ctypes.Array[ctypes.c_long]: ... +@overload +def as_ctypes(obj: NDArray[longlong]) -> ctypes.Array[ctypes.c_longlong]: ... +@overload +def as_ctypes(obj: NDArray[ubyte]) -> ctypes.Array[ctypes.c_ubyte]: ... +@overload +def as_ctypes(obj: NDArray[ushort]) -> ctypes.Array[ctypes.c_ushort]: ... +@overload +def as_ctypes(obj: NDArray[uintc]) -> ctypes.Array[ctypes.c_uint]: ... +@overload +def as_ctypes(obj: NDArray[uint]) -> ctypes.Array[ctypes.c_ulong]: ... +@overload +def as_ctypes(obj: NDArray[ulonglong]) -> ctypes.Array[ctypes.c_ulonglong]: ... +@overload +def as_ctypes(obj: NDArray[single]) -> ctypes.Array[ctypes.c_float]: ... +@overload +def as_ctypes(obj: NDArray[double]) -> ctypes.Array[ctypes.c_double]: ... +@overload +def as_ctypes(obj: NDArray[longdouble]) -> ctypes.Array[ctypes.c_longdouble]: ... +@overload +def as_ctypes(obj: NDArray[void]) -> ctypes.Array[Any]: ... # `ctypes.Union` or `ctypes.Structure` -- cgit v1.2.1 From 90a8d4a7b4fa9cc50d46fda0207dab1b9bd2c1a3 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 6 Feb 2021 14:42:12 +0100 Subject: TST: Add typing tests for `np.ctypeslib` --- numpy/typing/tests/data/reveal/ctypeslib.pyi | 80 ++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'numpy') diff --git a/numpy/typing/tests/data/reveal/ctypeslib.pyi b/numpy/typing/tests/data/reveal/ctypeslib.pyi index 0c32d70ed..ae0b6b345 100644 --- a/numpy/typing/tests/data/reveal/ctypeslib.pyi +++ b/numpy/typing/tests/data/reveal/ctypeslib.pyi @@ -1,3 +1,83 @@ +import ctypes +from typing import Any + import numpy as np +import numpy.typing as npt + +AR_bool: npt.NDArray[np.bool_] +AR_ubyte: npt.NDArray[np.ubyte] +AR_ushort: npt.NDArray[np.ushort] +AR_uintc: npt.NDArray[np.uintc] +AR_uint: npt.NDArray[np.uint] +AR_ulonglong: npt.NDArray[np.ulonglong] +AR_byte: npt.NDArray[np.byte] +AR_short: npt.NDArray[np.short] +AR_intc: npt.NDArray[np.intc] +AR_int: npt.NDArray[np.int_] +AR_longlong: npt.NDArray[np.longlong] +AR_single: npt.NDArray[np.single] +AR_double: npt.NDArray[np.double] +AR_void: npt.NDArray[np.void] + +pointer: ctypes.pointer[Any] reveal_type(np.ctypeslib.c_intp()) # E: {c_intp} + +reveal_type(np.ctypeslib.ndpointer()) # E: Type[numpy.ctypeslib._ndptr[None]] +reveal_type(np.ctypeslib.ndpointer(dtype=np.float64)) # E: Type[numpy.ctypeslib._ndptr[numpy.dtype[{float64}]]] +reveal_type(np.ctypeslib.ndpointer(dtype=float)) # E: Type[numpy.ctypeslib._ndptr[numpy.dtype[Any]]] +reveal_type(np.ctypeslib.ndpointer(shape=(10, 3))) # E: Type[numpy.ctypeslib._ndptr[None]] +reveal_type(np.ctypeslib.ndpointer(np.int64, shape=(10, 3))) # E: Type[numpy.ctypeslib._concrete_ndptr[numpy.dtype[{int64}]]] +reveal_type(np.ctypeslib.ndpointer(int, shape=(1,))) # E: Type[numpy.ctypeslib._concrete_ndptr[numpy.dtype[Any]]] + +reveal_type(np.ctypeslib.as_ctypes_type(np.bool_)) # E: Type[ctypes.c_bool] +reveal_type(np.ctypeslib.as_ctypes_type(np.ubyte)) # E: Type[ctypes.c_ubyte] +reveal_type(np.ctypeslib.as_ctypes_type(np.ushort)) # E: Type[ctypes.c_ushort] +reveal_type(np.ctypeslib.as_ctypes_type(np.uintc)) # E: Type[ctypes.c_uint] +reveal_type(np.ctypeslib.as_ctypes_type(np.uint)) # E: Type[ctypes.c_ulong] +reveal_type(np.ctypeslib.as_ctypes_type(np.ulonglong)) # E: Type[ctypes.c_ulong] +reveal_type(np.ctypeslib.as_ctypes_type(np.byte)) # E: Type[ctypes.c_byte] +reveal_type(np.ctypeslib.as_ctypes_type(np.short)) # E: Type[ctypes.c_short] +reveal_type(np.ctypeslib.as_ctypes_type(np.intc)) # E: Type[ctypes.c_int] +reveal_type(np.ctypeslib.as_ctypes_type(np.int_)) # E: Type[ctypes.c_long] +reveal_type(np.ctypeslib.as_ctypes_type(np.longlong)) # E: Type[ctypes.c_long] +reveal_type(np.ctypeslib.as_ctypes_type(np.single)) # E: Type[ctypes.c_float] +reveal_type(np.ctypeslib.as_ctypes_type(np.double)) # E: Type[ctypes.c_double] +reveal_type(np.ctypeslib.as_ctypes_type(ctypes.c_double)) # E: Type[ctypes.c_double] +reveal_type(np.ctypeslib.as_ctypes_type("q")) # E: Type[ctypes.c_longlong] +reveal_type(np.ctypeslib.as_ctypes_type([("i8", np.int64), ("f8", np.float64)])) # E: Type[Any] +reveal_type(np.ctypeslib.as_ctypes_type("i8")) # E: Type[Any] +reveal_type(np.ctypeslib.as_ctypes_type("f8")) # E: Type[Any] + +reveal_type(np.ctypeslib.as_ctypes(AR_bool.take(0))) # E: ctypes.c_bool +reveal_type(np.ctypeslib.as_ctypes(AR_ubyte.take(0))) # E: ctypes.c_ubyte +reveal_type(np.ctypeslib.as_ctypes(AR_ushort.take(0))) # E: ctypes.c_ushort +reveal_type(np.ctypeslib.as_ctypes(AR_uintc.take(0))) # E: ctypes.c_uint +reveal_type(np.ctypeslib.as_ctypes(AR_uint.take(0))) # E: ctypes.c_ulong +reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong.take(0))) # E: ctypes.c_ulong +reveal_type(np.ctypeslib.as_ctypes(AR_byte.take(0))) # E: ctypes.c_byte +reveal_type(np.ctypeslib.as_ctypes(AR_short.take(0))) # E: ctypes.c_short +reveal_type(np.ctypeslib.as_ctypes(AR_intc.take(0))) # E: ctypes.c_int +reveal_type(np.ctypeslib.as_ctypes(AR_int.take(0))) # E: ctypes.c_long +reveal_type(np.ctypeslib.as_ctypes(AR_longlong.take(0))) # E: ctypes.c_long +reveal_type(np.ctypeslib.as_ctypes(AR_single.take(0))) # E: ctypes.c_float +reveal_type(np.ctypeslib.as_ctypes(AR_double.take(0))) # E: ctypes.c_double +reveal_type(np.ctypeslib.as_ctypes(AR_void.take(0))) # E: Any +reveal_type(np.ctypeslib.as_ctypes(AR_bool)) # E: ctypes.Array[ctypes.c_bool] +reveal_type(np.ctypeslib.as_ctypes(AR_ubyte)) # E: ctypes.Array[ctypes.c_ubyte] +reveal_type(np.ctypeslib.as_ctypes(AR_ushort)) # E: ctypes.Array[ctypes.c_ushort] +reveal_type(np.ctypeslib.as_ctypes(AR_uintc)) # E: ctypes.Array[ctypes.c_uint] +reveal_type(np.ctypeslib.as_ctypes(AR_uint)) # E: ctypes.Array[ctypes.c_ulong] +reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong)) # E: ctypes.Array[ctypes.c_ulong] +reveal_type(np.ctypeslib.as_ctypes(AR_byte)) # E: ctypes.Array[ctypes.c_byte] +reveal_type(np.ctypeslib.as_ctypes(AR_short)) # E: ctypes.Array[ctypes.c_short] +reveal_type(np.ctypeslib.as_ctypes(AR_intc)) # E: ctypes.Array[ctypes.c_int] +reveal_type(np.ctypeslib.as_ctypes(AR_int)) # E: ctypes.Array[ctypes.c_long] +reveal_type(np.ctypeslib.as_ctypes(AR_longlong)) # E: ctypes.Array[ctypes.c_long] +reveal_type(np.ctypeslib.as_ctypes(AR_single)) # E: ctypes.Array[ctypes.c_float] +reveal_type(np.ctypeslib.as_ctypes(AR_double)) # E: ctypes.Array[ctypes.c_double] +reveal_type(np.ctypeslib.as_ctypes(AR_void)) # E: ctypes.Array[Any] + +reveal_type(np.ctypeslib.as_array(AR_ubyte)) # E: numpy.ndarray[Any, numpy.dtype[{ubyte}]] +reveal_type(np.ctypeslib.as_array(1)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.ctypeslib.as_array(pointer)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -- cgit v1.2.1 From a2b4bb7b67651d91a7bc9831c0a053800b658293 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 29 Oct 2021 16:42:56 +0200 Subject: TST,ENH: Print the relevant expression whenever a `test_fail` or `test_reveal` test fails --- numpy/typing/tests/test_typing.py | 43 +++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 15 deletions(-) (limited to 'numpy') diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index 4dd6530ff..71e459df6 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -11,6 +11,7 @@ from typing import IO, TYPE_CHECKING import pytest import numpy as np +import numpy.typing as npt from numpy.typing.mypy_plugin import ( _PRECISION_DICT, _EXTENDED_PRECISION_LIST, @@ -150,9 +151,9 @@ def test_fail(path: str) -> None: target_line = lines[lineno - 1] if "# E:" in target_line: - marker = target_line.split("# E:")[-1].strip() - expected_error = errors.get(lineno) - _test_fail(path, marker, expected_error, lineno) + expression, _, marker = target_line.partition(" # E: ") + expected_error = errors[lineno] + _test_fail(path, expression, marker.strip(), expected_error.strip(), lineno) else: pytest.fail( f"Unexpected mypy output at line {lineno}\n\n{errors[lineno]}" @@ -161,11 +162,13 @@ def test_fail(path: str) -> None: _FAIL_MSG1 = """Extra error at line {} +Expression: {} Extra error: {!r} """ _FAIL_MSG2 = """Error mismatch at line {} +Expression: {} Expected error: {!r} Observed error: {!r} """ @@ -173,14 +176,15 @@ Observed error: {!r} def _test_fail( path: str, + expression: str, error: str, expected_error: None | str, lineno: int, ) -> None: if expected_error is None: - raise AssertionError(_FAIL_MSG1.format(lineno, error)) + raise AssertionError(_FAIL_MSG1.format(lineno, expression, error)) elif error not in expected_error: - raise AssertionError(_FAIL_MSG2.format(lineno, expected_error, error)) + raise AssertionError(_FAIL_MSG2.format(lineno, expression, expected_error, error)) def _construct_format_dict() -> dict[str, str]: @@ -259,7 +263,7 @@ def _construct_format_dict() -> dict[str, str]: FORMAT_DICT: dict[str, str] = _construct_format_dict() -def _parse_reveals(file: IO[str]) -> list[str]: +def _parse_reveals(file: IO[str]) -> tuple[npt.NDArray[np.str_], list[str]]: """Extract and parse all ``" # E: "`` comments from the passed file-like object. @@ -269,8 +273,10 @@ def _parse_reveals(file: IO[str]) -> list[str]: """ string = file.read().replace("*", "") - # Grab all `# E:`-based comments - comments_array = np.char.partition(string.split("\n"), sep=" # E: ")[:, 2] + # Grab all `# E:`-based comments and matching expressions + expression_array, _, comments_array = np.char.partition( + string.split("\n"), sep=" # E: " + ).T comments = "/n".join(comments_array) # Only search for the `{*}` pattern within comments, otherwise @@ -282,7 +288,7 @@ def _parse_reveals(file: IO[str]) -> list[str]: } fmt_str = comments.format(**kwargs) - return fmt_str.split("/n") + return expression_array, fmt_str.split("/n") @pytest.mark.slow @@ -295,7 +301,7 @@ def test_reveal(path: str) -> None: __tracebackhide__ = True with open(path) as fin: - lines = _parse_reveals(fin) + expression_array, reveal_list = _parse_reveals(fin) output_mypy = OUTPUT_MYPY assert path in output_mypy @@ -310,12 +316,14 @@ def test_reveal(path: str) -> None: lineno = int(match.group('lineno')) - 1 assert "Revealed type is" in error_line - marker = lines[lineno] - _test_reveal(path, marker, error_line, 1 + lineno) + marker = reveal_list[lineno] + expression = expression_array[lineno] + _test_reveal(path, expression, marker, error_line, 1 + lineno) _REVEAL_MSG = """Reveal mismatch at line {} +Expression: {} Expected reveal: {!r} Observed reveal: {!r} """ @@ -323,6 +331,7 @@ Observed reveal: {!r} def _test_reveal( path: str, + expression: str, reveal: str, expected_reveal: str, lineno: int, @@ -330,7 +339,7 @@ def _test_reveal( """Error-reporting helper function for `test_reveal`.""" if reveal not in expected_reveal: raise AssertionError( - _REVEAL_MSG.format(lineno, expected_reveal, reveal) + _REVEAL_MSG.format(lineno, expression, expected_reveal, reveal) ) @@ -375,11 +384,15 @@ def test_extended_precision() -> None: output_mypy = OUTPUT_MYPY assert path in output_mypy + with open(path, "r") as f: + expression_list = f.readlines() + for _msg in output_mypy[path]: *_, _lineno, msg_typ, msg = _msg.split(":") msg = _strip_filename(msg) lineno = int(_lineno) + expression = expression_list[lineno - 1].rstrip("\n") msg_typ = msg_typ.strip() assert msg_typ in {"error", "note"} @@ -388,8 +401,8 @@ def test_extended_precision() -> None: raise ValueError(f"Unexpected reveal line format: {lineno}") else: marker = FORMAT_DICT[LINENO_MAPPING[lineno]] - _test_reveal(path, marker, msg, lineno) + _test_reveal(path, expression, marker, msg, lineno) else: if msg_typ == "error": marker = "Module has no attribute" - _test_fail(path, marker, msg, lineno) + _test_fail(path, expression, marker, msg, lineno) -- cgit v1.2.1 From aac516a50b64bd1b25d9d5216c89de10b4baf61f Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 29 Oct 2021 17:35:13 +0200 Subject: TST: Account for the platform-specific mapping of numpy- to ctypes-types --- numpy/typing/tests/data/reveal/ctypeslib.pyi | 78 +++++++++++++++------------- numpy/typing/tests/test_typing.py | 33 ++++++++++++ 2 files changed, 74 insertions(+), 37 deletions(-) (limited to 'numpy') diff --git a/numpy/typing/tests/data/reveal/ctypeslib.pyi b/numpy/typing/tests/data/reveal/ctypeslib.pyi index ae0b6b345..461a447d9 100644 --- a/numpy/typing/tests/data/reveal/ctypeslib.pyi +++ b/numpy/typing/tests/data/reveal/ctypeslib.pyi @@ -17,6 +17,7 @@ AR_int: npt.NDArray[np.int_] AR_longlong: npt.NDArray[np.longlong] AR_single: npt.NDArray[np.single] AR_double: npt.NDArray[np.double] +AR_longdouble: npt.NDArray[np.longdouble] AR_void: npt.NDArray[np.void] pointer: ctypes.pointer[Any] @@ -31,51 +32,54 @@ reveal_type(np.ctypeslib.ndpointer(np.int64, shape=(10, 3))) # E: Type[numpy.ct reveal_type(np.ctypeslib.ndpointer(int, shape=(1,))) # E: Type[numpy.ctypeslib._concrete_ndptr[numpy.dtype[Any]]] reveal_type(np.ctypeslib.as_ctypes_type(np.bool_)) # E: Type[ctypes.c_bool] -reveal_type(np.ctypeslib.as_ctypes_type(np.ubyte)) # E: Type[ctypes.c_ubyte] -reveal_type(np.ctypeslib.as_ctypes_type(np.ushort)) # E: Type[ctypes.c_ushort] -reveal_type(np.ctypeslib.as_ctypes_type(np.uintc)) # E: Type[ctypes.c_uint] -reveal_type(np.ctypeslib.as_ctypes_type(np.uint)) # E: Type[ctypes.c_ulong] -reveal_type(np.ctypeslib.as_ctypes_type(np.ulonglong)) # E: Type[ctypes.c_ulong] -reveal_type(np.ctypeslib.as_ctypes_type(np.byte)) # E: Type[ctypes.c_byte] -reveal_type(np.ctypeslib.as_ctypes_type(np.short)) # E: Type[ctypes.c_short] -reveal_type(np.ctypeslib.as_ctypes_type(np.intc)) # E: Type[ctypes.c_int] -reveal_type(np.ctypeslib.as_ctypes_type(np.int_)) # E: Type[ctypes.c_long] -reveal_type(np.ctypeslib.as_ctypes_type(np.longlong)) # E: Type[ctypes.c_long] -reveal_type(np.ctypeslib.as_ctypes_type(np.single)) # E: Type[ctypes.c_float] -reveal_type(np.ctypeslib.as_ctypes_type(np.double)) # E: Type[ctypes.c_double] -reveal_type(np.ctypeslib.as_ctypes_type(ctypes.c_double)) # E: Type[ctypes.c_double] +reveal_type(np.ctypeslib.as_ctypes_type(np.ubyte)) # E: Type[{c_ubyte}] +reveal_type(np.ctypeslib.as_ctypes_type(np.ushort)) # E: Type[{c_ushort}] +reveal_type(np.ctypeslib.as_ctypes_type(np.uintc)) # E: Type[{c_uint}] +reveal_type(np.ctypeslib.as_ctypes_type(np.uint)) # E: Type[{c_ulong}] +reveal_type(np.ctypeslib.as_ctypes_type(np.ulonglong)) # E: Type[{c_ulonglong}] +reveal_type(np.ctypeslib.as_ctypes_type(np.byte)) # E: Type[{c_byte}] +reveal_type(np.ctypeslib.as_ctypes_type(np.short)) # E: Type[{c_short}] +reveal_type(np.ctypeslib.as_ctypes_type(np.intc)) # E: Type[{c_int}] +reveal_type(np.ctypeslib.as_ctypes_type(np.int_)) # E: Type[{c_long}] +reveal_type(np.ctypeslib.as_ctypes_type(np.longlong)) # E: Type[{c_longlong}] +reveal_type(np.ctypeslib.as_ctypes_type(np.single)) # E: Type[{c_float}] +reveal_type(np.ctypeslib.as_ctypes_type(np.double)) # E: Type[{c_double}] +reveal_type(np.ctypeslib.as_ctypes_type(np.longdouble)) # E: Type[{c_longdouble}] +reveal_type(np.ctypeslib.as_ctypes_type(ctypes.c_double)) # E: Type[{c_double}] reveal_type(np.ctypeslib.as_ctypes_type("q")) # E: Type[ctypes.c_longlong] reveal_type(np.ctypeslib.as_ctypes_type([("i8", np.int64), ("f8", np.float64)])) # E: Type[Any] reveal_type(np.ctypeslib.as_ctypes_type("i8")) # E: Type[Any] reveal_type(np.ctypeslib.as_ctypes_type("f8")) # E: Type[Any] reveal_type(np.ctypeslib.as_ctypes(AR_bool.take(0))) # E: ctypes.c_bool -reveal_type(np.ctypeslib.as_ctypes(AR_ubyte.take(0))) # E: ctypes.c_ubyte -reveal_type(np.ctypeslib.as_ctypes(AR_ushort.take(0))) # E: ctypes.c_ushort -reveal_type(np.ctypeslib.as_ctypes(AR_uintc.take(0))) # E: ctypes.c_uint -reveal_type(np.ctypeslib.as_ctypes(AR_uint.take(0))) # E: ctypes.c_ulong -reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong.take(0))) # E: ctypes.c_ulong -reveal_type(np.ctypeslib.as_ctypes(AR_byte.take(0))) # E: ctypes.c_byte -reveal_type(np.ctypeslib.as_ctypes(AR_short.take(0))) # E: ctypes.c_short -reveal_type(np.ctypeslib.as_ctypes(AR_intc.take(0))) # E: ctypes.c_int -reveal_type(np.ctypeslib.as_ctypes(AR_int.take(0))) # E: ctypes.c_long -reveal_type(np.ctypeslib.as_ctypes(AR_longlong.take(0))) # E: ctypes.c_long -reveal_type(np.ctypeslib.as_ctypes(AR_single.take(0))) # E: ctypes.c_float -reveal_type(np.ctypeslib.as_ctypes(AR_double.take(0))) # E: ctypes.c_double +reveal_type(np.ctypeslib.as_ctypes(AR_ubyte.take(0))) # E: {c_ubyte} +reveal_type(np.ctypeslib.as_ctypes(AR_ushort.take(0))) # E: {c_ushort} +reveal_type(np.ctypeslib.as_ctypes(AR_uintc.take(0))) # E: {c_uint} +reveal_type(np.ctypeslib.as_ctypes(AR_uint.take(0))) # E: {c_ulong} +reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong.take(0))) # E: {c_ulonglong} +reveal_type(np.ctypeslib.as_ctypes(AR_byte.take(0))) # E: {c_byte} +reveal_type(np.ctypeslib.as_ctypes(AR_short.take(0))) # E: {c_short} +reveal_type(np.ctypeslib.as_ctypes(AR_intc.take(0))) # E: {c_int} +reveal_type(np.ctypeslib.as_ctypes(AR_int.take(0))) # E: {c_long} +reveal_type(np.ctypeslib.as_ctypes(AR_longlong.take(0))) # E: {c_longlong} +reveal_type(np.ctypeslib.as_ctypes(AR_single.take(0))) # E: {c_float} +reveal_type(np.ctypeslib.as_ctypes(AR_double.take(0))) # E: {c_double} +reveal_type(np.ctypeslib.as_ctypes(AR_longdouble.take(0))) # E: {c_longdouble} reveal_type(np.ctypeslib.as_ctypes(AR_void.take(0))) # E: Any reveal_type(np.ctypeslib.as_ctypes(AR_bool)) # E: ctypes.Array[ctypes.c_bool] -reveal_type(np.ctypeslib.as_ctypes(AR_ubyte)) # E: ctypes.Array[ctypes.c_ubyte] -reveal_type(np.ctypeslib.as_ctypes(AR_ushort)) # E: ctypes.Array[ctypes.c_ushort] -reveal_type(np.ctypeslib.as_ctypes(AR_uintc)) # E: ctypes.Array[ctypes.c_uint] -reveal_type(np.ctypeslib.as_ctypes(AR_uint)) # E: ctypes.Array[ctypes.c_ulong] -reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong)) # E: ctypes.Array[ctypes.c_ulong] -reveal_type(np.ctypeslib.as_ctypes(AR_byte)) # E: ctypes.Array[ctypes.c_byte] -reveal_type(np.ctypeslib.as_ctypes(AR_short)) # E: ctypes.Array[ctypes.c_short] -reveal_type(np.ctypeslib.as_ctypes(AR_intc)) # E: ctypes.Array[ctypes.c_int] -reveal_type(np.ctypeslib.as_ctypes(AR_int)) # E: ctypes.Array[ctypes.c_long] -reveal_type(np.ctypeslib.as_ctypes(AR_longlong)) # E: ctypes.Array[ctypes.c_long] -reveal_type(np.ctypeslib.as_ctypes(AR_single)) # E: ctypes.Array[ctypes.c_float] -reveal_type(np.ctypeslib.as_ctypes(AR_double)) # E: ctypes.Array[ctypes.c_double] +reveal_type(np.ctypeslib.as_ctypes(AR_ubyte)) # E: ctypes.Array[{c_ubyte}] +reveal_type(np.ctypeslib.as_ctypes(AR_ushort)) # E: ctypes.Array[{c_ushort}] +reveal_type(np.ctypeslib.as_ctypes(AR_uintc)) # E: ctypes.Array[{c_uint}] +reveal_type(np.ctypeslib.as_ctypes(AR_uint)) # E: ctypes.Array[{c_ulong}] +reveal_type(np.ctypeslib.as_ctypes(AR_ulonglong)) # E: ctypes.Array[{c_ulonglong}] +reveal_type(np.ctypeslib.as_ctypes(AR_byte)) # E: ctypes.Array[{c_byte}] +reveal_type(np.ctypeslib.as_ctypes(AR_short)) # E: ctypes.Array[{c_short}] +reveal_type(np.ctypeslib.as_ctypes(AR_intc)) # E: ctypes.Array[{c_int}] +reveal_type(np.ctypeslib.as_ctypes(AR_int)) # E: ctypes.Array[{c_long}] +reveal_type(np.ctypeslib.as_ctypes(AR_longlong)) # E: ctypes.Array[{c_longlong}] +reveal_type(np.ctypeslib.as_ctypes(AR_single)) # E: ctypes.Array[{c_float}] +reveal_type(np.ctypeslib.as_ctypes(AR_double)) # E: ctypes.Array[{c_double}] +reveal_type(np.ctypeslib.as_ctypes(AR_longdouble)) # E: ctypes.Array[{c_longdouble}] reveal_type(np.ctypeslib.as_ctypes(AR_void)) # E: ctypes.Array[Any] reveal_type(np.ctypeslib.as_array(AR_ubyte)) # E: numpy.ndarray[Any, numpy.dtype[{ubyte}]] diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index 71e459df6..e0bcacd06 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -187,6 +187,38 @@ def _test_fail( raise AssertionError(_FAIL_MSG2.format(lineno, expression, expected_error, error)) +def _construct_ctypes_dict() -> dict[str, str]: + dct = { + "ubyte": "c_ubyte", + "ushort": "c_ushort", + "uintc": "c_uint", + "uint": "c_ulong", + "ulonglong": "c_ulonglong", + "byte": "c_byte", + "short": "c_short", + "intc": "c_int", + "int_": "c_long", + "longlong": "c_longlong", + "single": "c_float", + "double": "c_double", + "longdouble": "c_longdouble", + } + + # Match `ctypes` names to the first ctypes type with a given kind and + # precision, e.g. {"c_double": "c_double", "c_longdouble": "c_double"} + # if both types represent 64-bit floats. + # In this context "first" is defined by the order of `dct` + ret = {} + visited: dict[tuple[str, int], str] = {} + for np_name, ct_name in dct.items(): + np_scalar = getattr(np, np_name)() + + # Find the first `ctypes` type for a given `kind`/`itemsize` combo + key = (np_scalar.dtype.kind, np_scalar.dtype.itemsize) + ret[ct_name] = visited.setdefault(key, f"ctypes.{ct_name}") + return ret + + def _construct_format_dict() -> dict[str, str]: dct = {k.split(".")[-1]: v.replace("numpy", "numpy.typing") for k, v in _PRECISION_DICT.items()} @@ -261,6 +293,7 @@ def _construct_format_dict() -> dict[str, str]: #: A dictionary with all supported format keys (as keys) #: and matching values FORMAT_DICT: dict[str, str] = _construct_format_dict() +FORMAT_DICT.update(_construct_ctypes_dict()) def _parse_reveals(file: IO[str]) -> tuple[npt.NDArray[np.str_], list[str]]: -- cgit v1.2.1 From f5090701de7181d5cab7b7b0c7eec029c00356db Mon Sep 17 00:00:00 2001 From: Michael McCann Date: Fri, 29 Oct 2021 10:42:13 -0600 Subject: DOC: fix typo in example, throws ValueError if return statement is not inside the with: --- numpy/core/_add_newdocs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index dbbb43009..d12e917e3 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -328,7 +328,7 @@ add_newdoc('numpy.core', 'nditer', ... with it: ... for (a, b, c) in it: ... addop(a, b, out=c) - ... return it.operands[2] + ... return it.operands[2] Here is the same function, but following the C-style pattern: -- cgit v1.2.1 From 87c6199494abc01c863422a7965a1033f0a53138 Mon Sep 17 00:00:00 2001 From: Mike McCann <57153404+Michael-T-McCann@users.noreply.github.com> Date: Fri, 29 Oct 2021 10:29:47 -0700 Subject: Update numpy/core/_add_newdocs.py Co-authored-by: Warren Weckesser --- numpy/core/_add_newdocs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index d12e917e3..c8a24db0c 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -328,7 +328,7 @@ add_newdoc('numpy.core', 'nditer', ... with it: ... for (a, b, c) in it: ... addop(a, b, out=c) - ... return it.operands[2] + ... return it.operands[2] Here is the same function, but following the C-style pattern: -- cgit v1.2.1 From d1899f23b2dc824e83906bed8fa0e0f273e10d5b Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 29 Oct 2021 13:42:09 -0500 Subject: BUG: `VOID_nonzero` could sometimes mutate alignment flag This fixes that invocations of `VOID_nonzero` could flip the alignment flag on the original array even though the original array is not modified. --- numpy/core/src/multiarray/arraytypes.c.src | 4 ++-- numpy/core/tests/test_numeric.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index 9fe76845a..71808cc48 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -2759,10 +2759,10 @@ VOID_nonzero (char *ip, PyArrayObject *ap) dummy_fields.descr = new; if ((new->alignment > 1) && !__ALIGNED(ip + offset, new->alignment)) { - PyArray_CLEARFLAGS(ap, NPY_ARRAY_ALIGNED); + PyArray_CLEARFLAGS(dummy_arr, NPY_ARRAY_ALIGNED); } else { - PyArray_ENABLEFLAGS(ap, NPY_ARRAY_ALIGNED); + PyArray_ENABLEFLAGS(dummy_arr, NPY_ARRAY_ALIGNED); } if (new->f->nonzero(ip+offset, dummy_arr)) { nonz = NPY_TRUE; diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 96173a482..ad9437911 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1484,6 +1484,18 @@ class TestNonzero: a = np.array([[False], [TrueThenFalse()]]) assert_raises(RuntimeError, np.nonzero, a) + def test_nonzero_sideffects_structured_void(self): + # Checks that structured void does not mutate alignment flag of + # original array. + arr = np.zeros(5, dtype="i1,i8,i8") # `ones` may short-circuit + assert arr.flags.aligned # structs are considered "aligned" + assert not arr["f2"].flags.aligned + # make sure that nonzero/count_nonzero do not flip the flag: + np.nonzero(arr) + assert arr.flags.aligned + np.count_nonzero(arr) + assert arr.flags.aligned + def test_nonzero_exception_safe(self): # gh-13930 -- cgit v1.2.1 From 66df704846ae7911e9a2692944038eac389ce1b0 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Fri, 29 Oct 2021 21:34:33 +0200 Subject: STY: Linting fixes --- numpy/typing/tests/test_typing.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index e0bcacd06..2dcfd6082 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -152,8 +152,9 @@ def test_fail(path: str) -> None: target_line = lines[lineno - 1] if "# E:" in target_line: expression, _, marker = target_line.partition(" # E: ") - expected_error = errors[lineno] - _test_fail(path, expression, marker.strip(), expected_error.strip(), lineno) + expected_error = errors[lineno].strip() + marker = marker.strip() + _test_fail(path, expression, marker, expected_error, lineno) else: pytest.fail( f"Unexpected mypy output at line {lineno}\n\n{errors[lineno]}" @@ -184,7 +185,9 @@ def _test_fail( if expected_error is None: raise AssertionError(_FAIL_MSG1.format(lineno, expression, error)) elif error not in expected_error: - raise AssertionError(_FAIL_MSG2.format(lineno, expression, expected_error, error)) + raise AssertionError(_FAIL_MSG2.format( + lineno, expression, expected_error, error + )) def _construct_ctypes_dict() -> dict[str, str]: -- cgit v1.2.1 From 4b219979759945e231d03c60804930e3e65a4c1d Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 29 Oct 2021 16:36:47 -0500 Subject: BUG: Fix environment checking logic for `NUMPY_WARN_IF_NO_MEM_POLICY` The logic was reversed accidentally, since the default is to not warn. I would be happy to dig a bit deeper and move the `getenv` to import time (with an small internal helper to set it). Since the `getenv` right now will be called for quite a bit of pythran code currently. --- numpy/core/src/multiarray/arrayobject.c | 2 +- numpy/core/tests/test_mem_policy.py | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index 237fb9b72..c8aaced4e 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -503,7 +503,7 @@ array_dealloc(PyArrayObject *self) } if (fa->mem_handler == NULL) { char *env = getenv("NUMPY_WARN_IF_NO_MEM_POLICY"); - if ((env == NULL) || (strncmp(env, "1", 1) == 0)) { + if ((env != NULL) && (strncmp(env, "1", 1) == 0)) { char const * msg = "Trying to dealloc data, but a memory policy " "is not set. If you take ownership of the data, you must " "set a base owning the data (e.g. a PyCapsule)."; diff --git a/numpy/core/tests/test_mem_policy.py b/numpy/core/tests/test_mem_policy.py index 4ec9b3b11..90971908d 100644 --- a/numpy/core/tests/test_mem_policy.py +++ b/numpy/core/tests/test_mem_policy.py @@ -357,24 +357,36 @@ def test_new_policy(get_module): c = np.arange(10) assert np.core.multiarray.get_handler_name(c) == orig_policy_name -def test_switch_owner(get_module): +@pytest.mark.parametrize("policy", ["0", "1", None]) +def test_switch_owner(get_module, policy): a = get_module.get_array() assert np.core.multiarray.get_handler_name(a) is None get_module.set_own(a) oldval = os.environ.get('NUMPY_WARN_IF_NO_MEM_POLICY', None) - os.environ['NUMPY_WARN_IF_NO_MEM_POLICY'] = "1" + if policy is None: + if 'NUMPY_WARN_IF_NO_MEM_POLICY' in os.environ: + os.environ.pop('NUMPY_WARN_IF_NO_MEM_POLICY') + else: + os.environ['NUMPY_WARN_IF_NO_MEM_POLICY'] = policy try: # The policy should be NULL, so we have to assume we can call - # "free" - with assert_warns(RuntimeWarning) as w: + # "free". A warning is given if the policy == "1" + if policy == "1": + with assert_warns(RuntimeWarning) as w: + del a + gc.collect() + else: del a gc.collect() + finally: if oldval is None: - os.environ.pop('NUMPY_WARN_IF_NO_MEM_POLICY') + if 'NUMPY_WARN_IF_NO_MEM_POLICY' in os.environ: + os.environ.pop('NUMPY_WARN_IF_NO_MEM_POLICY') else: os.environ['NUMPY_WARN_IF_NO_MEM_POLICY'] = oldval +def test_owner_is_base(get_module): a = get_module.get_array_with_base() with pytest.warns(UserWarning, match='warn_on_free'): del a -- cgit v1.2.1 From d86cad8f714c16f00f483d84f08cc89ec2bf029c Mon Sep 17 00:00:00 2001 From: lalitmusmade Date: Sat, 30 Oct 2021 15:57:00 +0000 Subject: DOC: Fixes wording for fmod and remainder functions. --- numpy/core/code_generators/ufunc_docstrings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/code_generators/ufunc_docstrings.py b/numpy/core/code_generators/ufunc_docstrings.py index ab5f74df3..8d9316f2c 100644 --- a/numpy/core/code_generators/ufunc_docstrings.py +++ b/numpy/core/code_generators/ufunc_docstrings.py @@ -1420,7 +1420,7 @@ add_newdoc('numpy.core.umath', 'floor_divide', add_newdoc('numpy.core.umath', 'fmod', """ - Return the element-wise remainder of division. + Returns the element-wise remainder of division. This is the NumPy implementation of the C library function fmod, the remainder has the same sign as the dividend `x1`. It is equivalent to @@ -3332,7 +3332,7 @@ add_newdoc('numpy.core.umath', 'reciprocal', add_newdoc('numpy.core.umath', 'remainder', """ - Return element-wise remainder of division. + Returns the element-wise remainder of division. Computes the remainder complementary to the `floor_divide` function. It is equivalent to the Python modulus operator``x1 % x2`` and has the same sign -- cgit v1.2.1 From 7c512e05192376ae118f9c2a05faf0fe50673aa4 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 29 Oct 2021 13:42:09 -0500 Subject: BUG: `VOID_nonzero` could sometimes mutate alignment flag This fixes that invocations of `VOID_nonzero` could flip the alignment flag on the original array even though the original array is not modified. --- numpy/core/src/multiarray/arraytypes.c.src | 4 ++-- numpy/core/tests/test_numeric.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index 9fe76845a..71808cc48 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -2759,10 +2759,10 @@ VOID_nonzero (char *ip, PyArrayObject *ap) dummy_fields.descr = new; if ((new->alignment > 1) && !__ALIGNED(ip + offset, new->alignment)) { - PyArray_CLEARFLAGS(ap, NPY_ARRAY_ALIGNED); + PyArray_CLEARFLAGS(dummy_arr, NPY_ARRAY_ALIGNED); } else { - PyArray_ENABLEFLAGS(ap, NPY_ARRAY_ALIGNED); + PyArray_ENABLEFLAGS(dummy_arr, NPY_ARRAY_ALIGNED); } if (new->f->nonzero(ip+offset, dummy_arr)) { nonz = NPY_TRUE; diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 96173a482..ad9437911 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1484,6 +1484,18 @@ class TestNonzero: a = np.array([[False], [TrueThenFalse()]]) assert_raises(RuntimeError, np.nonzero, a) + def test_nonzero_sideffects_structured_void(self): + # Checks that structured void does not mutate alignment flag of + # original array. + arr = np.zeros(5, dtype="i1,i8,i8") # `ones` may short-circuit + assert arr.flags.aligned # structs are considered "aligned" + assert not arr["f2"].flags.aligned + # make sure that nonzero/count_nonzero do not flip the flag: + np.nonzero(arr) + assert arr.flags.aligned + np.count_nonzero(arr) + assert arr.flags.aligned + def test_nonzero_exception_safe(self): # gh-13930 -- cgit v1.2.1 From fce2a361dad67566a6252553492239ec7b0c916d Mon Sep 17 00:00:00 2001 From: Muhammad Motawe Date: Sat, 30 Oct 2021 16:44:37 +0000 Subject: DOC: Clarify behavior of np.lib.scimath.sqrt in the presence of negative 0 --- numpy/core/code_generators/ufunc_docstrings.py | 1 + numpy/lib/scimath.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) (limited to 'numpy') diff --git a/numpy/core/code_generators/ufunc_docstrings.py b/numpy/core/code_generators/ufunc_docstrings.py index ab5f74df3..191f4f1cc 100644 --- a/numpy/core/code_generators/ufunc_docstrings.py +++ b/numpy/core/code_generators/ufunc_docstrings.py @@ -3827,6 +3827,7 @@ add_newdoc('numpy.core.umath', 'sqrt', -------- lib.scimath.sqrt A version which returns complex numbers when given negative reals. + Note: 0.0 and -0.0 are handled differently for complex inputs. Notes ----- diff --git a/numpy/lib/scimath.py b/numpy/lib/scimath.py index 308f1328b..afaf4b4c9 100644 --- a/numpy/lib/scimath.py +++ b/numpy/lib/scimath.py @@ -234,6 +234,20 @@ def sqrt(x): >>> np.emath.sqrt([-1,4]) array([0.+1.j, 2.+0.j]) + Different results are expected because: + floating point 0.0 and -0.0 are distinct. + + For more control, explicitly use complex() as follows: + + >>> np.emath.sqrt(complex(-4.0, 0.0)) + 2j + >>> np.emath.sqrt(complex(-4.0, -0.0)) + -2j + + + + + """ x = _fix_real_lt_zero(x) return nx.sqrt(x) -- cgit v1.2.1 From 8cb5466d55dec6b63ed4e58ce14a369adb41b7ca Mon Sep 17 00:00:00 2001 From: Muhammad Motawe Date: Sat, 30 Oct 2021 19:17:52 +0200 Subject: MAINT: Address review comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Melissa Weber Mendonça --- numpy/lib/scimath.py | 5 ----- 1 file changed, 5 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/scimath.py b/numpy/lib/scimath.py index afaf4b4c9..b7ef0d710 100644 --- a/numpy/lib/scimath.py +++ b/numpy/lib/scimath.py @@ -243,11 +243,6 @@ def sqrt(x): 2j >>> np.emath.sqrt(complex(-4.0, -0.0)) -2j - - - - - """ x = _fix_real_lt_zero(x) return nx.sqrt(x) -- cgit v1.2.1 From 8f53afdeca22ad1ca7636f9023a36daf525eadd5 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Sat, 30 Oct 2021 12:53:46 -0500 Subject: Update numpy/core/tests/test_mem_policy.py Co-authored-by: Matti Picus --- numpy/core/tests/test_mem_policy.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'numpy') diff --git a/numpy/core/tests/test_mem_policy.py b/numpy/core/tests/test_mem_policy.py index 90971908d..7fec8897f 100644 --- a/numpy/core/tests/test_mem_policy.py +++ b/numpy/core/tests/test_mem_policy.py @@ -357,6 +357,9 @@ def test_new_policy(get_module): c = np.arange(10) assert np.core.multiarray.get_handler_name(c) == orig_policy_name +@pytest.mark.xfail(sys.implementation.name == "pypy", + reason=("bad interaction between getenv and " + "os.environ inside pytest")) @pytest.mark.parametrize("policy", ["0", "1", None]) def test_switch_owner(get_module, policy): a = get_module.get_array() -- cgit v1.2.1 From 927feff8788f27f29e76e803f187a4c041098ced Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Sun, 31 Oct 2021 21:57:11 -0700 Subject: DOC: Fix math block in hermmulx, lagmulx (#20261) The math expression needs to be indented, Otherwise it is seen as being outside the directive and not rendered. --- numpy/polynomial/hermite.py | 2 +- numpy/polynomial/hermite_e.py | 2 +- numpy/polynomial/laguerre.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/polynomial/hermite.py b/numpy/polynomial/hermite.py index 9800063f0..9b0735a9a 100644 --- a/numpy/polynomial/hermite.py +++ b/numpy/polynomial/hermite.py @@ -419,7 +419,7 @@ def hermmulx(c): .. math:: - xP_i(x) = (P_{i + 1}(x)/2 + i*P_{i - 1}(x)) + xP_i(x) = (P_{i + 1}(x)/2 + i*P_{i - 1}(x)) Examples -------- diff --git a/numpy/polynomial/hermite_e.py b/numpy/polynomial/hermite_e.py index abd27361e..182c562c2 100644 --- a/numpy/polynomial/hermite_e.py +++ b/numpy/polynomial/hermite_e.py @@ -414,7 +414,7 @@ def hermemulx(c): .. math:: - xP_i(x) = (P_{i + 1}(x) + iP_{i - 1}(x))) + xP_i(x) = (P_{i + 1}(x) + iP_{i - 1}(x))) Examples -------- diff --git a/numpy/polynomial/laguerre.py b/numpy/polynomial/laguerre.py index f564be482..d9ca373dd 100644 --- a/numpy/polynomial/laguerre.py +++ b/numpy/polynomial/laguerre.py @@ -414,7 +414,7 @@ def lagmulx(c): .. math:: - xP_i(x) = (-(i + 1)*P_{i + 1}(x) + (2i + 1)P_{i}(x) - iP_{i - 1}(x)) + xP_i(x) = (-(i + 1)*P_{i + 1}(x) + (2i + 1)P_{i}(x) - iP_{i - 1}(x)) Examples -------- -- cgit v1.2.1 From 78dc4c6ce7ea6429317e6ab36c936d60da6c1d2c Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 1 Nov 2021 11:32:11 -0500 Subject: BUG: Fix headers for unviersal2 builds --- numpy/core/include/numpy/numpyconfig.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'numpy') diff --git a/numpy/core/include/numpy/numpyconfig.h b/numpy/core/include/numpy/numpyconfig.h index b2ce66244..1c3686769 100644 --- a/numpy/core/include/numpy/numpyconfig.h +++ b/numpy/core/include/numpy/numpyconfig.h @@ -19,6 +19,19 @@ #define NPY_SIZEOF_LONG 4 #define NPY_SIZEOF_PY_INTPTR_T 4 #endif + + #undef NPY_SIZEOF_LONGDOUBLE + #undef NPY_SIZEOF_COMPLEX_LONGDOUBLE + + #ifdef __x86_64 + #define NPY_SIZEOF_LONGDOUBLE 16 + #define NPY_SIZEOF_COMPLEX_LONGDOUBLE 32 + #elif defined(__arm64__) + #define NPY_SIZEOF_LONGDOUBLE 8 + #define NPY_SIZEOF_COMPLEX_LONGDOUBLE 16 + #else + #error "unknown architecture" + #endif #endif /** -- cgit v1.2.1 From 4149a37f99f259d61ebf8dbdc18366a560139d21 Mon Sep 17 00:00:00 2001 From: HowJMay Date: Tue, 24 Aug 2021 11:10:58 +0800 Subject: ENH: Add conversion for 512bit intrin to 256bit intrin 512-bits intrinsics sometimes not exist in some operations. We can construct the 512-bits intrinsics with two 256-bits intrinsics. Therfore, NPYV_IMPL_AVX512_FROM_AVX2_PS_1ARG and NPYV_IMPL_AVX512_FROM_AVX2_PD_1ARG are added. And the error in npyv512_combine_ps256 has been fixed too. --- numpy/core/src/common/simd/avx512/utils.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/common/simd/avx512/utils.h b/numpy/core/src/common/simd/avx512/utils.h index 8066283c6..c3079283f 100644 --- a/numpy/core/src/common/simd/avx512/utils.h +++ b/numpy/core/src/common/simd/avx512/utils.h @@ -26,7 +26,7 @@ #define npyv512_combine_ps256(A, B) _mm512_insertf32x8(_mm512_castps256_ps512(A), B, 1) #else #define npyv512_combine_ps256(A, B) \ - _mm512_castsi512_ps(npyv512_combine_si256(_mm512_castps_si512(A), _mm512_castps_si512(B))) + _mm512_castsi512_ps(npyv512_combine_si256(_mm256_castps_si256(A), _mm256_castps_si256(B))) #endif #define NPYV_IMPL_AVX512_FROM_AVX2_1ARG(FN_NAME, INTRIN) \ @@ -39,6 +39,26 @@ return npyv512_combine_si256(l_a, h_a); \ } +#define NPYV_IMPL_AVX512_FROM_AVX2_PS_1ARG(FN_NAME, INTRIN) \ + NPY_FINLINE __m512 FN_NAME(__m512 a) \ + { \ + __m256 l_a = npyv512_lower_ps256(a); \ + __m256 h_a = npyv512_higher_ps256(a); \ + l_a = INTRIN(l_a); \ + h_a = INTRIN(h_a); \ + return npyv512_combine_ps256(l_a, h_a); \ + } + +#define NPYV_IMPL_AVX512_FROM_AVX2_PD_1ARG(FN_NAME, INTRIN) \ + NPY_FINLINE __m512d FN_NAME(__m512d a) \ + { \ + __m256d l_a = npyv512_lower_pd256(a); \ + __m256d h_a = npyv512_higher_pd256(a); \ + l_a = INTRIN(l_a); \ + h_a = INTRIN(h_a); \ + return npyv512_combine_pd256(l_a, h_a); \ + } + #define NPYV_IMPL_AVX512_FROM_AVX2_2ARG(FN_NAME, INTRIN) \ NPY_FINLINE __m512i FN_NAME(__m512i a, __m512i b) \ { \ -- cgit v1.2.1 From 6ccad06d45c682f5fd199cf61a175575f431cbac Mon Sep 17 00:00:00 2001 From: HowJMay Date: Tue, 24 Aug 2021 11:11:41 +0800 Subject: ENH: Implement SIMD for ceil --- numpy/core/src/_simd/_simd.dispatch.c.src | 4 ++-- numpy/core/src/common/simd/avx2/math.h | 4 ++++ numpy/core/src/common/simd/avx512/math.h | 4 ++++ numpy/core/src/common/simd/neon/math.h | 16 ++++++++++++++++ numpy/core/src/common/simd/sse/math.h | 31 +++++++++++++++++++++++++++++++ numpy/core/src/common/simd/vsx/math.h | 4 ++++ numpy/core/tests/test_simd.py | 27 +++++++++++++++++++++++++++ 7 files changed, 88 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/_simd/_simd.dispatch.c.src b/numpy/core/src/_simd/_simd.dispatch.c.src index 54770959c..5c494ae7a 100644 --- a/numpy/core/src/_simd/_simd.dispatch.c.src +++ b/numpy/core/src/_simd/_simd.dispatch.c.src @@ -381,7 +381,7 @@ SIMD_IMPL_INTRIN_1(sumup_@sfx@, @esfx@, v@sfx@) ***************************/ #if @fp_only@ /**begin repeat1 - * #intrin = sqrt, recip, abs, square# + * #intrin = sqrt, recip, abs, square, ceil# */ SIMD_IMPL_INTRIN_1(@intrin@_@sfx@, v@sfx@, v@sfx@) /**end repeat1**/ @@ -615,7 +615,7 @@ SIMD_INTRIN_DEF(sumup_@sfx@) ***************************/ #if @fp_only@ /**begin repeat1 - * #intrin = sqrt, recip, abs, square# + * #intrin = sqrt, recip, abs, square, ceil# */ SIMD_INTRIN_DEF(@intrin@_@sfx@) /**end repeat1**/ diff --git a/numpy/core/src/common/simd/avx2/math.h b/numpy/core/src/common/simd/avx2/math.h index 9460183df..b1f3915a6 100644 --- a/numpy/core/src/common/simd/avx2/math.h +++ b/numpy/core/src/common/simd/avx2/math.h @@ -105,4 +105,8 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b) return _mm256_blendv_epi8(a, b, _mm256_cmpgt_epi64(a, b)); } +// ceil +#define npyv_ceil_f32 _mm256_ceil_ps +#define npyv_ceil_f64 _mm256_ceil_pd + #endif // _NPY_SIMD_AVX2_MATH_H diff --git a/numpy/core/src/common/simd/avx512/math.h b/numpy/core/src/common/simd/avx512/math.h index 0949b2b06..c4f8d3410 100644 --- a/numpy/core/src/common/simd/avx512/math.h +++ b/numpy/core/src/common/simd/avx512/math.h @@ -112,4 +112,8 @@ NPY_FINLINE npyv_f64 npyv_minp_f64(npyv_f64 a, npyv_f64 b) #define npyv_min_u64 _mm512_min_epu64 #define npyv_min_s64 _mm512_min_epi64 +// ceil +#define npyv_ceil_f32(A) _mm512_roundscale_ps(A, _MM_FROUND_TO_POS_INF) +#define npyv_ceil_f64(A) _mm512_roundscale_pd(A, _MM_FROUND_TO_POS_INF) + #endif // _NPY_SIMD_AVX512_MATH_H diff --git a/numpy/core/src/common/simd/neon/math.h b/numpy/core/src/common/simd/neon/math.h index 19ea6f22f..8d370e624 100644 --- a/numpy/core/src/common/simd/neon/math.h +++ b/numpy/core/src/common/simd/neon/math.h @@ -153,4 +153,20 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b) return vbslq_s64(npyv_cmplt_s64(a, b), a, b); } +// ceil +#ifdef NPY_HAVE_ASIMD + #define npyv_ceil_f32 vrndpq_f32 +#else + NPY_FINLINE npyv_f32 npyv_ceil_f32(npyv_f32 a) + { + npyv_f32 conv_trunc = vcvtq_f32_s32(vcvtq_s32_f32(a)); + npyv_f32 conv_trunc_add_one = npyv_add_f32(conv_trunc, vdupq_n_f32(1.0f)); + npyv_u32 mask = vcltq_f32(conv_trunc, a); + return vbslq_f32(mask, conv_trunc, conv_trunc_add_one); + } +#endif +#if NPY_SIMD_F64 + #define npyv_ceil_f64 vrndpq_f64 +#endif // NPY_SIMD_F64 + #endif // _NPY_SIMD_NEON_MATH_H diff --git a/numpy/core/src/common/simd/sse/math.h b/numpy/core/src/common/simd/sse/math.h index 97d35afc5..02eb06a29 100644 --- a/numpy/core/src/common/simd/sse/math.h +++ b/numpy/core/src/common/simd/sse/math.h @@ -143,4 +143,35 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b) return npyv_select_s64(npyv_cmplt_s64(a, b), a, b); } +// ceil +#ifdef NPY_HAVE_SSE41 + #define npyv_ceil_f32 _mm_ceil_ps + #define npyv_ceil_f64 _mm_ceil_pd +#else + NPY_FINLINE npyv_f32 npyv_ceil_f32(npyv_f32 a) + { + const npyv_f32 szero = _mm_set1_ps(-0.0f); + const npyv_f32 one = _mm_set1_ps(1.0f); + npyv_s32 roundi = _mm_cvttps_epi32(a); + npyv_f32 round = _mm_cvtepi32_ps(roundi); + npyv_f32 ceil = _mm_add_ps(round, _mm_and_ps(_mm_cmplt_ps(round, a), one)); + // respect signed zero, e.g. -0.5 -> -0.0 + npyv_f32 rzero = _mm_or_ps(ceil, _mm_and_ps(a, szero)); + // if overflow return a + return npyv_select_f32(_mm_cmpeq_epi32(roundi, _mm_castps_si128(szero)), a, rzero); + } + NPY_FINLINE npyv_f64 npyv_ceil_f64(npyv_f64 a) + { + const npyv_f64 szero = _mm_set1_pd(-0.0); + const npyv_f64 one = _mm_set1_pd(1.0); + const npyv_f64 two_power_52 = _mm_set1_pd(0x10000000000000); + npyv_f64 sign_two52 = _mm_or_pd(two_power_52, _mm_and_pd(a, szero)); + // round by add magic number 2^52 + npyv_f64 round = _mm_sub_pd(_mm_add_pd(a, sign_two52), sign_two52); + npyv_f64 ceil = _mm_add_pd(round, _mm_and_pd(_mm_cmplt_pd(round, a), one)); + // respect signed zero, e.g. -0.5 -> -0.0 + return _mm_or_pd(ceil, _mm_and_pd(a, szero)); + } +#endif + #endif // _NPY_SIMD_SSE_MATH_H diff --git a/numpy/core/src/common/simd/vsx/math.h b/numpy/core/src/common/simd/vsx/math.h index b2e393c7c..f387dac4d 100644 --- a/numpy/core/src/common/simd/vsx/math.h +++ b/numpy/core/src/common/simd/vsx/math.h @@ -69,4 +69,8 @@ NPY_FINLINE npyv_f64 npyv_square_f64(npyv_f64 a) #define npyv_min_u64 vec_min #define npyv_min_s64 vec_min +// ceil +#define npyv_ceil_f32 vec_ceil +#define npyv_ceil_f64 vec_ceil + #endif // _NPY_SIMD_VSX_MATH_H diff --git a/numpy/core/tests/test_simd.py b/numpy/core/tests/test_simd.py index 0270ad901..379fef8af 100644 --- a/numpy/core/tests/test_simd.py +++ b/numpy/core/tests/test_simd.py @@ -330,6 +330,33 @@ class _SIMD_FP(_Test_Utility): square = self.square(vdata) assert square == data_square + @pytest.mark.parametrize("intrin, func", [("self.ceil", math.ceil)]) + def test_rounding(self, intrin, func): + """ + Test intrinsics: + npyv_ceil_##SFX + """ + intrin = eval(intrin) + pinf, ninf, nan = self._pinfinity(), self._ninfinity(), self._nan() + # special cases + round_cases = ((nan, nan), (pinf, pinf), (ninf, ninf)) + for case, desired in round_cases: + data_round = [desired]*self.nlanes + _round = intrin(self.setall(case)) + assert _round == pytest.approx(data_round, nan_ok=True) + for x in range(0, 2**20, 256**2): + for w in (-1.05, -1.10, -1.15, 1.05, 1.10, 1.15): + data = [x*w+a for a in range(self.nlanes)] + vdata = self.load(data) + data_round = [func(x) for x in data] + _round = intrin(vdata) + assert _round == data_round + # signed zero + for w in (-0.25, -0.30, -0.45): + _round = self._to_unsigned(intrin(self.setall(w))) + data_round = self._to_unsigned(self.setall(-0.0)) + assert _round == data_round + def test_max(self): """ Test intrinsics: -- cgit v1.2.1 From 521b3afc15b7a8c181ebb7c043f3744555c1e9c9 Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Mon, 1 Nov 2021 18:49:15 +0200 Subject: SIMD: Fix impl of intrinsic `npyv_ceil_f32` on armv7/neon --- numpy/core/src/common/simd/neon/math.h | 41 +++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/common/simd/neon/math.h b/numpy/core/src/common/simd/neon/math.h index 8d370e624..38c3899e4 100644 --- a/numpy/core/src/common/simd/neon/math.h +++ b/numpy/core/src/common/simd/neon/math.h @@ -88,16 +88,16 @@ NPY_FINLINE npyv_f32 npyv_recip_f32(npyv_f32 a) #define npyv_max_f64 vmaxq_f64 // Maximum, supports IEEE floating-point arithmetic (IEC 60559), // - If one of the two vectors contains NaN, the equivalent element of the other vector is set -// - Only if both corresponded elements are NaN, NaN is set. +// - Only if both corresponded elements are NaN, NaN is set. #ifdef NPY_HAVE_ASIMD #define npyv_maxp_f32 vmaxnmq_f32 #else NPY_FINLINE npyv_f32 npyv_maxp_f32(npyv_f32 a, npyv_f32 b) - { + { npyv_u32 nn_a = vceqq_f32(a, a); npyv_u32 nn_b = vceqq_f32(b, b); return vmaxq_f32(vbslq_f32(nn_a, a, b), vbslq_f32(nn_b, b, a)); - } + } #endif #if NPY_SIMD_F64 #define npyv_maxp_f64 vmaxnmq_f64 @@ -123,16 +123,16 @@ NPY_FINLINE npyv_s64 npyv_max_s64(npyv_s64 a, npyv_s64 b) #define npyv_min_f64 vminq_f64 // Minimum, supports IEEE floating-point arithmetic (IEC 60559), // - If one of the two vectors contains NaN, the equivalent element of the other vector is set -// - Only if both corresponded elements are NaN, NaN is set. +// - Only if both corresponded elements are NaN, NaN is set. #ifdef NPY_HAVE_ASIMD #define npyv_minp_f32 vminnmq_f32 #else NPY_FINLINE npyv_f32 npyv_minp_f32(npyv_f32 a, npyv_f32 b) - { + { npyv_u32 nn_a = vceqq_f32(a, a); npyv_u32 nn_b = vceqq_f32(b, b); return vminq_f32(vbslq_f32(nn_a, a, b), vbslq_f32(nn_b, b, a)); - } + } #endif #if NPY_SIMD_F64 #define npyv_minp_f64 vminnmq_f64 @@ -159,10 +159,31 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b) #else NPY_FINLINE npyv_f32 npyv_ceil_f32(npyv_f32 a) { - npyv_f32 conv_trunc = vcvtq_f32_s32(vcvtq_s32_f32(a)); - npyv_f32 conv_trunc_add_one = npyv_add_f32(conv_trunc, vdupq_n_f32(1.0f)); - npyv_u32 mask = vcltq_f32(conv_trunc, a); - return vbslq_f32(mask, conv_trunc, conv_trunc_add_one); + const npyv_s32 szero = vreinterpretq_s32_f32(vdupq_n_f32(-0.0f)); + const npyv_u32 one = vreinterpretq_u32_f32(vdupq_n_f32(1.0f)); + const npyv_s32 max_int = vdupq_n_s32(0x7fffffff); + /** + * On armv7, vcvtq.f32 handles special cases as follows: + * NaN return 0 + * +inf or +outrange return 0x80000000(-0.0f) + * -inf or -outrange return 0x7fffffff(nan) + */ + npyv_s32 roundi = vcvtq_s32_f32(a); + npyv_f32 round = vcvtq_f32_s32(roundi); + npyv_f32 ceil = vaddq_f32(round, vreinterpretq_f32_u32( + vandq_u32(vcltq_f32(round, a), one)) + ); + // respect signed zero, e.g. -0.5 -> -0.0 + npyv_f32 rzero = vreinterpretq_f32_s32(vorrq_s32( + vreinterpretq_s32_f32(ceil), + vandq_s32(vreinterpretq_s32_f32(a), szero) + )); + // if nan or overflow return a + npyv_u32 nnan = npyv_notnan_f32(a); + npyv_u32 overflow = vorrq_u32( + vceqq_s32(roundi, szero), vceqq_s32(roundi, max_int) + ); + return vbslq_f32(vbicq_u32(nnan, overflow), rzero, a); } #endif #if NPY_SIMD_F64 -- cgit v1.2.1 From 35e2d8fc23adf982994d00d959ab3a7ec27c84f1 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 1 Nov 2021 22:01:05 +0100 Subject: BUG: Fix shadowed reference of `str` in the type stubs --- numpy/__init__.pyi | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index bc95c5d06..de01f92ef 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -682,7 +682,7 @@ _DTypeScalar_co = TypeVar("_DTypeScalar_co", covariant=True, bound=generic) _ByteOrder = L["S", "<", ">", "=", "|", "L", "B", "N", "I"] class dtype(Generic[_DTypeScalar_co]): - names: None | Tuple[str, ...] + names: None | Tuple[builtins.str, ...] # Overload for subclass of generic @overload def __new__( @@ -709,7 +709,7 @@ class dtype(Generic[_DTypeScalar_co]): @overload def __new__(cls, dtype: Type[complex], align: bool = ..., copy: bool = ...) -> dtype[complex_]: ... @overload - def __new__(cls, dtype: Type[str], align: bool = ..., copy: bool = ...) -> dtype[str_]: ... + def __new__(cls, dtype: Type[builtins.str], align: bool = ..., copy: bool = ...) -> dtype[str_]: ... @overload def __new__(cls, dtype: Type[bytes], align: bool = ..., copy: bool = ...) -> dtype[bytes_]: ... @@ -823,7 +823,7 @@ class dtype(Generic[_DTypeScalar_co]): @overload def __new__( cls, - dtype: str, + dtype: builtins.str, align: bool = ..., copy: bool = ..., ) -> dtype[Any]: ... @@ -848,9 +848,9 @@ class dtype(Generic[_DTypeScalar_co]): def __class_getitem__(self, item: Any) -> GenericAlias: ... @overload - def __getitem__(self: dtype[void], key: List[str]) -> dtype[void]: ... + def __getitem__(self: dtype[void], key: List[builtins.str]) -> dtype[void]: ... @overload - def __getitem__(self: dtype[void], key: str | SupportsIndex) -> dtype[Any]: ... + def __getitem__(self: dtype[void], key: builtins.str | SupportsIndex) -> dtype[Any]: ... # NOTE: In the future 1-based multiplications will also yield `flexible` dtypes @overload @@ -884,15 +884,15 @@ class dtype(Generic[_DTypeScalar_co]): @property def base(self) -> dtype[Any]: ... @property - def byteorder(self) -> str: ... + def byteorder(self) -> builtins.str: ... @property - def char(self) -> str: ... + def char(self) -> builtins.str: ... @property - def descr(self) -> List[Tuple[str, str] | Tuple[str, str, _Shape]]: ... + def descr(self) -> List[Tuple[builtins.str, builtins.str] | Tuple[builtins.str, builtins.str, _Shape]]: ... @property def fields( self, - ) -> None | MappingProxyType[str, Tuple[dtype[Any], int] | Tuple[dtype[Any], int, Any]]: ... + ) -> None | MappingProxyType[builtins.str, Tuple[dtype[Any], int] | Tuple[dtype[Any], int, Any]]: ... @property def flags(self) -> int: ... @property @@ -906,11 +906,11 @@ class dtype(Generic[_DTypeScalar_co]): @property def itemsize(self) -> int: ... @property - def kind(self) -> str: ... + def kind(self) -> builtins.str: ... @property - def metadata(self) -> None | MappingProxyType[str, Any]: ... + def metadata(self) -> None | MappingProxyType[builtins.str, Any]: ... @property - def name(self) -> str: ... + def name(self) -> builtins.str: ... @property def num(self) -> int: ... @property @@ -920,8 +920,6 @@ class dtype(Generic[_DTypeScalar_co]): @property def subdtype(self) -> None | Tuple[dtype[Any], _Shape]: ... def newbyteorder(self: _DType, __new_order: _ByteOrder = ...) -> _DType: ... - # Leave str and type for end to avoid having to use `builtins.str` - # everywhere. See https://github.com/python/mypy/issues/3775 @property def str(self) -> builtins.str: ... @property -- cgit v1.2.1 From 987d25bebb3564eca4a86712ccdeba93016758ed Mon Sep 17 00:00:00 2001 From: vinith2 <85550536+vinith2@users.noreply.github.com> Date: Mon, 1 Nov 2021 22:34:56 +0100 Subject: DOC: Add multi-axis examples to numpy.roll docstring (#20267) Added exampled where we use tuple in for axis and shifts separately. This shows how the multiple shift examples works Co-authored-by: Ross Barnowski --- numpy/core/numeric.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index d8a0cf9a6..1654e8364 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1184,7 +1184,7 @@ def roll(a, shift, axis=None): >>> np.roll(x, -2) array([2, 3, 4, 5, 6, 7, 8, 9, 0, 1]) - >>> x2 = np.reshape(x, (2,5)) + >>> x2 = np.reshape(x, (2, 5)) >>> x2 array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) @@ -1206,6 +1206,12 @@ def roll(a, shift, axis=None): >>> np.roll(x2, -1, axis=1) array([[1, 2, 3, 4, 0], [6, 7, 8, 9, 5]]) + >>> np.roll(x2, (1, 1), axis=(1, 0)) + array([[9, 5, 6, 7, 8], + [4, 0, 1, 2, 3]]) + >>> np.roll(x2, (2, 1), axis=(1, 0)) + array([[8, 9, 5, 6, 7], + [3, 4, 0, 1, 2]]) """ a = asanyarray(a) -- cgit v1.2.1 From fae6fa47a3cf9b9c64af2f5bd11a3b644b1763d2 Mon Sep 17 00:00:00 2001 From: Ganesh Kathiresan Date: Tue, 2 Nov 2021 03:35:27 +0530 Subject: ENH: Adding `scalar.bit_count()` (popcount) (#19355) Adding bitcount method to scalars, e.g.: a = np.int32(1023).bit_count() * ENH: Implementation of bit_count (popcount) * ENH: Add bit_count to integer scalar type * ENH: Annotations for bit_count * ENH, WIP: Documentation for bit_count * DOC: Added `bit_count` (#19355) * BUG: Fixed windows 32 bit issue with no `__popcnt64` * DOC: Refined docstring for bit_count * TST: Tests for bit_count * ENH, MAINT: Changed return type to uint_8 | Removed extra braces and fixed typo * BUG: Fixed syntax of bit_count * DOC, BUG: Fixed bit_count example * DOC, BUG: (#19355) Removed bit_count from routines.math.rst | Improved release notes * BUG: Added type suffix to magic constants * ENH: Handle 32 bit windows popcount | Refactored popcount implementation to new function * MAINT: Refactor type_methods, separate integer definitions * DOC: Added double-ticks --- numpy/__init__.pyi | 1 + numpy/core/_add_newdocs_scalars.py | 19 ++++++ numpy/core/include/numpy/npy_math.h | 11 ++++ numpy/core/src/multiarray/scalartypes.c.src | 52 +++++++++++++++- numpy/core/src/npymath/npy_math_internal.h.src | 86 ++++++++++++++++++++++++++ numpy/core/tests/test_scalar_methods.py | 18 ++++++ 6 files changed, 185 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 463d4a713..229b6b916 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -2750,6 +2750,7 @@ class integer(number[_NBit1]): # type: ignore ) -> int: ... def tolist(self) -> int: ... def is_integer(self) -> L[True]: ... + def bit_count(self: _ScalarType) -> int: ... def __index__(self) -> int: ... __truediv__: _IntTrueDiv[_NBit1] __rtruediv__: _IntTrueDiv[_NBit1] diff --git a/numpy/core/_add_newdocs_scalars.py b/numpy/core/_add_newdocs_scalars.py index 8773d6c96..94859a9d5 100644 --- a/numpy/core/_add_newdocs_scalars.py +++ b/numpy/core/_add_newdocs_scalars.py @@ -290,3 +290,22 @@ for float_name in ('half', 'single', 'double', 'longdouble'): >>> np.{float_name}(3.2).is_integer() False """)) + +for int_name in ('int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', + 'int64', 'uint64', 'int64', 'uint64', 'int64', 'uint64'): + # Add negative examples for signed cases by checking typecode + add_newdoc('numpy.core.numerictypes', int_name, ('bit_count', + f""" + {int_name}.bit_count() -> int + + Computes the number of 1-bits in the absolute value of the input. + Analogous to the builtin `int.bit_count` or ``popcount`` in C++. + + Examples + -------- + >>> np.{int_name}(127).bit_count() + 7""" + + (f""" + >>> np.{int_name}(-127).bit_count() + 7 + """ if dtype(int_name).char.islower() else ""))) diff --git a/numpy/core/include/numpy/npy_math.h b/numpy/core/include/numpy/npy_math.h index b1e6363e3..bead0dc14 100644 --- a/numpy/core/include/numpy/npy_math.h +++ b/numpy/core/include/numpy/npy_math.h @@ -150,6 +150,17 @@ NPY_INPLACE npy_long npy_lshiftl(npy_long a, npy_long b); NPY_INPLACE npy_longlong npy_rshiftll(npy_longlong a, npy_longlong b); NPY_INPLACE npy_longlong npy_lshiftll(npy_longlong a, npy_longlong b); +NPY_INPLACE uint8_t npy_popcountuhh(npy_ubyte a); +NPY_INPLACE uint8_t npy_popcountuh(npy_ushort a); +NPY_INPLACE uint8_t npy_popcountu(npy_uint a); +NPY_INPLACE uint8_t npy_popcountul(npy_ulong a); +NPY_INPLACE uint8_t npy_popcountull(npy_ulonglong a); +NPY_INPLACE uint8_t npy_popcounthh(npy_byte a); +NPY_INPLACE uint8_t npy_popcounth(npy_short a); +NPY_INPLACE uint8_t npy_popcount(npy_int a); +NPY_INPLACE uint8_t npy_popcountl(npy_long a); +NPY_INPLACE uint8_t npy_popcountll(npy_longlong a); + /* * C99 double math funcs */ diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src index 0d52211a8..bbbc5bfa2 100644 --- a/numpy/core/src/multiarray/scalartypes.c.src +++ b/numpy/core/src/multiarray/scalartypes.c.src @@ -218,6 +218,27 @@ gentype_multiply(PyObject *m1, PyObject *m2) return PyArray_Type.tp_as_number->nb_multiply(m1, m2); } +/**begin repeat + * #TYPE = BYTE, UBYTE, SHORT, USHORT, INT, UINT, + * LONG, ULONG, LONGLONG, ULONGLONG# + * #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint, + * npy_long, npy_ulong, npy_longlong, npy_ulonglong# + * #c = hh, uhh, h, uh,, u, l, ul, ll, ull# + * #Name = Byte, UByte, Short, UShort, Int, UInt, + * Long, ULong, LongLong, ULongLong# + * #convert = Long*8, LongLong*2# + */ +static PyObject * +@type@_bit_count(PyObject *self) +{ + @type@ scalar = PyArrayScalar_VAL(self, @Name@); + uint8_t count = npy_popcount@c@(scalar); + PyObject *result = PyLong_From@convert@(count); + + return result; +} +/**end repeat**/ + /**begin repeat * * #name = positive, negative, absolute, invert, int, float# @@ -2316,8 +2337,7 @@ static PyMethodDef @name@type_methods[] = { /**end repeat**/ /**begin repeat - * #name = byte, short, int, long, longlong, ubyte, ushort, - * uint, ulong, ulonglong, timedelta, cdouble# + * #name = timedelta, cdouble# */ static PyMethodDef @name@type_methods[] = { /* for typing; requires python >= 3.9 */ @@ -2328,6 +2348,23 @@ static PyMethodDef @name@type_methods[] = { }; /**end repeat**/ +/**begin repeat + * #name = byte, ubyte, short, ushort, int, uint, + * long, ulong, longlong, ulonglong# + */ +static PyMethodDef @name@type_methods[] = { + /* for typing; requires python >= 3.9 */ + {"__class_getitem__", + (PyCFunction)numbertype_class_getitem, + METH_CLASS | METH_O, NULL}, + {"bit_count", + (PyCFunction)npy_@name@_bit_count, + METH_NOARGS, NULL}, + {NULL, NULL, 0, NULL} /* sentinel */ +}; +/**end repeat**/ + + /************* As_mapping functions for void array scalar ************/ static Py_ssize_t @@ -4104,6 +4141,17 @@ initialize_numeric_types(void) /**end repeat**/ + /**begin repeat + * #name = byte, short, int, long, longlong, + * ubyte, ushort, uint, ulong, ulonglong# + * #Name = Byte, Short, Int, Long, LongLong, + * UByte, UShort, UInt, ULong, ULongLong# + */ + + Py@Name@ArrType_Type.tp_methods = @name@type_methods; + + /**end repeat**/ + /**begin repeat * #name = half, float, double, longdouble# * #Name = Half, Float, Double, LongDouble# diff --git a/numpy/core/src/npymath/npy_math_internal.h.src b/numpy/core/src/npymath/npy_math_internal.h.src index cae84befe..dd2424db8 100644 --- a/numpy/core/src/npymath/npy_math_internal.h.src +++ b/numpy/core/src/npymath/npy_math_internal.h.src @@ -55,6 +55,29 @@ */ #include "npy_math_private.h" +/* Magic binary numbers used by bit_count + * For type T, the magic numbers are computed as follows: + * Magic[0]: 01 01 01 01 01 01... = (T)~(T)0/3 + * Magic[1]: 0011 0011 0011... = (T)~(T)0/15 * 3 + * Magic[2]: 00001111 00001111... = (T)~(T)0/255 * 15 + * Magic[3]: 00000001 00000001... = (T)~(T)0/255 + * + * Counting bits set, in parallel + * Based on: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel + * + * Generic Algorithm for type T: + * a = a - ((a >> 1) & (T)~(T)0/3); + * a = (a & (T)~(T)0/15*3) + ((a >> 2) & (T)~(T)0/15*3); + * a = (a + (a >> 4)) & (T)~(T)0/255*15; + * c = (T)(a * ((T)~(T)0/255)) >> (sizeof(T) - 1) * CHAR_BIT; +*/ + +static const npy_uint8 MAGIC8[] = {0x55u, 0x33u, 0x0Fu, 0x01u}; +static const npy_uint16 MAGIC16[] = {0x5555u, 0x3333u, 0x0F0Fu, 0x0101u}; +static const npy_uint32 MAGIC32[] = {0x55555555ul, 0x33333333ul, 0x0F0F0F0Ful, 0x01010101ul}; +static const npy_uint64 MAGIC64[] = {0x5555555555555555ull, 0x3333333333333333ull, 0x0F0F0F0F0F0F0F0Full, 0x0101010101010101ull}; + + /* ***************************************************************************** ** BASIC MATH FUNCTIONS ** @@ -814,3 +837,66 @@ npy_rshift@u@@c@(npy_@u@@type@ a, npy_@u@@type@ b) } /**end repeat1**/ /**end repeat**/ + + +#define __popcnt32 __popcnt +/**begin repeat + * + * #type = ubyte, ushort, uint, ulong, ulonglong# + * #STYPE = BYTE, SHORT, INT, LONG, LONGLONG# + * #c = hh, h, , l, ll# + */ +#undef TO_BITS_LEN +#if 0 +/**begin repeat1 + * #len = 8, 16, 32, 64# + */ +#elif NPY_BITSOF_@STYPE@ == @len@ + #define TO_BITS_LEN(X) X##@len@ +/**end repeat1**/ +#endif + + +NPY_INPLACE uint8_t +npy_popcount_parallel@c@(npy_@type@ a) +{ + a = a - ((a >> 1) & (npy_@type@) TO_BITS_LEN(MAGIC)[0]); + a = ((a & (npy_@type@) TO_BITS_LEN(MAGIC)[1])) + ((a >> 2) & (npy_@type@) TO_BITS_LEN(MAGIC)[1]); + a = (a + (a >> 4)) & (npy_@type@) TO_BITS_LEN(MAGIC)[2]; + return (npy_@type@) (a * (npy_@type@) TO_BITS_LEN(MAGIC)[3]) >> ((NPY_SIZEOF_@STYPE@ - 1) * CHAR_BIT); +} + +NPY_INPLACE uint8_t +npy_popcountu@c@(npy_@type@ a) +{ +/* use built-in popcount if present, else use our implementation */ +#if (defined(__clang__) || defined(__GNUC__)) && NPY_BITSOF_@STYPE@ >= 32 + return __builtin_popcount@c@(a); +#elif defined(_MSC_VER) && NPY_BITSOF_@STYPE@ >= 16 + /* no builtin __popcnt64 for 32 bits */ + #if defined(_WIN64) || (defined(_WIN32) && NPY_BITSOF_@STYPE@ != 64) + return TO_BITS_LEN(__popcnt)(a); + /* split 64 bit number into two 32 bit ints and return sum of counts */ + #elif (defined(_WIN32) && NPY_BITSOF_@STYPE@ == 64) + npy_uint32 left = (npy_uint32) (a>>32); + npy_uint32 right = (npy_uint32) a; + return __popcnt32(left) + __popcnt32(right); + #endif +#else + return npy_popcount_parallel@c@(a); +#endif +} +/**end repeat**/ + +/**begin repeat + * + * #type = byte, short, int, long, longlong# + * #c = hh, h, , l, ll# + */ +NPY_INPLACE uint8_t +npy_popcount@c@(npy_@type@ a) +{ + /* Return popcount of abs(a) */ + return npy_popcountu@c@(a < 0 ? -a : a); +} +/**end repeat**/ diff --git a/numpy/core/tests/test_scalar_methods.py b/numpy/core/tests/test_scalar_methods.py index 6077c8f75..eef4c1433 100644 --- a/numpy/core/tests/test_scalar_methods.py +++ b/numpy/core/tests/test_scalar_methods.py @@ -183,3 +183,21 @@ def test_class_getitem_38(cls: Type[np.number]) -> None: match = "Type subscription requires python >= 3.9" with pytest.raises(TypeError, match=match): cls[Any] + + +class TestBitCount: + # derived in part from the cpython test "test_bit_count" + + @pytest.mark.parametrize("itype", np.sctypes['int']+np.sctypes['uint']) + def test_small(self, itype): + for a in range(max(np.iinfo(itype).min, 0), 128): + msg = f"Smoke test for {itype}({a}).bit_count()" + assert itype(a).bit_count() == bin(a).count("1"), msg + + def test_bit_count(self): + for exp in [10, 17, 63]: + a = 2**exp + assert np.uint64(a).bit_count() == 1 + assert np.uint64(a - 1).bit_count() == exp + assert np.uint64(a ^ 63).bit_count() == 7 + assert np.uint64((a - 1) ^ 510).bit_count() == exp - 8 -- cgit v1.2.1 From 01443e8d32dd586ac27fe5aebe1a781460190e6a Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Tue, 2 Nov 2021 14:18:52 +1100 Subject: TST: turn glibc_older_than_2.17 into boolean instead of xfail this anticipates reuse of this boolean within the test module --- numpy/core/tests/test_umath.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 8f5a85824..2f7322412 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -28,9 +28,7 @@ def get_glibc_version(): glibcver = get_glibc_version() -glibc_newerthan_2_17 = pytest.mark.xfail( - glibcver != '0.0' and glibcver < '2.17', - reason="Older glibc versions may not raise appropriate FP exceptions") +glibc_older_than_2_17 = (glibcver != '0.0' and glibcver < '2.17') def on_powerpc(): """ True if we are running on a Power PC platform.""" @@ -1024,7 +1022,10 @@ class TestSpecialFloats: # Older version of glibc may not raise the correct FP exceptions # See: https://github.com/numpy/numpy/issues/19192 - @glibc_newerthan_2_17 + @pytest.mark.xfail( + glibc_older_than_2_17, + reason="Older glibc versions may not raise appropriate FP exceptions" + ) def test_exp_exceptions(self): with np.errstate(over='raise'): assert_raises(FloatingPointError, np.exp, np.float32(100.)) -- cgit v1.2.1 From 56268d5cf8ded5ebe0b51cca6c23da4b0586807c Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Tue, 2 Nov 2021 14:19:24 +1100 Subject: TST: use existence of glibc-version to clean up a test --- numpy/core/tests/test_umath.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 2f7322412..ffe653d18 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -48,14 +48,6 @@ def bad_arcsinh(): # The eps for float128 is 1-e33, so this is way bigger return abs((v1 / v2) - 1.0) > 1e-23 -if platform.machine() == 'aarch64' and bad_arcsinh(): - skip_longcomplex_msg = ('Trig functions of np.longcomplex values known to be ' - 'inaccurate on aarch64 for some compilation ' - 'configurations, should be fixed by building on a ' - 'platform using glibc>2.17') -else: - skip_longcomplex_msg = '' - class _FilterInvalids: def setup(self): @@ -3440,13 +3432,14 @@ class TestComplexFunctions: x_series = np.logspace(-20, -3.001, 200) x_basic = np.logspace(-2.999, 0, 10, endpoint=False) - if dtype is np.longcomplex: + if glibc_older_than_2_17 and dtype is np.longcomplex: + if (platform.machine() == 'aarch64' and bad_arcsinh()): + pytest.skip("Trig functions of np.longcomplex values known " + "to be inaccurate on aarch64 for some compilation " + "configurations.") # It's not guaranteed that the system-provided arc functions # are accurate down to a few epsilons. (Eg. on Linux 64-bit) # So, give more leeway for long complex tests here: - # Can use 2.1 for > Ubuntu LTS Trusty (2014), glibc = 2.19. - if skip_longcomplex_msg: - pytest.skip(skip_longcomplex_msg) check(x_series, 50.0*eps) else: check(x_series, 2.1*eps) -- cgit v1.2.1 From 12923c259c71ad0601a49ecc3de4748adaba0957 Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Tue, 2 Nov 2021 14:21:01 +1100 Subject: TST: skip coverage of large elements in sincos_float32 for ancient glibc Fixes #15179 --- numpy/core/tests/test_umath.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index ffe653d18..b04faecb9 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -1398,8 +1398,10 @@ class TestAVXFloat32Transcendental: M = np.int_(N/20) index = np.random.randint(low=0, high=N, size=M) x_f32 = np.float32(np.random.uniform(low=-100.,high=100.,size=N)) - # test coverage for elements > 117435.992f for which glibc is used - x_f32[index] = np.float32(10E+10*np.random.rand(M)) + if not glibc_older_than_2_17: + # test coverage for elements > 117435.992f for which glibc is used + # this is known to be problematic on old glibc, so skip it there + x_f32[index] = np.float32(10E+10*np.random.rand(M)) x_f64 = np.float64(x_f32) assert_array_max_ulp(np.sin(x_f32), np.float32(np.sin(x_f64)), maxulp=2) assert_array_max_ulp(np.cos(x_f32), np.float32(np.cos(x_f64)), maxulp=2) -- cgit v1.2.1 From 22448b4247bec30ef4bd4d6f649a234ddb1f4cdd Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Tue, 2 Nov 2021 16:51:59 +1100 Subject: TST: parametrize glibc-version check in test_umath.py --- numpy/core/tests/test_umath.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index b04faecb9..fc7c592f0 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -28,7 +28,7 @@ def get_glibc_version(): glibcver = get_glibc_version() -glibc_older_than_2_17 = (glibcver != '0.0' and glibcver < '2.17') +glibc_older_than = lambda x: (glibcver != '0.0' and glibcver < x) def on_powerpc(): """ True if we are running on a Power PC platform.""" @@ -1012,10 +1012,9 @@ class TestSpecialFloats: yf = np.array(y, dtype=dt) assert_equal(np.exp(yf), xf) - # Older version of glibc may not raise the correct FP exceptions # See: https://github.com/numpy/numpy/issues/19192 @pytest.mark.xfail( - glibc_older_than_2_17, + glibc_older_than("2.17"), reason="Older glibc versions may not raise appropriate FP exceptions" ) def test_exp_exceptions(self): @@ -1398,7 +1397,7 @@ class TestAVXFloat32Transcendental: M = np.int_(N/20) index = np.random.randint(low=0, high=N, size=M) x_f32 = np.float32(np.random.uniform(low=-100.,high=100.,size=N)) - if not glibc_older_than_2_17: + if not glibc_older_than("2.17"): # test coverage for elements > 117435.992f for which glibc is used # this is known to be problematic on old glibc, so skip it there x_f32[index] = np.float32(10E+10*np.random.rand(M)) @@ -3434,7 +3433,7 @@ class TestComplexFunctions: x_series = np.logspace(-20, -3.001, 200) x_basic = np.logspace(-2.999, 0, 10, endpoint=False) - if glibc_older_than_2_17 and dtype is np.longcomplex: + if glibc_older_than("2.19") and dtype is np.longcomplex: if (platform.machine() == 'aarch64' and bad_arcsinh()): pytest.skip("Trig functions of np.longcomplex values known " "to be inaccurate on aarch64 for some compilation " -- cgit v1.2.1 From 790f927fcedb7debc477746ffb7a4a2eb1668693 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Tue, 2 Nov 2021 13:04:51 +0530 Subject: Addressed reviews --- numpy/core/include/numpy/ndarraytypes.h | 6 +++--- numpy/core/src/multiarray/conversion_utils.c | 25 ++++++------------------- numpy/core/src/multiarray/conversion_utils.h | 2 +- numpy/core/src/multiarray/ctors.c | 12 ++++++------ numpy/core/src/multiarray/methods.c | 4 ++-- numpy/core/src/multiarray/multiarraymodule.c | 6 +++--- numpy/core/tests/test_multiarray.py | 15 +++++++++++++++ 7 files changed, 36 insertions(+), 34 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index a1d1c01dc..566eae357 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -449,11 +449,11 @@ typedef struct { int len; } PyArray_Dims; -typedef enum PyNpCopyMode { +typedef enum { NPY_COPY_IF_NEEDED, NPY_COPY_ALWAYS, NPY_COPY_NEVER -} PyArray_CopyMode; +} _PyArray_CopyMode; typedef struct { /* @@ -938,7 +938,7 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *); #define NPY_ARRAY_UPDATEIFCOPY 0x1000 /* Deprecated in 1.14 */ #define NPY_ARRAY_WRITEBACKIFCOPY 0x2000 -#define NPY_ARRAY_ENSURENOCOPY 0x4000 +#define _NPY_ARRAY_ENSURENOCOPY 0x4000 /* * NOTE: there are also internal flags defined in multiarray/arrayobject.h, diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 59e3b4922..983890433 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -166,13 +166,10 @@ PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq) } NPY_NO_EXPORT int -PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { +PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) { if (obj == Py_None) { PyErr_SetString(PyExc_ValueError, - "NoneType copy mode not allowed. Please choose one of " - "np.array_api._CopyMode.ALWAYS, " - "np.array_api._CopyMode.IF_NEEDED, " - "np.array_api._CopyMode.NEVER."); + "NoneType copy mode not allowed."); return NPY_FAIL; } @@ -180,7 +177,7 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { PyObject* numpy_CopyMode = NULL; npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode); - if (numpy_CopyMode != NULL && PyObject_IsInstance(obj, numpy_CopyMode)) { + if (numpy_CopyMode != NULL && PyObject_TypeCheck(obj, numpy_CopyMode)) { PyObject* mode_value = PyObject_GetAttrString(obj, "value"); if (mode_value == NULL) { return NPY_FAIL; @@ -188,7 +185,8 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { if (!PyArray_PythonPyIntFromInt(mode_value, &int_copymode)) { return NPY_FAIL; } - } else { + } + else { npy_bool bool_copymode; if( !PyArray_BoolConverter(obj, &bool_copymode) ) { return NPY_FAIL; @@ -196,18 +194,7 @@ PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copymode) { int_copymode = (int) bool_copymode; } - if( int_copymode != NPY_COPY_ALWAYS && - int_copymode != NPY_COPY_IF_NEEDED && - int_copymode != NPY_COPY_NEVER ) { - PyErr_Format(PyExc_ValueError, - "Unrecognized copy mode %d. Please choose one of " - "np._CopyMode.ALWAYS, np._CopyMode.IF_NEEDED, " - "np._CopyMode.NEVER, " - "True/np.True_, False/np.False_", int_copymode); - return NPY_FAIL; - } - - *copymode = (PyArray_CopyMode) int_copymode; + *copymode = (_PyArray_CopyMode) int_copymode; return NPY_SUCCEED; } diff --git a/numpy/core/src/multiarray/conversion_utils.h b/numpy/core/src/multiarray/conversion_utils.h index 4662c6a8b..643b67d59 100644 --- a/numpy/core/src/multiarray/conversion_utils.h +++ b/numpy/core/src/multiarray/conversion_utils.h @@ -10,7 +10,7 @@ NPY_NO_EXPORT int PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq); NPY_NO_EXPORT int -PyArray_CopyConverter(PyObject *obj, PyArray_CopyMode *copyflag); +PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copyflag); NPY_NO_EXPORT int PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf); diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 27fd3a057..fdc393b97 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1305,7 +1305,7 @@ _array_from_array_like(PyObject *op, PyErr_Clear(); } else { - tmp = _array_from_buffer_3118(memoryview); // Assume: never creates a copy + tmp = _array_from_buffer_3118(memoryview); Py_DECREF(memoryview); if (tmp == NULL) { return NULL; @@ -1347,7 +1347,7 @@ _array_from_array_like(PyObject *op, * this should be changed! */ if (!writeable && tmp == Py_NotImplemented) { - tmp = PyArray_FromArrayAttr(op, requested_dtype, context); // Assume: array was copied. + tmp = PyArray_FromArrayAttr(op, requested_dtype, context); if (tmp == NULL) { return NULL; } @@ -1738,7 +1738,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, /* Create a new array and copy the data */ Py_INCREF(dtype); /* hold on in case of a subarray that is replaced */ - if( flags & NPY_ARRAY_ENSURENOCOPY ) { + if( flags & _NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating " "an array from descriptor."); @@ -1802,7 +1802,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, * NPY_ARRAY_ALIGNED, * NPY_ARRAY_WRITEABLE, * NPY_ARRAY_NOTSWAPPED, - * NPY_ARRAY_ENSURECOPY, + * _NPY_ARRAY_ENSURECOPY, * NPY_ARRAY_UPDATEIFCOPY, * NPY_ARRAY_WRITEBACKIFCOPY, * NPY_ARRAY_FORCECAST, @@ -1872,7 +1872,7 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, if ((requires & NPY_ARRAY_ELEMENTSTRIDES) && !PyArray_ElementStrides(obj)) { PyObject *ret; - if( requires & NPY_ARRAY_ENSURENOCOPY ) { + if( requires & _NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating a new array."); return NULL; @@ -1952,7 +1952,7 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) if (copy) { - if( flags & NPY_ARRAY_ENSURENOCOPY ) { + if( flags & _NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating " "an array from given array."); diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index bb0006e32..7530a2e36 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -833,7 +833,7 @@ array_astype(PyArrayObject *self, */ NPY_CASTING casting = NPY_UNSAFE_CASTING; NPY_ORDER order = NPY_KEEPORDER; - PyArray_CopyMode forcecopy = 1; + _PyArray_CopyMode forcecopy = 1; int subok = 1; NPY_PREPARE_ARGPARSER; if (npy_parse_arguments("astype", args, len_args, kwnames, @@ -877,7 +877,7 @@ array_astype(PyArrayObject *self, if( forcecopy == NPY_COPY_NEVER ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while casting in " - "np._CopyMode.NEVER"); + "never copy mode."); Py_DECREF(dtype); return NULL; } diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index c00f14045..bf60314ad 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1560,7 +1560,7 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin, NPY_ORDER order) static NPY_INLINE PyObject * _array_fromobject_generic( - PyObject *op, PyArray_Descr *type, PyArray_CopyMode copy, NPY_ORDER order, + PyObject *op, PyArray_Descr *type, _PyArray_CopyMode copy, NPY_ORDER order, npy_bool subok, int ndmin) { PyArrayObject *oparr = NULL, *ret = NULL; @@ -1623,7 +1623,7 @@ _array_fromobject_generic( if (copy == NPY_COPY_ALWAYS) { flags = NPY_ARRAY_ENSURECOPY; } else if( copy == NPY_COPY_NEVER ) { - flags = NPY_ARRAY_ENSURENOCOPY; + flags = _NPY_ARRAY_ENSURENOCOPY; } if (order == NPY_CORDER) { flags |= NPY_ARRAY_C_CONTIGUOUS; @@ -1668,7 +1668,7 @@ array_array(PyObject *NPY_UNUSED(ignored), { PyObject *op; npy_bool subok = NPY_FALSE; - PyArray_CopyMode copy = NPY_COPY_ALWAYS; + _PyArray_CopyMode copy = NPY_COPY_ALWAYS; int ndmin = 0; PyArray_Descr *type = NULL; NPY_ORDER order = NPY_KEEPORDER; diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index fa7f254a6..3919f31d8 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7816,6 +7816,11 @@ class TestNewBufferProtocol: class TestArrayCreationCopyArgument(object): + class RaiseOnBool: + + def __bool__(self): + raise ValueError + true_vals = [True, np._CopyMode.ALWAYS, np.True_] false_vals = [False, np._CopyMode.IF_NEEDED, np.False_] @@ -7831,6 +7836,10 @@ class TestArrayCreationCopyArgument(object): copy=np._CopyMode.NEVER) assert_raises(ValueError, np.array, pyscalar, copy=np._CopyMode.NEVER) + assert_raises(ValueError, np.array, pyscalar, + copy=None) + assert_raises(ValueError, np.array, pyscalar, + copy=self.RaiseOnBool()) def test_compatible_cast(self): @@ -7874,6 +7883,9 @@ class TestArrayCreationCopyArgument(object): assert_raises(ValueError, np.array, arr, copy=np._CopyMode.NEVER, dtype=int2) + assert_raises(ValueError, np.array, + arr, copy=None, + dtype=int2) def test_buffer_interface(self): @@ -7988,6 +8000,9 @@ class TestArrayCreationCopyArgument(object): assert_raises(ValueError, np.array, view, copy=np._CopyMode.NEVER, order=order2) + assert_raises(ValueError, np.array, + view, copy=None, + order=order2) class TestArrayAttributeDeletion: -- cgit v1.2.1 From a888ae827d34a47eb8fab7a01353175ab39bba76 Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Mon, 24 May 2021 15:22:01 +0200 Subject: ENH: Add the __dlpack__ and __dlpack_device__ methods to ndarray. --- numpy/__init__.pyi | 10 ++ numpy/core/src/common/dlpack/dlpack.h | 188 ++++++++++++++++++++++++++++++++++ numpy/core/src/multiarray/methods.c | 163 +++++++++++++++++++++++++++++ 3 files changed, 361 insertions(+) create mode 100644 numpy/core/src/common/dlpack/dlpack.h (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 637f8578a..a162a637c 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1432,6 +1432,10 @@ _ArrayTD64_co = NDArray[Union[bool_, integer[Any], timedelta64]] # Introduce an alias for `dtype` to avoid naming conflicts. _dtype = dtype +# `builtins.PyCapsule` unfortunately lacks annotations as of the moment; +# use `Any` as a stopgap measure +_PyCapsule = Any + class _SupportsItem(Protocol[_T_co]): def item(self, args: Any, /) -> _T_co: ... @@ -2439,6 +2443,12 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __ior__(self: NDArray[signedinteger[_NBit1]], other: _ArrayLikeInt_co) -> NDArray[signedinteger[_NBit1]]: ... @overload def __ior__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... + @overload + def __ior__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... + @overload + def __dlpack__(self: NDArray[number[Any]], *, stream: None = ...) -> _PyCapsule: ... + @overload + def __dlpack_device__(self) -> Tuple[L[1], L[0]]: ... # Keep `dtype` at the bottom to avoid name conflicts with `np.dtype` @property diff --git a/numpy/core/src/common/dlpack/dlpack.h b/numpy/core/src/common/dlpack/dlpack.h new file mode 100644 index 000000000..84afca248 --- /dev/null +++ b/numpy/core/src/common/dlpack/dlpack.h @@ -0,0 +1,188 @@ +/*! + * Copyright (c) 2017 by Contributors + * \file dlpack.h + * \brief The common header of DLPack. + */ +#ifndef DLPACK_DLPACK_H_ +#define DLPACK_DLPACK_H_ + +#ifdef __cplusplus +#define DLPACK_EXTERN_C extern "C" +#else +#define DLPACK_EXTERN_C +#endif + +/*! \brief The current version of dlpack */ +#define DLPACK_VERSION 050 + +/*! \brief DLPACK_DLL prefix for windows */ +#ifdef _WIN32 +#ifdef DLPACK_EXPORTS +#define DLPACK_DLL __declspec(dllexport) +#else +#define DLPACK_DLL __declspec(dllimport) +#endif +#else +#define DLPACK_DLL +#endif + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif +/*! + * \brief The device type in DLDevice. + */ +typedef enum { + /*! \brief CPU device */ + kDLCPU = 1, + /*! \brief CUDA GPU device */ + kDLCUDA = 2, + /*! + * \brief Pinned CUDA CPU memory by cudaMallocHost + */ + kDLCUDAHost = 3, + /*! \brief OpenCL devices. */ + kDLOpenCL = 4, + /*! \brief Vulkan buffer for next generation graphics. */ + kDLVulkan = 7, + /*! \brief Metal for Apple GPU. */ + kDLMetal = 8, + /*! \brief Verilog simulator buffer */ + kDLVPI = 9, + /*! \brief ROCm GPUs for AMD GPUs */ + kDLROCM = 10, + /*! + * \brief Reserved extension device type, + * used for quickly test extension device + * The semantics can differ depending on the implementation. + */ + kDLExtDev = 12, +} DLDeviceType; + +/*! + * \brief A Device for Tensor and operator. + */ +typedef struct { + /*! \brief The device type used in the device. */ + DLDeviceType device_type; + /*! \brief The device index */ + int device_id; +} DLDevice; + +/*! + * \brief The type code options DLDataType. + */ +typedef enum { + /*! \brief signed integer */ + kDLInt = 0U, + /*! \brief unsigned integer */ + kDLUInt = 1U, + /*! \brief IEEE floating point */ + kDLFloat = 2U, + /*! + * \brief Opaque handle type, reserved for testing purposes. + * Frameworks need to agree on the handle data type for the exchange to be well-defined. + */ + kDLOpaqueHandle = 3U, + /*! \brief bfloat16 */ + kDLBfloat = 4U, + /*! + * \brief complex number + * (C/C++/Python layout: compact struct per complex number) + */ + kDLComplex = 5U, +} DLDataTypeCode; + +/*! + * \brief The data type the tensor can hold. + * + * Examples + * - float: type_code = 2, bits = 32, lanes=1 + * - float4(vectorized 4 float): type_code = 2, bits = 32, lanes=4 + * - int8: type_code = 0, bits = 8, lanes=1 + * - std::complex: type_code = 5, bits = 64, lanes = 1 + */ +typedef struct { + /*! + * \brief Type code of base types. + * We keep it uint8_t instead of DLDataTypeCode for minimal memory + * footprint, but the value should be one of DLDataTypeCode enum values. + * */ + uint8_t code; + /*! + * \brief Number of bits, common choices are 8, 16, 32. + */ + uint8_t bits; + /*! \brief Number of lanes in the type, used for vector types. */ + uint16_t lanes; +} DLDataType; + +/*! + * \brief Plain C Tensor object, does not manage memory. + */ +typedef struct { + /*! + * \brief The opaque data pointer points to the allocated data. This will be + * CUDA device pointer or cl_mem handle in OpenCL. This pointer is always + * aligned to 256 bytes as in CUDA. + * + * For given DLTensor, the size of memory required to store the contents of + * data is calculated as follows: + * + * \code{.c} + * static inline size_t GetDataSize(const DLTensor* t) { + * size_t size = 1; + * for (tvm_index_t i = 0; i < t->ndim; ++i) { + * size *= t->shape[i]; + * } + * size *= (t->dtype.bits * t->dtype.lanes + 7) / 8; + * return size; + * } + * \endcode + */ + void* data; + /*! \brief The device of the tensor */ + DLDevice device; + /*! \brief Number of dimensions */ + int ndim; + /*! \brief The data type of the pointer*/ + DLDataType dtype; + /*! \brief The shape of the tensor */ + int64_t* shape; + /*! + * \brief strides of the tensor (in number of elements, not bytes) + * can be NULL, indicating tensor is compact and row-majored. + */ + int64_t* strides; + /*! \brief The offset in bytes to the beginning pointer to data */ + uint64_t byte_offset; +} DLTensor; + +/*! + * \brief C Tensor object, manage memory of DLTensor. This data structure is + * intended to facilitate the borrowing of DLTensor by another framework. It is + * not meant to transfer the tensor. When the borrowing framework doesn't need + * the tensor, it should call the deleter to notify the host that the resource + * is no longer needed. + */ +typedef struct DLManagedTensor { + /*! \brief DLTensor which is being memory managed */ + DLTensor dl_tensor; + /*! \brief the context of the original host framework of DLManagedTensor in + * which DLManagedTensor is used in the framework. It can also be NULL. + */ + void * manager_ctx; + /*! \brief Destructor signature void (*)(void*) - this should be called + * to destruct manager_ctx which holds the DLManagedTensor. It can be NULL + * if there is no way for the caller to provide a reasonable destructor. + * The destructors deletes the argument self as well. + */ + void (*deleter)(struct DLManagedTensor * self); +} DLManagedTensor; +#ifdef __cplusplus +} // DLPACK_EXTERN_C +#endif +#endif // DLPACK_DLPACK_H_ diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 2d66c77dc..2251d4b69 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -31,6 +31,7 @@ #include "alloc.h" #include +#include "common/dlpack/dlpack.h" /* NpyArg_ParseKeywords @@ -2762,6 +2763,158 @@ array_class_getitem(PyObject *cls, PyObject *args) generic_alias = NULL; #endif return generic_alias; + +#define NPY_DLPACK_CAPSULE_NAME "dltensor" +#define NPY_DLPACK_USED_CAPSULE_NAME "used_dltensor" + +static void array_dlpack_capsule_deleter(PyObject *self) +{ + if (!PyCapsule_IsValid(self, NPY_DLPACK_CAPSULE_NAME)) { + if (!PyCapsule_IsValid(self, NPY_DLPACK_USED_CAPSULE_NAME)) { + PyErr_SetString(PyExc_RuntimeError, "Invalid capsule name."); + } + return; + } + + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_CAPSULE_NAME); + managed->deleter(managed); +} + +static void array_dlpack_deleter(DLManagedTensor *self) +{ + PyArrayObject *array = (PyArrayObject *)self->manager_ctx; + // This will also free the strides as it's one allocation. + PyMem_Free(self->dl_tensor.shape); + PyMem_Free(self); + + PyArray_XDECREF(array); +} + +static PyObject * +array_dlpack(PyArrayObject *self, + PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) +{ + PyObject *stream = Py_None; + NPY_PREPARE_ARGPARSER; + if (npy_parse_arguments("__dlpack__", args, len_args, kwnames, + "$stream", NULL, &stream, NULL, NULL, NULL)) { + return NULL; + } + + if (stream != Py_None) { + PyErr_SetString(PyExc_RuntimeError, "NumPy only supports " + "stream=None."); + return NULL; + } + + npy_intp itemsize = PyArray_ITEMSIZE(self); + int ndim = PyArray_NDIM(self); + npy_intp *strides = PyArray_STRIDES(self); + npy_intp *shape = PyArray_SHAPE(self); + + for (int i = 0; i < ndim; ++i) { + if (strides[i] % itemsize != 0) { + PyErr_SetString(PyExc_RuntimeError, + "DLPack only supports strides which are a multiple of " + "itemsize."); + return NULL; + } + } + + DLDataType managed_dtype; + PyArray_Descr *dtype = PyArray_DESCR(self); + + if (PyDataType_ISBYTESWAPPED(dtype)) { + PyErr_SetString(PyExc_TypeError, "DLPack only supports native " + "byte swapping."); + return NULL; + } + + managed_dtype.bits = 8 * itemsize; + managed_dtype.lanes = 1; + + if (PyDataType_ISSIGNED(dtype)) { + managed_dtype.code = kDLInt; + } else if (PyDataType_ISUNSIGNED(dtype)) { + managed_dtype.code = kDLUInt; + } else if (PyDataType_ISFLOAT(dtype)) { + // We can't be sure that the dtype is + // IEEE or padded. + if (itemsize > 8) { + PyErr_SetString(PyExc_TypeError, "DLPack only supports IEEE " + "floating point types without padding."); + return NULL; + } + managed_dtype.code = kDLFloat; + } else if (PyDataType_ISCOMPLEX(dtype)) { + // We can't be sure that the dtype is + // IEEE or padded. + if (itemsize > 16) { + PyErr_SetString(PyExc_TypeError, "DLPack only supports IEEE " + "complex point types without padding."); + return NULL; + } + managed_dtype.code = kDLComplex; + } else { + PyErr_SetString(PyExc_TypeError, + "DLPack only supports signed/unsigned integers, float " + "and complex dtypes."); + return NULL; + } + + DLManagedTensor *managed = PyMem_Malloc(sizeof(DLManagedTensor)); + if (managed == NULL) { + PyErr_NoMemory(); + return NULL; + } + + managed->dl_tensor.data = PyArray_DATA(self); + managed->dl_tensor.device.device_type = kDLCPU; + managed->dl_tensor.device.device_id = 0; + managed->dl_tensor.dtype = managed_dtype; + + + int64_t *managed_shape_strides = PyMem_Malloc(sizeof(int64_t) * ndim * 2); + if (managed_shape_strides == NULL) { + PyErr_NoMemory(); + PyMem_Free(managed); + return NULL; + } + + int64_t *managed_shape = managed_shape_strides; + int64_t *managed_strides = managed_shape_strides + ndim; + for (int i = 0; i < ndim; ++i) { + managed_shape[i] = shape[i]; + // Strides in DLPack are items; in NumPy are bytes. + managed_strides[i] = strides[i] / itemsize; + } + + managed->dl_tensor.ndim = ndim; + managed->dl_tensor.shape = managed_shape; + managed->dl_tensor.strides = managed_strides; + managed->dl_tensor.byte_offset = 0; + managed->manager_ctx = self; + managed->deleter = array_dlpack_deleter; + + PyObject *capsule = PyCapsule_New(managed, NPY_DLPACK_CAPSULE_NAME, + array_dlpack_capsule_deleter); + if (capsule == NULL) { + PyMem_Free(managed); + PyMem_Free(managed_shape_strides); + return NULL; + } + + // the capsule holds a reference + PyArray_INCREF(self); + return capsule; +} + +static PyObject * +array_dlpack_device(PyArrayObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) +{ + return Py_BuildValue("ii", kDLCPU, 0); +>>>>>>> ENH: Add the __dlpack__ and __dlpack_device__ methods to ndarray. } NPY_NO_EXPORT PyMethodDef array_methods[] = { @@ -2989,5 +3142,15 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = { {"view", (PyCFunction)array_view, METH_FASTCALL | METH_KEYWORDS, NULL}, + + // For data interchange between libraries + {"__dlpack__", + (PyCFunction)array_dlpack, + METH_FASTCALL | METH_KEYWORDS, NULL}, + + {"__dlpack_device__", + (PyCFunction)array_dlpack_device, + METH_NOARGS, NULL}, + {NULL, NULL, 0, NULL} /* sentinel */ }; -- cgit v1.2.1 From 3163d57bd1550591e5f6ff1b063d4423d8d2123e Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Thu, 27 May 2021 12:03:55 +0200 Subject: ENH, TST: Add the from_dlpack method and test DLPack. --- numpy/core/_add_newdocs.py | 5 + numpy/core/multiarray.py | 9 +- numpy/core/numeric.py | 6 +- numpy/core/setup.py | 2 + numpy/core/src/common/npy_dlpack.h | 25 +++++ numpy/core/src/multiarray/methods.c | 25 +++-- numpy/core/src/multiarray/multiarraymodule.c | 153 +++++++++++++++++++++++++++ numpy/core/tests/test_dlpack.py | 76 +++++++++++++ 8 files changed, 287 insertions(+), 14 deletions(-) create mode 100644 numpy/core/src/common/npy_dlpack.h create mode 100644 numpy/core/tests/test_dlpack.py (limited to 'numpy') diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index c8a24db0c..a4c588a3b 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -1573,6 +1573,11 @@ add_newdoc('numpy.core.multiarray', 'frombuffer', array_function_like_doc, )) +add_newdoc('numpy.core.multiarray', 'from_dlpack', + """ + Create a NumPy array from a DLPack struct. + """) + add_newdoc('numpy.core', 'fastCopyAndTranspose', """_fastCopyAndTranspose(a)""") diff --git a/numpy/core/multiarray.py b/numpy/core/multiarray.py index 351cd3a1b..9f431f01b 100644 --- a/numpy/core/multiarray.py +++ b/numpy/core/multiarray.py @@ -31,10 +31,10 @@ __all__ = [ 'count_nonzero', 'c_einsum', 'datetime_as_string', 'datetime_data', 'dot', 'dragon4_positional', 'dragon4_scientific', 'dtype', 'empty', 'empty_like', 'error', 'flagsobj', 'flatiter', 'format_longfloat', - 'frombuffer', 'fromfile', 'fromiter', 'fromstring', 'get_handler_name', - 'inner', 'interp', 'interp_complex', 'is_busday', 'lexsort', - 'matmul', 'may_share_memory', 'min_scalar_type', 'ndarray', 'nditer', - 'nested_iters', 'normalize_axis_index', 'packbits', + 'frombuffer', 'from_dlpack', 'fromfile', 'fromiter', 'fromstring', + 'get_handler_name', 'inner', 'interp', 'interp_complex', 'is_busday', + 'lexsort', 'matmul', 'may_share_memory', 'min_scalar_type', 'ndarray', + 'nditer', 'nested_iters', 'normalize_axis_index', 'packbits', 'promote_types', 'putmask', 'ravel_multi_index', 'result_type', 'scalar', 'set_datetimeparse_function', 'set_legacy_print_mode', 'set_numeric_ops', 'set_string_function', 'set_typeDict', 'shares_memory', @@ -55,6 +55,7 @@ asfortranarray.__module__ = 'numpy' datetime_data.__module__ = 'numpy' empty.__module__ = 'numpy' frombuffer.__module__ = 'numpy' +from_dlpack.__module__ = 'numpy' fromfile.__module__ = 'numpy' fromiter.__module__ = 'numpy' frompyfunc.__module__ = 'numpy' diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 1654e8364..987470f92 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -13,8 +13,8 @@ from .multiarray import ( WRAP, arange, array, asarray, asanyarray, ascontiguousarray, asfortranarray, broadcast, can_cast, compare_chararrays, concatenate, copyto, dot, dtype, empty, - empty_like, flatiter, frombuffer, fromfile, fromiter, fromstring, - inner, lexsort, matmul, may_share_memory, + empty_like, flatiter, frombuffer, from_dlpack, fromfile, fromiter, + fromstring, inner, lexsort, matmul, may_share_memory, min_scalar_type, ndarray, nditer, nested_iters, promote_types, putmask, result_type, set_numeric_ops, shares_memory, vdot, where, zeros, normalize_axis_index) @@ -41,7 +41,7 @@ __all__ = [ 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', 'arange', 'array', 'asarray', 'asanyarray', 'ascontiguousarray', 'asfortranarray', 'zeros', 'count_nonzero', 'empty', 'broadcast', 'dtype', - 'fromstring', 'fromfile', 'frombuffer', 'where', + 'fromstring', 'fromfile', 'frombuffer', 'from_dlpack', 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', 'min_scalar_type', 'result_type', 'isfortran', 'empty_like', 'zeros_like', 'ones_like', diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 3e1ed4c9b..5c6fd4dcf 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -740,6 +740,7 @@ def configuration(parent_package='',top_path=None): ####################################################################### common_deps = [ + join('src', 'common', 'dlpack', 'dlpack.h'), join('src', 'common', 'array_assign.h'), join('src', 'common', 'binop_override.h'), join('src', 'common', 'cblasfuncs.h'), @@ -749,6 +750,7 @@ def configuration(parent_package='',top_path=None): join('src', 'common', 'npy_cblas.h'), join('src', 'common', 'npy_config.h'), join('src', 'common', 'npy_ctypes.h'), + join('src', 'common', 'npy_dlpack.h'), join('src', 'common', 'npy_extint128.h'), join('src', 'common', 'npy_import.h'), join('src', 'common', 'npy_hashtable.h'), diff --git a/numpy/core/src/common/npy_dlpack.h b/numpy/core/src/common/npy_dlpack.h new file mode 100644 index 000000000..407469058 --- /dev/null +++ b/numpy/core/src/common/npy_dlpack.h @@ -0,0 +1,25 @@ +#include "Python.h" +#include "dlpack/dlpack.h" + +#ifndef NPY_DLPACK_H +#define NPY_DLPACK_H + +#define NPY_DLPACK_CAPSULE_NAME "dltensor" +#define NPY_DLPACK_USED_CAPSULE_NAME "used_dltensor" +#define NPY_DLPACK_INTERNAL_CAPSULE_NAME "numpy_dltensor" + +static void array_dlpack_capsule_deleter(PyObject *self) +{ + if (!PyCapsule_IsValid(self, NPY_DLPACK_CAPSULE_NAME) && + !PyCapsule_IsValid(self, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { + if (!PyCapsule_IsValid(self, NPY_DLPACK_USED_CAPSULE_NAME)) { + PyErr_SetString(PyExc_RuntimeError, "Invalid capsule name."); + } + return; + } + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(self, PyCapsule_GetName(self)); + managed->deleter(managed); +} + +#endif \ No newline at end of file diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 2251d4b69..1f1a5d019 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -31,7 +31,9 @@ #include "alloc.h" #include + #include "common/dlpack/dlpack.h" +#include "common/npy_dlpack.h" /* NpyArg_ParseKeywords @@ -2582,8 +2584,6 @@ array_round(PyArrayObject *self, PyObject *args, PyObject *kwds) } } - - static PyObject * array_setflags(PyArrayObject *self, PyObject *args, PyObject *kwds) { @@ -2781,16 +2781,17 @@ static void array_dlpack_capsule_deleter(PyObject *self) managed->deleter(managed); } -static void array_dlpack_deleter(DLManagedTensor *self) +static void +array_dlpack_deleter(DLManagedTensor *self) { PyArrayObject *array = (PyArrayObject *)self->manager_ctx; // This will also free the strides as it's one allocation. PyMem_Free(self->dl_tensor.shape); PyMem_Free(self); - - PyArray_XDECREF(array); + Py_XDECREF(array); } + static PyObject * array_dlpack(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) @@ -2906,13 +2907,23 @@ array_dlpack(PyArrayObject *self, } // the capsule holds a reference - PyArray_INCREF(self); + Py_INCREF(self); return capsule; } static PyObject * -array_dlpack_device(PyArrayObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) +array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)) { + PyObject *base = PyArray_BASE(self); + if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { + DLManagedTensor *managed = PyCapsule_GetPointer(base, + NPY_DLPACK_INTERNAL_CAPSULE_NAME); + if (managed == NULL) { + return NULL; + } + return Py_BuildValue("ii", managed->dl_tensor.device.device_type, + managed->dl_tensor.device.device_id); + } return Py_BuildValue("ii", kDLCPU, 0); >>>>>>> ENH: Add the __dlpack__ and __dlpack_device__ methods to ndarray. } diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index dea828ed9..7414cb9a8 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -70,6 +70,8 @@ NPY_NO_EXPORT int NPY_NUMUSERTYPES = 0; #include "get_attr_string.h" #include "experimental_public_dtype_api.h" /* _get_experimental_dtype_api */ +#include "common/npy_dlpack.h" + /* ***************************************************************************** ** INCLUDE GENERATED CODE ** @@ -4231,6 +4233,155 @@ _reload_guard(PyObject *NPY_UNUSED(self)) { Py_RETURN_NONE; } +static void array_dlpack_deleter(DLManagedTensor *self) +{ + PyArrayObject *array = (PyArrayObject *)self->manager_ctx; + // This will also free the strides as it's one allocation. + PyMem_Free(self->dl_tensor.shape); + PyMem_Free(self); + Py_XDECREF(array); +} + + +NPY_NO_EXPORT PyObject * +from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { + PyObject *capsule = PyObject_CallMethod(obj, "__dlpack__", NULL); + if (capsule == NULL) { + return NULL; + } + + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(capsule, + NPY_DLPACK_CAPSULE_NAME); + + if (managed == NULL) { + Py_XDECREF(capsule); + return NULL; + } + + const int ndim = managed->dl_tensor.ndim; + if (ndim >= NPY_MAXDIMS) { + PyErr_SetString(PyExc_RuntimeError, + "maxdims of DLPack tensor is higher than the supported " + "maxdims."); + Py_XDECREF(capsule); + return NULL; + } + + if (managed->dl_tensor.device.device_type != kDLCPU && + managed->dl_tensor.device.device_type != kDLCUDAHost) { + PyErr_SetString(PyExc_RuntimeError, + "Unsupported device in DLTensor."); + Py_XDECREF(capsule); + return NULL; + } + + if (managed->dl_tensor.dtype.lanes != 1) { + PyErr_SetString(PyExc_RuntimeError, + "Unsupported lanes in DLTensor dtype."); + Py_XDECREF(capsule); + return NULL; + } + + int typenum = -1; + const uint8_t bits = managed->dl_tensor.dtype.bits; + const npy_intp itemsize = bits / 8; + switch(managed->dl_tensor.dtype.code) { + case kDLInt: + switch (bits) + { + case 8: typenum = NPY_INT8; break; + case 16: typenum = NPY_INT16; break; + case 32: typenum = NPY_INT32; break; + case 64: typenum = NPY_INT64; break; + } + break; + case kDLUInt: + switch (bits) + { + case 8: typenum = NPY_UINT8; break; + case 16: typenum = NPY_UINT16; break; + case 32: typenum = NPY_UINT32; break; + case 64: typenum = NPY_UINT64; break; + } + break; + case kDLFloat: + switch (bits) + { + case 16: typenum = NPY_FLOAT16; break; + case 32: typenum = NPY_FLOAT32; break; + case 64: typenum = NPY_FLOAT64; break; + } + break; + case kDLComplex: + switch (bits) + { + case 64: typenum = NPY_COMPLEX64; break; + case 128: typenum = NPY_COMPLEX128; break; + } + break; + } + + if (typenum == -1) { + PyErr_SetString(PyExc_RuntimeError, + "Unsupported dtype in DLTensor."); + Py_XDECREF(capsule); + return NULL; + } + + PyArray_Descr *descr = PyArray_DescrFromType(typenum); + if (descr == NULL) { + Py_XDECREF(capsule); + return NULL; + } + + npy_intp shape[NPY_MAXDIMS]; + npy_intp strides[NPY_MAXDIMS]; + + for (int i = 0; i < ndim; ++i) { + shape[i] = managed->dl_tensor.shape[i]; + strides[i] = managed->dl_tensor.strides[i] * itemsize; + } + + char *data = (char *)managed->dl_tensor.data + + managed->dl_tensor.byte_offset; + + PyObject *ret = PyArray_NewFromDescr(&PyArray_Type, descr, ndim, shape, + strides, data, 0, NULL); + if (ret == NULL) { + Py_XDECREF(capsule); + Py_XDECREF(descr); + return NULL; + } + + PyObject *new_capsule = PyCapsule_New(managed, + NPY_DLPACK_INTERNAL_CAPSULE_NAME, array_dlpack_capsule_deleter); + if (new_capsule == NULL) { + Py_XDECREF(descr); + Py_XDECREF(ret); + Py_XDECREF(capsule); + return NULL; + } + + if (PyArray_SetBaseObject((PyArrayObject *)ret, new_capsule) < 0) { + Py_XDECREF(descr); + Py_XDECREF(ret); + Py_XDECREF(new_capsule); + Py_XDECREF(capsule); + return NULL; + } + + if (PyCapsule_SetName(capsule, NPY_DLPACK_USED_CAPSULE_NAME) < 0) { + Py_XDECREF(descr); + Py_XDECREF(ret); + Py_XDECREF(new_capsule); + Py_XDECREF(capsule); + return NULL; + } + + Py_XDECREF(capsule); + return ret; +} static struct PyMethodDef array_module_methods[] = { {"_get_implementing_args", @@ -4445,6 +4596,8 @@ static struct PyMethodDef array_module_methods[] = { {"_reload_guard", (PyCFunction)_reload_guard, METH_NOARGS, "Give a warning on reload and big warning in sub-interpreters."}, + {"from_dlpack", (PyCFunction)from_dlpack, + METH_O, NULL}, {NULL, NULL, 0, NULL} /* sentinel */ }; diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py new file mode 100644 index 000000000..72fbab0de --- /dev/null +++ b/numpy/core/tests/test_dlpack.py @@ -0,0 +1,76 @@ +import sys +import pytest + +import numpy as np +from numpy.testing import assert_array_equal + + +class TestDLPack: + def test_dunder_dlpack_refcount(self): + x = np.arange(5) + y = x.__dlpack__() + assert sys.getrefcount(x) == 3 + del y + assert sys.getrefcount(x) == 2 + + def test_dunder_dlpack_stream(self): + x = np.arange(5) + x.__dlpack__(stream=None) + + dt = np.dtype([('int', np.int32), ('char', np.int8)]) + y = np.zeros((5,), dtype=dt) + z = y['int'] + + with pytest.raises(RuntimeError): + np.from_dlpack(z) + + def test_from_dlpack_refcount(self): + x = np.arange(5) + y = np.from_dlpack(x) + assert sys.getrefcount(x) == 3 + del y + assert sys.getrefcount(x) == 2 + + @pytest.mark.parametrize("dtype", [ + np.int8, np.int16, np.int32, np.int64, + np.uint8, np.uint16, np.uint32, np.uint64, + np.float16, np.float32, np.float64, + np.complex64, np.complex64 + ]) + def test_dtype_passthrough(self, dtype): + x = np.arange(5, dtype=dtype) + y = np.from_dlpack(x) + + assert y.dtype == x.dtype + assert_array_equal(x, y) + + def test_invalid_dtype(self): + x = np.asarray(np.datetime64('2021-05-27')) + + with pytest.raises(TypeError): + np.from_dlpack(x) + + def test_invalid_byte_swapping(self): + dt = np.dtype('=i8').newbyteorder() + x = np.arange(5, dtype=dt) + + with pytest.raises(TypeError): + np.from_dlpack(x) + + def test_non_contiguous(self): + x = np.arange(25).reshape((5, 5)) + + y1 = x[0] + assert_array_equal(y1, np.from_dlpack(y1)) + + y2 = x[:, 0] + assert_array_equal(y2, np.from_dlpack(y2)) + + y3 = x[1, :] + assert_array_equal(y3, np.from_dlpack(y3)) + + y4 = x[1] + assert_array_equal(y4, np.from_dlpack(y4)) + + y5 = np.diagonal(x) + assert_array_equal(y5, np.from_dlpack(y5)) -- cgit v1.2.1 From eb6ee260c94e14481325a0d5ef5db89bbd64349f Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Thu, 27 May 2021 17:26:40 +0200 Subject: MAINT, BUG: Documentation for DLPack protocol and refcounting bug fixes. --- numpy/__init__.pyi | 7 ++++++ numpy/core/_add_newdocs.py | 19 +++++++++++++++- numpy/core/src/common/dlpack/dlpack.h | 13 ++++++++++- numpy/core/src/common/npy_dlpack.h | 5 +++++ numpy/core/src/multiarray/multiarraymodule.c | 33 ++++++++++------------------ numpy/core/tests/test_dlpack.py | 6 +++-- 6 files changed, 58 insertions(+), 25 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index a162a637c..c808f0baf 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1413,6 +1413,7 @@ _SupportsBuffer = Union[ _T = TypeVar("_T") _T_co = TypeVar("_T_co", covariant=True) +_T_contra = TypeVar("_T_contra", contravariant=True) _2Tuple = Tuple[_T, _T] _CastingKind = L["no", "equiv", "safe", "same_kind", "unsafe"] @@ -4330,3 +4331,9 @@ class chararray(ndarray[_ShapeType, _CharDType]): # NOTE: Deprecated # class MachAr: ... + +class _SupportsDLPack(Protocol[_T_contra]): + def __dlpack__(self, *, stream: Optional[int] = ...) -> _PyCapsule: ... + +def from_dlpack(__obj: _SupportsDLPack[None]) -> NDArray[Any]: ... + diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index a4c588a3b..f1a42dffe 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -1575,7 +1575,15 @@ add_newdoc('numpy.core.multiarray', 'frombuffer', add_newdoc('numpy.core.multiarray', 'from_dlpack', """ - Create a NumPy array from a DLPack struct. + from_dlpack(x, /) + + Create a NumPy array from an object implementing the ``__dlpack__`` + protocol. + + See Also + -------- + `Array API documentation + `_ """) add_newdoc('numpy.core', 'fastCopyAndTranspose', @@ -2268,6 +2276,15 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('__array_priority__', add_newdoc('numpy.core.multiarray', 'ndarray', ('__array_struct__', """Array protocol: C-struct side.""")) +add_newdoc('numpy.core.multiarray', 'ndarray', ('__dlpack__', + """a.__dlpack__(*, stream=None) + + DLPack Protocol: Part of the Array API.""")) + +add_newdoc('numpy.core.multiarray', 'ndarray', ('__dlpack_device__', + """a.__dlpack_device__() + + DLPack Protocol: Part of the Array API.""")) add_newdoc('numpy.core.multiarray', 'ndarray', ('base', """ diff --git a/numpy/core/src/common/dlpack/dlpack.h b/numpy/core/src/common/dlpack/dlpack.h index 84afca248..8b19ea2b1 100644 --- a/numpy/core/src/common/dlpack/dlpack.h +++ b/numpy/core/src/common/dlpack/dlpack.h @@ -54,12 +54,20 @@ typedef enum { kDLVPI = 9, /*! \brief ROCm GPUs for AMD GPUs */ kDLROCM = 10, + /*! + * \brief Pinned ROCm CPU memory allocated by hipMallocHost + */ + kDLROCMHost = 11, /*! * \brief Reserved extension device type, * used for quickly test extension device * The semantics can differ depending on the implementation. */ kDLExtDev = 12, + /*! + * \brief CUDA managed/unified memory allocated by cudaMallocManaged + */ + kDLCUDAManaged = 13, } DLDeviceType; /*! @@ -68,7 +76,10 @@ typedef enum { typedef struct { /*! \brief The device type used in the device. */ DLDeviceType device_type; - /*! \brief The device index */ + /*! + * \brief The device index. + * For vanilla CPU memory, pinned memory, or managed memory, this is set to 0. + */ int device_id; } DLDevice; diff --git a/numpy/core/src/common/npy_dlpack.h b/numpy/core/src/common/npy_dlpack.h index 407469058..47559191e 100644 --- a/numpy/core/src/common/npy_dlpack.h +++ b/numpy/core/src/common/npy_dlpack.h @@ -4,8 +4,13 @@ #ifndef NPY_DLPACK_H #define NPY_DLPACK_H +// Part of the Array API specification. #define NPY_DLPACK_CAPSULE_NAME "dltensor" #define NPY_DLPACK_USED_CAPSULE_NAME "used_dltensor" + +// Used internally by NumPy to store a base object +// as it has to release a reference to the original +// capsule. #define NPY_DLPACK_INTERNAL_CAPSULE_NAME "numpy_dltensor" static void array_dlpack_capsule_deleter(PyObject *self) diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 7414cb9a8..e348a36cb 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4233,19 +4233,11 @@ _reload_guard(PyObject *NPY_UNUSED(self)) { Py_RETURN_NONE; } -static void array_dlpack_deleter(DLManagedTensor *self) -{ - PyArrayObject *array = (PyArrayObject *)self->manager_ctx; - // This will also free the strides as it's one allocation. - PyMem_Free(self->dl_tensor.shape); - PyMem_Free(self); - Py_XDECREF(array); -} - NPY_NO_EXPORT PyObject * from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { - PyObject *capsule = PyObject_CallMethod(obj, "__dlpack__", NULL); + PyObject *capsule = PyObject_CallMethod((PyObject *)obj->ob_type, + "__dlpack__", "O", obj); if (capsule == NULL) { return NULL; } @@ -4260,7 +4252,7 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { } const int ndim = managed->dl_tensor.ndim; - if (ndim >= NPY_MAXDIMS) { + if (ndim > NPY_MAXDIMS) { PyErr_SetString(PyExc_RuntimeError, "maxdims of DLPack tensor is higher than the supported " "maxdims."); @@ -4268,8 +4260,11 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { return NULL; } - if (managed->dl_tensor.device.device_type != kDLCPU && - managed->dl_tensor.device.device_type != kDLCUDAHost) { + DLDeviceType device_type = managed->dl_tensor.device.device_type; + if (device_type != kDLCPU && + device_type != kDLCUDAHost && + device_type != kDLROCMHost && + device_type != kDLCUDAManaged) { PyErr_SetString(PyExc_RuntimeError, "Unsupported device in DLTensor."); Py_XDECREF(capsule); @@ -4340,6 +4335,7 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { for (int i = 0; i < ndim; ++i) { shape[i] = managed->dl_tensor.shape[i]; + // DLPack has elements as stride units, NumPy has bytes. strides[i] = managed->dl_tensor.strides[i] * itemsize; } @@ -4357,25 +4353,20 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { PyObject *new_capsule = PyCapsule_New(managed, NPY_DLPACK_INTERNAL_CAPSULE_NAME, array_dlpack_capsule_deleter); if (new_capsule == NULL) { - Py_XDECREF(descr); - Py_XDECREF(ret); Py_XDECREF(capsule); + Py_XDECREF(ret); return NULL; } if (PyArray_SetBaseObject((PyArrayObject *)ret, new_capsule) < 0) { - Py_XDECREF(descr); - Py_XDECREF(ret); - Py_XDECREF(new_capsule); Py_XDECREF(capsule); + Py_XDECREF(ret); return NULL; } if (PyCapsule_SetName(capsule, NPY_DLPACK_USED_CAPSULE_NAME) < 0) { - Py_XDECREF(descr); - Py_XDECREF(ret); - Py_XDECREF(new_capsule); Py_XDECREF(capsule); + Py_XDECREF(ret); return NULL; } diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 72fbab0de..9f5cf9192 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -2,10 +2,11 @@ import sys import pytest import numpy as np -from numpy.testing import assert_array_equal +from numpy.testing import assert_array_equal, IS_PYPY class TestDLPack: + @pytest.mark.skipif(IS_PYPY, reason="PyPy can't get refcounts.") def test_dunder_dlpack_refcount(self): x = np.arange(5) y = x.__dlpack__() @@ -24,6 +25,7 @@ class TestDLPack: with pytest.raises(RuntimeError): np.from_dlpack(z) + @pytest.mark.skipif(IS_PYPY, reason="PyPy can't get refcounts.") def test_from_dlpack_refcount(self): x = np.arange(5) y = np.from_dlpack(x) @@ -35,7 +37,7 @@ class TestDLPack: np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64, np.float16, np.float32, np.float64, - np.complex64, np.complex64 + np.complex64, np.complex128 ]) def test_dtype_passthrough(self, dtype): x = np.arange(5, dtype=dtype) -- cgit v1.2.1 From 3e5b274164ee8b44ea881922019d61d4905b790f Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Thu, 27 May 2021 18:52:43 +0200 Subject: MAINT: Add URL to DLPack GitHub. --- numpy/core/src/common/dlpack/dlpack.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'numpy') diff --git a/numpy/core/src/common/dlpack/dlpack.h b/numpy/core/src/common/dlpack/dlpack.h index 8b19ea2b1..29209aee1 100644 --- a/numpy/core/src/common/dlpack/dlpack.h +++ b/numpy/core/src/common/dlpack/dlpack.h @@ -1,3 +1,5 @@ +// Taken from: +// https://github.com/dmlc/dlpack/blob/9b6176fdecb55e9bf39b16f08b96913ed3f275b4/include/dlpack/dlpack.h /*! * Copyright (c) 2017 by Contributors * \file dlpack.h -- cgit v1.2.1 From e12695d85bc98193014259ef4cf3ce46a750820f Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Thu, 27 May 2021 19:00:52 +0200 Subject: MAINT: Split up capsule deleter. --- numpy/core/src/common/npy_dlpack.h | 23 +++++++++++++++++------ numpy/core/src/multiarray/multiarraymodule.c | 3 ++- 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/common/npy_dlpack.h b/numpy/core/src/common/npy_dlpack.h index 47559191e..ef2c7c6fd 100644 --- a/numpy/core/src/common/npy_dlpack.h +++ b/numpy/core/src/common/npy_dlpack.h @@ -13,17 +13,28 @@ // capsule. #define NPY_DLPACK_INTERNAL_CAPSULE_NAME "numpy_dltensor" +/* This is exactly as mandated by dlpack */ static void array_dlpack_capsule_deleter(PyObject *self) { - if (!PyCapsule_IsValid(self, NPY_DLPACK_CAPSULE_NAME) && - !PyCapsule_IsValid(self, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { - if (!PyCapsule_IsValid(self, NPY_DLPACK_USED_CAPSULE_NAME)) { - PyErr_SetString(PyExc_RuntimeError, "Invalid capsule name."); - } + if (PyCapsule_IsValid(self, NPY_DLPACK_USED_CAPSULE_NAME)) { return; } DLManagedTensor *managed = - (DLManagedTensor *)PyCapsule_GetPointer(self, PyCapsule_GetName(self)); + (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_CAPSULE_NAME); + if (managed == NULL) { + return; + } + managed->deleter(managed); +} + +/* used internally */ +static void array_dlpack_internal_capsule_deleter(PyObject *self) +{ + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_INTERNAL_CAPSULE_NAME); + if (managed == NULL) { + return; + } managed->deleter(managed); } diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index e348a36cb..ad77177cd 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4351,7 +4351,8 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { } PyObject *new_capsule = PyCapsule_New(managed, - NPY_DLPACK_INTERNAL_CAPSULE_NAME, array_dlpack_capsule_deleter); + NPY_DLPACK_INTERNAL_CAPSULE_NAME, + array_dlpack_internal_capsule_deleter); if (new_capsule == NULL) { Py_XDECREF(capsule); Py_XDECREF(ret); -- cgit v1.2.1 From 094e0aac120519216a68244d241a5d3bc9497d52 Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Thu, 27 May 2021 19:20:37 +0200 Subject: MAINT: Move around code so that there are no more unused warnings. --- numpy/core/src/common/npy_dlpack.h | 25 ------------------------- numpy/core/src/multiarray/methods.c | 13 +++++++++++++ numpy/core/src/multiarray/multiarraymodule.c | 11 +++++++++++ 3 files changed, 24 insertions(+), 25 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/common/npy_dlpack.h b/numpy/core/src/common/npy_dlpack.h index ef2c7c6fd..446d44afa 100644 --- a/numpy/core/src/common/npy_dlpack.h +++ b/numpy/core/src/common/npy_dlpack.h @@ -13,29 +13,4 @@ // capsule. #define NPY_DLPACK_INTERNAL_CAPSULE_NAME "numpy_dltensor" -/* This is exactly as mandated by dlpack */ -static void array_dlpack_capsule_deleter(PyObject *self) -{ - if (PyCapsule_IsValid(self, NPY_DLPACK_USED_CAPSULE_NAME)) { - return; - } - DLManagedTensor *managed = - (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_CAPSULE_NAME); - if (managed == NULL) { - return; - } - managed->deleter(managed); -} - -/* used internally */ -static void array_dlpack_internal_capsule_deleter(PyObject *self) -{ - DLManagedTensor *managed = - (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_INTERNAL_CAPSULE_NAME); - if (managed == NULL) { - return; - } - managed->deleter(managed); -} - #endif \ No newline at end of file diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 1f1a5d019..0cc860514 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -2791,6 +2791,19 @@ array_dlpack_deleter(DLManagedTensor *self) Py_XDECREF(array); } +/* This is exactly as mandated by dlpack */ +static void array_dlpack_capsule_deleter(PyObject *self) +{ + if (PyCapsule_IsValid(self, NPY_DLPACK_USED_CAPSULE_NAME)) { + return; + } + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_CAPSULE_NAME); + if (managed == NULL) { + return; + } + managed->deleter(managed); +} static PyObject * array_dlpack(PyArrayObject *self, diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index ad77177cd..517234c88 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4233,6 +4233,17 @@ _reload_guard(PyObject *NPY_UNUSED(self)) { Py_RETURN_NONE; } +/* used internally */ +static void array_dlpack_internal_capsule_deleter(PyObject *self) +{ + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_INTERNAL_CAPSULE_NAME); + if (managed == NULL) { + return; + } + managed->deleter(managed); +} + NPY_NO_EXPORT PyObject * from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { -- cgit v1.2.1 From 158c7283b5de5ec7b832407c15fafacc95cb8ee6 Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Fri, 28 May 2021 09:17:08 +0200 Subject: TST: Improve testing coverage for DLPack. --- numpy/core/tests/test_dlpack.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'numpy') diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 9f5cf9192..4553c943d 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -18,6 +18,10 @@ class TestDLPack: x = np.arange(5) x.__dlpack__(stream=None) + with pytest.raises(RuntimeError): + x.__dlpack__(stream=1) + + def test_strides_not_multiple_of_itemsize(self): dt = np.dtype([('int', np.int32), ('char', np.int8)]) y = np.zeros((5,), dtype=dt) z = y['int'] @@ -76,3 +80,10 @@ class TestDLPack: y5 = np.diagonal(x) assert_array_equal(y5, np.from_dlpack(y5)) + + @pytest.mark.parametrize("ndim", range(33)) + def test_higher_dims(self, ndim): + shape = (1,) * ndim + x = np.zeros(shape, dtype=np.float64, order='C') + + assert shape == np.from_dlpack(x).shape -- cgit v1.2.1 From 9ebee26c6c5cd89623d531608eed25a770d01fff Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Fri, 28 May 2021 18:21:55 +0200 Subject: BUG: Fix handling of C-contiguous and 1-element arrays. --- numpy/core/src/multiarray/methods.c | 19 ++++++++++++------- numpy/core/src/multiarray/multiarraymodule.c | 7 +++++-- numpy/core/tests/test_dlpack.py | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 0cc860514..42464014c 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -2827,12 +2827,14 @@ array_dlpack(PyArrayObject *self, npy_intp *strides = PyArray_STRIDES(self); npy_intp *shape = PyArray_SHAPE(self); - for (int i = 0; i < ndim; ++i) { - if (strides[i] % itemsize != 0) { - PyErr_SetString(PyExc_RuntimeError, - "DLPack only supports strides which are a multiple of " - "itemsize."); - return NULL; + if (!PyArray_IS_C_CONTIGUOUS(self) && PyArray_SIZE(self) != 1) { + for (int i = 0; i < ndim; ++i) { + if (strides[i] % itemsize != 0) { + PyErr_SetString(PyExc_RuntimeError, + "DLPack only supports strides which are a multiple of " + "itemsize."); + return NULL; + } } } @@ -2906,7 +2908,10 @@ array_dlpack(PyArrayObject *self, managed->dl_tensor.ndim = ndim; managed->dl_tensor.shape = managed_shape; - managed->dl_tensor.strides = managed_strides; + managed->dl_tensor.strides = NULL; + if (PyArray_SIZE(self) != 1 && !PyArray_IS_C_CONTIGUOUS(self)) { + managed->dl_tensor.strides = managed_strides; + } managed->dl_tensor.byte_offset = 0; managed->manager_ctx = self; managed->deleter = array_dlpack_deleter; diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 517234c88..b04debe10 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4347,14 +4347,17 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { for (int i = 0; i < ndim; ++i) { shape[i] = managed->dl_tensor.shape[i]; // DLPack has elements as stride units, NumPy has bytes. - strides[i] = managed->dl_tensor.strides[i] * itemsize; + if (managed->dl_tensor.strides != NULL) + { + strides[i] = managed->dl_tensor.strides[i] * itemsize; + } } char *data = (char *)managed->dl_tensor.data + managed->dl_tensor.byte_offset; PyObject *ret = PyArray_NewFromDescr(&PyArray_Type, descr, ndim, shape, - strides, data, 0, NULL); + managed->dl_tensor.strides != NULL ? strides : NULL, data, 0, NULL); if (ret == NULL) { Py_XDECREF(capsule); Py_XDECREF(descr); diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 4553c943d..19f4e9281 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -84,6 +84,6 @@ class TestDLPack: @pytest.mark.parametrize("ndim", range(33)) def test_higher_dims(self, ndim): shape = (1,) * ndim - x = np.zeros(shape, dtype=np.float64, order='C') + x = np.zeros(shape, dtype=np.float64) assert shape == np.from_dlpack(x).shape -- cgit v1.2.1 From e83b8d8044763d36c42d3ab103a5437893fb09d8 Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Sun, 30 May 2021 11:57:46 +0200 Subject: BUG, TST: Device bugfix and test __dl_device__. --- numpy/__init__.pyi | 2 +- numpy/core/src/multiarray/methods.c | 39 ++++++++++++++++++++++++------------- numpy/core/tests/test_dlpack.py | 5 +++++ 3 files changed, 32 insertions(+), 14 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index c808f0baf..63e723a35 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -2449,7 +2449,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __dlpack__(self: NDArray[number[Any]], *, stream: None = ...) -> _PyCapsule: ... @overload - def __dlpack_device__(self) -> Tuple[L[1], L[0]]: ... + def __dlpack_device__(self) -> Tuple[int, L[0]]: ... # Keep `dtype` at the bottom to avoid name conflicts with `np.dtype` @property diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 42464014c..29372fe2f 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -2805,6 +2805,23 @@ static void array_dlpack_capsule_deleter(PyObject *self) managed->deleter(managed); } +static DLDevice +array_get_dl_device(PyArrayObject *self) { + DLDevice ret; + ret.device_type = kDLCPU; + ret.device_id = 0; + PyObject *base = PyArray_BASE(self); + if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { + DLManagedTensor *managed = PyCapsule_GetPointer( + base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); + if (managed == NULL) { + return ret; + } + return managed->dl_tensor.device; + } + return ret; +} + static PyObject * array_dlpack(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) @@ -2886,8 +2903,11 @@ array_dlpack(PyArrayObject *self, } managed->dl_tensor.data = PyArray_DATA(self); - managed->dl_tensor.device.device_type = kDLCPU; - managed->dl_tensor.device.device_id = 0; + managed->dl_tensor.device = array_get_dl_device(self); + if (PyErr_Occurred()) { + PyMem_Free(managed); + return NULL; + } managed->dl_tensor.dtype = managed_dtype; @@ -2932,18 +2952,11 @@ array_dlpack(PyArrayObject *self, static PyObject * array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)) { - PyObject *base = PyArray_BASE(self); - if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { - DLManagedTensor *managed = PyCapsule_GetPointer(base, - NPY_DLPACK_INTERNAL_CAPSULE_NAME); - if (managed == NULL) { - return NULL; - } - return Py_BuildValue("ii", managed->dl_tensor.device.device_type, - managed->dl_tensor.device.device_id); + DLDevice device = array_get_dl_device(self); + if (PyErr_Occurred()) { + return NULL; } - return Py_BuildValue("ii", kDLCPU, 0); ->>>>>>> ENH: Add the __dlpack__ and __dlpack_device__ methods to ndarray. + return Py_BuildValue("ii", device.device_type, device.device_id); } NPY_NO_EXPORT PyMethodDef array_methods[] = { diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 19f4e9281..926668c59 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -87,3 +87,8 @@ class TestDLPack: x = np.zeros(shape, dtype=np.float64) assert shape == np.from_dlpack(x).shape + + def test_dlpack_device(self): + x = np.arange(5) + assert x.__dlpack_device__() == (1, 0) + assert np.from_dlpack(x).__dlpack_device__() == (1, 0) -- cgit v1.2.1 From e167da747b53baed13a5148750c9c82746ffcb30 Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Mon, 31 May 2021 09:46:20 +0200 Subject: MAINT: Robustify dlpack_capsule_deleter and add comments. --- numpy/core/src/multiarray/methods.c | 33 +++++++++++++++++++++++++-------- numpy/core/tests/test_dlpack.py | 9 +++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 29372fe2f..9a16adecd 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -2792,25 +2792,42 @@ array_dlpack_deleter(DLManagedTensor *self) } /* This is exactly as mandated by dlpack */ -static void array_dlpack_capsule_deleter(PyObject *self) -{ - if (PyCapsule_IsValid(self, NPY_DLPACK_USED_CAPSULE_NAME)) { +static void dlpack_capsule_deleter(PyObject *self){ + if (PyCapsule_IsValid(self, "used_dltensor")) { return; } - DLManagedTensor *managed = - (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_CAPSULE_NAME); + + /* an exception may be in-flight, we must save it in case we create another one */ + PyObject *type, *value, *traceback; + PyErr_Fetch(&type, &value, &traceback); + + DLManagedTensor *managed = (DLManagedTensor *)PyCapsule_GetPointer(self, "dltensor"); if (managed == NULL) { - return; + PyErr_WriteUnraisable(self); + goto done; } - managed->deleter(managed); + /* the spec says the deleter can be NULL if there is no way for the caller to provide a reasonable destructor. */ + if (managed->deleter) { + managed->deleter(managed); + /* TODO: is the deleter allowed to set a python exception? */ + assert(!PyErr_Occurred()); + } + +done: + PyErr_Restore(type, value, traceback); } +// This function cannot return NULL, but it can fail, +// So call PyErr_Occurred to check if it failed after +// calling it. static DLDevice array_get_dl_device(PyArrayObject *self) { DLDevice ret; ret.device_type = kDLCPU; ret.device_id = 0; PyObject *base = PyArray_BASE(self); + // The outer if is due to the fact that NumPy arrays are on the CPU + // by default. if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { DLManagedTensor *managed = PyCapsule_GetPointer( base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); @@ -2937,7 +2954,7 @@ array_dlpack(PyArrayObject *self, managed->deleter = array_dlpack_deleter; PyObject *capsule = PyCapsule_New(managed, NPY_DLPACK_CAPSULE_NAME, - array_dlpack_capsule_deleter); + dlpack_capsule_deleter); if (capsule == NULL) { PyMem_Free(managed); PyMem_Free(managed_shape_strides); diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 926668c59..2561991e8 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -92,3 +92,12 @@ class TestDLPack: x = np.arange(5) assert x.__dlpack_device__() == (1, 0) assert np.from_dlpack(x).__dlpack_device__() == (1, 0) + + def dlpack_deleter_exception(self): + x = np.arange(5) + _ = x.__dlpack__() + raise RuntimeError + + def test_dlpack_destructor_exception(self): + with pytest.raises(RuntimeError): + self.dlpack_deleter_exception() -- cgit v1.2.1 From a4cc97abdc4e347b293886f4847d1ddd5a9bfaa7 Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Mon, 31 May 2021 14:32:48 +0200 Subject: BUG: Offset not properly stored/computed. --- numpy/core/src/multiarray/methods.c | 37 +++++++++++++++++++++------- numpy/core/src/multiarray/multiarraymodule.c | 2 +- 2 files changed, 29 insertions(+), 10 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 9a16adecd..d3162e549 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -2792,7 +2792,7 @@ array_dlpack_deleter(DLManagedTensor *self) } /* This is exactly as mandated by dlpack */ -static void dlpack_capsule_deleter(PyObject *self){ +static void dlpack_capsule_deleter(PyObject *self) { if (PyCapsule_IsValid(self, "used_dltensor")) { return; } @@ -2827,7 +2827,7 @@ array_get_dl_device(PyArrayObject *self) { ret.device_id = 0; PyObject *base = PyArray_BASE(self); // The outer if is due to the fact that NumPy arrays are on the CPU - // by default. + // by default (if not created from DLPack). if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { DLManagedTensor *managed = PyCapsule_GetPointer( base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); @@ -2839,6 +2839,20 @@ array_get_dl_device(PyArrayObject *self) { return ret; } +static char * +array_get_dl_data(PyArrayObject *self) { + PyObject *base = PyArray_BASE(self); + if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { + DLManagedTensor *managed = PyCapsule_GetPointer( + base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); + if (managed == NULL) { + return NULL; + } + return managed->dl_tensor.data; + } + return PyArray_DATA(self); +} + static PyObject * array_dlpack(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) @@ -2913,18 +2927,23 @@ array_dlpack(PyArrayObject *self, return NULL; } + DLDevice device = array_get_dl_device(self); + if (PyErr_Occurred()) { + return NULL; + } + char *data = array_get_dl_data(self); + if (data == NULL) { + return NULL; + } + DLManagedTensor *managed = PyMem_Malloc(sizeof(DLManagedTensor)); if (managed == NULL) { PyErr_NoMemory(); return NULL; } - managed->dl_tensor.data = PyArray_DATA(self); - managed->dl_tensor.device = array_get_dl_device(self); - if (PyErr_Occurred()) { - PyMem_Free(managed); - return NULL; - } + managed->dl_tensor.data = data; + managed->dl_tensor.device = device; managed->dl_tensor.dtype = managed_dtype; @@ -2949,7 +2968,7 @@ array_dlpack(PyArrayObject *self, if (PyArray_SIZE(self) != 1 && !PyArray_IS_C_CONTIGUOUS(self)) { managed->dl_tensor.strides = managed_strides; } - managed->dl_tensor.byte_offset = 0; + managed->dl_tensor.byte_offset = (char *)PyArray_DATA(self) - data; managed->manager_ctx = self; managed->deleter = array_dlpack_deleter; diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index b04debe10..60e3875fc 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4384,7 +4384,7 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { Py_XDECREF(ret); return NULL; } - + Py_XDECREF(capsule); return ret; } -- cgit v1.2.1 From f96aaa2ac484cab4e9f40b36902544e1174e0513 Mon Sep 17 00:00:00 2001 From: mattip Date: Mon, 1 Nov 2021 09:09:33 +0200 Subject: move dlpack functions to a new file --- numpy/core/code_generators/genapi.py | 1 + numpy/core/setup.py | 1 + numpy/core/src/common/npy_dlpack.h | 14 +- numpy/core/src/multiarray/dlpack.c | 382 +++++++++++++++++++++++++++ numpy/core/src/multiarray/dlpack.h | 18 ++ numpy/core/src/multiarray/methods.c | 238 +---------------- numpy/core/src/multiarray/multiarraymodule.c | 158 +---------- 7 files changed, 419 insertions(+), 393 deletions(-) create mode 100644 numpy/core/src/multiarray/dlpack.c create mode 100644 numpy/core/src/multiarray/dlpack.h (limited to 'numpy') diff --git a/numpy/core/code_generators/genapi.py b/numpy/core/code_generators/genapi.py index c2458c2b5..b401ee6a5 100644 --- a/numpy/core/code_generators/genapi.py +++ b/numpy/core/code_generators/genapi.py @@ -41,6 +41,7 @@ API_FILES = [join('multiarray', 'alloc.c'), join('multiarray', 'datetime_busdaycal.c'), join('multiarray', 'datetime_strings.c'), join('multiarray', 'descriptor.c'), + join('multiarray', 'dlpack.c'), join('multiarray', 'dtypemeta.c'), join('multiarray', 'einsum.c.src'), join('multiarray', 'flagsobject.c'), diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 5c6fd4dcf..2c99060ec 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -883,6 +883,7 @@ def configuration(parent_package='',top_path=None): join('src', 'multiarray', 'datetime_busday.c'), join('src', 'multiarray', 'datetime_busdaycal.c'), join('src', 'multiarray', 'descriptor.c'), + join('src', 'multiarray', 'dlpack.c'), join('src', 'multiarray', 'dtypemeta.c'), join('src', 'multiarray', 'dragon4.c'), join('src', 'multiarray', 'dtype_transfer.c'), diff --git a/numpy/core/src/common/npy_dlpack.h b/numpy/core/src/common/npy_dlpack.h index 446d44afa..cb926a262 100644 --- a/numpy/core/src/common/npy_dlpack.h +++ b/numpy/core/src/common/npy_dlpack.h @@ -13,4 +13,16 @@ // capsule. #define NPY_DLPACK_INTERNAL_CAPSULE_NAME "numpy_dltensor" -#endif \ No newline at end of file +PyObject * +array_dlpack(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, + PyObject *kwnames); + + +PyObject * +array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)); + + +NPY_NO_EXPORT PyObject * +from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj); + +#endif diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c new file mode 100644 index 000000000..591eddfaf --- /dev/null +++ b/numpy/core/src/multiarray/dlpack.c @@ -0,0 +1,382 @@ +#define NPY_NO_DEPRECATED_API NPY_API_VERSION +#define _MULTIARRAYMODULE + +#define PY_SSIZE_T_CLEAN +#include + +#include "numpy/arrayobject.h" +#include "common/npy_argparse.h" + +#include "common/dlpack/dlpack.h" +#include "common/npy_dlpack.h" + +static void +array_dlpack_deleter(DLManagedTensor *self) +{ + PyArrayObject *array = (PyArrayObject *)self->manager_ctx; + // This will also free the strides as it's one allocation. + PyMem_Free(self->dl_tensor.shape); + PyMem_Free(self); + Py_XDECREF(array); +} + +/* This is exactly as mandated by dlpack */ +static void dlpack_capsule_deleter(PyObject *self) { + if (PyCapsule_IsValid(self, "used_dltensor")) { + return; + } + + /* an exception may be in-flight, we must save it in case we create another one */ + PyObject *type, *value, *traceback; + PyErr_Fetch(&type, &value, &traceback); + + DLManagedTensor *managed = (DLManagedTensor *)PyCapsule_GetPointer(self, "dltensor"); + if (managed == NULL) { + PyErr_WriteUnraisable(self); + goto done; + } + /* the spec says the deleter can be NULL if there is no way for the caller to provide a reasonable destructor. */ + if (managed->deleter) { + managed->deleter(managed); + /* TODO: is the deleter allowed to set a python exception? */ + assert(!PyErr_Occurred()); + } + +done: + PyErr_Restore(type, value, traceback); +} + +// This function cannot return NULL, but it can fail, +// So call PyErr_Occurred to check if it failed after +// calling it. +static DLDevice +array_get_dl_device(PyArrayObject *self) { + DLDevice ret; + ret.device_type = kDLCPU; + ret.device_id = 0; + PyObject *base = PyArray_BASE(self); + // The outer if is due to the fact that NumPy arrays are on the CPU + // by default (if not created from DLPack). + if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { + DLManagedTensor *managed = PyCapsule_GetPointer( + base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); + if (managed == NULL) { + return ret; + } + return managed->dl_tensor.device; + } + return ret; +} + +static char * +array_get_dl_data(PyArrayObject *self) { + PyObject *base = PyArray_BASE(self); + if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { + DLManagedTensor *managed = PyCapsule_GetPointer( + base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); + if (managed == NULL) { + return NULL; + } + return managed->dl_tensor.data; + } + return PyArray_DATA(self); +} + +/* used internally */ +static void array_dlpack_internal_capsule_deleter(PyObject *self) +{ + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_INTERNAL_CAPSULE_NAME); + if (managed == NULL) { + return; + } + managed->deleter(managed); +} + +PyObject * +array_dlpack(PyArrayObject *self, + PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) +{ + PyObject *stream = Py_None; + NPY_PREPARE_ARGPARSER; + if (npy_parse_arguments("__dlpack__", args, len_args, kwnames, + "$stream", NULL, &stream, NULL, NULL, NULL)) { + return NULL; + } + + if (stream != Py_None) { + PyErr_SetString(PyExc_RuntimeError, "NumPy only supports " + "stream=None."); + return NULL; + } + + npy_intp itemsize = PyArray_ITEMSIZE(self); + int ndim = PyArray_NDIM(self); + npy_intp *strides = PyArray_STRIDES(self); + npy_intp *shape = PyArray_SHAPE(self); + + if (!PyArray_IS_C_CONTIGUOUS(self) && PyArray_SIZE(self) != 1) { + for (int i = 0; i < ndim; ++i) { + if (strides[i] % itemsize != 0) { + PyErr_SetString(PyExc_RuntimeError, + "DLPack only supports strides which are a multiple of " + "itemsize."); + return NULL; + } + } + } + + DLDataType managed_dtype; + PyArray_Descr *dtype = PyArray_DESCR(self); + + if (PyDataType_ISBYTESWAPPED(dtype)) { + PyErr_SetString(PyExc_TypeError, "DLPack only supports native " + "byte swapping."); + return NULL; + } + + managed_dtype.bits = 8 * itemsize; + managed_dtype.lanes = 1; + + if (PyDataType_ISSIGNED(dtype)) { + managed_dtype.code = kDLInt; + } else if (PyDataType_ISUNSIGNED(dtype)) { + managed_dtype.code = kDLUInt; + } else if (PyDataType_ISFLOAT(dtype)) { + // We can't be sure that the dtype is + // IEEE or padded. + if (itemsize > 8) { + PyErr_SetString(PyExc_TypeError, "DLPack only supports IEEE " + "floating point types without padding."); + return NULL; + } + managed_dtype.code = kDLFloat; + } else if (PyDataType_ISCOMPLEX(dtype)) { + // We can't be sure that the dtype is + // IEEE or padded. + if (itemsize > 16) { + PyErr_SetString(PyExc_TypeError, "DLPack only supports IEEE " + "complex point types without padding."); + return NULL; + } + managed_dtype.code = kDLComplex; + } else { + PyErr_SetString(PyExc_TypeError, + "DLPack only supports signed/unsigned integers, float " + "and complex dtypes."); + return NULL; + } + + DLDevice device = array_get_dl_device(self); + if (PyErr_Occurred()) { + return NULL; + } + char *data = array_get_dl_data(self); + if (data == NULL) { + return NULL; + } + + DLManagedTensor *managed = PyMem_Malloc(sizeof(DLManagedTensor)); + if (managed == NULL) { + PyErr_NoMemory(); + return NULL; + } + + managed->dl_tensor.data = data; + managed->dl_tensor.device = device; + managed->dl_tensor.dtype = managed_dtype; + + + int64_t *managed_shape_strides = PyMem_Malloc(sizeof(int64_t) * ndim * 2); + if (managed_shape_strides == NULL) { + PyErr_NoMemory(); + PyMem_Free(managed); + return NULL; + } + + int64_t *managed_shape = managed_shape_strides; + int64_t *managed_strides = managed_shape_strides + ndim; + for (int i = 0; i < ndim; ++i) { + managed_shape[i] = shape[i]; + // Strides in DLPack are items; in NumPy are bytes. + managed_strides[i] = strides[i] / itemsize; + } + + managed->dl_tensor.ndim = ndim; + managed->dl_tensor.shape = managed_shape; + managed->dl_tensor.strides = NULL; + if (PyArray_SIZE(self) != 1 && !PyArray_IS_C_CONTIGUOUS(self)) { + managed->dl_tensor.strides = managed_strides; + } + managed->dl_tensor.byte_offset = (char *)PyArray_DATA(self) - data; + managed->manager_ctx = self; + managed->deleter = array_dlpack_deleter; + + PyObject *capsule = PyCapsule_New(managed, NPY_DLPACK_CAPSULE_NAME, + dlpack_capsule_deleter); + if (capsule == NULL) { + PyMem_Free(managed); + PyMem_Free(managed_shape_strides); + return NULL; + } + + // the capsule holds a reference + Py_INCREF(self); + return capsule; +} + +PyObject * +array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)) +{ + DLDevice device = array_get_dl_device(self); + if (PyErr_Occurred()) { + return NULL; + } + return Py_BuildValue("ii", device.device_type, device.device_id); +} + +NPY_NO_EXPORT PyObject * +from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { + PyObject *capsule = PyObject_CallMethod((PyObject *)obj->ob_type, + "__dlpack__", "O", obj); + if (capsule == NULL) { + return NULL; + } + + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(capsule, + NPY_DLPACK_CAPSULE_NAME); + + if (managed == NULL) { + Py_XDECREF(capsule); + return NULL; + } + + const int ndim = managed->dl_tensor.ndim; + if (ndim > NPY_MAXDIMS) { + PyErr_SetString(PyExc_RuntimeError, + "maxdims of DLPack tensor is higher than the supported " + "maxdims."); + Py_XDECREF(capsule); + return NULL; + } + + DLDeviceType device_type = managed->dl_tensor.device.device_type; + if (device_type != kDLCPU && + device_type != kDLCUDAHost && + device_type != kDLROCMHost && + device_type != kDLCUDAManaged) { + PyErr_SetString(PyExc_RuntimeError, + "Unsupported device in DLTensor."); + Py_XDECREF(capsule); + return NULL; + } + + if (managed->dl_tensor.dtype.lanes != 1) { + PyErr_SetString(PyExc_RuntimeError, + "Unsupported lanes in DLTensor dtype."); + Py_XDECREF(capsule); + return NULL; + } + + int typenum = -1; + const uint8_t bits = managed->dl_tensor.dtype.bits; + const npy_intp itemsize = bits / 8; + switch(managed->dl_tensor.dtype.code) { + case kDLInt: + switch (bits) + { + case 8: typenum = NPY_INT8; break; + case 16: typenum = NPY_INT16; break; + case 32: typenum = NPY_INT32; break; + case 64: typenum = NPY_INT64; break; + } + break; + case kDLUInt: + switch (bits) + { + case 8: typenum = NPY_UINT8; break; + case 16: typenum = NPY_UINT16; break; + case 32: typenum = NPY_UINT32; break; + case 64: typenum = NPY_UINT64; break; + } + break; + case kDLFloat: + switch (bits) + { + case 16: typenum = NPY_FLOAT16; break; + case 32: typenum = NPY_FLOAT32; break; + case 64: typenum = NPY_FLOAT64; break; + } + break; + case kDLComplex: + switch (bits) + { + case 64: typenum = NPY_COMPLEX64; break; + case 128: typenum = NPY_COMPLEX128; break; + } + break; + } + + if (typenum == -1) { + PyErr_SetString(PyExc_RuntimeError, + "Unsupported dtype in DLTensor."); + Py_XDECREF(capsule); + return NULL; + } + + PyArray_Descr *descr = PyArray_DescrFromType(typenum); + if (descr == NULL) { + Py_XDECREF(capsule); + return NULL; + } + + npy_intp shape[NPY_MAXDIMS]; + npy_intp strides[NPY_MAXDIMS]; + + for (int i = 0; i < ndim; ++i) { + shape[i] = managed->dl_tensor.shape[i]; + // DLPack has elements as stride units, NumPy has bytes. + if (managed->dl_tensor.strides != NULL) + { + strides[i] = managed->dl_tensor.strides[i] * itemsize; + } + } + + char *data = (char *)managed->dl_tensor.data + + managed->dl_tensor.byte_offset; + + PyObject *ret = PyArray_NewFromDescr(&PyArray_Type, descr, ndim, shape, + managed->dl_tensor.strides != NULL ? strides : NULL, data, 0, NULL); + if (ret == NULL) { + Py_XDECREF(capsule); + Py_XDECREF(descr); + return NULL; + } + + PyObject *new_capsule = PyCapsule_New(managed, + NPY_DLPACK_INTERNAL_CAPSULE_NAME, + array_dlpack_internal_capsule_deleter); + if (new_capsule == NULL) { + Py_XDECREF(capsule); + Py_XDECREF(ret); + return NULL; + } + + if (PyArray_SetBaseObject((PyArrayObject *)ret, new_capsule) < 0) { + Py_XDECREF(capsule); + Py_XDECREF(ret); + return NULL; + } + + if (PyCapsule_SetName(capsule, NPY_DLPACK_USED_CAPSULE_NAME) < 0) { + Py_XDECREF(capsule); + Py_XDECREF(ret); + return NULL; + } + + Py_XDECREF(capsule); + return ret; +} + + diff --git a/numpy/core/src/multiarray/dlpack.h b/numpy/core/src/multiarray/dlpack.h new file mode 100644 index 000000000..7d4a289d9 --- /dev/null +++ b/numpy/core/src/multiarray/dlpack.h @@ -0,0 +1,18 @@ +#define NPY_NO_DEPRECATED_API NPY_API_VERSION +#define _MULTIARRAYMODULE + +#define PY_SSIZE_T_CLEAN +#include +#include "numpy/arrayobject.h" + +static PyObject * +array_dlpack(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, + PyObject *kwnames); + + +static PyObject * +array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)); + + +NPY_NO_EXPORT PyObject * +from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj); diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index d3162e549..f40db492c 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -26,15 +26,13 @@ #include "shape.h" #include "strfuncs.h" #include "array_assign.h" +#include "npy_dlpack.h" #include "methods.h" #include "alloc.h" #include -#include "common/dlpack/dlpack.h" -#include "common/npy_dlpack.h" - /* NpyArg_ParseKeywords * @@ -2584,6 +2582,8 @@ array_round(PyArrayObject *self, PyObject *args, PyObject *kwds) } } + + static PyObject * array_setflags(PyArrayObject *self, PyObject *args, PyObject *kwds) { @@ -2763,236 +2763,6 @@ array_class_getitem(PyObject *cls, PyObject *args) generic_alias = NULL; #endif return generic_alias; - -#define NPY_DLPACK_CAPSULE_NAME "dltensor" -#define NPY_DLPACK_USED_CAPSULE_NAME "used_dltensor" - -static void array_dlpack_capsule_deleter(PyObject *self) -{ - if (!PyCapsule_IsValid(self, NPY_DLPACK_CAPSULE_NAME)) { - if (!PyCapsule_IsValid(self, NPY_DLPACK_USED_CAPSULE_NAME)) { - PyErr_SetString(PyExc_RuntimeError, "Invalid capsule name."); - } - return; - } - - DLManagedTensor *managed = - (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_CAPSULE_NAME); - managed->deleter(managed); -} - -static void -array_dlpack_deleter(DLManagedTensor *self) -{ - PyArrayObject *array = (PyArrayObject *)self->manager_ctx; - // This will also free the strides as it's one allocation. - PyMem_Free(self->dl_tensor.shape); - PyMem_Free(self); - Py_XDECREF(array); -} - -/* This is exactly as mandated by dlpack */ -static void dlpack_capsule_deleter(PyObject *self) { - if (PyCapsule_IsValid(self, "used_dltensor")) { - return; - } - - /* an exception may be in-flight, we must save it in case we create another one */ - PyObject *type, *value, *traceback; - PyErr_Fetch(&type, &value, &traceback); - - DLManagedTensor *managed = (DLManagedTensor *)PyCapsule_GetPointer(self, "dltensor"); - if (managed == NULL) { - PyErr_WriteUnraisable(self); - goto done; - } - /* the spec says the deleter can be NULL if there is no way for the caller to provide a reasonable destructor. */ - if (managed->deleter) { - managed->deleter(managed); - /* TODO: is the deleter allowed to set a python exception? */ - assert(!PyErr_Occurred()); - } - -done: - PyErr_Restore(type, value, traceback); -} - -// This function cannot return NULL, but it can fail, -// So call PyErr_Occurred to check if it failed after -// calling it. -static DLDevice -array_get_dl_device(PyArrayObject *self) { - DLDevice ret; - ret.device_type = kDLCPU; - ret.device_id = 0; - PyObject *base = PyArray_BASE(self); - // The outer if is due to the fact that NumPy arrays are on the CPU - // by default (if not created from DLPack). - if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { - DLManagedTensor *managed = PyCapsule_GetPointer( - base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); - if (managed == NULL) { - return ret; - } - return managed->dl_tensor.device; - } - return ret; -} - -static char * -array_get_dl_data(PyArrayObject *self) { - PyObject *base = PyArray_BASE(self); - if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { - DLManagedTensor *managed = PyCapsule_GetPointer( - base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); - if (managed == NULL) { - return NULL; - } - return managed->dl_tensor.data; - } - return PyArray_DATA(self); -} - -static PyObject * -array_dlpack(PyArrayObject *self, - PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) -{ - PyObject *stream = Py_None; - NPY_PREPARE_ARGPARSER; - if (npy_parse_arguments("__dlpack__", args, len_args, kwnames, - "$stream", NULL, &stream, NULL, NULL, NULL)) { - return NULL; - } - - if (stream != Py_None) { - PyErr_SetString(PyExc_RuntimeError, "NumPy only supports " - "stream=None."); - return NULL; - } - - npy_intp itemsize = PyArray_ITEMSIZE(self); - int ndim = PyArray_NDIM(self); - npy_intp *strides = PyArray_STRIDES(self); - npy_intp *shape = PyArray_SHAPE(self); - - if (!PyArray_IS_C_CONTIGUOUS(self) && PyArray_SIZE(self) != 1) { - for (int i = 0; i < ndim; ++i) { - if (strides[i] % itemsize != 0) { - PyErr_SetString(PyExc_RuntimeError, - "DLPack only supports strides which are a multiple of " - "itemsize."); - return NULL; - } - } - } - - DLDataType managed_dtype; - PyArray_Descr *dtype = PyArray_DESCR(self); - - if (PyDataType_ISBYTESWAPPED(dtype)) { - PyErr_SetString(PyExc_TypeError, "DLPack only supports native " - "byte swapping."); - return NULL; - } - - managed_dtype.bits = 8 * itemsize; - managed_dtype.lanes = 1; - - if (PyDataType_ISSIGNED(dtype)) { - managed_dtype.code = kDLInt; - } else if (PyDataType_ISUNSIGNED(dtype)) { - managed_dtype.code = kDLUInt; - } else if (PyDataType_ISFLOAT(dtype)) { - // We can't be sure that the dtype is - // IEEE or padded. - if (itemsize > 8) { - PyErr_SetString(PyExc_TypeError, "DLPack only supports IEEE " - "floating point types without padding."); - return NULL; - } - managed_dtype.code = kDLFloat; - } else if (PyDataType_ISCOMPLEX(dtype)) { - // We can't be sure that the dtype is - // IEEE or padded. - if (itemsize > 16) { - PyErr_SetString(PyExc_TypeError, "DLPack only supports IEEE " - "complex point types without padding."); - return NULL; - } - managed_dtype.code = kDLComplex; - } else { - PyErr_SetString(PyExc_TypeError, - "DLPack only supports signed/unsigned integers, float " - "and complex dtypes."); - return NULL; - } - - DLDevice device = array_get_dl_device(self); - if (PyErr_Occurred()) { - return NULL; - } - char *data = array_get_dl_data(self); - if (data == NULL) { - return NULL; - } - - DLManagedTensor *managed = PyMem_Malloc(sizeof(DLManagedTensor)); - if (managed == NULL) { - PyErr_NoMemory(); - return NULL; - } - - managed->dl_tensor.data = data; - managed->dl_tensor.device = device; - managed->dl_tensor.dtype = managed_dtype; - - - int64_t *managed_shape_strides = PyMem_Malloc(sizeof(int64_t) * ndim * 2); - if (managed_shape_strides == NULL) { - PyErr_NoMemory(); - PyMem_Free(managed); - return NULL; - } - - int64_t *managed_shape = managed_shape_strides; - int64_t *managed_strides = managed_shape_strides + ndim; - for (int i = 0; i < ndim; ++i) { - managed_shape[i] = shape[i]; - // Strides in DLPack are items; in NumPy are bytes. - managed_strides[i] = strides[i] / itemsize; - } - - managed->dl_tensor.ndim = ndim; - managed->dl_tensor.shape = managed_shape; - managed->dl_tensor.strides = NULL; - if (PyArray_SIZE(self) != 1 && !PyArray_IS_C_CONTIGUOUS(self)) { - managed->dl_tensor.strides = managed_strides; - } - managed->dl_tensor.byte_offset = (char *)PyArray_DATA(self) - data; - managed->manager_ctx = self; - managed->deleter = array_dlpack_deleter; - - PyObject *capsule = PyCapsule_New(managed, NPY_DLPACK_CAPSULE_NAME, - dlpack_capsule_deleter); - if (capsule == NULL) { - PyMem_Free(managed); - PyMem_Free(managed_shape_strides); - return NULL; - } - - // the capsule holds a reference - Py_INCREF(self); - return capsule; -} - -static PyObject * -array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)) -{ - DLDevice device = array_get_dl_device(self); - if (PyErr_Occurred()) { - return NULL; - } - return Py_BuildValue("ii", device.device_type, device.device_id); } NPY_NO_EXPORT PyMethodDef array_methods[] = { @@ -3220,7 +2990,6 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = { {"view", (PyCFunction)array_view, METH_FASTCALL | METH_KEYWORDS, NULL}, - // For data interchange between libraries {"__dlpack__", (PyCFunction)array_dlpack, @@ -3229,6 +2998,5 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = { {"__dlpack_device__", (PyCFunction)array_dlpack_device, METH_NOARGS, NULL}, - {NULL, NULL, 0, NULL} /* sentinel */ }; diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 60e3875fc..ac4c1ae0b 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -70,7 +70,7 @@ NPY_NO_EXPORT int NPY_NUMUSERTYPES = 0; #include "get_attr_string.h" #include "experimental_public_dtype_api.h" /* _get_experimental_dtype_api */ -#include "common/npy_dlpack.h" +#include "npy_dlpack.h" /* ***************************************************************************** @@ -4233,162 +4233,6 @@ _reload_guard(PyObject *NPY_UNUSED(self)) { Py_RETURN_NONE; } -/* used internally */ -static void array_dlpack_internal_capsule_deleter(PyObject *self) -{ - DLManagedTensor *managed = - (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_INTERNAL_CAPSULE_NAME); - if (managed == NULL) { - return; - } - managed->deleter(managed); -} - - -NPY_NO_EXPORT PyObject * -from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { - PyObject *capsule = PyObject_CallMethod((PyObject *)obj->ob_type, - "__dlpack__", "O", obj); - if (capsule == NULL) { - return NULL; - } - - DLManagedTensor *managed = - (DLManagedTensor *)PyCapsule_GetPointer(capsule, - NPY_DLPACK_CAPSULE_NAME); - - if (managed == NULL) { - Py_XDECREF(capsule); - return NULL; - } - - const int ndim = managed->dl_tensor.ndim; - if (ndim > NPY_MAXDIMS) { - PyErr_SetString(PyExc_RuntimeError, - "maxdims of DLPack tensor is higher than the supported " - "maxdims."); - Py_XDECREF(capsule); - return NULL; - } - - DLDeviceType device_type = managed->dl_tensor.device.device_type; - if (device_type != kDLCPU && - device_type != kDLCUDAHost && - device_type != kDLROCMHost && - device_type != kDLCUDAManaged) { - PyErr_SetString(PyExc_RuntimeError, - "Unsupported device in DLTensor."); - Py_XDECREF(capsule); - return NULL; - } - - if (managed->dl_tensor.dtype.lanes != 1) { - PyErr_SetString(PyExc_RuntimeError, - "Unsupported lanes in DLTensor dtype."); - Py_XDECREF(capsule); - return NULL; - } - - int typenum = -1; - const uint8_t bits = managed->dl_tensor.dtype.bits; - const npy_intp itemsize = bits / 8; - switch(managed->dl_tensor.dtype.code) { - case kDLInt: - switch (bits) - { - case 8: typenum = NPY_INT8; break; - case 16: typenum = NPY_INT16; break; - case 32: typenum = NPY_INT32; break; - case 64: typenum = NPY_INT64; break; - } - break; - case kDLUInt: - switch (bits) - { - case 8: typenum = NPY_UINT8; break; - case 16: typenum = NPY_UINT16; break; - case 32: typenum = NPY_UINT32; break; - case 64: typenum = NPY_UINT64; break; - } - break; - case kDLFloat: - switch (bits) - { - case 16: typenum = NPY_FLOAT16; break; - case 32: typenum = NPY_FLOAT32; break; - case 64: typenum = NPY_FLOAT64; break; - } - break; - case kDLComplex: - switch (bits) - { - case 64: typenum = NPY_COMPLEX64; break; - case 128: typenum = NPY_COMPLEX128; break; - } - break; - } - - if (typenum == -1) { - PyErr_SetString(PyExc_RuntimeError, - "Unsupported dtype in DLTensor."); - Py_XDECREF(capsule); - return NULL; - } - - PyArray_Descr *descr = PyArray_DescrFromType(typenum); - if (descr == NULL) { - Py_XDECREF(capsule); - return NULL; - } - - npy_intp shape[NPY_MAXDIMS]; - npy_intp strides[NPY_MAXDIMS]; - - for (int i = 0; i < ndim; ++i) { - shape[i] = managed->dl_tensor.shape[i]; - // DLPack has elements as stride units, NumPy has bytes. - if (managed->dl_tensor.strides != NULL) - { - strides[i] = managed->dl_tensor.strides[i] * itemsize; - } - } - - char *data = (char *)managed->dl_tensor.data + - managed->dl_tensor.byte_offset; - - PyObject *ret = PyArray_NewFromDescr(&PyArray_Type, descr, ndim, shape, - managed->dl_tensor.strides != NULL ? strides : NULL, data, 0, NULL); - if (ret == NULL) { - Py_XDECREF(capsule); - Py_XDECREF(descr); - return NULL; - } - - PyObject *new_capsule = PyCapsule_New(managed, - NPY_DLPACK_INTERNAL_CAPSULE_NAME, - array_dlpack_internal_capsule_deleter); - if (new_capsule == NULL) { - Py_XDECREF(capsule); - Py_XDECREF(ret); - return NULL; - } - - if (PyArray_SetBaseObject((PyArrayObject *)ret, new_capsule) < 0) { - Py_XDECREF(capsule); - Py_XDECREF(ret); - return NULL; - } - - if (PyCapsule_SetName(capsule, NPY_DLPACK_USED_CAPSULE_NAME) < 0) { - Py_XDECREF(capsule); - Py_XDECREF(ret); - return NULL; - } - - Py_XDECREF(capsule); - return ret; -} - static struct PyMethodDef array_module_methods[] = { {"_get_implementing_args", (PyCFunction)array__get_implementing_args, -- cgit v1.2.1 From 0c992dca1cc23f12af14a8d66101166ef6c92355 Mon Sep 17 00:00:00 2001 From: mattip Date: Mon, 1 Nov 2021 09:15:18 +0200 Subject: BUG: fixes from review --- numpy/core/src/multiarray/dlpack.c | 104 +++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 40 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c index 591eddfaf..f1591bb1f 100644 --- a/numpy/core/src/multiarray/dlpack.c +++ b/numpy/core/src/multiarray/dlpack.c @@ -22,7 +22,7 @@ array_dlpack_deleter(DLManagedTensor *self) /* This is exactly as mandated by dlpack */ static void dlpack_capsule_deleter(PyObject *self) { - if (PyCapsule_IsValid(self, "used_dltensor")) { + if (PyCapsule_IsValid(self, NPY_DLPACK_USED_CAPSULE_NAME)) { return; } @@ -30,12 +30,16 @@ static void dlpack_capsule_deleter(PyObject *self) { PyObject *type, *value, *traceback; PyErr_Fetch(&type, &value, &traceback); - DLManagedTensor *managed = (DLManagedTensor *)PyCapsule_GetPointer(self, "dltensor"); + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_CAPSULE_NAME); if (managed == NULL) { PyErr_WriteUnraisable(self); goto done; } - /* the spec says the deleter can be NULL if there is no way for the caller to provide a reasonable destructor. */ + /* + * the spec says the deleter can be NULL if there is no way for the caller + * to provide a reasonable destructor. + */ if (managed->deleter) { managed->deleter(managed); /* TODO: is the deleter allowed to set a python exception? */ @@ -46,6 +50,34 @@ done: PyErr_Restore(type, value, traceback); } +/* used internally, almost identical to dlpack_capsule_deleter() */ +static void array_dlpack_internal_capsule_deleter(PyObject *self) +{ + /* an exception may be in-flight, we must save it in case we create another one */ + PyObject *type, *value, *traceback; + PyErr_Fetch(&type, &value, &traceback); + + DLManagedTensor *managed = + (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_INTERNAL_CAPSULE_NAME); + if (managed == NULL) { + PyErr_WriteUnraisable(self); + goto done; + } + /* + * the spec says the deleter can be NULL if there is no way for the caller + * to provide a reasonable destructor. + */ + if (managed->deleter) { + managed->deleter(managed); + /* TODO: is the deleter allowed to set a python exception? */ + assert(!PyErr_Occurred()); + } + +done: + PyErr_Restore(type, value, traceback); +} + + // This function cannot return NULL, but it can fail, // So call PyErr_Occurred to check if it failed after // calling it. @@ -82,17 +114,6 @@ array_get_dl_data(PyArrayObject *self) { return PyArray_DATA(self); } -/* used internally */ -static void array_dlpack_internal_capsule_deleter(PyObject *self) -{ - DLManagedTensor *managed = - (DLManagedTensor *)PyCapsule_GetPointer(self, NPY_DLPACK_INTERNAL_CAPSULE_NAME); - if (managed == NULL) { - return; - } - managed->deleter(managed); -} - PyObject * array_dlpack(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) @@ -140,9 +161,11 @@ array_dlpack(PyArrayObject *self, if (PyDataType_ISSIGNED(dtype)) { managed_dtype.code = kDLInt; - } else if (PyDataType_ISUNSIGNED(dtype)) { + } + else if (PyDataType_ISUNSIGNED(dtype)) { managed_dtype.code = kDLUInt; - } else if (PyDataType_ISFLOAT(dtype)) { + } + else if (PyDataType_ISFLOAT(dtype)) { // We can't be sure that the dtype is // IEEE or padded. if (itemsize > 8) { @@ -151,7 +174,8 @@ array_dlpack(PyArrayObject *self, return NULL; } managed_dtype.code = kDLFloat; - } else if (PyDataType_ISCOMPLEX(dtype)) { + } + else if (PyDataType_ISCOMPLEX(dtype)) { // We can't be sure that the dtype is // IEEE or padded. if (itemsize > 16) { @@ -160,7 +184,8 @@ array_dlpack(PyArrayObject *self, return NULL; } managed_dtype.code = kDLComplex; - } else { + } + else { PyErr_SetString(PyExc_TypeError, "DLPack only supports signed/unsigned integers, float " "and complex dtypes."); @@ -243,12 +268,12 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { return NULL; } - DLManagedTensor *managed = + DLManagedTensor *managed = (DLManagedTensor *)PyCapsule_GetPointer(capsule, NPY_DLPACK_CAPSULE_NAME); if (managed == NULL) { - Py_XDECREF(capsule); + Py_DECREF(capsule); return NULL; } @@ -257,7 +282,7 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { PyErr_SetString(PyExc_RuntimeError, "maxdims of DLPack tensor is higher than the supported " "maxdims."); - Py_XDECREF(capsule); + Py_DECREF(capsule); return NULL; } @@ -268,14 +293,14 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { device_type != kDLCUDAManaged) { PyErr_SetString(PyExc_RuntimeError, "Unsupported device in DLTensor."); - Py_XDECREF(capsule); + Py_DECREF(capsule); return NULL; } if (managed->dl_tensor.dtype.lanes != 1) { PyErr_SetString(PyExc_RuntimeError, "Unsupported lanes in DLTensor dtype."); - Py_XDECREF(capsule); + Py_DECREF(capsule); return NULL; } @@ -321,13 +346,7 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { if (typenum == -1) { PyErr_SetString(PyExc_RuntimeError, "Unsupported dtype in DLTensor."); - Py_XDECREF(capsule); - return NULL; - } - - PyArray_Descr *descr = PyArray_DescrFromType(typenum); - if (descr == NULL) { - Py_XDECREF(capsule); + Py_DECREF(capsule); return NULL; } @@ -346,11 +365,16 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { char *data = (char *)managed->dl_tensor.data + managed->dl_tensor.byte_offset; + PyArray_Descr *descr = PyArray_DescrFromType(typenum); + if (descr == NULL) { + Py_DECREF(capsule); + return NULL; + } + PyObject *ret = PyArray_NewFromDescr(&PyArray_Type, descr, ndim, shape, managed->dl_tensor.strides != NULL ? strides : NULL, data, 0, NULL); if (ret == NULL) { - Py_XDECREF(capsule); - Py_XDECREF(descr); + Py_DECREF(capsule); return NULL; } @@ -358,24 +382,24 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { NPY_DLPACK_INTERNAL_CAPSULE_NAME, array_dlpack_internal_capsule_deleter); if (new_capsule == NULL) { - Py_XDECREF(capsule); - Py_XDECREF(ret); + Py_DECREF(capsule); + Py_DECREF(ret); return NULL; } if (PyArray_SetBaseObject((PyArrayObject *)ret, new_capsule) < 0) { - Py_XDECREF(capsule); - Py_XDECREF(ret); + Py_DECREF(capsule); + Py_DECREF(ret); return NULL; } if (PyCapsule_SetName(capsule, NPY_DLPACK_USED_CAPSULE_NAME) < 0) { - Py_XDECREF(capsule); - Py_XDECREF(ret); + Py_DECREF(capsule); + Py_DECREF(ret); return NULL; } - - Py_XDECREF(capsule); + + Py_DECREF(capsule); return ret; } -- cgit v1.2.1 From ef04f59d1a5cc7320866c48d75059aa52d390003 Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Tue, 2 Nov 2021 09:50:33 +0200 Subject: Updates Co-authored-by: Sebastian Berg Co-authored-by: Bas van Beek <43369155+BvB93@users.noreply.github.com> --- numpy/__init__.pyi | 2 +- numpy/core/src/multiarray/dlpack.c | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 63e723a35..1562ce89e 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -4333,7 +4333,7 @@ class chararray(ndarray[_ShapeType, _CharDType]): # class MachAr: ... class _SupportsDLPack(Protocol[_T_contra]): - def __dlpack__(self, *, stream: Optional[int] = ...) -> _PyCapsule: ... + def __dlpack__(self, *, stream: None | _T_contra = ...) -> _PyCapsule: ... def from_dlpack(__obj: _SupportsDLPack[None]) -> NDArray[Any]: ... diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c index f1591bb1f..9de304379 100644 --- a/numpy/core/src/multiarray/dlpack.c +++ b/numpy/core/src/multiarray/dlpack.c @@ -211,7 +211,6 @@ array_dlpack(PyArrayObject *self, managed->dl_tensor.device = device; managed->dl_tensor.dtype = managed_dtype; - int64_t *managed_shape_strides = PyMem_Malloc(sizeof(int64_t) * ndim * 2); if (managed_shape_strides == NULL) { PyErr_NoMemory(); @@ -307,7 +306,7 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { int typenum = -1; const uint8_t bits = managed->dl_tensor.dtype.bits; const npy_intp itemsize = bits / 8; - switch(managed->dl_tensor.dtype.code) { + switch (managed->dl_tensor.dtype.code) { case kDLInt: switch (bits) { @@ -356,8 +355,7 @@ from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { for (int i = 0; i < ndim; ++i) { shape[i] = managed->dl_tensor.shape[i]; // DLPack has elements as stride units, NumPy has bytes. - if (managed->dl_tensor.strides != NULL) - { + if (managed->dl_tensor.strides != NULL) { strides[i] = managed->dl_tensor.strides[i] * itemsize; } } -- cgit v1.2.1 From ba8fcbe2ba0a26cd52dfa9bf40dd2e945e5b298f Mon Sep 17 00:00:00 2001 From: mattip Date: Tue, 2 Nov 2021 11:30:32 +0200 Subject: change from_dlpack to _dlpack, remove unused header --- numpy/__init__.pyi | 2 +- numpy/array_api/__init__.py | 4 ++-- numpy/array_api/_creation_functions.py | 2 +- numpy/core/_add_newdocs.py | 4 ++-- numpy/core/multiarray.py | 17 +++++++++-------- numpy/core/numeric.py | 4 ++-- numpy/core/src/common/npy_dlpack.h | 2 +- numpy/core/src/multiarray/dlpack.c | 2 +- numpy/core/src/multiarray/dlpack.h | 18 ------------------ numpy/core/src/multiarray/multiarraymodule.c | 2 +- numpy/core/tests/test_dlpack.py | 24 ++++++++++++------------ 11 files changed, 32 insertions(+), 49 deletions(-) delete mode 100644 numpy/core/src/multiarray/dlpack.h (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 1562ce89e..b2e9eec77 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -4335,5 +4335,5 @@ class chararray(ndarray[_ShapeType, _CharDType]): class _SupportsDLPack(Protocol[_T_contra]): def __dlpack__(self, *, stream: None | _T_contra = ...) -> _PyCapsule: ... -def from_dlpack(__obj: _SupportsDLPack[None]) -> NDArray[Any]: ... +def _from_dlpack(__obj: _SupportsDLPack[None]) -> NDArray[Any]: ... diff --git a/numpy/array_api/__init__.py b/numpy/array_api/__init__.py index d8b29057e..89f5e9cba 100644 --- a/numpy/array_api/__init__.py +++ b/numpy/array_api/__init__.py @@ -136,7 +136,7 @@ from ._creation_functions import ( empty, empty_like, eye, - from_dlpack, + _from_dlpack, full, full_like, linspace, @@ -155,7 +155,7 @@ __all__ += [ "empty", "empty_like", "eye", - "from_dlpack", + "_from_dlpack", "full", "full_like", "linspace", diff --git a/numpy/array_api/_creation_functions.py b/numpy/array_api/_creation_functions.py index e36807468..c3644ac2c 100644 --- a/numpy/array_api/_creation_functions.py +++ b/numpy/array_api/_creation_functions.py @@ -151,7 +151,7 @@ def eye( return Array._new(np.eye(n_rows, M=n_cols, k=k, dtype=dtype)) -def from_dlpack(x: object, /) -> Array: +def _from_dlpack(x: object, /) -> Array: # Note: dlpack support is not yet implemented on Array raise NotImplementedError("DLPack support is not yet implemented") diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index f1a42dffe..cae5bc281 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -1573,9 +1573,9 @@ add_newdoc('numpy.core.multiarray', 'frombuffer', array_function_like_doc, )) -add_newdoc('numpy.core.multiarray', 'from_dlpack', +add_newdoc('numpy.core.multiarray', '_from_dlpack', """ - from_dlpack(x, /) + _from_dlpack(x, /) Create a NumPy array from an object implementing the ``__dlpack__`` protocol. diff --git a/numpy/core/multiarray.py b/numpy/core/multiarray.py index 9f431f01b..f96274263 100644 --- a/numpy/core/multiarray.py +++ b/numpy/core/multiarray.py @@ -14,8 +14,9 @@ from ._multiarray_umath import * # noqa: F403 # do not change them. issue gh-15518 # _get_ndarray_c_version is semi-public, on purpose not added to __all__ from ._multiarray_umath import ( - _fastCopyAndTranspose, _flagdict, _insert, _reconstruct, _vec_string, - _ARRAY_API, _monotonicity, _get_ndarray_c_version, _set_madvise_hugepage, + _fastCopyAndTranspose, _flagdict, _from_dlpack, _insert, _reconstruct, + _vec_string, _ARRAY_API, _monotonicity, _get_ndarray_c_version, + _set_madvise_hugepage, ) __all__ = [ @@ -23,15 +24,15 @@ __all__ = [ 'ITEM_HASOBJECT', 'ITEM_IS_POINTER', 'LIST_PICKLE', 'MAXDIMS', 'MAY_SHARE_BOUNDS', 'MAY_SHARE_EXACT', 'NEEDS_INIT', 'NEEDS_PYAPI', 'RAISE', 'USE_GETITEM', 'USE_SETITEM', 'WRAP', '_fastCopyAndTranspose', - '_flagdict', '_insert', '_reconstruct', '_vec_string', '_monotonicity', - 'add_docstring', 'arange', 'array', 'asarray', 'asanyarray', - 'ascontiguousarray', 'asfortranarray', 'bincount', 'broadcast', - 'busday_count', 'busday_offset', 'busdaycalendar', 'can_cast', + '_flagdict', '_from_dlpack', '_insert', '_reconstruct', '_vec_string', + '_monotonicity', 'add_docstring', 'arange', 'array', 'asarray', + 'asanyarray', 'ascontiguousarray', 'asfortranarray', 'bincount', + 'broadcast', 'busday_count', 'busday_offset', 'busdaycalendar', 'can_cast', 'compare_chararrays', 'concatenate', 'copyto', 'correlate', 'correlate2', 'count_nonzero', 'c_einsum', 'datetime_as_string', 'datetime_data', 'dot', 'dragon4_positional', 'dragon4_scientific', 'dtype', 'empty', 'empty_like', 'error', 'flagsobj', 'flatiter', 'format_longfloat', - 'frombuffer', 'from_dlpack', 'fromfile', 'fromiter', 'fromstring', + 'frombuffer', 'fromfile', 'fromiter', 'fromstring', 'get_handler_name', 'inner', 'interp', 'interp_complex', 'is_busday', 'lexsort', 'matmul', 'may_share_memory', 'min_scalar_type', 'ndarray', 'nditer', 'nested_iters', 'normalize_axis_index', 'packbits', @@ -46,6 +47,7 @@ _reconstruct.__module__ = 'numpy.core.multiarray' scalar.__module__ = 'numpy.core.multiarray' +_from_dlpack.__module__ = 'numpy' arange.__module__ = 'numpy' array.__module__ = 'numpy' asarray.__module__ = 'numpy' @@ -55,7 +57,6 @@ asfortranarray.__module__ = 'numpy' datetime_data.__module__ = 'numpy' empty.__module__ = 'numpy' frombuffer.__module__ = 'numpy' -from_dlpack.__module__ = 'numpy' fromfile.__module__ = 'numpy' fromiter.__module__ = 'numpy' frompyfunc.__module__ = 'numpy' diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 987470f92..344d40d93 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -13,7 +13,7 @@ from .multiarray import ( WRAP, arange, array, asarray, asanyarray, ascontiguousarray, asfortranarray, broadcast, can_cast, compare_chararrays, concatenate, copyto, dot, dtype, empty, - empty_like, flatiter, frombuffer, from_dlpack, fromfile, fromiter, + empty_like, flatiter, frombuffer, _from_dlpack, fromfile, fromiter, fromstring, inner, lexsort, matmul, may_share_memory, min_scalar_type, ndarray, nditer, nested_iters, promote_types, putmask, result_type, set_numeric_ops, shares_memory, vdot, where, @@ -41,7 +41,7 @@ __all__ = [ 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', 'arange', 'array', 'asarray', 'asanyarray', 'ascontiguousarray', 'asfortranarray', 'zeros', 'count_nonzero', 'empty', 'broadcast', 'dtype', - 'fromstring', 'fromfile', 'frombuffer', 'from_dlpack', 'where', + 'fromstring', 'fromfile', 'frombuffer', '_from_dlpack', 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', 'min_scalar_type', 'result_type', 'isfortran', 'empty_like', 'zeros_like', 'ones_like', diff --git a/numpy/core/src/common/npy_dlpack.h b/numpy/core/src/common/npy_dlpack.h index cb926a262..14ca352c0 100644 --- a/numpy/core/src/common/npy_dlpack.h +++ b/numpy/core/src/common/npy_dlpack.h @@ -23,6 +23,6 @@ array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)); NPY_NO_EXPORT PyObject * -from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj); +_from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj); #endif diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c index 9de304379..21930b0ef 100644 --- a/numpy/core/src/multiarray/dlpack.c +++ b/numpy/core/src/multiarray/dlpack.c @@ -260,7 +260,7 @@ array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)) } NPY_NO_EXPORT PyObject * -from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { +_from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { PyObject *capsule = PyObject_CallMethod((PyObject *)obj->ob_type, "__dlpack__", "O", obj); if (capsule == NULL) { diff --git a/numpy/core/src/multiarray/dlpack.h b/numpy/core/src/multiarray/dlpack.h deleted file mode 100644 index 7d4a289d9..000000000 --- a/numpy/core/src/multiarray/dlpack.h +++ /dev/null @@ -1,18 +0,0 @@ -#define NPY_NO_DEPRECATED_API NPY_API_VERSION -#define _MULTIARRAYMODULE - -#define PY_SSIZE_T_CLEAN -#include -#include "numpy/arrayobject.h" - -static PyObject * -array_dlpack(PyArrayObject *self, PyObject *const *args, Py_ssize_t len_args, - PyObject *kwnames); - - -static PyObject * -array_dlpack_device(PyArrayObject *self, PyObject *NPY_UNUSED(args)); - - -NPY_NO_EXPORT PyObject * -from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj); diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index ac4c1ae0b..3cb4e83f1 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4446,7 +4446,7 @@ static struct PyMethodDef array_module_methods[] = { {"_reload_guard", (PyCFunction)_reload_guard, METH_NOARGS, "Give a warning on reload and big warning in sub-interpreters."}, - {"from_dlpack", (PyCFunction)from_dlpack, + {"_from_dlpack", (PyCFunction)_from_dlpack, METH_O, NULL}, {NULL, NULL, 0, NULL} /* sentinel */ }; diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 2561991e8..06fc042ec 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -27,12 +27,12 @@ class TestDLPack: z = y['int'] with pytest.raises(RuntimeError): - np.from_dlpack(z) + np._from_dlpack(z) @pytest.mark.skipif(IS_PYPY, reason="PyPy can't get refcounts.") def test_from_dlpack_refcount(self): x = np.arange(5) - y = np.from_dlpack(x) + y = np._from_dlpack(x) assert sys.getrefcount(x) == 3 del y assert sys.getrefcount(x) == 2 @@ -45,7 +45,7 @@ class TestDLPack: ]) def test_dtype_passthrough(self, dtype): x = np.arange(5, dtype=dtype) - y = np.from_dlpack(x) + y = np._from_dlpack(x) assert y.dtype == x.dtype assert_array_equal(x, y) @@ -54,44 +54,44 @@ class TestDLPack: x = np.asarray(np.datetime64('2021-05-27')) with pytest.raises(TypeError): - np.from_dlpack(x) + np._from_dlpack(x) def test_invalid_byte_swapping(self): dt = np.dtype('=i8').newbyteorder() x = np.arange(5, dtype=dt) with pytest.raises(TypeError): - np.from_dlpack(x) + np._from_dlpack(x) def test_non_contiguous(self): x = np.arange(25).reshape((5, 5)) y1 = x[0] - assert_array_equal(y1, np.from_dlpack(y1)) + assert_array_equal(y1, np._from_dlpack(y1)) y2 = x[:, 0] - assert_array_equal(y2, np.from_dlpack(y2)) + assert_array_equal(y2, np._from_dlpack(y2)) y3 = x[1, :] - assert_array_equal(y3, np.from_dlpack(y3)) + assert_array_equal(y3, np._from_dlpack(y3)) y4 = x[1] - assert_array_equal(y4, np.from_dlpack(y4)) + assert_array_equal(y4, np._from_dlpack(y4)) y5 = np.diagonal(x) - assert_array_equal(y5, np.from_dlpack(y5)) + assert_array_equal(y5, np._from_dlpack(y5)) @pytest.mark.parametrize("ndim", range(33)) def test_higher_dims(self, ndim): shape = (1,) * ndim x = np.zeros(shape, dtype=np.float64) - assert shape == np.from_dlpack(x).shape + assert shape == np._from_dlpack(x).shape def test_dlpack_device(self): x = np.arange(5) assert x.__dlpack_device__() == (1, 0) - assert np.from_dlpack(x).__dlpack_device__() == (1, 0) + assert np._from_dlpack(x).__dlpack_device__() == (1, 0) def dlpack_deleter_exception(self): x = np.arange(5) -- cgit v1.2.1 From 267778847df8afdc25c91ced9e467902eb1cb94f Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Tue, 2 Nov 2021 16:44:06 +0530 Subject: Fixed type check --- numpy/core/src/multiarray/conversion_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 983890433..d8b5ea6b4 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -177,7 +177,7 @@ PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) { PyObject* numpy_CopyMode = NULL; npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode); - if (numpy_CopyMode != NULL && PyObject_TypeCheck(obj, numpy_CopyMode)) { + if (numpy_CopyMode != NULL && PyObject_Type(obj) == numpy_CopyMode) { PyObject* mode_value = PyObject_GetAttrString(obj, "value"); if (mode_value == NULL) { return NPY_FAIL; -- cgit v1.2.1 From f4a992c12b94df0f5c69d08a72ed2d89816d3b0d Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Tue, 2 Nov 2021 14:23:32 +0100 Subject: ENH: Add annotations for `np.fft` --- numpy/fft/__init__.pyi | 43 ++++++++++-------- numpy/fft/_pocketfft.pyi | 111 +++++++++++++++++++++++++++++++++++++++++++++++ numpy/fft/helper.pyi | 50 +++++++++++++++++++++ 3 files changed, 185 insertions(+), 19 deletions(-) create mode 100644 numpy/fft/_pocketfft.pyi create mode 100644 numpy/fft/helper.pyi (limited to 'numpy') diff --git a/numpy/fft/__init__.pyi b/numpy/fft/__init__.pyi index 648b0bf79..510e576d3 100644 --- a/numpy/fft/__init__.pyi +++ b/numpy/fft/__init__.pyi @@ -2,25 +2,30 @@ from typing import Any, List from numpy._pytesttester import PytestTester +from numpy.fft._pocketfft import ( + fft as fft, + ifft as ifft, + rfft as rfft, + irfft as irfft, + hfft as hfft, + ihfft as ihfft, + rfftn as rfftn, + irfftn as irfftn, + rfft2 as rfft2, + irfft2 as irfft2, + fft2 as fft2, + ifft2 as ifft2, + fftn as fftn, + ifftn as ifftn, +) + +from numpy.fft.helper import ( + fftshift as fftshift, + ifftshift as ifftshift, + fftfreq as fftfreq, + rfftfreq as rfftfreq, +) + __all__: List[str] __path__: List[str] test: PytestTester - -def fft(a, n=..., axis=..., norm=...): ... -def ifft(a, n=..., axis=..., norm=...): ... -def rfft(a, n=..., axis=..., norm=...): ... -def irfft(a, n=..., axis=..., norm=...): ... -def hfft(a, n=..., axis=..., norm=...): ... -def ihfft(a, n=..., axis=..., norm=...): ... -def fftn(a, s=..., axes=..., norm=...): ... -def ifftn(a, s=..., axes=..., norm=...): ... -def rfftn(a, s=..., axes=..., norm=...): ... -def irfftn(a, s=..., axes=..., norm=...): ... -def fft2(a, s=..., axes=..., norm=...): ... -def ifft2(a, s=..., axes=..., norm=...): ... -def rfft2(a, s=..., axes=..., norm=...): ... -def irfft2(a, s=..., axes=..., norm=...): ... -def fftshift(x, axes=...): ... -def ifftshift(x, axes=...): ... -def fftfreq(n, d=...): ... -def rfftfreq(n, d=...): ... diff --git a/numpy/fft/_pocketfft.pyi b/numpy/fft/_pocketfft.pyi new file mode 100644 index 000000000..86cf6a60d --- /dev/null +++ b/numpy/fft/_pocketfft.pyi @@ -0,0 +1,111 @@ +from typing import ( + Literal as L, + List, + Sequence, +) + +from numpy import complex128, float64 +from numpy.typing import ArrayLike, NDArray, _ArrayLikeNumber_co + +_NormKind = L[None, "backward", "ortho", "forward"] + +__all__: List[str] + +def fft( + a: ArrayLike, + n: None | int = ..., + axis: int = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def ifft( + a: ArrayLike, + n: None | int = ..., + axis: int = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def rfft( + a: ArrayLike, + n: None | int = ..., + axis: int = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def irfft( + a: ArrayLike, + n: None | int = ..., + axis: int = ..., + norm: _NormKind = ..., +) -> NDArray[float64]: ... + +# Input array must be compatible with `np.conjugate` +def hfft( + a: _ArrayLikeNumber_co, + n: None | int = ..., + axis: int = ..., + norm: _NormKind = ..., +) -> NDArray[float64]: ... + +def ihfft( + a: ArrayLike, + n: None | int = ..., + axis: int = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def fftn( + a: ArrayLike, + s: None | Sequence[int] = ..., + axes: None | Sequence[int] = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def ifftn( + a: ArrayLike, + s: None | Sequence[int] = ..., + axes: None | Sequence[int] = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def rfftn( + a: ArrayLike, + s: None | Sequence[int] = ..., + axes: None | Sequence[int] = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def irfftn( + a: ArrayLike, + s: None | Sequence[int] = ..., + axes: None | Sequence[int] = ..., + norm: _NormKind = ..., +) -> NDArray[float64]: ... + +def fft2( + a: ArrayLike, + s: None | Sequence[int] = ..., + axes: None | Sequence[int] = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def ifft2( + a: ArrayLike, + s: None | Sequence[int] = ..., + axes: None | Sequence[int] = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def rfft2( + a: ArrayLike, + s: None | Sequence[int] = ..., + axes: None | Sequence[int] = ..., + norm: _NormKind = ..., +) -> NDArray[complex128]: ... + +def irfft2( + a: ArrayLike, + s: None | Sequence[int] = ..., + axes: None | Sequence[int] = ..., + norm: _NormKind = ..., +) -> NDArray[float64]: ... diff --git a/numpy/fft/helper.pyi b/numpy/fft/helper.pyi new file mode 100644 index 000000000..d75826f4e --- /dev/null +++ b/numpy/fft/helper.pyi @@ -0,0 +1,50 @@ +from typing import List, Any, TypeVar, overload + +from numpy import generic, dtype, integer, floating, complexfloating +from numpy.typing import ( + NDArray, + ArrayLike, + _ShapeLike, + _SupportsArray, + _FiniteNestedSequence, + _ArrayLikeFloat_co, + _ArrayLikeComplex_co, +) + +_SCT = TypeVar("_SCT", bound=generic) + +_ArrayLike = _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]] + +__all__: List[str] + +@overload +def fftshift(x: _ArrayLike[_SCT], axes: None | _ShapeLike = ...) -> NDArray[_SCT]: ... +@overload +def fftshift(x: ArrayLike, axes: None | _ShapeLike = ...) -> NDArray[Any]: ... + +@overload +def ifftshift(x: _ArrayLike[_SCT], axes: None | _ShapeLike = ...) -> NDArray[_SCT]: ... +@overload +def ifftshift(x: ArrayLike, axes: None | _ShapeLike = ...) -> NDArray[Any]: ... + +@overload +def fftfreq( + n: int | integer[Any], + d: _ArrayLikeFloat_co, +) -> NDArray[floating[Any]]: ... +@overload +def fftfreq( + n: int | integer[Any], + d: _ArrayLikeComplex_co, +) -> NDArray[complexfloating[Any, Any]]: ... + +@overload +def rfftfreq( + n: int | integer[Any], + d: _ArrayLikeFloat_co, +) -> NDArray[floating[Any]]: ... +@overload +def rfftfreq( + n: int | integer[Any], + d: _ArrayLikeComplex_co, +) -> NDArray[complexfloating[Any, Any]]: ... -- cgit v1.2.1 From fb3a205b0c41b9bfeffb960a2840f6fd63011e23 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Tue, 2 Nov 2021 14:33:02 +0100 Subject: TST: Add typing tests for `np.fft` --- numpy/typing/tests/data/reveal/fft.pyi | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 numpy/typing/tests/data/reveal/fft.pyi (limited to 'numpy') diff --git a/numpy/typing/tests/data/reveal/fft.pyi b/numpy/typing/tests/data/reveal/fft.pyi new file mode 100644 index 000000000..316ddd189 --- /dev/null +++ b/numpy/typing/tests/data/reveal/fft.pyi @@ -0,0 +1,35 @@ +import numpy as np +import numpy.typing as npt + +AR_f8: npt.NDArray[np.float64] +AR_c16: npt.NDArray[np.complex128] +AR_LIKE_f8: list[float] + +reveal_type(np.fft.fftshift(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.fft.fftshift(AR_LIKE_f8, axes=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] + +reveal_type(np.fft.ifftshift(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.fft.ifftshift(AR_LIKE_f8, axes=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] + +reveal_type(np.fft.fftfreq(5, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.fft.fftfreq(np.int64(), AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] + +reveal_type(np.fft.fftfreq(5, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.fft.fftfreq(np.int64(), AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] + +reveal_type(np.fft.fft(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.fft.ifft(AR_f8, axis=1)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.fft.rfft(AR_f8, n=None)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.fft.irfft(AR_f8, norm="ortho")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.fft.hfft(AR_f8, n=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.fft.ihfft(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] + +reveal_type(np.fft.fftn(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.fft.ifftn(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.fft.rfftn(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.fft.irfftn(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] + +reveal_type(np.fft.rfft2(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.fft.ifft2(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.fft.fft2(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.fft.irfft2(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -- cgit v1.2.1 From ae4af75cb766d3f5d6b791658ef51a9f16249e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Eertmans?= Date: Tue, 2 Nov 2021 16:14:03 +0100 Subject: ENH: Check that the lengths of the inputs to histogram2d are the same. (#20228) Improves exception message when inputs have different shapes. Closes gh-20050 Co-authored-by: Ross Barnowski --- numpy/lib/tests/test_twodim_base.py | 10 ++++++++++ numpy/lib/twodim_base.py | 3 +++ 2 files changed, 13 insertions(+) (limited to 'numpy') diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py index cce683bfe..c1c5a1615 100644 --- a/numpy/lib/tests/test_twodim_base.py +++ b/numpy/lib/tests/test_twodim_base.py @@ -18,6 +18,9 @@ import numpy as np from numpy.core.tests.test_overrides import requires_array_function +import pytest + + def get_mat(n): data = arange(n) data = add.outer(data, data) @@ -295,6 +298,13 @@ class TestHistogram2d: r = histogram2d(xy, xy, weights=s_d) assert_(r, ((ShouldDispatch,), (xy, xy), dict(weights=s_d))) + @pytest.mark.parametrize(("x_len", "y_len"), [(10, 11), (20, 19)]) + def test_bad_length(self, x_len, y_len): + x, y = np.ones(x_len), np.ones(y_len) + with pytest.raises(ValueError, + match='x and y must have the same length.'): + histogram2d(x, y) + class TestTri: def test_dtype(self): diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py index 811faff79..3e5ad31ff 100644 --- a/numpy/lib/twodim_base.py +++ b/numpy/lib/twodim_base.py @@ -804,6 +804,9 @@ def histogram2d(x, y, bins=10, range=None, normed=None, weights=None, >>> plt.show() """ from numpy import histogramdd + + if len(x) != len(y): + raise ValueError('x and y must have the same length.') try: N = len(bins) -- cgit v1.2.1 From 9a067abf210ecd9696167f300f2b8454b48af4f5 Mon Sep 17 00:00:00 2001 From: Daniel Ching Date: Tue, 2 Nov 2021 15:19:07 -0500 Subject: DOC: Correct grammar in isfinite docstring Finiteness means both not infinity and also not NaN. --- numpy/core/code_generators/ufunc_docstrings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/code_generators/ufunc_docstrings.py b/numpy/core/code_generators/ufunc_docstrings.py index 8d9316f2c..c9be94569 100644 --- a/numpy/core/code_generators/ufunc_docstrings.py +++ b/numpy/core/code_generators/ufunc_docstrings.py @@ -1678,7 +1678,7 @@ add_newdoc('numpy.core.umath', 'invert', add_newdoc('numpy.core.umath', 'isfinite', """ - Test element-wise for finiteness (not infinity or not Not a Number). + Test element-wise for finiteness (not infinity and not Not a Number). The result is returned as a boolean array. -- cgit v1.2.1 From eb5e7c683fd9cc4b5c967e2260ae11f4365494a6 Mon Sep 17 00:00:00 2001 From: warren Date: Wed, 3 Nov 2021 01:20:07 -0400 Subject: BUG: Fix duplication of names in 'numpy.__all__'. Closes gh-10198. --- numpy/__init__.py | 4 ++++ numpy/core/__init__.py | 1 - numpy/tests/test__all__.py | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 numpy/tests/test__all__.py (limited to 'numpy') diff --git a/numpy/__init__.py b/numpy/__init__.py index c34434e75..8f7a42d10 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -233,6 +233,10 @@ else: __all__.extend(lib.__all__) __all__.extend(['linalg', 'fft', 'random', 'ctypeslib', 'ma']) + # Remove one of the two occurrences of `issubdtype`, which is exposed as + # both `numpy.core.issubdtype` and `numpy.lib.issubdtype`. + __all__.remove('issubdtype') + # These are exported by np.core, but are replaced by the builtins below # remove them to ensure that we don't end up with `np.long == np.int_`, # which would be a breaking change. diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py index 332f9940e..b89e27f0f 100644 --- a/numpy/core/__init__.py +++ b/numpy/core/__init__.py @@ -106,7 +106,6 @@ from . import _methods __all__ = ['char', 'rec', 'memmap'] __all__ += numeric.__all__ -__all__ += fromnumeric.__all__ __all__ += ['record', 'recarray', 'format_parser'] __all__ += ['chararray'] __all__ += function_base.__all__ diff --git a/numpy/tests/test__all__.py b/numpy/tests/test__all__.py new file mode 100644 index 000000000..e44bda3d5 --- /dev/null +++ b/numpy/tests/test__all__.py @@ -0,0 +1,9 @@ + +import collections +import numpy as np + + +def test_no_duplicates_in_np__all__(): + # Regression test for gh-10198. + dups = {k: v for k, v in collections.Counter(np.__all__).items() if v > 1} + assert len(dups) == 0 -- cgit v1.2.1 From bc087bb2b3104547b097e03e669fb5fa77b16f05 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 3 Nov 2021 11:09:28 +0000 Subject: TST: Add a test for device property in `array_api` namespace (#20271) --- numpy/array_api/tests/test_array_object.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'numpy') diff --git a/numpy/array_api/tests/test_array_object.py b/numpy/array_api/tests/test_array_object.py index 7959f92b4..fb42cf621 100644 --- a/numpy/array_api/tests/test_array_object.py +++ b/numpy/array_api/tests/test_array_object.py @@ -285,3 +285,14 @@ def test_python_scalar_construtors(): assert_raises(TypeError, lambda: operator.index(b)) assert_raises(TypeError, lambda: operator.index(f)) + + +def test_device_property(): + a = ones((3, 4)) + assert a.device == 'cpu' + + assert np.array_equal(a.to_device('cpu'), a) + assert_raises(ValueError, lambda: a.to_device('gpu')) + + assert np.array_equal(asarray(a, device='cpu'), a) + assert_raises(ValueError, lambda: asarray(a, device='gpu')) -- cgit v1.2.1 From b7dc11cbc1dc4c41475f001f2ee5ccbfe6d1a847 Mon Sep 17 00:00:00 2001 From: Sayantika Banik Date: Wed, 3 Nov 2021 22:06:49 +0530 Subject: TST,MAINT: Allow module names to be excluded in typing tests (#20287) --- numpy/typing/tests/data/reveal/arithmetic.pyi | 280 +-- .../tests/data/reveal/array_constructors.pyi | 266 +-- numpy/typing/tests/data/reveal/arraypad.pyi | 8 +- numpy/typing/tests/data/reveal/arraysetops.pyi | 98 +- numpy/typing/tests/data/reveal/arrayterator.pyi | 16 +- numpy/typing/tests/data/reveal/bitwise_ops.pyi | 36 +- numpy/typing/tests/data/reveal/char.pyi | 204 +-- numpy/typing/tests/data/reveal/chararray.pyi | 176 +- numpy/typing/tests/data/reveal/comparisons.pyi | 418 ++--- numpy/typing/tests/data/reveal/constants.pyi | 4 +- numpy/typing/tests/data/reveal/ctypeslib.pyi | 18 +- numpy/typing/tests/data/reveal/dtype.pyi | 92 +- numpy/typing/tests/data/reveal/einsumfunc.pyi | 4 +- numpy/typing/tests/data/reveal/fft.pyi | 56 +- numpy/typing/tests/data/reveal/flatiter.pyi | 20 +- numpy/typing/tests/data/reveal/fromnumeric.pyi | 150 +- numpy/typing/tests/data/reveal/getlimits.pyi | 12 +- numpy/typing/tests/data/reveal/histograms.pyi | 22 +- numpy/typing/tests/data/reveal/index_tricks.pyi | 42 +- .../typing/tests/data/reveal/lib_function_base.pyi | 172 +- numpy/typing/tests/data/reveal/lib_polynomial.pyi | 172 +- numpy/typing/tests/data/reveal/linalg.pyi | 122 +- numpy/typing/tests/data/reveal/matrix.pyi | 86 +- numpy/typing/tests/data/reveal/memmap.pyi | 6 +- numpy/typing/tests/data/reveal/mod.pyi | 32 +- numpy/typing/tests/data/reveal/modules.pyi | 2 +- numpy/typing/tests/data/reveal/multiarray.pyi | 72 +- .../tests/data/reveal/ndarray_conversion.pyi | 38 +- numpy/typing/tests/data/reveal/ndarray_misc.pyi | 80 +- .../data/reveal/ndarray_shape_manipulation.pyi | 34 +- numpy/typing/tests/data/reveal/nditer.pyi | 30 +- numpy/typing/tests/data/reveal/npyio.pyi | 64 +- numpy/typing/tests/data/reveal/numeric.pyi | 144 +- numpy/typing/tests/data/reveal/numerictypes.pyi | 2 +- numpy/typing/tests/data/reveal/random.pyi | 1840 ++++++++++---------- numpy/typing/tests/data/reveal/rec.pyi | 34 +- numpy/typing/tests/data/reveal/scalars.pyi | 70 +- numpy/typing/tests/data/reveal/shape_base.pyi | 58 +- numpy/typing/tests/data/reveal/stride_tricks.pyi | 26 +- numpy/typing/tests/data/reveal/testing.pyi | 4 +- numpy/typing/tests/data/reveal/twodim_base.pyi | 66 +- numpy/typing/tests/data/reveal/type_check.pyi | 44 +- numpy/typing/tests/data/reveal/ufunc_config.pyi | 24 +- numpy/typing/tests/data/reveal/ufunclike.pyi | 30 +- numpy/typing/tests/data/reveal/ufuncs.pyi | 14 +- .../tests/data/reveal/warnings_and_errors.pyi | 14 +- numpy/typing/tests/test_typing.py | 15 +- 47 files changed, 2614 insertions(+), 2603 deletions(-) (limited to 'numpy') diff --git a/numpy/typing/tests/data/reveal/arithmetic.pyi b/numpy/typing/tests/data/reveal/arithmetic.pyi index 0d9132e5b..c5b467469 100644 --- a/numpy/typing/tests/data/reveal/arithmetic.pyi +++ b/numpy/typing/tests/data/reveal/arithmetic.pyi @@ -45,104 +45,104 @@ AR_LIKE_O: List[np.object_] # Array subtraction -reveal_type(AR_b - AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_b - AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_b - AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_b - AR_LIKE_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_b - AR_LIKE_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(AR_b - AR_LIKE_u) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_b - AR_LIKE_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_b - AR_LIKE_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_b - AR_LIKE_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_b - AR_LIKE_m) # E: ndarray[Any, dtype[timedelta64]] reveal_type(AR_b - AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_u - AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_LIKE_i - AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_f - AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_c - AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_LIKE_m - AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_LIKE_M - AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] +reveal_type(AR_LIKE_u - AR_b) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_LIKE_i - AR_b) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_f - AR_b) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_c - AR_b) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_LIKE_m - AR_b) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_LIKE_M - AR_b) # E: ndarray[Any, dtype[datetime64]] reveal_type(AR_LIKE_O - AR_b) # E: Any -reveal_type(AR_u - AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_u - AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_u - AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_u - AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_u - AR_LIKE_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_u - AR_LIKE_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(AR_u - AR_LIKE_b) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_u - AR_LIKE_u) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_u - AR_LIKE_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_u - AR_LIKE_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_u - AR_LIKE_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_u - AR_LIKE_m) # E: ndarray[Any, dtype[timedelta64]] reveal_type(AR_u - AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_b - AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_LIKE_u - AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_LIKE_i - AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_f - AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_c - AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_LIKE_m - AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_LIKE_M - AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] +reveal_type(AR_LIKE_b - AR_u) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_LIKE_u - AR_u) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_LIKE_i - AR_u) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_f - AR_u) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_c - AR_u) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_LIKE_m - AR_u) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_LIKE_M - AR_u) # E: ndarray[Any, dtype[datetime64]] reveal_type(AR_LIKE_O - AR_u) # E: Any -reveal_type(AR_i - AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_i - AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_i - AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_i - AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_i - AR_LIKE_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_i - AR_LIKE_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(AR_i - AR_LIKE_b) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_i - AR_LIKE_u) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_i - AR_LIKE_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_i - AR_LIKE_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_i - AR_LIKE_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_i - AR_LIKE_m) # E: ndarray[Any, dtype[timedelta64]] reveal_type(AR_i - AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_b - AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_u - AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_i - AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_f - AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_c - AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_LIKE_m - AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_LIKE_M - AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] +reveal_type(AR_LIKE_b - AR_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_u - AR_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_i - AR_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_f - AR_i) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_c - AR_i) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_LIKE_m - AR_i) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_LIKE_M - AR_i) # E: ndarray[Any, dtype[datetime64]] reveal_type(AR_LIKE_O - AR_i) # E: Any -reveal_type(AR_f - AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_f - AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_f - AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_f - AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_f - AR_LIKE_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(AR_f - AR_LIKE_b) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_f - AR_LIKE_u) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_f - AR_LIKE_i) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_f - AR_LIKE_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_f - AR_LIKE_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] reveal_type(AR_f - AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_b - AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_u - AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_i - AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_f - AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_c - AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(AR_LIKE_b - AR_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_u - AR_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_i - AR_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_f - AR_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_c - AR_f) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] reveal_type(AR_LIKE_O - AR_f) # E: Any -reveal_type(AR_c - AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_c - AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_c - AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_c - AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_c - AR_LIKE_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(AR_c - AR_LIKE_b) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_c - AR_LIKE_u) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_c - AR_LIKE_i) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_c - AR_LIKE_f) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_c - AR_LIKE_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] reveal_type(AR_c - AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_b - AR_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_LIKE_u - AR_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_LIKE_i - AR_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_LIKE_f - AR_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(AR_LIKE_c - AR_c) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(AR_LIKE_b - AR_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_LIKE_u - AR_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_LIKE_i - AR_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_LIKE_f - AR_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(AR_LIKE_c - AR_c) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] reveal_type(AR_LIKE_O - AR_c) # E: Any -reveal_type(AR_m - AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_m - AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_m - AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_m - AR_LIKE_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(AR_m - AR_LIKE_b) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_m - AR_LIKE_u) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_m - AR_LIKE_i) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_m - AR_LIKE_m) # E: ndarray[Any, dtype[timedelta64]] reveal_type(AR_m - AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_b - AR_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_LIKE_u - AR_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_LIKE_i - AR_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_LIKE_m - AR_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_LIKE_M - AR_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] +reveal_type(AR_LIKE_b - AR_m) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_LIKE_u - AR_m) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_LIKE_i - AR_m) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_LIKE_m - AR_m) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_LIKE_M - AR_m) # E: ndarray[Any, dtype[datetime64]] reveal_type(AR_LIKE_O - AR_m) # E: Any -reveal_type(AR_M - AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(AR_M - AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(AR_M - AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(AR_M - AR_LIKE_m) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(AR_M - AR_LIKE_M) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(AR_M - AR_LIKE_b) # E: ndarray[Any, dtype[datetime64]] +reveal_type(AR_M - AR_LIKE_u) # E: ndarray[Any, dtype[datetime64]] +reveal_type(AR_M - AR_LIKE_i) # E: ndarray[Any, dtype[datetime64]] +reveal_type(AR_M - AR_LIKE_m) # E: ndarray[Any, dtype[datetime64]] +reveal_type(AR_M - AR_LIKE_M) # E: ndarray[Any, dtype[timedelta64]] reveal_type(AR_M - AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_M - AR_M) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(AR_LIKE_M - AR_M) # E: ndarray[Any, dtype[timedelta64]] reveal_type(AR_LIKE_O - AR_M) # E: Any reveal_type(AR_O - AR_LIKE_b) # E: Any @@ -165,64 +165,64 @@ reveal_type(AR_LIKE_O - AR_O) # E: Any # Array floor division -reveal_type(AR_b // AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[{int8}]] -reveal_type(AR_b // AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_b // AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_b // AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(AR_b // AR_LIKE_b) # E: ndarray[Any, dtype[{int8}]] +reveal_type(AR_b // AR_LIKE_u) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_b // AR_LIKE_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_b // AR_LIKE_f) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(AR_b // AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_b // AR_b) # E: numpy.ndarray[Any, numpy.dtype[{int8}]] -reveal_type(AR_LIKE_u // AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_LIKE_i // AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_f // AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(AR_LIKE_b // AR_b) # E: ndarray[Any, dtype[{int8}]] +reveal_type(AR_LIKE_u // AR_b) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_LIKE_i // AR_b) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_f // AR_b) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(AR_LIKE_O // AR_b) # E: Any -reveal_type(AR_u // AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_u // AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_u // AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_u // AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(AR_u // AR_LIKE_b) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_u // AR_LIKE_u) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_u // AR_LIKE_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_u // AR_LIKE_f) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(AR_u // AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_b // AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_LIKE_u // AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(AR_LIKE_i // AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_f // AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_m // AR_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(AR_LIKE_b // AR_u) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_LIKE_u // AR_u) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(AR_LIKE_i // AR_u) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_f // AR_u) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_m // AR_u) # E: ndarray[Any, dtype[timedelta64]] reveal_type(AR_LIKE_O // AR_u) # E: Any -reveal_type(AR_i // AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_i // AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_i // AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_i // AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(AR_i // AR_LIKE_b) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_i // AR_LIKE_u) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_i // AR_LIKE_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_i // AR_LIKE_f) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(AR_i // AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_b // AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_u // AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_i // AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(AR_LIKE_f // AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_m // AR_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(AR_LIKE_b // AR_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_u // AR_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_i // AR_i) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(AR_LIKE_f // AR_i) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_m // AR_i) # E: ndarray[Any, dtype[timedelta64]] reveal_type(AR_LIKE_O // AR_i) # E: Any -reveal_type(AR_f // AR_LIKE_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_f // AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_f // AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_f // AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(AR_f // AR_LIKE_b) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_f // AR_LIKE_u) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_f // AR_LIKE_i) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_f // AR_LIKE_f) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(AR_f // AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_b // AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_u // AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_i // AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_f // AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(AR_LIKE_m // AR_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] +reveal_type(AR_LIKE_b // AR_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_u // AR_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_i // AR_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_f // AR_f) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(AR_LIKE_m // AR_f) # E: ndarray[Any, dtype[timedelta64]] reveal_type(AR_LIKE_O // AR_f) # E: Any -reveal_type(AR_m // AR_LIKE_u) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_m // AR_LIKE_i) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_m // AR_LIKE_f) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(AR_m // AR_LIKE_m) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(AR_m // AR_LIKE_u) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_m // AR_LIKE_i) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_m // AR_LIKE_f) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(AR_m // AR_LIKE_m) # E: ndarray[Any, dtype[{int64}]] reveal_type(AR_m // AR_LIKE_O) # E: Any -reveal_type(AR_LIKE_m // AR_m) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(AR_LIKE_m // AR_m) # E: ndarray[Any, dtype[{int64}]] reveal_type(AR_LIKE_O // AR_m) # E: Any reveal_type(AR_O // AR_LIKE_b) # E: Any @@ -252,7 +252,7 @@ reveal_type(-i8) # E: {int64} reveal_type(-i4) # E: {int32} reveal_type(-u8) # E: {uint64} reveal_type(-u4) # E: {uint32} -reveal_type(-td) # E: numpy.timedelta64 +reveal_type(-td) # E: timedelta64 reveal_type(-AR_f) # E: Any reveal_type(+f16) # E: {float128} @@ -264,7 +264,7 @@ reveal_type(+i8) # E: {int64} reveal_type(+i4) # E: {int32} reveal_type(+u8) # E: {uint64} reveal_type(+u4) # E: {uint32} -reveal_type(+td) # E: numpy.timedelta64 +reveal_type(+td) # E: timedelta64 reveal_type(+AR_f) # E: Any reveal_type(abs(f16)) # E: {float128} @@ -276,32 +276,32 @@ reveal_type(abs(i8)) # E: {int64} reveal_type(abs(i4)) # E: {int32} reveal_type(abs(u8)) # E: {uint64} reveal_type(abs(u4)) # E: {uint32} -reveal_type(abs(td)) # E: numpy.timedelta64 -reveal_type(abs(b_)) # E: numpy.bool_ +reveal_type(abs(td)) # E: timedelta64 +reveal_type(abs(b_)) # E: bool_ reveal_type(abs(AR_f)) # E: Any # Time structures -reveal_type(dt + td) # E: numpy.datetime64 -reveal_type(dt + i) # E: numpy.datetime64 -reveal_type(dt + i4) # E: numpy.datetime64 -reveal_type(dt + i8) # E: numpy.datetime64 -reveal_type(dt - dt) # E: numpy.timedelta64 -reveal_type(dt - i) # E: numpy.datetime64 -reveal_type(dt - i4) # E: numpy.datetime64 -reveal_type(dt - i8) # E: numpy.datetime64 - -reveal_type(td + td) # E: numpy.timedelta64 -reveal_type(td + i) # E: numpy.timedelta64 -reveal_type(td + i4) # E: numpy.timedelta64 -reveal_type(td + i8) # E: numpy.timedelta64 -reveal_type(td - td) # E: numpy.timedelta64 -reveal_type(td - i) # E: numpy.timedelta64 -reveal_type(td - i4) # E: numpy.timedelta64 -reveal_type(td - i8) # E: numpy.timedelta64 -reveal_type(td / f) # E: numpy.timedelta64 -reveal_type(td / f4) # E: numpy.timedelta64 -reveal_type(td / f8) # E: numpy.timedelta64 +reveal_type(dt + td) # E: datetime64 +reveal_type(dt + i) # E: datetime64 +reveal_type(dt + i4) # E: datetime64 +reveal_type(dt + i8) # E: datetime64 +reveal_type(dt - dt) # E: timedelta64 +reveal_type(dt - i) # E: datetime64 +reveal_type(dt - i4) # E: datetime64 +reveal_type(dt - i8) # E: datetime64 + +reveal_type(td + td) # E: timedelta64 +reveal_type(td + i) # E: timedelta64 +reveal_type(td + i4) # E: timedelta64 +reveal_type(td + i8) # E: timedelta64 +reveal_type(td - td) # E: timedelta64 +reveal_type(td - i) # E: timedelta64 +reveal_type(td - i4) # E: timedelta64 +reveal_type(td - i8) # E: timedelta64 +reveal_type(td / f) # E: timedelta64 +reveal_type(td / f4) # E: timedelta64 +reveal_type(td / f8) # E: timedelta64 reveal_type(td / td) # E: {float64} reveal_type(td // td) # E: {int64} @@ -378,7 +378,7 @@ reveal_type(c8 + b_) # E: {complex64} reveal_type(c8 + b) # E: {complex64} reveal_type(c8 + c) # E: {complex128} reveal_type(c8 + f) # E: {complex128} -reveal_type(c8 + i) # E: numpy.complexfloating[{_NBitInt}, {_NBitInt}] +reveal_type(c8 + i) # E: complexfloating[{_NBitInt}, {_NBitInt}] reveal_type(c8 + AR_f) # E: Any reveal_type(f16 + c8) # E: {complex256} @@ -392,7 +392,7 @@ reveal_type(b_ + c8) # E: {complex64} reveal_type(b + c8) # E: {complex64} reveal_type(c + c8) # E: {complex128} reveal_type(f + c8) # E: {complex128} -reveal_type(i + c8) # E: numpy.complexfloating[{_NBitInt}, {_NBitInt}] +reveal_type(i + c8) # E: complexfloating[{_NBitInt}, {_NBitInt}] reveal_type(AR_f + c8) # E: Any # Float @@ -430,7 +430,7 @@ reveal_type(f4 + b_) # E: {float32} reveal_type(f4 + b) # E: {float32} reveal_type(f4 + c) # E: {complex128} reveal_type(f4 + f) # E: {float64} -reveal_type(f4 + i) # E: numpy.floating[{_NBitInt}] +reveal_type(f4 + i) # E: floating[{_NBitInt}] reveal_type(f4 + AR_f) # E: Any reveal_type(f16 + f4) # E: {float128} @@ -442,7 +442,7 @@ reveal_type(b_ + f4) # E: {float32} reveal_type(b + f4) # E: {float32} reveal_type(c + f4) # E: {complex128} reveal_type(f + f4) # E: {float64} -reveal_type(i + f4) # E: numpy.floating[{_NBitInt}] +reveal_type(i + f4) # E: floating[{_NBitInt}] reveal_type(AR_f + f4) # E: Any # Int diff --git a/numpy/typing/tests/data/reveal/array_constructors.pyi b/numpy/typing/tests/data/reveal/array_constructors.pyi index 0aea4ea96..233988e63 100644 --- a/numpy/typing/tests/data/reveal/array_constructors.pyi +++ b/numpy/typing/tests/data/reveal/array_constructors.pyi @@ -16,167 +16,167 @@ C: List[int] def func(i: int, j: int, **kwargs: Any) -> SubClass[np.float64]: ... -reveal_type(np.empty_like(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.empty_like(A)) # E: ndarray[Any, dtype[{float64}]] reveal_type(np.empty_like(B)) # E: SubClass[{float64}] -reveal_type(np.empty_like([1, 1.0])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.empty_like(A, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.empty_like(A, dtype='c16')) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.empty_like([1, 1.0])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.empty_like(A, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.empty_like(A, dtype='c16')) # E: ndarray[Any, dtype[Any]] -reveal_type(np.array(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.array(B)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.array(A)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.array(B)) # E: ndarray[Any, dtype[{float64}]] reveal_type(np.array(B, subok=True)) # E: SubClass[{float64}] -reveal_type(np.array([1, 1.0])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.array(A, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.array(A, dtype='c16')) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.zeros([1, 5, 6])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.zeros([1, 5, 6], dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.zeros([1, 5, 6], dtype='c16')) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.empty([1, 5, 6])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.empty([1, 5, 6], dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.empty([1, 5, 6], dtype='c16')) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.concatenate(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.concatenate([1, 1.0])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.concatenate(A, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.concatenate(A, dtype='c16')) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.concatenate([1, 1.0], out=A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] - -reveal_type(np.asarray(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.asarray(B)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.asarray([1, 1.0])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.asarray(A, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.asarray(A, dtype='c16')) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.asanyarray(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.array([1, 1.0])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.array(A, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.array(A, dtype='c16')) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.zeros([1, 5, 6])) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.zeros([1, 5, 6], dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.zeros([1, 5, 6], dtype='c16')) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.empty([1, 5, 6])) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.empty([1, 5, 6], dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.empty([1, 5, 6], dtype='c16')) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.concatenate(A)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.concatenate([1, 1.0])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.concatenate(A, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.concatenate(A, dtype='c16')) # E: ndarray[Any, dtype[Any]] +reveal_type(np.concatenate([1, 1.0], out=A)) # E: ndarray[Any, dtype[{float64}]] + +reveal_type(np.asarray(A)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.asarray(B)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.asarray([1, 1.0])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.asarray(A, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.asarray(A, dtype='c16')) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.asanyarray(A)) # E: ndarray[Any, dtype[{float64}]] reveal_type(np.asanyarray(B)) # E: SubClass[{float64}] -reveal_type(np.asanyarray([1, 1.0])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.asanyarray(A, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.asanyarray(A, dtype='c16')) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.ascontiguousarray(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.ascontiguousarray(B)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.ascontiguousarray([1, 1.0])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.ascontiguousarray(A, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.ascontiguousarray(A, dtype='c16')) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.asfortranarray(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.asfortranarray(B)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.asfortranarray([1, 1.0])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.asfortranarray(A, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.asfortranarray(A, dtype='c16')) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.fromstring("1 1 1", sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fromstring(b"1 1 1", sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fromstring("1 1 1", dtype=np.int64, sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.fromstring(b"1 1 1", dtype=np.int64, sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.fromstring("1 1 1", dtype="c16", sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.fromstring(b"1 1 1", dtype="c16", sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.fromfile("test.txt", sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fromfile("test.txt", dtype=np.int64, sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.fromfile("test.txt", dtype="c16", sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.asanyarray([1, 1.0])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.asanyarray(A, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.asanyarray(A, dtype='c16')) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.ascontiguousarray(A)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.ascontiguousarray(B)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.ascontiguousarray([1, 1.0])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.ascontiguousarray(A, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.ascontiguousarray(A, dtype='c16')) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.asfortranarray(A)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.asfortranarray(B)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.asfortranarray([1, 1.0])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.asfortranarray(A, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.asfortranarray(A, dtype='c16')) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.fromstring("1 1 1", sep=" ")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fromstring(b"1 1 1", sep=" ")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fromstring("1 1 1", dtype=np.int64, sep=" ")) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.fromstring(b"1 1 1", dtype=np.int64, sep=" ")) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.fromstring("1 1 1", dtype="c16", sep=" ")) # E: ndarray[Any, dtype[Any]] +reveal_type(np.fromstring(b"1 1 1", dtype="c16", sep=" ")) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.fromfile("test.txt", sep=" ")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fromfile("test.txt", dtype=np.int64, sep=" ")) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.fromfile("test.txt", dtype="c16", sep=" ")) # E: ndarray[Any, dtype[Any]] with open("test.txt") as f: - reveal_type(np.fromfile(f, sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] - reveal_type(np.fromfile(b"test.txt", sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] - reveal_type(np.fromfile(Path("test.txt"), sep=" ")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] - -reveal_type(np.fromiter("12345", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fromiter("12345", float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.frombuffer(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.frombuffer(A, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.frombuffer(A, dtype="c16")) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.arange(False, True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.arange(10)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.arange(0, 10, step=2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.arange(10.0)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.arange(start=0, stop=10.0)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.arange(np.timedelta64(0))) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(np.arange(0, np.timedelta64(10))) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(np.arange(np.datetime64("0"), np.datetime64("10"))) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(np.arange(10, dtype=np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.arange(0, 10, step=2, dtype=np.int16)) # E: numpy.ndarray[Any, numpy.dtype[{int16}]] -reveal_type(np.arange(10, dtype=int)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.arange(0, 10, dtype="f8")) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.require(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] + reveal_type(np.fromfile(f, sep=" ")) # E: ndarray[Any, dtype[{float64}]] + reveal_type(np.fromfile(b"test.txt", sep=" ")) # E: ndarray[Any, dtype[{float64}]] + reveal_type(np.fromfile(Path("test.txt"), sep=" ")) # E: ndarray[Any, dtype[{float64}]] + +reveal_type(np.fromiter("12345", np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fromiter("12345", float)) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.frombuffer(A)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.frombuffer(A, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.frombuffer(A, dtype="c16")) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.arange(False, True)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.arange(10)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.arange(0, 10, step=2)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.arange(10.0)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.arange(start=0, stop=10.0)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.arange(np.timedelta64(0))) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(np.arange(0, np.timedelta64(10))) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(np.arange(np.datetime64("0"), np.datetime64("10"))) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.arange(10, dtype=np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.arange(0, 10, step=2, dtype=np.int16)) # E: ndarray[Any, dtype[{int16}]] +reveal_type(np.arange(10, dtype=int)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.arange(0, 10, dtype="f8")) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.require(A)) # E: ndarray[Any, dtype[{float64}]] reveal_type(np.require(B)) # E: SubClass[{float64}] reveal_type(np.require(B, requirements=None)) # E: SubClass[{float64}] -reveal_type(np.require(B, dtype=int)) # E: numpy.ndarray[Any, Any] -reveal_type(np.require(B, requirements="E")) # E: numpy.ndarray[Any, Any] -reveal_type(np.require(B, requirements=["ENSUREARRAY"])) # E: numpy.ndarray[Any, Any] -reveal_type(np.require(B, requirements={"F", "E"})) # E: numpy.ndarray[Any, Any] +reveal_type(np.require(B, dtype=int)) # E: ndarray[Any, Any] +reveal_type(np.require(B, requirements="E")) # E: ndarray[Any, Any] +reveal_type(np.require(B, requirements=["ENSUREARRAY"])) # E: ndarray[Any, Any] +reveal_type(np.require(B, requirements={"F", "E"})) # E: ndarray[Any, Any] reveal_type(np.require(B, requirements=["C", "OWNDATA"])) # E: SubClass[{float64}] reveal_type(np.require(B, requirements="W")) # E: SubClass[{float64}] reveal_type(np.require(B, requirements="A")) # E: SubClass[{float64}] -reveal_type(np.require(C)) # E: numpy.ndarray[Any, Any] +reveal_type(np.require(C)) # E: ndarray[Any, Any] -reveal_type(np.linspace(0, 10)) # E: numpy.ndarray[Any, Any] -reveal_type(np.linspace(0, 10, retstep=True)) # E: Tuple[numpy.ndarray[Any, Any], Any] -reveal_type(np.logspace(0, 10)) # E: numpy.ndarray[Any, Any] -reveal_type(np.geomspace(1, 10)) # E: numpy.ndarray[Any, Any] +reveal_type(np.linspace(0, 10)) # E: ndarray[Any, Any] +reveal_type(np.linspace(0, 10, retstep=True)) # E: Tuple[ndarray[Any, Any], Any] +reveal_type(np.logspace(0, 10)) # E: ndarray[Any, Any] +reveal_type(np.geomspace(1, 10)) # E: ndarray[Any, Any] -reveal_type(np.zeros_like(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.zeros_like(C)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.zeros_like(A, dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.zeros_like(A)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.zeros_like(C)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.zeros_like(A, dtype=float)) # E: ndarray[Any, dtype[Any]] reveal_type(np.zeros_like(B)) # E: SubClass[{float64}] -reveal_type(np.zeros_like(B, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.zeros_like(B, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] -reveal_type(np.ones_like(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.ones_like(C)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.ones_like(A, dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.ones_like(A)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.ones_like(C)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.ones_like(A, dtype=float)) # E: ndarray[Any, dtype[Any]] reveal_type(np.ones_like(B)) # E: SubClass[{float64}] -reveal_type(np.ones_like(B, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.ones_like(B, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] -reveal_type(np.full_like(A, i8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.full_like(C, i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.full_like(A, i8, dtype=int)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.full_like(A, i8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.full_like(C, i8)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.full_like(A, i8, dtype=int)) # E: ndarray[Any, dtype[Any]] reveal_type(np.full_like(B, i8)) # E: SubClass[{float64}] -reveal_type(np.full_like(B, i8, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.full_like(B, i8, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] -reveal_type(np.ones(1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.ones([1, 1, 1])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.ones(5, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.ones(5, dtype=int)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.ones(1)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.ones([1, 1, 1])) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.ones(5, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.ones(5, dtype=int)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.full(1, i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.full([1, 1, 1], i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.full(1, i8, dtype=np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.full(1, i8, dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.full(1, i8)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.full([1, 1, 1], i8)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.full(1, i8, dtype=np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.full(1, i8, dtype=float)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.indices([1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.indices([1, 2, 3], sparse=True)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{int_}]]] +reveal_type(np.indices([1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.indices([1, 2, 3], sparse=True)) # E: tuple[ndarray[Any, dtype[{int_}]]] reveal_type(np.fromfunction(func, (3, 5))) # E: SubClass[{float64}] -reveal_type(np.identity(10)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.identity(10, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.identity(10, dtype=int)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.identity(10)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.identity(10, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.identity(10, dtype=int)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.atleast_1d(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.atleast_1d(C)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.atleast_1d(A, A)) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] -reveal_type(np.atleast_1d(A, C)) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] -reveal_type(np.atleast_1d(C, C)) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.atleast_1d(A)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.atleast_1d(C)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.atleast_1d(A, A)) # E: list[ndarray[Any, dtype[Any]]] +reveal_type(np.atleast_1d(A, C)) # E: list[ndarray[Any, dtype[Any]]] +reveal_type(np.atleast_1d(C, C)) # E: list[ndarray[Any, dtype[Any]]] -reveal_type(np.atleast_2d(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.atleast_2d(A)) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.atleast_3d(A)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.atleast_3d(A)) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.vstack([A, A])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.vstack([A, C])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.vstack([C, C])) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.vstack([A, A])) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.vstack([A, C])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.vstack([C, C])) # E: ndarray[Any, dtype[Any]] -reveal_type(np.hstack([A, A])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.hstack([A, A])) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.stack([A, A])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.stack([A, C])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.stack([C, C])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.stack([A, A], axis=0)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.stack([A, A])) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.stack([A, C])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.stack([C, C])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.stack([A, A], axis=0)) # E: ndarray[Any, dtype[{float64}]] reveal_type(np.stack([A, A], out=B)) # E: SubClass[{float64}] -reveal_type(np.block([[A, A], [A, A]])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.block(C)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.block([[A, A], [A, A]])) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.block(C)) # E: ndarray[Any, dtype[Any]] diff --git a/numpy/typing/tests/data/reveal/arraypad.pyi b/numpy/typing/tests/data/reveal/arraypad.pyi index 03c03fb4e..995f82b57 100644 --- a/numpy/typing/tests/data/reveal/arraypad.pyi +++ b/numpy/typing/tests/data/reveal/arraypad.pyi @@ -14,8 +14,8 @@ AR_i8: npt.NDArray[np.int64] AR_f8: npt.NDArray[np.float64] AR_LIKE: List[int] -reveal_type(np.pad(AR_i8, (2, 3), "constant")) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.pad(AR_LIKE, (2, 3), "constant")) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.pad(AR_i8, (2, 3), "constant")) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.pad(AR_LIKE, (2, 3), "constant")) # E: ndarray[Any, dtype[Any]] -reveal_type(np.pad(AR_f8, (2, 3), mode_func)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.pad(AR_f8, (2, 3), mode_func, a=1, b=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.pad(AR_f8, (2, 3), mode_func)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.pad(AR_f8, (2, 3), mode_func, a=1, b=2)) # E: ndarray[Any, dtype[{float64}]] diff --git a/numpy/typing/tests/data/reveal/arraysetops.pyi b/numpy/typing/tests/data/reveal/arraysetops.pyi index c8aeb03ab..9deff8a8e 100644 --- a/numpy/typing/tests/data/reveal/arraysetops.pyi +++ b/numpy/typing/tests/data/reveal/arraysetops.pyi @@ -9,52 +9,52 @@ AR_O: npt.NDArray[np.object_] AR_LIKE_f8: list[float] -reveal_type(np.ediff1d(AR_b)) # E: numpy.ndarray[Any, numpy.dtype[{int8}]] -reveal_type(np.ediff1d(AR_i8, to_end=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.ediff1d(AR_M)) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(np.ediff1d(AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(np.ediff1d(AR_LIKE_f8, to_begin=[1, 1.5])) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.intersect1d(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.intersect1d(AR_M, AR_M, assume_unique=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(np.intersect1d(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.intersect1d(AR_f8, AR_f8, return_indices=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] - -reveal_type(np.setxor1d(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.setxor1d(AR_M, AR_M, assume_unique=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(np.setxor1d(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.in1d(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.in1d(AR_M, AR_M, assume_unique=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.in1d(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.in1d(AR_f8, AR_LIKE_f8, invert=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(np.isin(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isin(AR_M, AR_M, assume_unique=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isin(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isin(AR_f8, AR_LIKE_f8, invert=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(np.union1d(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.union1d(AR_M, AR_M)) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(np.union1d(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.setdiff1d(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.setdiff1d(AR_M, AR_M, assume_unique=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(np.setdiff1d(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.unique(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.unique(AR_LIKE_f8, axis=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.unique(AR_f8, return_index=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_LIKE_f8, return_index=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_f8, return_inverse=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_LIKE_f8, return_inverse=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_f8, return_counts=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_LIKE_f8, return_counts=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_f8, return_index=True, return_inverse=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_LIKE_f8, return_index=True, return_inverse=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_f8, return_index=True, return_counts=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_LIKE_f8, return_index=True, return_counts=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_f8, return_inverse=True, return_counts=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_LIKE_f8, return_inverse=True, return_counts=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_f8, return_index=True, return_inverse=True, return_counts=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unique(AR_LIKE_f8, return_index=True, return_inverse=True, return_counts=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] +reveal_type(np.ediff1d(AR_b)) # E: ndarray[Any, dtype[{int8}]] +reveal_type(np.ediff1d(AR_i8, to_end=[1, 2, 3])) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.ediff1d(AR_M)) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(np.ediff1d(AR_O)) # E: ndarray[Any, dtype[object_]] +reveal_type(np.ediff1d(AR_LIKE_f8, to_begin=[1, 1.5])) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.intersect1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.intersect1d(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.intersect1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.intersect1d(AR_f8, AR_f8, return_indices=True)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] + +reveal_type(np.setxor1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.setxor1d(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.setxor1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.in1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.in1d(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.in1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.in1d(AR_f8, AR_LIKE_f8, invert=True)) # E: ndarray[Any, dtype[bool_]] + +reveal_type(np.isin(AR_i8, AR_i8)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isin(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isin(AR_f8, AR_i8)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isin(AR_f8, AR_LIKE_f8, invert=True)) # E: ndarray[Any, dtype[bool_]] + +reveal_type(np.union1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.union1d(AR_M, AR_M)) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.union1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.setdiff1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.setdiff1d(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.setdiff1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.unique(AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.unique(AR_LIKE_f8, axis=0)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.unique(AR_f8, return_index=True)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_LIKE_f8, return_index=True)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_f8, return_inverse=True)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_LIKE_f8, return_inverse=True)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_f8, return_counts=True)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_LIKE_f8, return_counts=True)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_f8, return_index=True, return_inverse=True)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_LIKE_f8, return_index=True, return_inverse=True)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_f8, return_index=True, return_counts=True)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_LIKE_f8, return_index=True, return_counts=True)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_f8, return_inverse=True, return_counts=True)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_LIKE_f8, return_inverse=True, return_counts=True)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_f8, return_index=True, return_inverse=True, return_counts=True)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.unique(AR_LIKE_f8, return_index=True, return_inverse=True, return_counts=True)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] diff --git a/numpy/typing/tests/data/reveal/arrayterator.pyi b/numpy/typing/tests/data/reveal/arrayterator.pyi index ea4e75612..2dab9d08c 100644 --- a/numpy/typing/tests/data/reveal/arrayterator.pyi +++ b/numpy/typing/tests/data/reveal/arrayterator.pyi @@ -4,7 +4,7 @@ import numpy as np AR_i8: np.ndarray[Any, np.dtype[np.int64]] ar_iter = np.lib.Arrayterator(AR_i8) -reveal_type(ar_iter.var) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(ar_iter.var) # E: ndarray[Any, dtype[{int64}]] reveal_type(ar_iter.buf_size) # E: Union[None, builtins.int] reveal_type(ar_iter.start) # E: builtins.list[builtins.int] reveal_type(ar_iter.stop) # E: builtins.list[builtins.int] @@ -12,13 +12,13 @@ reveal_type(ar_iter.step) # E: builtins.list[builtins.int] reveal_type(ar_iter.shape) # E: builtins.tuple[builtins.int] reveal_type(ar_iter.flat) # E: typing.Generator[{int64}, None, None] -reveal_type(ar_iter.__array__()) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(ar_iter.__array__()) # E: ndarray[Any, dtype[{int64}]] for i in ar_iter: - reveal_type(i) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] + reveal_type(i) # E: ndarray[Any, dtype[{int64}]] -reveal_type(ar_iter[0]) # E: numpy.lib.arrayterator.Arrayterator[Any, numpy.dtype[{int64}]] -reveal_type(ar_iter[...]) # E: numpy.lib.arrayterator.Arrayterator[Any, numpy.dtype[{int64}]] -reveal_type(ar_iter[:]) # E: numpy.lib.arrayterator.Arrayterator[Any, numpy.dtype[{int64}]] -reveal_type(ar_iter[0, 0, 0]) # E: numpy.lib.arrayterator.Arrayterator[Any, numpy.dtype[{int64}]] -reveal_type(ar_iter[..., 0, :]) # E: numpy.lib.arrayterator.Arrayterator[Any, numpy.dtype[{int64}]] +reveal_type(ar_iter[0]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]] +reveal_type(ar_iter[...]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]] +reveal_type(ar_iter[:]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]] +reveal_type(ar_iter[0, 0, 0]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]] +reveal_type(ar_iter[..., 0, :]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]] diff --git a/numpy/typing/tests/data/reveal/bitwise_ops.pyi b/numpy/typing/tests/data/reveal/bitwise_ops.pyi index 6b9969568..f293ef65b 100644 --- a/numpy/typing/tests/data/reveal/bitwise_ops.pyi +++ b/numpy/typing/tests/data/reveal/bitwise_ops.pyi @@ -75,17 +75,17 @@ reveal_type(u4 | u4) # E: {uint32} reveal_type(u4 ^ u4) # E: {uint32} reveal_type(u4 & u4) # E: {uint32} -reveal_type(u4 << i4) # E: numpy.signedinteger[Any] -reveal_type(u4 >> i4) # E: numpy.signedinteger[Any] -reveal_type(u4 | i4) # E: numpy.signedinteger[Any] -reveal_type(u4 ^ i4) # E: numpy.signedinteger[Any] -reveal_type(u4 & i4) # E: numpy.signedinteger[Any] - -reveal_type(u4 << i) # E: numpy.signedinteger[Any] -reveal_type(u4 >> i) # E: numpy.signedinteger[Any] -reveal_type(u4 | i) # E: numpy.signedinteger[Any] -reveal_type(u4 ^ i) # E: numpy.signedinteger[Any] -reveal_type(u4 & i) # E: numpy.signedinteger[Any] +reveal_type(u4 << i4) # E: signedinteger[Any] +reveal_type(u4 >> i4) # E: signedinteger[Any] +reveal_type(u4 | i4) # E: signedinteger[Any] +reveal_type(u4 ^ i4) # E: signedinteger[Any] +reveal_type(u4 & i4) # E: signedinteger[Any] + +reveal_type(u4 << i) # E: signedinteger[Any] +reveal_type(u4 >> i) # E: signedinteger[Any] +reveal_type(u4 | i) # E: signedinteger[Any] +reveal_type(u4 ^ i) # E: signedinteger[Any] +reveal_type(u4 & i) # E: signedinteger[Any] reveal_type(u8 << b_) # E: {uint64} reveal_type(u8 >> b_) # E: {uint64} @@ -101,9 +101,9 @@ reveal_type(u8 & b) # E: {uint64} reveal_type(b_ << b_) # E: {int8} reveal_type(b_ >> b_) # E: {int8} -reveal_type(b_ | b_) # E: numpy.bool_ -reveal_type(b_ ^ b_) # E: numpy.bool_ -reveal_type(b_ & b_) # E: numpy.bool_ +reveal_type(b_ | b_) # E: bool_ +reveal_type(b_ ^ b_) # E: bool_ +reveal_type(b_ & b_) # E: bool_ reveal_type(b_ << AR) # E: Any reveal_type(b_ >> AR) # E: Any @@ -113,9 +113,9 @@ reveal_type(b_ & AR) # E: Any reveal_type(b_ << b) # E: {int8} reveal_type(b_ >> b) # E: {int8} -reveal_type(b_ | b) # E: numpy.bool_ -reveal_type(b_ ^ b) # E: numpy.bool_ -reveal_type(b_ & b) # E: numpy.bool_ +reveal_type(b_ | b) # E: bool_ +reveal_type(b_ ^ b) # E: bool_ +reveal_type(b_ & b) # E: bool_ reveal_type(b_ << i) # E: {int_} reveal_type(b_ >> i) # E: {int_} @@ -127,5 +127,5 @@ reveal_type(~i8) # E: {int64} reveal_type(~i4) # E: {int32} reveal_type(~u8) # E: {uint64} reveal_type(~u4) # E: {uint32} -reveal_type(~b_) # E: numpy.bool_ +reveal_type(~b_) # E: bool_ reveal_type(~AR) # E: Any diff --git a/numpy/typing/tests/data/reveal/char.pyi b/numpy/typing/tests/data/reveal/char.pyi index dd2e76a2d..ce8c1b269 100644 --- a/numpy/typing/tests/data/reveal/char.pyi +++ b/numpy/typing/tests/data/reveal/char.pyi @@ -5,143 +5,143 @@ from typing import Sequence AR_U: npt.NDArray[np.str_] AR_S: npt.NDArray[np.bytes_] -reveal_type(np.char.equal(AR_U, AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.equal(AR_S, AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.equal(AR_U, AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.equal(AR_S, AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.not_equal(AR_U, AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.not_equal(AR_S, AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.not_equal(AR_U, AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.not_equal(AR_S, AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.greater_equal(AR_U, AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.greater_equal(AR_S, AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.greater_equal(AR_U, AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.greater_equal(AR_S, AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.less_equal(AR_U, AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.less_equal(AR_S, AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.less_equal(AR_U, AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.less_equal(AR_S, AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.greater(AR_U, AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.greater(AR_S, AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.greater(AR_U, AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.greater(AR_S, AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.less(AR_U, AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.less(AR_S, AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.less(AR_U, AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.less(AR_S, AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.multiply(AR_U, 5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.multiply(AR_S, [5, 4, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.multiply(AR_U, 5)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.multiply(AR_S, [5, 4, 3])) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.mod(AR_U, "test")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.mod(AR_S, "test")) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.mod(AR_U, "test")) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.mod(AR_S, "test")) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.capitalize(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.capitalize(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.capitalize(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.capitalize(AR_S)) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.center(AR_U, 5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.center(AR_S, [2, 3, 4], b"a")) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.center(AR_U, 5)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.center(AR_S, [2, 3, 4], b"a")) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.encode(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.decode(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.char.encode(AR_U)) # E: ndarray[Any, dtype[bytes_]] +reveal_type(np.char.decode(AR_S)) # E: ndarray[Any, dtype[str_]] -reveal_type(np.char.expandtabs(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.expandtabs(AR_S, tabsize=4)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.expandtabs(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.expandtabs(AR_S, tabsize=4)) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.join(AR_U, "_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.join(AR_S, [b"_", b""])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.join(AR_U, "_")) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.join(AR_S, [b"_", b""])) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.ljust(AR_U, 5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.ljust(AR_S, [4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.rjust(AR_U, 5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.rjust(AR_S, [4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.ljust(AR_U, 5)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.ljust(AR_S, [4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: ndarray[Any, dtype[bytes_]] +reveal_type(np.char.rjust(AR_U, 5)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.rjust(AR_S, [4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.lstrip(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.lstrip(AR_S, chars=b"_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.rstrip(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.rstrip(AR_S, chars=b"_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.strip(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.strip(AR_S, chars=b"_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.lstrip(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.lstrip(AR_S, chars=b"_")) # E: ndarray[Any, dtype[bytes_]] +reveal_type(np.char.rstrip(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.rstrip(AR_S, chars=b"_")) # E: ndarray[Any, dtype[bytes_]] +reveal_type(np.char.strip(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.strip(AR_S, chars=b"_")) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.partition(AR_U, "\n")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.partition(AR_S, [b"a", b"b", b"c"])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.rpartition(AR_U, "\n")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.rpartition(AR_S, [b"a", b"b", b"c"])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.partition(AR_U, "\n")) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.partition(AR_S, [b"a", b"b", b"c"])) # E: ndarray[Any, dtype[bytes_]] +reveal_type(np.char.rpartition(AR_U, "\n")) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.rpartition(AR_S, [b"a", b"b", b"c"])) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.replace(AR_U, "_", "-")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.replace(AR_S, [b"_", b""], [b"a", b"b"])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.replace(AR_U, "_", "-")) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.replace(AR_S, [b"_", b""], [b"a", b"b"])) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.split(AR_U, "_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(np.char.split(AR_S, maxsplit=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(np.char.rsplit(AR_U, "_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(np.char.rsplit(AR_S, maxsplit=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(np.char.split(AR_U, "_")) # E: ndarray[Any, dtype[object_]] +reveal_type(np.char.split(AR_S, maxsplit=[1, 2, 3])) # E: ndarray[Any, dtype[object_]] +reveal_type(np.char.rsplit(AR_U, "_")) # E: ndarray[Any, dtype[object_]] +reveal_type(np.char.rsplit(AR_S, maxsplit=[1, 2, 3])) # E: ndarray[Any, dtype[object_]] -reveal_type(np.char.splitlines(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(np.char.splitlines(AR_S, keepends=[True, True, False])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(np.char.splitlines(AR_U)) # E: ndarray[Any, dtype[object_]] +reveal_type(np.char.splitlines(AR_S, keepends=[True, True, False])) # E: ndarray[Any, dtype[object_]] -reveal_type(np.char.swapcase(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.swapcase(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.swapcase(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.swapcase(AR_S)) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.title(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.title(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.title(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.title(AR_S)) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.upper(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.upper(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.upper(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.upper(AR_S)) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.zfill(AR_U, 5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.zfill(AR_S, [2, 3, 4])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(np.char.zfill(AR_U, 5)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.char.zfill(AR_S, [2, 3, 4])) # E: ndarray[Any, dtype[bytes_]] -reveal_type(np.char.count(AR_U, "a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.char.count(AR_S, [b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(np.char.count(AR_U, "a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.char.count(AR_S, [b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] -reveal_type(np.char.endswith(AR_U, "a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.endswith(AR_S, [b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.startswith(AR_U, "a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.startswith(AR_S, [b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.endswith(AR_U, "a", start=[1, 2, 3])) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.endswith(AR_S, [b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.startswith(AR_U, "a", start=[1, 2, 3])) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.startswith(AR_S, [b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.find(AR_U, "a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.char.find(AR_S, [b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.char.rfind(AR_U, "a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.char.rfind(AR_S, [b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(np.char.find(AR_U, "a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.char.find(AR_S, [b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.char.rfind(AR_U, "a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.char.rfind(AR_S, [b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] -reveal_type(np.char.index(AR_U, "a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.char.index(AR_S, [b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.char.rindex(AR_U, "a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.char.rindex(AR_S, [b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(np.char.index(AR_U, "a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.char.index(AR_S, [b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.char.rindex(AR_U, "a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.char.rindex(AR_S, [b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] -reveal_type(np.char.isalpha(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.isalpha(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.isalpha(AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.isalpha(AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.isalnum(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.isalnum(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.isalnum(AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.isalnum(AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.isdecimal(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.isdecimal(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.isdecimal(AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.isdecimal(AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.isdigit(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.isdigit(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.isdigit(AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.isdigit(AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.islower(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.islower(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.islower(AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.islower(AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.isnumeric(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.isnumeric(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.isnumeric(AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.isnumeric(AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.isspace(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.isspace(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.isspace(AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.isspace(AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.istitle(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.istitle(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.istitle(AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.istitle(AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.isupper(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.char.isupper(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.char.isupper(AR_U)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.char.isupper(AR_S)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.char.str_len(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.char.str_len(AR_S)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(np.char.str_len(AR_U)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.char.str_len(AR_S)) # E: ndarray[Any, dtype[{int_}]] -reveal_type(np.char.array(AR_U)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.array(AR_S, order="K")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.array("bob", copy=True)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.array(b"bob", itemsize=5)) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.array(1, unicode=False)) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.array(1, unicode=True)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.char.array(AR_U)) # E: chararray[Any, dtype[str_]] +reveal_type(np.char.array(AR_S, order="K")) # E: chararray[Any, dtype[bytes_]] +reveal_type(np.char.array("bob", copy=True)) # E: chararray[Any, dtype[str_]] +reveal_type(np.char.array(b"bob", itemsize=5)) # E: chararray[Any, dtype[bytes_]] +reveal_type(np.char.array(1, unicode=False)) # E: chararray[Any, dtype[bytes_]] +reveal_type(np.char.array(1, unicode=True)) # E: chararray[Any, dtype[str_]] -reveal_type(np.char.asarray(AR_U)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.asarray(AR_S, order="K")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.asarray("bob")) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.char.asarray(b"bob", itemsize=5)) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.asarray(1, unicode=False)) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(np.char.asarray(1, unicode=True)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.char.asarray(AR_U)) # E: chararray[Any, dtype[str_]] +reveal_type(np.char.asarray(AR_S, order="K")) # E: chararray[Any, dtype[bytes_]] +reveal_type(np.char.asarray("bob")) # E: chararray[Any, dtype[str_]] +reveal_type(np.char.asarray(b"bob", itemsize=5)) # E: chararray[Any, dtype[bytes_]] +reveal_type(np.char.asarray(1, unicode=False)) # E: chararray[Any, dtype[bytes_]] +reveal_type(np.char.asarray(1, unicode=True)) # E: chararray[Any, dtype[str_]] diff --git a/numpy/typing/tests/data/reveal/chararray.pyi b/numpy/typing/tests/data/reveal/chararray.pyi index c0a39c92b..3da2e1599 100644 --- a/numpy/typing/tests/data/reveal/chararray.pyi +++ b/numpy/typing/tests/data/reveal/chararray.pyi @@ -4,126 +4,126 @@ from typing import Any AR_U: np.chararray[Any, np.dtype[np.str_]] AR_S: np.chararray[Any, np.dtype[np.bytes_]] -reveal_type(AR_U == AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S == AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U == AR_U) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S == AR_S) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U != AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S != AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U != AR_U) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S != AR_S) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U >= AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S >= AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U >= AR_U) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S >= AR_S) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U <= AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S <= AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U <= AR_U) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S <= AR_S) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U > AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S > AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U > AR_U) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S > AR_S) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U < AR_U) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S < AR_S) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U < AR_U) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S < AR_S) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U * 5) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S * [5]) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U * 5) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S * [5]) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U % "test") # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S % b"test") # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U % "test") # E: chararray[Any, dtype[str_]] +reveal_type(AR_S % b"test") # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.capitalize()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.capitalize()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.capitalize()) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.capitalize()) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.center(5)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.center([2, 3, 4], b"a")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.center(5)) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.center([2, 3, 4], b"a")) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.encode()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(AR_S.decode()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_U.encode()) # E: chararray[Any, dtype[bytes_]] +reveal_type(AR_S.decode()) # E: chararray[Any, dtype[str_]] -reveal_type(AR_U.expandtabs()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.expandtabs(tabsize=4)) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.expandtabs()) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.expandtabs(tabsize=4)) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.join("_")) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.join([b"_", b""])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.join("_")) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.join([b"_", b""])) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.ljust(5)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.ljust([4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(AR_U.rjust(5)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.rjust([4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.ljust(5)) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.ljust([4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: chararray[Any, dtype[bytes_]] +reveal_type(AR_U.rjust(5)) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.rjust([4, 3, 1], fillchar=[b"a", b"b", b"c"])) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.lstrip()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.lstrip(chars=b"_")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(AR_U.rstrip()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.rstrip(chars=b"_")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(AR_U.strip()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.strip(chars=b"_")) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.lstrip()) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.lstrip(chars=b"_")) # E: chararray[Any, dtype[bytes_]] +reveal_type(AR_U.rstrip()) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.rstrip(chars=b"_")) # E: chararray[Any, dtype[bytes_]] +reveal_type(AR_U.strip()) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.strip(chars=b"_")) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.partition("\n")) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.partition([b"a", b"b", b"c"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] -reveal_type(AR_U.rpartition("\n")) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.rpartition([b"a", b"b", b"c"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.partition("\n")) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.partition([b"a", b"b", b"c"])) # E: chararray[Any, dtype[bytes_]] +reveal_type(AR_U.rpartition("\n")) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.rpartition([b"a", b"b", b"c"])) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.replace("_", "-")) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.replace([b"_", b""], [b"a", b"b"])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.replace("_", "-")) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.replace([b"_", b""], [b"a", b"b"])) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.split("_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(AR_S.split(maxsplit=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(AR_U.rsplit("_")) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(AR_S.rsplit(maxsplit=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(AR_U.split("_")) # E: ndarray[Any, dtype[object_]] +reveal_type(AR_S.split(maxsplit=[1, 2, 3])) # E: ndarray[Any, dtype[object_]] +reveal_type(AR_U.rsplit("_")) # E: ndarray[Any, dtype[object_]] +reveal_type(AR_S.rsplit(maxsplit=[1, 2, 3])) # E: ndarray[Any, dtype[object_]] -reveal_type(AR_U.splitlines()) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(AR_S.splitlines(keepends=[True, True, False])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(AR_U.splitlines()) # E: ndarray[Any, dtype[object_]] +reveal_type(AR_S.splitlines(keepends=[True, True, False])) # E: ndarray[Any, dtype[object_]] -reveal_type(AR_U.swapcase()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.swapcase()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.swapcase()) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.swapcase()) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.title()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.title()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.title()) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.title()) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.upper()) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.upper()) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.upper()) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.upper()) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.zfill(5)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(AR_S.zfill([2, 3, 4])) # E: numpy.chararray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(AR_U.zfill(5)) # E: chararray[Any, dtype[str_]] +reveal_type(AR_S.zfill([2, 3, 4])) # E: chararray[Any, dtype[bytes_]] -reveal_type(AR_U.count("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(AR_S.count([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_U.count("a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(AR_S.count([b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] -reveal_type(AR_U.endswith("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.endswith([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_U.startswith("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.startswith([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.endswith("a", start=[1, 2, 3])) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.endswith([b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_U.startswith("a", start=[1, 2, 3])) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.startswith([b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U.find("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(AR_S.find([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(AR_U.rfind("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(AR_S.rfind([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_U.find("a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(AR_S.find([b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(AR_U.rfind("a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(AR_S.rfind([b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] -reveal_type(AR_U.index("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(AR_S.index([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(AR_U.rindex("a", start=[1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(AR_S.rindex([b"a", b"b", b"c"], end=9)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(AR_U.index("a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(AR_S.index([b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(AR_U.rindex("a", start=[1, 2, 3])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(AR_S.rindex([b"a", b"b", b"c"], end=9)) # E: ndarray[Any, dtype[{int_}]] -reveal_type(AR_U.isalpha()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.isalpha()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.isalpha()) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.isalpha()) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U.isalnum()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.isalnum()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.isalnum()) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.isalnum()) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U.isdecimal()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.isdecimal()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.isdecimal()) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.isdecimal()) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U.isdigit()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.isdigit()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.isdigit()) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.isdigit()) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U.islower()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.islower()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.islower()) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.islower()) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U.isnumeric()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.isnumeric()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.isnumeric()) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.isnumeric()) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U.isspace()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.isspace()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.isspace()) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.isspace()) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U.istitle()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.istitle()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.istitle()) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.istitle()) # E: ndarray[Any, dtype[bool_]] -reveal_type(AR_U.isupper()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR_S.isupper()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(AR_U.isupper()) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR_S.isupper()) # E: ndarray[Any, dtype[bool_]] diff --git a/numpy/typing/tests/data/reveal/comparisons.pyi b/numpy/typing/tests/data/reveal/comparisons.pyi index 16f21cc39..ecd8ea690 100644 --- a/numpy/typing/tests/data/reveal/comparisons.pyi +++ b/numpy/typing/tests/data/reveal/comparisons.pyi @@ -27,226 +27,226 @@ SEQ = (0, 1, 2, 3, 4) # Time structures -reveal_type(dt > dt) # E: numpy.bool_ +reveal_type(dt > dt) # E: bool_ -reveal_type(td > td) # E: numpy.bool_ -reveal_type(td > i) # E: numpy.bool_ -reveal_type(td > i4) # E: numpy.bool_ -reveal_type(td > i8) # E: numpy.bool_ +reveal_type(td > td) # E: bool_ +reveal_type(td > i) # E: bool_ +reveal_type(td > i4) # E: bool_ +reveal_type(td > i8) # E: bool_ -reveal_type(td > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(td > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(AR > td) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > td) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(td > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(td > SEQ) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR > SEQ) # E: ndarray[Any, dtype[bool_]] +reveal_type(AR > td) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > td) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > AR) # E: ndarray[Any, dtype[bool_]] # boolean -reveal_type(b_ > b) # E: numpy.bool_ -reveal_type(b_ > b_) # E: numpy.bool_ -reveal_type(b_ > i) # E: numpy.bool_ -reveal_type(b_ > i8) # E: numpy.bool_ -reveal_type(b_ > i4) # E: numpy.bool_ -reveal_type(b_ > u8) # E: numpy.bool_ -reveal_type(b_ > u4) # E: numpy.bool_ -reveal_type(b_ > f) # E: numpy.bool_ -reveal_type(b_ > f8) # E: numpy.bool_ -reveal_type(b_ > f4) # E: numpy.bool_ -reveal_type(b_ > c) # E: numpy.bool_ -reveal_type(b_ > c16) # E: numpy.bool_ -reveal_type(b_ > c8) # E: numpy.bool_ -reveal_type(b_ > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(b_ > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(b_ > b) # E: bool_ +reveal_type(b_ > b_) # E: bool_ +reveal_type(b_ > i) # E: bool_ +reveal_type(b_ > i8) # E: bool_ +reveal_type(b_ > i4) # E: bool_ +reveal_type(b_ > u8) # E: bool_ +reveal_type(b_ > u4) # E: bool_ +reveal_type(b_ > f) # E: bool_ +reveal_type(b_ > f8) # E: bool_ +reveal_type(b_ > f4) # E: bool_ +reveal_type(b_ > c) # E: bool_ +reveal_type(b_ > c16) # E: bool_ +reveal_type(b_ > c8) # E: bool_ +reveal_type(b_ > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(b_ > SEQ) # E: ndarray[Any, dtype[bool_]] # Complex -reveal_type(c16 > c16) # E: numpy.bool_ -reveal_type(c16 > f8) # E: numpy.bool_ -reveal_type(c16 > i8) # E: numpy.bool_ -reveal_type(c16 > c8) # E: numpy.bool_ -reveal_type(c16 > f4) # E: numpy.bool_ -reveal_type(c16 > i4) # E: numpy.bool_ -reveal_type(c16 > b_) # E: numpy.bool_ -reveal_type(c16 > b) # E: numpy.bool_ -reveal_type(c16 > c) # E: numpy.bool_ -reveal_type(c16 > f) # E: numpy.bool_ -reveal_type(c16 > i) # E: numpy.bool_ -reveal_type(c16 > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(c16 > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(c16 > c16) # E: numpy.bool_ -reveal_type(f8 > c16) # E: numpy.bool_ -reveal_type(i8 > c16) # E: numpy.bool_ -reveal_type(c8 > c16) # E: numpy.bool_ -reveal_type(f4 > c16) # E: numpy.bool_ -reveal_type(i4 > c16) # E: numpy.bool_ -reveal_type(b_ > c16) # E: numpy.bool_ -reveal_type(b > c16) # E: numpy.bool_ -reveal_type(c > c16) # E: numpy.bool_ -reveal_type(f > c16) # E: numpy.bool_ -reveal_type(i > c16) # E: numpy.bool_ -reveal_type(AR > c16) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > c16) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(c8 > c16) # E: numpy.bool_ -reveal_type(c8 > f8) # E: numpy.bool_ -reveal_type(c8 > i8) # E: numpy.bool_ -reveal_type(c8 > c8) # E: numpy.bool_ -reveal_type(c8 > f4) # E: numpy.bool_ -reveal_type(c8 > i4) # E: numpy.bool_ -reveal_type(c8 > b_) # E: numpy.bool_ -reveal_type(c8 > b) # E: numpy.bool_ -reveal_type(c8 > c) # E: numpy.bool_ -reveal_type(c8 > f) # E: numpy.bool_ -reveal_type(c8 > i) # E: numpy.bool_ -reveal_type(c8 > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(c8 > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(c16 > c8) # E: numpy.bool_ -reveal_type(f8 > c8) # E: numpy.bool_ -reveal_type(i8 > c8) # E: numpy.bool_ -reveal_type(c8 > c8) # E: numpy.bool_ -reveal_type(f4 > c8) # E: numpy.bool_ -reveal_type(i4 > c8) # E: numpy.bool_ -reveal_type(b_ > c8) # E: numpy.bool_ -reveal_type(b > c8) # E: numpy.bool_ -reveal_type(c > c8) # E: numpy.bool_ -reveal_type(f > c8) # E: numpy.bool_ -reveal_type(i > c8) # E: numpy.bool_ -reveal_type(AR > c8) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > c8) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(c16 > c16) # E: bool_ +reveal_type(c16 > f8) # E: bool_ +reveal_type(c16 > i8) # E: bool_ +reveal_type(c16 > c8) # E: bool_ +reveal_type(c16 > f4) # E: bool_ +reveal_type(c16 > i4) # E: bool_ +reveal_type(c16 > b_) # E: bool_ +reveal_type(c16 > b) # E: bool_ +reveal_type(c16 > c) # E: bool_ +reveal_type(c16 > f) # E: bool_ +reveal_type(c16 > i) # E: bool_ +reveal_type(c16 > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(c16 > SEQ) # E: ndarray[Any, dtype[bool_]] + +reveal_type(c16 > c16) # E: bool_ +reveal_type(f8 > c16) # E: bool_ +reveal_type(i8 > c16) # E: bool_ +reveal_type(c8 > c16) # E: bool_ +reveal_type(f4 > c16) # E: bool_ +reveal_type(i4 > c16) # E: bool_ +reveal_type(b_ > c16) # E: bool_ +reveal_type(b > c16) # E: bool_ +reveal_type(c > c16) # E: bool_ +reveal_type(f > c16) # E: bool_ +reveal_type(i > c16) # E: bool_ +reveal_type(AR > c16) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > c16) # E: ndarray[Any, dtype[bool_]] + +reveal_type(c8 > c16) # E: bool_ +reveal_type(c8 > f8) # E: bool_ +reveal_type(c8 > i8) # E: bool_ +reveal_type(c8 > c8) # E: bool_ +reveal_type(c8 > f4) # E: bool_ +reveal_type(c8 > i4) # E: bool_ +reveal_type(c8 > b_) # E: bool_ +reveal_type(c8 > b) # E: bool_ +reveal_type(c8 > c) # E: bool_ +reveal_type(c8 > f) # E: bool_ +reveal_type(c8 > i) # E: bool_ +reveal_type(c8 > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(c8 > SEQ) # E: ndarray[Any, dtype[bool_]] + +reveal_type(c16 > c8) # E: bool_ +reveal_type(f8 > c8) # E: bool_ +reveal_type(i8 > c8) # E: bool_ +reveal_type(c8 > c8) # E: bool_ +reveal_type(f4 > c8) # E: bool_ +reveal_type(i4 > c8) # E: bool_ +reveal_type(b_ > c8) # E: bool_ +reveal_type(b > c8) # E: bool_ +reveal_type(c > c8) # E: bool_ +reveal_type(f > c8) # E: bool_ +reveal_type(i > c8) # E: bool_ +reveal_type(AR > c8) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > c8) # E: ndarray[Any, dtype[bool_]] # Float -reveal_type(f8 > f8) # E: numpy.bool_ -reveal_type(f8 > i8) # E: numpy.bool_ -reveal_type(f8 > f4) # E: numpy.bool_ -reveal_type(f8 > i4) # E: numpy.bool_ -reveal_type(f8 > b_) # E: numpy.bool_ -reveal_type(f8 > b) # E: numpy.bool_ -reveal_type(f8 > c) # E: numpy.bool_ -reveal_type(f8 > f) # E: numpy.bool_ -reveal_type(f8 > i) # E: numpy.bool_ -reveal_type(f8 > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(f8 > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(f8 > f8) # E: numpy.bool_ -reveal_type(i8 > f8) # E: numpy.bool_ -reveal_type(f4 > f8) # E: numpy.bool_ -reveal_type(i4 > f8) # E: numpy.bool_ -reveal_type(b_ > f8) # E: numpy.bool_ -reveal_type(b > f8) # E: numpy.bool_ -reveal_type(c > f8) # E: numpy.bool_ -reveal_type(f > f8) # E: numpy.bool_ -reveal_type(i > f8) # E: numpy.bool_ -reveal_type(AR > f8) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > f8) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(f4 > f8) # E: numpy.bool_ -reveal_type(f4 > i8) # E: numpy.bool_ -reveal_type(f4 > f4) # E: numpy.bool_ -reveal_type(f4 > i4) # E: numpy.bool_ -reveal_type(f4 > b_) # E: numpy.bool_ -reveal_type(f4 > b) # E: numpy.bool_ -reveal_type(f4 > c) # E: numpy.bool_ -reveal_type(f4 > f) # E: numpy.bool_ -reveal_type(f4 > i) # E: numpy.bool_ -reveal_type(f4 > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(f4 > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(f8 > f4) # E: numpy.bool_ -reveal_type(i8 > f4) # E: numpy.bool_ -reveal_type(f4 > f4) # E: numpy.bool_ -reveal_type(i4 > f4) # E: numpy.bool_ -reveal_type(b_ > f4) # E: numpy.bool_ -reveal_type(b > f4) # E: numpy.bool_ -reveal_type(c > f4) # E: numpy.bool_ -reveal_type(f > f4) # E: numpy.bool_ -reveal_type(i > f4) # E: numpy.bool_ -reveal_type(AR > f4) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > f4) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(f8 > f8) # E: bool_ +reveal_type(f8 > i8) # E: bool_ +reveal_type(f8 > f4) # E: bool_ +reveal_type(f8 > i4) # E: bool_ +reveal_type(f8 > b_) # E: bool_ +reveal_type(f8 > b) # E: bool_ +reveal_type(f8 > c) # E: bool_ +reveal_type(f8 > f) # E: bool_ +reveal_type(f8 > i) # E: bool_ +reveal_type(f8 > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(f8 > SEQ) # E: ndarray[Any, dtype[bool_]] + +reveal_type(f8 > f8) # E: bool_ +reveal_type(i8 > f8) # E: bool_ +reveal_type(f4 > f8) # E: bool_ +reveal_type(i4 > f8) # E: bool_ +reveal_type(b_ > f8) # E: bool_ +reveal_type(b > f8) # E: bool_ +reveal_type(c > f8) # E: bool_ +reveal_type(f > f8) # E: bool_ +reveal_type(i > f8) # E: bool_ +reveal_type(AR > f8) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > f8) # E: ndarray[Any, dtype[bool_]] + +reveal_type(f4 > f8) # E: bool_ +reveal_type(f4 > i8) # E: bool_ +reveal_type(f4 > f4) # E: bool_ +reveal_type(f4 > i4) # E: bool_ +reveal_type(f4 > b_) # E: bool_ +reveal_type(f4 > b) # E: bool_ +reveal_type(f4 > c) # E: bool_ +reveal_type(f4 > f) # E: bool_ +reveal_type(f4 > i) # E: bool_ +reveal_type(f4 > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(f4 > SEQ) # E: ndarray[Any, dtype[bool_]] + +reveal_type(f8 > f4) # E: bool_ +reveal_type(i8 > f4) # E: bool_ +reveal_type(f4 > f4) # E: bool_ +reveal_type(i4 > f4) # E: bool_ +reveal_type(b_ > f4) # E: bool_ +reveal_type(b > f4) # E: bool_ +reveal_type(c > f4) # E: bool_ +reveal_type(f > f4) # E: bool_ +reveal_type(i > f4) # E: bool_ +reveal_type(AR > f4) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > f4) # E: ndarray[Any, dtype[bool_]] # Int -reveal_type(i8 > i8) # E: numpy.bool_ -reveal_type(i8 > u8) # E: numpy.bool_ -reveal_type(i8 > i4) # E: numpy.bool_ -reveal_type(i8 > u4) # E: numpy.bool_ -reveal_type(i8 > b_) # E: numpy.bool_ -reveal_type(i8 > b) # E: numpy.bool_ -reveal_type(i8 > c) # E: numpy.bool_ -reveal_type(i8 > f) # E: numpy.bool_ -reveal_type(i8 > i) # E: numpy.bool_ -reveal_type(i8 > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(i8 > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(u8 > u8) # E: numpy.bool_ -reveal_type(u8 > i4) # E: numpy.bool_ -reveal_type(u8 > u4) # E: numpy.bool_ -reveal_type(u8 > b_) # E: numpy.bool_ -reveal_type(u8 > b) # E: numpy.bool_ -reveal_type(u8 > c) # E: numpy.bool_ -reveal_type(u8 > f) # E: numpy.bool_ -reveal_type(u8 > i) # E: numpy.bool_ -reveal_type(u8 > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(u8 > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(i8 > i8) # E: numpy.bool_ -reveal_type(u8 > i8) # E: numpy.bool_ -reveal_type(i4 > i8) # E: numpy.bool_ -reveal_type(u4 > i8) # E: numpy.bool_ -reveal_type(b_ > i8) # E: numpy.bool_ -reveal_type(b > i8) # E: numpy.bool_ -reveal_type(c > i8) # E: numpy.bool_ -reveal_type(f > i8) # E: numpy.bool_ -reveal_type(i > i8) # E: numpy.bool_ -reveal_type(AR > i8) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > i8) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(u8 > u8) # E: numpy.bool_ -reveal_type(i4 > u8) # E: numpy.bool_ -reveal_type(u4 > u8) # E: numpy.bool_ -reveal_type(b_ > u8) # E: numpy.bool_ -reveal_type(b > u8) # E: numpy.bool_ -reveal_type(c > u8) # E: numpy.bool_ -reveal_type(f > u8) # E: numpy.bool_ -reveal_type(i > u8) # E: numpy.bool_ -reveal_type(AR > u8) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > u8) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(i4 > i8) # E: numpy.bool_ -reveal_type(i4 > i4) # E: numpy.bool_ -reveal_type(i4 > i) # E: numpy.bool_ -reveal_type(i4 > b_) # E: numpy.bool_ -reveal_type(i4 > b) # E: numpy.bool_ -reveal_type(i4 > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(i4 > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(u4 > i8) # E: numpy.bool_ -reveal_type(u4 > i4) # E: numpy.bool_ -reveal_type(u4 > u8) # E: numpy.bool_ -reveal_type(u4 > u4) # E: numpy.bool_ -reveal_type(u4 > i) # E: numpy.bool_ -reveal_type(u4 > b_) # E: numpy.bool_ -reveal_type(u4 > b) # E: numpy.bool_ -reveal_type(u4 > AR) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(u4 > SEQ) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(i8 > i4) # E: numpy.bool_ -reveal_type(i4 > i4) # E: numpy.bool_ -reveal_type(i > i4) # E: numpy.bool_ -reveal_type(b_ > i4) # E: numpy.bool_ -reveal_type(b > i4) # E: numpy.bool_ -reveal_type(AR > i4) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > i4) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] - -reveal_type(i8 > u4) # E: numpy.bool_ -reveal_type(i4 > u4) # E: numpy.bool_ -reveal_type(u8 > u4) # E: numpy.bool_ -reveal_type(u4 > u4) # E: numpy.bool_ -reveal_type(b_ > u4) # E: numpy.bool_ -reveal_type(b > u4) # E: numpy.bool_ -reveal_type(i > u4) # E: numpy.bool_ -reveal_type(AR > u4) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(SEQ > u4) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(i8 > i8) # E: bool_ +reveal_type(i8 > u8) # E: bool_ +reveal_type(i8 > i4) # E: bool_ +reveal_type(i8 > u4) # E: bool_ +reveal_type(i8 > b_) # E: bool_ +reveal_type(i8 > b) # E: bool_ +reveal_type(i8 > c) # E: bool_ +reveal_type(i8 > f) # E: bool_ +reveal_type(i8 > i) # E: bool_ +reveal_type(i8 > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(i8 > SEQ) # E: ndarray[Any, dtype[bool_]] + +reveal_type(u8 > u8) # E: bool_ +reveal_type(u8 > i4) # E: bool_ +reveal_type(u8 > u4) # E: bool_ +reveal_type(u8 > b_) # E: bool_ +reveal_type(u8 > b) # E: bool_ +reveal_type(u8 > c) # E: bool_ +reveal_type(u8 > f) # E: bool_ +reveal_type(u8 > i) # E: bool_ +reveal_type(u8 > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(u8 > SEQ) # E: ndarray[Any, dtype[bool_]] + +reveal_type(i8 > i8) # E: bool_ +reveal_type(u8 > i8) # E: bool_ +reveal_type(i4 > i8) # E: bool_ +reveal_type(u4 > i8) # E: bool_ +reveal_type(b_ > i8) # E: bool_ +reveal_type(b > i8) # E: bool_ +reveal_type(c > i8) # E: bool_ +reveal_type(f > i8) # E: bool_ +reveal_type(i > i8) # E: bool_ +reveal_type(AR > i8) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > i8) # E: ndarray[Any, dtype[bool_]] + +reveal_type(u8 > u8) # E: bool_ +reveal_type(i4 > u8) # E: bool_ +reveal_type(u4 > u8) # E: bool_ +reveal_type(b_ > u8) # E: bool_ +reveal_type(b > u8) # E: bool_ +reveal_type(c > u8) # E: bool_ +reveal_type(f > u8) # E: bool_ +reveal_type(i > u8) # E: bool_ +reveal_type(AR > u8) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > u8) # E: ndarray[Any, dtype[bool_]] + +reveal_type(i4 > i8) # E: bool_ +reveal_type(i4 > i4) # E: bool_ +reveal_type(i4 > i) # E: bool_ +reveal_type(i4 > b_) # E: bool_ +reveal_type(i4 > b) # E: bool_ +reveal_type(i4 > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(i4 > SEQ) # E: ndarray[Any, dtype[bool_]] + +reveal_type(u4 > i8) # E: bool_ +reveal_type(u4 > i4) # E: bool_ +reveal_type(u4 > u8) # E: bool_ +reveal_type(u4 > u4) # E: bool_ +reveal_type(u4 > i) # E: bool_ +reveal_type(u4 > b_) # E: bool_ +reveal_type(u4 > b) # E: bool_ +reveal_type(u4 > AR) # E: ndarray[Any, dtype[bool_]] +reveal_type(u4 > SEQ) # E: ndarray[Any, dtype[bool_]] + +reveal_type(i8 > i4) # E: bool_ +reveal_type(i4 > i4) # E: bool_ +reveal_type(i > i4) # E: bool_ +reveal_type(b_ > i4) # E: bool_ +reveal_type(b > i4) # E: bool_ +reveal_type(AR > i4) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > i4) # E: ndarray[Any, dtype[bool_]] + +reveal_type(i8 > u4) # E: bool_ +reveal_type(i4 > u4) # E: bool_ +reveal_type(u8 > u4) # E: bool_ +reveal_type(u4 > u4) # E: bool_ +reveal_type(b_ > u4) # E: bool_ +reveal_type(b > u4) # E: bool_ +reveal_type(i > u4) # E: bool_ +reveal_type(AR > u4) # E: ndarray[Any, dtype[bool_]] +reveal_type(SEQ > u4) # E: ndarray[Any, dtype[bool_]] diff --git a/numpy/typing/tests/data/reveal/constants.pyi b/numpy/typing/tests/data/reveal/constants.pyi index 9a46bfded..37f54ccda 100644 --- a/numpy/typing/tests/data/reveal/constants.pyi +++ b/numpy/typing/tests/data/reveal/constants.pyi @@ -43,8 +43,8 @@ reveal_type(np.WRAP) # E: Literal[1] reveal_type(np.tracemalloc_domain) # E: Literal[389047] reveal_type(np.little_endian) # E: bool -reveal_type(np.True_) # E: numpy.bool_ -reveal_type(np.False_) # E: numpy.bool_ +reveal_type(np.True_) # E: bool_ +reveal_type(np.False_) # E: bool_ reveal_type(np.UFUNC_PYVALS_NAME) # E: Literal['UFUNC_PYVALS'] diff --git a/numpy/typing/tests/data/reveal/ctypeslib.pyi b/numpy/typing/tests/data/reveal/ctypeslib.pyi index 461a447d9..ccbdfe36e 100644 --- a/numpy/typing/tests/data/reveal/ctypeslib.pyi +++ b/numpy/typing/tests/data/reveal/ctypeslib.pyi @@ -24,12 +24,12 @@ pointer: ctypes.pointer[Any] reveal_type(np.ctypeslib.c_intp()) # E: {c_intp} -reveal_type(np.ctypeslib.ndpointer()) # E: Type[numpy.ctypeslib._ndptr[None]] -reveal_type(np.ctypeslib.ndpointer(dtype=np.float64)) # E: Type[numpy.ctypeslib._ndptr[numpy.dtype[{float64}]]] -reveal_type(np.ctypeslib.ndpointer(dtype=float)) # E: Type[numpy.ctypeslib._ndptr[numpy.dtype[Any]]] -reveal_type(np.ctypeslib.ndpointer(shape=(10, 3))) # E: Type[numpy.ctypeslib._ndptr[None]] -reveal_type(np.ctypeslib.ndpointer(np.int64, shape=(10, 3))) # E: Type[numpy.ctypeslib._concrete_ndptr[numpy.dtype[{int64}]]] -reveal_type(np.ctypeslib.ndpointer(int, shape=(1,))) # E: Type[numpy.ctypeslib._concrete_ndptr[numpy.dtype[Any]]] +reveal_type(np.ctypeslib.ndpointer()) # E: Type[ctypeslib._ndptr[None]] +reveal_type(np.ctypeslib.ndpointer(dtype=np.float64)) # E: Type[ctypeslib._ndptr[dtype[{float64}]]] +reveal_type(np.ctypeslib.ndpointer(dtype=float)) # E: Type[ctypeslib._ndptr[dtype[Any]]] +reveal_type(np.ctypeslib.ndpointer(shape=(10, 3))) # E: Type[ctypeslib._ndptr[None]] +reveal_type(np.ctypeslib.ndpointer(np.int64, shape=(10, 3))) # E: Type[ctypeslib._concrete_ndptr[dtype[{int64}]]] +reveal_type(np.ctypeslib.ndpointer(int, shape=(1,))) # E: Type[ctypeslib._concrete_ndptr[dtype[Any]]] reveal_type(np.ctypeslib.as_ctypes_type(np.bool_)) # E: Type[ctypes.c_bool] reveal_type(np.ctypeslib.as_ctypes_type(np.ubyte)) # E: Type[{c_ubyte}] @@ -82,6 +82,6 @@ reveal_type(np.ctypeslib.as_ctypes(AR_double)) # E: ctypes.Array[{c_double}] reveal_type(np.ctypeslib.as_ctypes(AR_longdouble)) # E: ctypes.Array[{c_longdouble}] reveal_type(np.ctypeslib.as_ctypes(AR_void)) # E: ctypes.Array[Any] -reveal_type(np.ctypeslib.as_array(AR_ubyte)) # E: numpy.ndarray[Any, numpy.dtype[{ubyte}]] -reveal_type(np.ctypeslib.as_array(1)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.ctypeslib.as_array(pointer)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.ctypeslib.as_array(AR_ubyte)) # E: ndarray[Any, dtype[{ubyte}]] +reveal_type(np.ctypeslib.as_array(1)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.ctypeslib.as_array(pointer)) # E: ndarray[Any, dtype[Any]] diff --git a/numpy/typing/tests/data/reveal/dtype.pyi b/numpy/typing/tests/data/reveal/dtype.pyi index 364d1dcab..934d7da5e 100644 --- a/numpy/typing/tests/data/reveal/dtype.pyi +++ b/numpy/typing/tests/data/reveal/dtype.pyi @@ -5,72 +5,72 @@ dtype_U: np.dtype[np.str_] dtype_V: np.dtype[np.void] dtype_i8: np.dtype[np.int64] -reveal_type(np.dtype(np.float64)) # E: numpy.dtype[{float64}] -reveal_type(np.dtype(np.int64)) # E: numpy.dtype[{int64}] +reveal_type(np.dtype(np.float64)) # E: dtype[{float64}] +reveal_type(np.dtype(np.int64)) # E: dtype[{int64}] # String aliases -reveal_type(np.dtype("float64")) # E: numpy.dtype[{float64}] -reveal_type(np.dtype("float32")) # E: numpy.dtype[{float32}] -reveal_type(np.dtype("int64")) # E: numpy.dtype[{int64}] -reveal_type(np.dtype("int32")) # E: numpy.dtype[{int32}] -reveal_type(np.dtype("bool")) # E: numpy.dtype[numpy.bool_] -reveal_type(np.dtype("bytes")) # E: numpy.dtype[numpy.bytes_] -reveal_type(np.dtype("str")) # E: numpy.dtype[numpy.str_] +reveal_type(np.dtype("float64")) # E: dtype[{float64}] +reveal_type(np.dtype("float32")) # E: dtype[{float32}] +reveal_type(np.dtype("int64")) # E: dtype[{int64}] +reveal_type(np.dtype("int32")) # E: dtype[{int32}] +reveal_type(np.dtype("bool")) # E: dtype[bool_] +reveal_type(np.dtype("bytes")) # E: dtype[bytes_] +reveal_type(np.dtype("str")) # E: dtype[str_] # Python types -reveal_type(np.dtype(complex)) # E: numpy.dtype[{cdouble}] -reveal_type(np.dtype(float)) # E: numpy.dtype[{double}] -reveal_type(np.dtype(int)) # E: numpy.dtype[{int_}] -reveal_type(np.dtype(bool)) # E: numpy.dtype[numpy.bool_] -reveal_type(np.dtype(str)) # E: numpy.dtype[numpy.str_] -reveal_type(np.dtype(bytes)) # E: numpy.dtype[numpy.bytes_] -reveal_type(np.dtype(object)) # E: numpy.dtype[numpy.object_] +reveal_type(np.dtype(complex)) # E: dtype[{cdouble}] +reveal_type(np.dtype(float)) # E: dtype[{double}] +reveal_type(np.dtype(int)) # E: dtype[{int_}] +reveal_type(np.dtype(bool)) # E: dtype[bool_] +reveal_type(np.dtype(str)) # E: dtype[str_] +reveal_type(np.dtype(bytes)) # E: dtype[bytes_] +reveal_type(np.dtype(object)) # E: dtype[object_] # ctypes -reveal_type(np.dtype(ct.c_double)) # E: numpy.dtype[{double}] -reveal_type(np.dtype(ct.c_longlong)) # E: numpy.dtype[{longlong}] -reveal_type(np.dtype(ct.c_uint32)) # E: numpy.dtype[{uint32}] -reveal_type(np.dtype(ct.c_bool)) # E: numpy.dtype[numpy.bool_] -reveal_type(np.dtype(ct.c_char)) # E: numpy.dtype[numpy.bytes_] -reveal_type(np.dtype(ct.py_object)) # E: numpy.dtype[numpy.object_] +reveal_type(np.dtype(ct.c_double)) # E: dtype[{double}] +reveal_type(np.dtype(ct.c_longlong)) # E: dtype[{longlong}] +reveal_type(np.dtype(ct.c_uint32)) # E: dtype[{uint32}] +reveal_type(np.dtype(ct.c_bool)) # E: dtype[bool_] +reveal_type(np.dtype(ct.c_char)) # E: dtype[bytes_] +reveal_type(np.dtype(ct.py_object)) # E: dtype[object_] # Special case for None -reveal_type(np.dtype(None)) # E: numpy.dtype[{double}] +reveal_type(np.dtype(None)) # E: dtype[{double}] # Dtypes of dtypes -reveal_type(np.dtype(np.dtype(np.float64))) # E: numpy.dtype[{float64}] +reveal_type(np.dtype(np.dtype(np.float64))) # E: dtype[{float64}] # Parameterized dtypes -reveal_type(np.dtype("S8")) # E: numpy.dtype +reveal_type(np.dtype("S8")) # E: dtype # Void -reveal_type(np.dtype(("U", 10))) # E: numpy.dtype[numpy.void] +reveal_type(np.dtype(("U", 10))) # E: dtype[void] # Methods and attributes -reveal_type(dtype_U.base) # E: numpy.dtype[Any] -reveal_type(dtype_U.subdtype) # E: Union[None, Tuple[numpy.dtype[Any], builtins.tuple[builtins.int]]] -reveal_type(dtype_U.newbyteorder()) # E: numpy.dtype[numpy.str_] -reveal_type(dtype_U.type) # E: Type[numpy.str_] +reveal_type(dtype_U.base) # E: dtype[Any] +reveal_type(dtype_U.subdtype) # E: Union[None, Tuple[dtype[Any], builtins.tuple[builtins.int]]] +reveal_type(dtype_U.newbyteorder()) # E: dtype[str_] +reveal_type(dtype_U.type) # E: Type[str_] reveal_type(dtype_U.name) # E: str reveal_type(dtype_U.names) # E: Union[None, builtins.tuple[builtins.str]] -reveal_type(dtype_U * 0) # E: numpy.dtype[numpy.str_] -reveal_type(dtype_U * 1) # E: numpy.dtype[numpy.str_] -reveal_type(dtype_U * 2) # E: numpy.dtype[numpy.str_] +reveal_type(dtype_U * 0) # E: dtype[str_] +reveal_type(dtype_U * 1) # E: dtype[str_] +reveal_type(dtype_U * 2) # E: dtype[str_] -reveal_type(dtype_i8 * 0) # E: numpy.dtype[numpy.void] -reveal_type(dtype_i8 * 1) # E: numpy.dtype[{int64}] -reveal_type(dtype_i8 * 2) # E: numpy.dtype[numpy.void] +reveal_type(dtype_i8 * 0) # E: dtype[void] +reveal_type(dtype_i8 * 1) # E: dtype[{int64}] +reveal_type(dtype_i8 * 2) # E: dtype[void] -reveal_type(0 * dtype_U) # E: numpy.dtype[numpy.str_] -reveal_type(1 * dtype_U) # E: numpy.dtype[numpy.str_] -reveal_type(2 * dtype_U) # E: numpy.dtype[numpy.str_] +reveal_type(0 * dtype_U) # E: dtype[str_] +reveal_type(1 * dtype_U) # E: dtype[str_] +reveal_type(2 * dtype_U) # E: dtype[str_] -reveal_type(0 * dtype_i8) # E: numpy.dtype[Any] -reveal_type(1 * dtype_i8) # E: numpy.dtype[Any] -reveal_type(2 * dtype_i8) # E: numpy.dtype[Any] +reveal_type(0 * dtype_i8) # E: dtype[Any] +reveal_type(1 * dtype_i8) # E: dtype[Any] +reveal_type(2 * dtype_i8) # E: dtype[Any] -reveal_type(dtype_V["f0"]) # E: numpy.dtype[Any] -reveal_type(dtype_V[0]) # E: numpy.dtype[Any] -reveal_type(dtype_V[["f0", "f1"]]) # E: numpy.dtype[numpy.void] -reveal_type(dtype_V[["f0"]]) # E: numpy.dtype[numpy.void] +reveal_type(dtype_V["f0"]) # E: dtype[Any] +reveal_type(dtype_V[0]) # E: dtype[Any] +reveal_type(dtype_V[["f0", "f1"]]) # E: dtype[void] +reveal_type(dtype_V[["f0"]]) # E: dtype[void] diff --git a/numpy/typing/tests/data/reveal/einsumfunc.pyi b/numpy/typing/tests/data/reveal/einsumfunc.pyi index f1a90428d..5b07e6d3c 100644 --- a/numpy/typing/tests/data/reveal/einsumfunc.pyi +++ b/numpy/typing/tests/data/reveal/einsumfunc.pyi @@ -18,8 +18,8 @@ reveal_type(np.einsum("i,i->i", AR_LIKE_c, AR_LIKE_c)) # E: Any reveal_type(np.einsum("i,i->i", AR_LIKE_b, AR_LIKE_i)) # E: Any reveal_type(np.einsum("i,i,i,i->i", AR_LIKE_b, AR_LIKE_u, AR_LIKE_i, AR_LIKE_c)) # E: Any -reveal_type(np.einsum("i,i->i", AR_LIKE_c, AR_LIKE_c, out=OUT_f)) # E: numpy.ndarray[Any, numpy.dtype[{float64}] -reveal_type(np.einsum("i,i->i", AR_LIKE_U, AR_LIKE_U, dtype=bool, casting="unsafe", out=OUT_f)) # E: numpy.ndarray[Any, numpy.dtype[{float64}] +reveal_type(np.einsum("i,i->i", AR_LIKE_c, AR_LIKE_c, out=OUT_f)) # E: ndarray[Any, dtype[{float64}] +reveal_type(np.einsum("i,i->i", AR_LIKE_U, AR_LIKE_U, dtype=bool, casting="unsafe", out=OUT_f)) # E: ndarray[Any, dtype[{float64}] reveal_type(np.einsum("i,i->i", AR_LIKE_f, AR_LIKE_f, dtype="c16")) # E: Any reveal_type(np.einsum("i,i->i", AR_LIKE_U, AR_LIKE_U, dtype=bool, casting="unsafe")) # E: Any diff --git a/numpy/typing/tests/data/reveal/fft.pyi b/numpy/typing/tests/data/reveal/fft.pyi index 316ddd189..0667938e4 100644 --- a/numpy/typing/tests/data/reveal/fft.pyi +++ b/numpy/typing/tests/data/reveal/fft.pyi @@ -5,31 +5,31 @@ AR_f8: npt.NDArray[np.float64] AR_c16: npt.NDArray[np.complex128] AR_LIKE_f8: list[float] -reveal_type(np.fft.fftshift(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fft.fftshift(AR_LIKE_f8, axes=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.fft.ifftshift(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fft.ifftshift(AR_LIKE_f8, axes=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.fft.fftfreq(5, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.fft.fftfreq(np.int64(), AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] - -reveal_type(np.fft.fftfreq(5, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.fft.fftfreq(np.int64(), AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] - -reveal_type(np.fft.fft(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.fft.ifft(AR_f8, axis=1)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.fft.rfft(AR_f8, n=None)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.fft.irfft(AR_f8, norm="ortho")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fft.hfft(AR_f8, n=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fft.ihfft(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] - -reveal_type(np.fft.fftn(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.fft.ifftn(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.fft.rfftn(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.fft.irfftn(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] - -reveal_type(np.fft.rfft2(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.fft.ifft2(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.fft.fft2(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.fft.irfft2(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.fft.fftshift(AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fft.fftshift(AR_LIKE_f8, axes=0)) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.fft.ifftshift(AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fft.ifftshift(AR_LIKE_f8, axes=0)) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.fft.fftfreq(5, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.fft.fftfreq(np.int64(), AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] + +reveal_type(np.fft.fftfreq(5, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.fft.fftfreq(np.int64(), AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] + +reveal_type(np.fft.fft(AR_f8)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.fft.ifft(AR_f8, axis=1)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.fft.rfft(AR_f8, n=None)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.fft.irfft(AR_f8, norm="ortho")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fft.hfft(AR_f8, n=2)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fft.ihfft(AR_f8)) # E: ndarray[Any, dtype[{complex128}]] + +reveal_type(np.fft.fftn(AR_f8)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.fft.ifftn(AR_f8)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.fft.rfftn(AR_f8)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.fft.irfftn(AR_f8)) # E: ndarray[Any, dtype[{float64}]] + +reveal_type(np.fft.rfft2(AR_f8)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.fft.ifft2(AR_f8)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.fft.fft2(AR_f8)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.fft.irfft2(AR_f8)) # E: ndarray[Any, dtype[{float64}]] diff --git a/numpy/typing/tests/data/reveal/flatiter.pyi b/numpy/typing/tests/data/reveal/flatiter.pyi index 97776dd9f..ef89acb58 100644 --- a/numpy/typing/tests/data/reveal/flatiter.pyi +++ b/numpy/typing/tests/data/reveal/flatiter.pyi @@ -3,15 +3,15 @@ import numpy as np a: np.flatiter[np.ndarray[Any, np.dtype[np.str_]]] -reveal_type(a.base) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(a.copy()) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(a.base) # E: ndarray[Any, dtype[str_]] +reveal_type(a.copy()) # E: ndarray[Any, dtype[str_]] reveal_type(a.coords) # E: tuple[builtins.int] reveal_type(a.index) # E: int -reveal_type(iter(a)) # E: Iterator[numpy.str_] -reveal_type(next(a)) # E: numpy.str_ -reveal_type(a[0]) # E: numpy.str_ -reveal_type(a[[0, 1, 2]]) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(a[...]) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(a[:]) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(a.__array__()) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(a.__array__(np.dtype(np.float64))) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(iter(a)) # E: Iterator[str_] +reveal_type(next(a)) # E: str_ +reveal_type(a[0]) # E: str_ +reveal_type(a[[0, 1, 2]]) # E: ndarray[Any, dtype[str_]] +reveal_type(a[...]) # E: ndarray[Any, dtype[str_]] +reveal_type(a[:]) # E: ndarray[Any, dtype[str_]] +reveal_type(a.__array__()) # E: ndarray[Any, dtype[str_]] +reveal_type(a.__array__(np.dtype(np.float64))) # E: ndarray[Any, dtype[{float64}]] diff --git a/numpy/typing/tests/data/reveal/fromnumeric.pyi b/numpy/typing/tests/data/reveal/fromnumeric.pyi index bbcfbb85a..2ee1952cf 100644 --- a/numpy/typing/tests/data/reveal/fromnumeric.pyi +++ b/numpy/typing/tests/data/reveal/fromnumeric.pyi @@ -1,4 +1,4 @@ -"""Tests for :mod:`numpy.core.fromnumeric`.""" +"""Tests for :mod:`core.fromnumeric`.""" import numpy as np @@ -20,37 +20,37 @@ reveal_type(np.take(B, 0)) # E: Any reveal_type(np.take(A, [0])) # E: Any reveal_type(np.take(B, [0])) # E: Any -reveal_type(np.reshape(a, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.reshape(b, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.reshape(c, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.reshape(A, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.reshape(B, 1)) # E: numpy.ndarray[Any, Any] +reveal_type(np.reshape(a, 1)) # E: ndarray[Any, Any] +reveal_type(np.reshape(b, 1)) # E: ndarray[Any, Any] +reveal_type(np.reshape(c, 1)) # E: ndarray[Any, Any] +reveal_type(np.reshape(A, 1)) # E: ndarray[Any, Any] +reveal_type(np.reshape(B, 1)) # E: ndarray[Any, Any] reveal_type(np.choose(a, [True, True])) # E: Any reveal_type(np.choose(A, [True, True])) # E: Any -reveal_type(np.repeat(a, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.repeat(b, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.repeat(c, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.repeat(A, 1)) # E: numpy.ndarray[Any, Any] -reveal_type(np.repeat(B, 1)) # E: numpy.ndarray[Any, Any] +reveal_type(np.repeat(a, 1)) # E: ndarray[Any, Any] +reveal_type(np.repeat(b, 1)) # E: ndarray[Any, Any] +reveal_type(np.repeat(c, 1)) # E: ndarray[Any, Any] +reveal_type(np.repeat(A, 1)) # E: ndarray[Any, Any] +reveal_type(np.repeat(B, 1)) # E: ndarray[Any, Any] # TODO: Add tests for np.put() -reveal_type(np.swapaxes(A, 0, 0)) # E: numpy.ndarray[Any, Any] -reveal_type(np.swapaxes(B, 0, 0)) # E: numpy.ndarray[Any, Any] +reveal_type(np.swapaxes(A, 0, 0)) # E: ndarray[Any, Any] +reveal_type(np.swapaxes(B, 0, 0)) # E: ndarray[Any, Any] -reveal_type(np.transpose(a)) # E: numpy.ndarray[Any, Any] -reveal_type(np.transpose(b)) # E: numpy.ndarray[Any, Any] -reveal_type(np.transpose(c)) # E: numpy.ndarray[Any, Any] -reveal_type(np.transpose(A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.transpose(B)) # E: numpy.ndarray[Any, Any] +reveal_type(np.transpose(a)) # E: ndarray[Any, Any] +reveal_type(np.transpose(b)) # E: ndarray[Any, Any] +reveal_type(np.transpose(c)) # E: ndarray[Any, Any] +reveal_type(np.transpose(A)) # E: ndarray[Any, Any] +reveal_type(np.transpose(B)) # E: ndarray[Any, Any] -reveal_type(np.partition(a, 0, axis=None)) # E: numpy.ndarray[Any, Any] -reveal_type(np.partition(b, 0, axis=None)) # E: numpy.ndarray[Any, Any] -reveal_type(np.partition(c, 0, axis=None)) # E: numpy.ndarray[Any, Any] -reveal_type(np.partition(A, 0)) # E: numpy.ndarray[Any, Any] -reveal_type(np.partition(B, 0)) # E: numpy.ndarray[Any, Any] +reveal_type(np.partition(a, 0, axis=None)) # E: ndarray[Any, Any] +reveal_type(np.partition(b, 0, axis=None)) # E: ndarray[Any, Any] +reveal_type(np.partition(c, 0, axis=None)) # E: ndarray[Any, Any] +reveal_type(np.partition(A, 0)) # E: ndarray[Any, Any] +reveal_type(np.partition(B, 0)) # E: ndarray[Any, Any] reveal_type(np.argpartition(a, 0)) # E: Any reveal_type(np.argpartition(b, 0)) # E: Any @@ -58,11 +58,11 @@ reveal_type(np.argpartition(c, 0)) # E: Any reveal_type(np.argpartition(A, 0)) # E: Any reveal_type(np.argpartition(B, 0)) # E: Any -reveal_type(np.sort(A, 0)) # E: numpy.ndarray[Any, Any] -reveal_type(np.sort(B, 0)) # E: numpy.ndarray[Any, Any] +reveal_type(np.sort(A, 0)) # E: ndarray[Any, Any] +reveal_type(np.sort(B, 0)) # E: ndarray[Any, Any] -reveal_type(np.argsort(A, 0)) # E: numpy.ndarray[Any, Any] -reveal_type(np.argsort(B, 0)) # E: numpy.ndarray[Any, Any] +reveal_type(np.argsort(A, 0)) # E: ndarray[Any, Any] +reveal_type(np.argsort(B, 0)) # E: ndarray[Any, Any] reveal_type(np.argmax(A)) # E: {intp} reveal_type(np.argmax(B)) # E: {intp} @@ -76,38 +76,38 @@ reveal_type(np.argmin(B, axis=0)) # E: Any reveal_type(np.searchsorted(A[0], 0)) # E: {intp} reveal_type(np.searchsorted(B[0], 0)) # E: {intp} -reveal_type(np.searchsorted(A[0], [0])) # E: numpy.ndarray[Any, Any] -reveal_type(np.searchsorted(B[0], [0])) # E: numpy.ndarray[Any, Any] +reveal_type(np.searchsorted(A[0], [0])) # E: ndarray[Any, Any] +reveal_type(np.searchsorted(B[0], [0])) # E: ndarray[Any, Any] -reveal_type(np.resize(a, (5, 5))) # E: numpy.ndarray[Any, Any] -reveal_type(np.resize(b, (5, 5))) # E: numpy.ndarray[Any, Any] -reveal_type(np.resize(c, (5, 5))) # E: numpy.ndarray[Any, Any] -reveal_type(np.resize(A, (5, 5))) # E: numpy.ndarray[Any, Any] -reveal_type(np.resize(B, (5, 5))) # E: numpy.ndarray[Any, Any] +reveal_type(np.resize(a, (5, 5))) # E: ndarray[Any, Any] +reveal_type(np.resize(b, (5, 5))) # E: ndarray[Any, Any] +reveal_type(np.resize(c, (5, 5))) # E: ndarray[Any, Any] +reveal_type(np.resize(A, (5, 5))) # E: ndarray[Any, Any] +reveal_type(np.resize(B, (5, 5))) # E: ndarray[Any, Any] -reveal_type(np.squeeze(a)) # E: numpy.bool_ +reveal_type(np.squeeze(a)) # E: bool_ reveal_type(np.squeeze(b)) # E: {float32} -reveal_type(np.squeeze(c)) # E: numpy.ndarray[Any, Any] -reveal_type(np.squeeze(A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.squeeze(B)) # E: numpy.ndarray[Any, Any] +reveal_type(np.squeeze(c)) # E: ndarray[Any, Any] +reveal_type(np.squeeze(A)) # E: ndarray[Any, Any] +reveal_type(np.squeeze(B)) # E: ndarray[Any, Any] -reveal_type(np.diagonal(A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.diagonal(B)) # E: numpy.ndarray[Any, Any] +reveal_type(np.diagonal(A)) # E: ndarray[Any, Any] +reveal_type(np.diagonal(B)) # E: ndarray[Any, Any] reveal_type(np.trace(A)) # E: Any reveal_type(np.trace(B)) # E: Any -reveal_type(np.ravel(a)) # E: numpy.ndarray[Any, Any] -reveal_type(np.ravel(b)) # E: numpy.ndarray[Any, Any] -reveal_type(np.ravel(c)) # E: numpy.ndarray[Any, Any] -reveal_type(np.ravel(A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.ravel(B)) # E: numpy.ndarray[Any, Any] +reveal_type(np.ravel(a)) # E: ndarray[Any, Any] +reveal_type(np.ravel(b)) # E: ndarray[Any, Any] +reveal_type(np.ravel(c)) # E: ndarray[Any, Any] +reveal_type(np.ravel(A)) # E: ndarray[Any, Any] +reveal_type(np.ravel(B)) # E: ndarray[Any, Any] -reveal_type(np.nonzero(a)) # E: tuple[numpy.ndarray[Any, Any]] -reveal_type(np.nonzero(b)) # E: tuple[numpy.ndarray[Any, Any]] -reveal_type(np.nonzero(c)) # E: tuple[numpy.ndarray[Any, Any]] -reveal_type(np.nonzero(A)) # E: tuple[numpy.ndarray[Any, Any]] -reveal_type(np.nonzero(B)) # E: tuple[numpy.ndarray[Any, Any]] +reveal_type(np.nonzero(a)) # E: tuple[ndarray[Any, Any]] +reveal_type(np.nonzero(b)) # E: tuple[ndarray[Any, Any]] +reveal_type(np.nonzero(c)) # E: tuple[ndarray[Any, Any]] +reveal_type(np.nonzero(A)) # E: tuple[ndarray[Any, Any]] +reveal_type(np.nonzero(B)) # E: tuple[ndarray[Any, Any]] reveal_type(np.shape(a)) # E: tuple[builtins.int] reveal_type(np.shape(b)) # E: tuple[builtins.int] @@ -115,11 +115,11 @@ reveal_type(np.shape(c)) # E: tuple[builtins.int] reveal_type(np.shape(A)) # E: tuple[builtins.int] reveal_type(np.shape(B)) # E: tuple[builtins.int] -reveal_type(np.compress([True], a)) # E: numpy.ndarray[Any, Any] -reveal_type(np.compress([True], b)) # E: numpy.ndarray[Any, Any] -reveal_type(np.compress([True], c)) # E: numpy.ndarray[Any, Any] -reveal_type(np.compress([True], A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.compress([True], B)) # E: numpy.ndarray[Any, Any] +reveal_type(np.compress([True], a)) # E: ndarray[Any, Any] +reveal_type(np.compress([True], b)) # E: ndarray[Any, Any] +reveal_type(np.compress([True], c)) # E: ndarray[Any, Any] +reveal_type(np.compress([True], A)) # E: ndarray[Any, Any] +reveal_type(np.compress([True], B)) # E: ndarray[Any, Any] reveal_type(np.clip(a, 0, 1.0)) # E: Any reveal_type(np.clip(b, -1, 1)) # E: Any @@ -135,31 +135,31 @@ reveal_type(np.sum(B)) # E: Any reveal_type(np.sum(A, axis=0)) # E: Any reveal_type(np.sum(B, axis=0)) # E: Any -reveal_type(np.all(a)) # E: numpy.bool_ -reveal_type(np.all(b)) # E: numpy.bool_ -reveal_type(np.all(c)) # E: numpy.bool_ -reveal_type(np.all(A)) # E: numpy.bool_ -reveal_type(np.all(B)) # E: numpy.bool_ +reveal_type(np.all(a)) # E: bool_ +reveal_type(np.all(b)) # E: bool_ +reveal_type(np.all(c)) # E: bool_ +reveal_type(np.all(A)) # E: bool_ +reveal_type(np.all(B)) # E: bool_ reveal_type(np.all(A, axis=0)) # E: Any reveal_type(np.all(B, axis=0)) # E: Any reveal_type(np.all(A, keepdims=True)) # E: Any reveal_type(np.all(B, keepdims=True)) # E: Any -reveal_type(np.any(a)) # E: numpy.bool_ -reveal_type(np.any(b)) # E: numpy.bool_ -reveal_type(np.any(c)) # E: numpy.bool_ -reveal_type(np.any(A)) # E: numpy.bool_ -reveal_type(np.any(B)) # E: numpy.bool_ +reveal_type(np.any(a)) # E: bool_ +reveal_type(np.any(b)) # E: bool_ +reveal_type(np.any(c)) # E: bool_ +reveal_type(np.any(A)) # E: bool_ +reveal_type(np.any(B)) # E: bool_ reveal_type(np.any(A, axis=0)) # E: Any reveal_type(np.any(B, axis=0)) # E: Any reveal_type(np.any(A, keepdims=True)) # E: Any reveal_type(np.any(B, keepdims=True)) # E: Any -reveal_type(np.cumsum(a)) # E: numpy.ndarray[Any, Any] -reveal_type(np.cumsum(b)) # E: numpy.ndarray[Any, Any] -reveal_type(np.cumsum(c)) # E: numpy.ndarray[Any, Any] -reveal_type(np.cumsum(A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.cumsum(B)) # E: numpy.ndarray[Any, Any] +reveal_type(np.cumsum(a)) # E: ndarray[Any, Any] +reveal_type(np.cumsum(b)) # E: ndarray[Any, Any] +reveal_type(np.cumsum(c)) # E: ndarray[Any, Any] +reveal_type(np.cumsum(A)) # E: ndarray[Any, Any] +reveal_type(np.cumsum(B)) # E: ndarray[Any, Any] reveal_type(np.ptp(a)) # E: Any reveal_type(np.ptp(b)) # E: Any @@ -203,11 +203,11 @@ reveal_type(np.prod(B, keepdims=True)) # E: Any reveal_type(np.prod(b, out=d)) # E: Any reveal_type(np.prod(B, out=d)) # E: Any -reveal_type(np.cumprod(a)) # E: numpy.ndarray[Any, Any] -reveal_type(np.cumprod(b)) # E: numpy.ndarray[Any, Any] -reveal_type(np.cumprod(c)) # E: numpy.ndarray[Any, Any] -reveal_type(np.cumprod(A)) # E: numpy.ndarray[Any, Any] -reveal_type(np.cumprod(B)) # E: numpy.ndarray[Any, Any] +reveal_type(np.cumprod(a)) # E: ndarray[Any, Any] +reveal_type(np.cumprod(b)) # E: ndarray[Any, Any] +reveal_type(np.cumprod(c)) # E: ndarray[Any, Any] +reveal_type(np.cumprod(A)) # E: ndarray[Any, Any] +reveal_type(np.cumprod(B)) # E: ndarray[Any, Any] reveal_type(np.ndim(a)) # E: int reveal_type(np.ndim(b)) # E: int diff --git a/numpy/typing/tests/data/reveal/getlimits.pyi b/numpy/typing/tests/data/reveal/getlimits.pyi index 90bcb06c8..1614b577e 100644 --- a/numpy/typing/tests/data/reveal/getlimits.pyi +++ b/numpy/typing/tests/data/reveal/getlimits.pyi @@ -10,12 +10,12 @@ u4: np.uint32 finfo_f8: np.finfo[np.float64] iinfo_i8: np.iinfo[np.int64] -reveal_type(np.finfo(f)) # E: numpy.finfo[{double}] -reveal_type(np.finfo(f8)) # E: numpy.finfo[{float64}] -reveal_type(np.finfo(c8)) # E: numpy.finfo[{float32}] -reveal_type(np.finfo('f2')) # E: numpy.finfo[numpy.floating[Any]] +reveal_type(np.finfo(f)) # E: finfo[{double}] +reveal_type(np.finfo(f8)) # E: finfo[{float64}] +reveal_type(np.finfo(c8)) # E: finfo[{float32}] +reveal_type(np.finfo('f2')) # E: finfo[floating[Any]] -reveal_type(finfo_f8.dtype) # E: numpy.dtype[{float64}] +reveal_type(finfo_f8.dtype) # E: dtype[{float64}] reveal_type(finfo_f8.bits) # E: int reveal_type(finfo_f8.eps) # E: {float64} reveal_type(finfo_f8.epsneg) # E: {float64} @@ -39,7 +39,7 @@ reveal_type(np.iinfo(i8)) # E: iinfo[{int64}] reveal_type(np.iinfo(u4)) # E: iinfo[{uint32}] reveal_type(np.iinfo('i2')) # E: iinfo[Any] -reveal_type(iinfo_i8.dtype) # E: numpy.dtype[{int64}] +reveal_type(iinfo_i8.dtype) # E: dtype[{int64}] reveal_type(iinfo_i8.kind) # E: str reveal_type(iinfo_i8.bits) # E: int reveal_type(iinfo_i8.key) # E: str diff --git a/numpy/typing/tests/data/reveal/histograms.pyi b/numpy/typing/tests/data/reveal/histograms.pyi index 55fa9518f..d96e44f09 100644 --- a/numpy/typing/tests/data/reveal/histograms.pyi +++ b/numpy/typing/tests/data/reveal/histograms.pyi @@ -4,16 +4,16 @@ import numpy.typing as npt AR_i8: npt.NDArray[np.int64] AR_f8: npt.NDArray[np.float64] -reveal_type(np.histogram_bin_edges(AR_i8, bins="auto")) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.histogram_bin_edges(AR_i8, bins="rice", range=(0, 3))) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.histogram_bin_edges(AR_i8, bins="scott", weights=AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.histogram_bin_edges(AR_i8, bins="auto")) # E: ndarray[Any, dtype[Any]] +reveal_type(np.histogram_bin_edges(AR_i8, bins="rice", range=(0, 3))) # E: ndarray[Any, dtype[Any]] +reveal_type(np.histogram_bin_edges(AR_i8, bins="scott", weights=AR_f8)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.histogram(AR_i8, bins="auto")) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]] -reveal_type(np.histogram(AR_i8, bins="rice", range=(0, 3))) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]] -reveal_type(np.histogram(AR_i8, bins="scott", weights=AR_f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]] -reveal_type(np.histogram(AR_f8, bins=1, density=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.histogram(AR_i8, bins="auto")) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[Any]]] +reveal_type(np.histogram(AR_i8, bins="rice", range=(0, 3))) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[Any]]] +reveal_type(np.histogram(AR_i8, bins="scott", weights=AR_f8)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[Any]]] +reveal_type(np.histogram(AR_f8, bins=1, density=True)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[Any]]] -reveal_type(np.histogramdd(AR_i8, bins=[1])) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], builtins.list[numpy.ndarray[Any, numpy.dtype[Any]]]] -reveal_type(np.histogramdd(AR_i8, range=[(0, 3)])) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], builtins.list[numpy.ndarray[Any, numpy.dtype[Any]]]] -reveal_type(np.histogramdd(AR_i8, weights=AR_f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], builtins.list[numpy.ndarray[Any, numpy.dtype[Any]]]] -reveal_type(np.histogramdd(AR_f8, density=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], builtins.list[numpy.ndarray[Any, numpy.dtype[Any]]]] +reveal_type(np.histogramdd(AR_i8, bins=[1])) # E: Tuple[ndarray[Any, dtype[Any]], builtins.list[ndarray[Any, dtype[Any]]]] +reveal_type(np.histogramdd(AR_i8, range=[(0, 3)])) # E: Tuple[ndarray[Any, dtype[Any]], builtins.list[ndarray[Any, dtype[Any]]]] +reveal_type(np.histogramdd(AR_i8, weights=AR_f8)) # E: Tuple[ndarray[Any, dtype[Any]], builtins.list[ndarray[Any, dtype[Any]]]] +reveal_type(np.histogramdd(AR_f8, density=True)) # E: Tuple[ndarray[Any, dtype[Any]], builtins.list[ndarray[Any, dtype[Any]]]] diff --git a/numpy/typing/tests/data/reveal/index_tricks.pyi b/numpy/typing/tests/data/reveal/index_tricks.pyi index 863d60220..cee4d8c3e 100644 --- a/numpy/typing/tests/data/reveal/index_tricks.pyi +++ b/numpy/typing/tests/data/reveal/index_tricks.pyi @@ -8,41 +8,41 @@ AR_LIKE_U: List[str] AR_i8: np.ndarray[Any, np.dtype[np.int64]] -reveal_type(np.ndenumerate(AR_i8)) # E: numpy.ndenumerate[{int64}] -reveal_type(np.ndenumerate(AR_LIKE_f)) # E: numpy.ndenumerate[{double}] -reveal_type(np.ndenumerate(AR_LIKE_U)) # E: numpy.ndenumerate[numpy.str_] +reveal_type(np.ndenumerate(AR_i8)) # E: ndenumerate[{int64}] +reveal_type(np.ndenumerate(AR_LIKE_f)) # E: ndenumerate[{double}] +reveal_type(np.ndenumerate(AR_LIKE_U)) # E: ndenumerate[str_] -reveal_type(np.ndenumerate(AR_i8).iter) # E: numpy.flatiter[numpy.ndarray[Any, numpy.dtype[{int64}]]] -reveal_type(np.ndenumerate(AR_LIKE_f).iter) # E: numpy.flatiter[numpy.ndarray[Any, numpy.dtype[{double}]]] -reveal_type(np.ndenumerate(AR_LIKE_U).iter) # E: numpy.flatiter[numpy.ndarray[Any, numpy.dtype[numpy.str_]]] +reveal_type(np.ndenumerate(AR_i8).iter) # E: flatiter[ndarray[Any, dtype[{int64}]]] +reveal_type(np.ndenumerate(AR_LIKE_f).iter) # E: flatiter[ndarray[Any, dtype[{double}]]] +reveal_type(np.ndenumerate(AR_LIKE_U).iter) # E: flatiter[ndarray[Any, dtype[str_]]] reveal_type(next(np.ndenumerate(AR_i8))) # E: Tuple[builtins.tuple[builtins.int], {int64}] reveal_type(next(np.ndenumerate(AR_LIKE_f))) # E: Tuple[builtins.tuple[builtins.int], {double}] -reveal_type(next(np.ndenumerate(AR_LIKE_U))) # E: Tuple[builtins.tuple[builtins.int], numpy.str_] +reveal_type(next(np.ndenumerate(AR_LIKE_U))) # E: Tuple[builtins.tuple[builtins.int], str_] reveal_type(iter(np.ndenumerate(AR_i8))) # E: Iterator[Tuple[builtins.tuple[builtins.int], {int64}]] reveal_type(iter(np.ndenumerate(AR_LIKE_f))) # E: Iterator[Tuple[builtins.tuple[builtins.int], {double}]] -reveal_type(iter(np.ndenumerate(AR_LIKE_U))) # E: Iterator[Tuple[builtins.tuple[builtins.int], numpy.str_]] +reveal_type(iter(np.ndenumerate(AR_LIKE_U))) # E: Iterator[Tuple[builtins.tuple[builtins.int], str_]] reveal_type(iter(np.ndindex(1, 2, 3))) # E: Iterator[builtins.tuple[builtins.int]] reveal_type(next(np.ndindex(1, 2, 3))) # E: builtins.tuple[builtins.int] -reveal_type(np.unravel_index([22, 41, 37], (7, 6))) # E: tuple[numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.unravel_index([31, 41, 13], (7, 6), order="F")) # E: tuple[numpy.ndarray[Any, numpy.dtype[{intp}]]] +reveal_type(np.unravel_index([22, 41, 37], (7, 6))) # E: tuple[ndarray[Any, dtype[{intp}]]] +reveal_type(np.unravel_index([31, 41, 13], (7, 6), order="F")) # E: tuple[ndarray[Any, dtype[{intp}]]] reveal_type(np.unravel_index(1621, (6, 7, 8, 9))) # E: tuple[{intp}] -reveal_type(np.ravel_multi_index([[1]], (7, 6))) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] +reveal_type(np.ravel_multi_index([[1]], (7, 6))) # E: ndarray[Any, dtype[{intp}]] reveal_type(np.ravel_multi_index(AR_LIKE_i, (7, 6))) # E: {intp} reveal_type(np.ravel_multi_index(AR_LIKE_i, (7, 6), order="F")) # E: {intp} reveal_type(np.ravel_multi_index(AR_LIKE_i, (4, 6), mode="clip")) # E: {intp} reveal_type(np.ravel_multi_index(AR_LIKE_i, (4, 4), mode=("clip", "wrap"))) # E: {intp} reveal_type(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9))) # E: {intp} -reveal_type(np.mgrid[1:1:2]) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.mgrid[1:1:2, None:10]) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.mgrid[1:1:2]) # E: ndarray[Any, dtype[Any]] +reveal_type(np.mgrid[1:1:2, None:10]) # E: ndarray[Any, dtype[Any]] -reveal_type(np.ogrid[1:1:2]) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] -reveal_type(np.ogrid[1:1:2, None:10]) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.ogrid[1:1:2]) # E: list[ndarray[Any, dtype[Any]]] +reveal_type(np.ogrid[1:1:2, None:10]) # E: list[ndarray[Any, dtype[Any]]] reveal_type(np.index_exp[0:1]) # E: Tuple[builtins.slice] reveal_type(np.index_exp[0:1, None:3]) # E: Tuple[builtins.slice, builtins.slice] @@ -52,13 +52,13 @@ reveal_type(np.s_[0:1]) # E: builtins.slice reveal_type(np.s_[0:1, None:3]) # E: Tuple[builtins.slice, builtins.slice] reveal_type(np.s_[0, 0:1, ..., [0, 1, 3]]) # E: Tuple[Literal[0]?, builtins.slice, builtins.ellipsis, builtins.list[builtins.int]] -reveal_type(np.ix_(AR_LIKE_b)) # E: tuple[numpy.ndarray[Any, numpy.dtype[numpy.bool_]]] -reveal_type(np.ix_(AR_LIKE_i, AR_LIKE_f)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{double}]]] -reveal_type(np.ix_(AR_i8)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{int64}]]] +reveal_type(np.ix_(AR_LIKE_b)) # E: tuple[ndarray[Any, dtype[bool_]]] +reveal_type(np.ix_(AR_LIKE_i, AR_LIKE_f)) # E: tuple[ndarray[Any, dtype[{double}]]] +reveal_type(np.ix_(AR_i8)) # E: tuple[ndarray[Any, dtype[{int64}]]] reveal_type(np.fill_diagonal(AR_i8, 5)) # E: None -reveal_type(np.diag_indices(4)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{int_}]]] -reveal_type(np.diag_indices(2, 3)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{int_}]]] +reveal_type(np.diag_indices(4)) # E: tuple[ndarray[Any, dtype[{int_}]]] +reveal_type(np.diag_indices(2, 3)) # E: tuple[ndarray[Any, dtype[{int_}]]] -reveal_type(np.diag_indices_from(AR_i8)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{int_}]]] +reveal_type(np.diag_indices_from(AR_i8)) # E: tuple[ndarray[Any, dtype[{int_}]]] diff --git a/numpy/typing/tests/data/reveal/lib_function_base.pyi b/numpy/typing/tests/data/reveal/lib_function_base.pyi index bced08894..854b955b4 100644 --- a/numpy/typing/tests/data/reveal/lib_function_base.pyi +++ b/numpy/typing/tests/data/reveal/lib_function_base.pyi @@ -26,9 +26,9 @@ reveal_type(vectorized_func.signature) # E: Union[None, builtins.str] reveal_type(vectorized_func.otypes) # E: Union[None, builtins.str] reveal_type(vectorized_func.excluded) # E: set[Union[builtins.int, builtins.str]] reveal_type(vectorized_func.__doc__) # E: Union[None, builtins.str] -reveal_type(vectorized_func([1])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.vectorize(int)) # E: numpy.vectorize -reveal_type(np.vectorize( # E: numpy.vectorize +reveal_type(vectorized_func([1])) # E: ndarray[Any, dtype[Any]] +reveal_type(np.vectorize(int)) # E: vectorize +reveal_type(np.vectorize( # E: vectorize int, otypes="i", doc="doc", excluded=(), cache=True, signature=None )) @@ -36,63 +36,63 @@ reveal_type(np.add_newdoc("__main__", "blabla", doc="test doc")) # E: None reveal_type(np.add_newdoc("__main__", "blabla", doc=("meth", "test doc"))) # E: None reveal_type(np.add_newdoc("__main__", "blabla", doc=[("meth", "test doc")])) # E: None -reveal_type(np.rot90(AR_f8, k=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.rot90(AR_LIKE_f8, axes=(0, 1))) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.rot90(AR_f8, k=2)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.rot90(AR_LIKE_f8, axes=(0, 1))) # E: ndarray[Any, dtype[Any]] reveal_type(np.flip(f8)) # E: {float64} reveal_type(np.flip(1.0)) # E: Any -reveal_type(np.flip(AR_f8, axis=(0, 1))) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.flip(AR_LIKE_f8, axis=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.flip(AR_f8, axis=(0, 1))) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.flip(AR_LIKE_f8, axis=0)) # E: ndarray[Any, dtype[Any]] reveal_type(np.iterable(1)) # E: bool reveal_type(np.iterable([1])) # E: bool -reveal_type(np.average(AR_f8)) # E: numpy.floating[Any] -reveal_type(np.average(AR_f8, weights=AR_c16)) # E: numpy.complexfloating[Any, Any] +reveal_type(np.average(AR_f8)) # E: floating[Any] +reveal_type(np.average(AR_f8, weights=AR_c16)) # E: complexfloating[Any, Any] reveal_type(np.average(AR_O)) # E: Any -reveal_type(np.average(AR_f8, returned=True)) # E: Tuple[numpy.floating[Any], numpy.floating[Any]] -reveal_type(np.average(AR_f8, weights=AR_c16, returned=True)) # E: Tuple[numpy.complexfloating[Any, Any], numpy.complexfloating[Any, Any]] +reveal_type(np.average(AR_f8, returned=True)) # E: Tuple[floating[Any], floating[Any]] +reveal_type(np.average(AR_f8, weights=AR_c16, returned=True)) # E: Tuple[complexfloating[Any, Any], complexfloating[Any, Any]] reveal_type(np.average(AR_O, returned=True)) # E: Tuple[Any, Any] reveal_type(np.average(AR_f8, axis=0)) # E: Any reveal_type(np.average(AR_f8, axis=0, returned=True)) # E: Tuple[Any, Any] -reveal_type(np.asarray_chkfinite(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.asarray_chkfinite(AR_LIKE_f8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.asarray_chkfinite(AR_f8, dtype=np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.asarray_chkfinite(AR_f8, dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.asarray_chkfinite(AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.asarray_chkfinite(AR_LIKE_f8)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.asarray_chkfinite(AR_f8, dtype=np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.asarray_chkfinite(AR_f8, dtype=float)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.piecewise(AR_f8, AR_b, [func])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.piecewise(AR_LIKE_f8, AR_b, [func])) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.piecewise(AR_f8, AR_b, [func])) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.piecewise(AR_LIKE_f8, AR_b, [func])) # E: ndarray[Any, dtype[Any]] -reveal_type(np.select([AR_f8], [AR_f8])) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.select([AR_f8], [AR_f8])) # E: ndarray[Any, dtype[Any]] -reveal_type(np.copy(AR_LIKE_f8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.copy(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.copy(CHAR_AR_U)) # E: numpy.ndarray[Any, Any] -reveal_type(np.copy(CHAR_AR_U, "K", subok=True)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.copy(CHAR_AR_U, subok=True)) # E: numpy.chararray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.copy(AR_LIKE_f8)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.copy(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.copy(CHAR_AR_U)) # E: ndarray[Any, Any] +reveal_type(np.copy(CHAR_AR_U, "K", subok=True)) # E: chararray[Any, dtype[str_]] +reveal_type(np.copy(CHAR_AR_U, subok=True)) # E: chararray[Any, dtype[str_]] reveal_type(np.gradient(AR_f8, axis=None)) # E: Any reveal_type(np.gradient(AR_LIKE_f8, edge_order=2)) # E: Any reveal_type(np.diff("bob", n=0)) # E: str -reveal_type(np.diff(AR_f8, axis=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.diff(AR_LIKE_f8, prepend=1.5)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.diff(AR_f8, axis=0)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.diff(AR_LIKE_f8, prepend=1.5)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.angle(AR_f8)) # E: numpy.floating[Any] -reveal_type(np.angle(AR_c16, deg=True)) # E: numpy.complexfloating[Any, Any] +reveal_type(np.angle(AR_f8)) # E: floating[Any] +reveal_type(np.angle(AR_c16, deg=True)) # E: complexfloating[Any, Any] reveal_type(np.angle(AR_O)) # E: Any -reveal_type(np.unwrap(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.unwrap(AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(np.unwrap(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.unwrap(AR_O)) # E: ndarray[Any, dtype[object_]] -reveal_type(np.sort_complex(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(np.sort_complex(AR_f8)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] -reveal_type(np.trim_zeros(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.trim_zeros(AR_f8)) # E: ndarray[Any, dtype[{float64}]] reveal_type(np.trim_zeros(AR_LIKE_f8)) # E: list[builtins.float] -reveal_type(np.extract(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.extract(AR_i8, AR_LIKE_f8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.extract(AR_i8, AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.extract(AR_i8, AR_LIKE_f8)) # E: ndarray[Any, dtype[Any]] reveal_type(np.place(AR_f8, mask=AR_i8, vals=5.0)) # E: None @@ -100,81 +100,81 @@ reveal_type(np.disp(1, linefeed=True)) # E: None with open("test", "w") as f: reveal_type(np.disp("message", device=f)) # E: None -reveal_type(np.cov(AR_f8, bias=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.cov(AR_f8, AR_c16, ddof=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.cov(AR_f8, aweights=AR_f8, dtype=np.float32)) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(np.cov(AR_f8, fweights=AR_f8, dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.cov(AR_f8, bias=True)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.cov(AR_f8, AR_c16, ddof=1)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.cov(AR_f8, aweights=AR_f8, dtype=np.float32)) # E: ndarray[Any, dtype[{float32}]] +reveal_type(np.cov(AR_f8, fweights=AR_f8, dtype=float)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.corrcoef(AR_f8, rowvar=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.corrcoef(AR_f8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.corrcoef(AR_f8, dtype=np.float32)) # E: numpy.ndarray[Any, numpy.dtype[{float32}]] -reveal_type(np.corrcoef(AR_f8, dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.corrcoef(AR_f8, rowvar=True)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.corrcoef(AR_f8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.corrcoef(AR_f8, dtype=np.float32)) # E: ndarray[Any, dtype[{float32}]] +reveal_type(np.corrcoef(AR_f8, dtype=float)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.blackman(5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.bartlett(6)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.hanning(4.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.hamming(0)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.i0(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.kaiser(4, 5.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.blackman(5)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.bartlett(6)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.hanning(4.5)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.hamming(0)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.i0(AR_i8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.kaiser(4, 5.9)) # E: ndarray[Any, dtype[floating[Any]]] -reveal_type(np.sinc(1.0)) # E: numpy.floating[Any] -reveal_type(np.sinc(1j)) # E: numpy.complexfloating[Any, Any] -reveal_type(np.sinc(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.sinc(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(np.sinc(1.0)) # E: floating[Any] +reveal_type(np.sinc(1j)) # E: complexfloating[Any, Any] +reveal_type(np.sinc(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.sinc(AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] reveal_type(np.msort(CHAR_AR_U)) # E: Any -reveal_type(np.msort(AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.msort(AR_LIKE_f8)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.msort(AR_U)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.msort(AR_LIKE_f8)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.median(AR_f8, keepdims=False)) # E: numpy.floating[Any] -reveal_type(np.median(AR_c16, overwrite_input=True)) # E: numpy.complexfloating[Any, Any] -reveal_type(np.median(AR_m)) # E: numpy.timedelta64 +reveal_type(np.median(AR_f8, keepdims=False)) # E: floating[Any] +reveal_type(np.median(AR_c16, overwrite_input=True)) # E: complexfloating[Any, Any] +reveal_type(np.median(AR_m)) # E: timedelta64 reveal_type(np.median(AR_O)) # E: Any reveal_type(np.median(AR_f8, keepdims=True)) # E: Any reveal_type(np.median(AR_c16, axis=0)) # E: Any -reveal_type(np.median(AR_LIKE_f8, out=AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.median(AR_LIKE_f8, out=AR_c16)) # E: ndarray[Any, dtype[{complex128}]] reveal_type(np.add_newdoc_ufunc(np.add, "docstring")) # E: None -reveal_type(np.percentile(AR_f8, 50)) # E: numpy.floating[Any] -reveal_type(np.percentile(AR_c16, 50)) # E: numpy.complexfloating[Any, Any] -reveal_type(np.percentile(AR_m, 50)) # E: numpy.timedelta64 -reveal_type(np.percentile(AR_M, 50, overwrite_input=True)) # E: numpy.datetime64 +reveal_type(np.percentile(AR_f8, 50)) # E: floating[Any] +reveal_type(np.percentile(AR_c16, 50)) # E: complexfloating[Any, Any] +reveal_type(np.percentile(AR_m, 50)) # E: timedelta64 +reveal_type(np.percentile(AR_M, 50, overwrite_input=True)) # E: datetime64 reveal_type(np.percentile(AR_O, 50)) # E: Any -reveal_type(np.percentile(AR_f8, [50])) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.percentile(AR_c16, [50])) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.percentile(AR_m, [50])) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(np.percentile(AR_M, [50], interpolation="nearest")) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(np.percentile(AR_O, [50])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(np.percentile(AR_f8, [50])) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.percentile(AR_c16, [50])) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.percentile(AR_m, [50])) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(np.percentile(AR_M, [50], interpolation="nearest")) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.percentile(AR_O, [50])) # E: ndarray[Any, dtype[object_]] reveal_type(np.percentile(AR_f8, [50], keepdims=True)) # E: Any reveal_type(np.percentile(AR_f8, [50], axis=[1])) # E: Any -reveal_type(np.percentile(AR_f8, [50], out=AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.percentile(AR_f8, [50], out=AR_c16)) # E: ndarray[Any, dtype[{complex128}]] -reveal_type(np.quantile(AR_f8, 0.5)) # E: numpy.floating[Any] -reveal_type(np.quantile(AR_c16, 0.5)) # E: numpy.complexfloating[Any, Any] -reveal_type(np.quantile(AR_m, 0.5)) # E: numpy.timedelta64 -reveal_type(np.quantile(AR_M, 0.5, overwrite_input=True)) # E: numpy.datetime64 +reveal_type(np.quantile(AR_f8, 0.5)) # E: floating[Any] +reveal_type(np.quantile(AR_c16, 0.5)) # E: complexfloating[Any, Any] +reveal_type(np.quantile(AR_m, 0.5)) # E: timedelta64 +reveal_type(np.quantile(AR_M, 0.5, overwrite_input=True)) # E: datetime64 reveal_type(np.quantile(AR_O, 0.5)) # E: Any -reveal_type(np.quantile(AR_f8, [0.5])) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.quantile(AR_c16, [0.5])) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.quantile(AR_m, [0.5])) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(np.quantile(AR_M, [0.5], interpolation="nearest")) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(np.quantile(AR_O, [0.5])) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(np.quantile(AR_f8, [0.5])) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.quantile(AR_c16, [0.5])) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.quantile(AR_m, [0.5])) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(np.quantile(AR_M, [0.5], interpolation="nearest")) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.quantile(AR_O, [0.5])) # E: ndarray[Any, dtype[object_]] reveal_type(np.quantile(AR_f8, [0.5], keepdims=True)) # E: Any reveal_type(np.quantile(AR_f8, [0.5], axis=[1])) # E: Any -reveal_type(np.quantile(AR_f8, [0.5], out=AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] +reveal_type(np.quantile(AR_f8, [0.5], out=AR_c16)) # E: ndarray[Any, dtype[{complex128}]] -reveal_type(np.meshgrid(AR_f8, AR_i8, copy=False)) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] -reveal_type(np.meshgrid(AR_f8, AR_i8, AR_c16, indexing="ij")) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.meshgrid(AR_f8, AR_i8, copy=False)) # E: list[ndarray[Any, dtype[Any]]] +reveal_type(np.meshgrid(AR_f8, AR_i8, AR_c16, indexing="ij")) # E: list[ndarray[Any, dtype[Any]]] -reveal_type(np.delete(AR_f8, np.s_[:5])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.delete(AR_LIKE_f8, [0, 4, 9], axis=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.delete(AR_f8, np.s_[:5])) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.delete(AR_LIKE_f8, [0, 4, 9], axis=0)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.insert(AR_f8, np.s_[:5], 5)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.insert(AR_LIKE_f8, [0, 4, 9], [0.5, 9.2, 7], axis=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.insert(AR_f8, np.s_[:5], 5)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.insert(AR_LIKE_f8, [0, 4, 9], [0.5, 9.2, 7], axis=0)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.append(AR_f8, 5)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.append(AR_LIKE_f8, 1j, axis=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.append(AR_f8, 5)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.append(AR_LIKE_f8, 1j, axis=0)) # E: ndarray[Any, dtype[Any]] reveal_type(np.digitize(4.5, [1])) # E: {intp} -reveal_type(np.digitize(AR_f8, [1, 2, 3])) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] +reveal_type(np.digitize(AR_f8, [1, 2, 3])) # E: ndarray[Any, dtype[{intp}]] diff --git a/numpy/typing/tests/data/reveal/lib_polynomial.pyi b/numpy/typing/tests/data/reveal/lib_polynomial.pyi index 5a4a3c424..de8950724 100644 --- a/numpy/typing/tests/data/reveal/lib_polynomial.pyi +++ b/numpy/typing/tests/data/reveal/lib_polynomial.pyi @@ -13,99 +13,99 @@ poly_obj: np.poly1d reveal_type(poly_obj.variable) # E: str reveal_type(poly_obj.order) # E: int reveal_type(poly_obj.o) # E: int -reveal_type(poly_obj.roots) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(poly_obj.r) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(poly_obj.coeffs) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(poly_obj.c) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(poly_obj.coef) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(poly_obj.coefficients) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(poly_obj.roots) # E: ndarray[Any, dtype[Any]] +reveal_type(poly_obj.r) # E: ndarray[Any, dtype[Any]] +reveal_type(poly_obj.coeffs) # E: ndarray[Any, dtype[Any]] +reveal_type(poly_obj.c) # E: ndarray[Any, dtype[Any]] +reveal_type(poly_obj.coef) # E: ndarray[Any, dtype[Any]] +reveal_type(poly_obj.coefficients) # E: ndarray[Any, dtype[Any]] reveal_type(poly_obj.__hash__) # E: None reveal_type(poly_obj(1)) # E: Any -reveal_type(poly_obj([1])) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(poly_obj(poly_obj)) # E: numpy.poly1d +reveal_type(poly_obj([1])) # E: ndarray[Any, dtype[Any]] +reveal_type(poly_obj(poly_obj)) # E: poly1d reveal_type(len(poly_obj)) # E: int -reveal_type(-poly_obj) # E: numpy.poly1d -reveal_type(+poly_obj) # E: numpy.poly1d - -reveal_type(poly_obj * 5) # E: numpy.poly1d -reveal_type(5 * poly_obj) # E: numpy.poly1d -reveal_type(poly_obj + 5) # E: numpy.poly1d -reveal_type(5 + poly_obj) # E: numpy.poly1d -reveal_type(poly_obj - 5) # E: numpy.poly1d -reveal_type(5 - poly_obj) # E: numpy.poly1d -reveal_type(poly_obj**1) # E: numpy.poly1d -reveal_type(poly_obj**1.0) # E: numpy.poly1d -reveal_type(poly_obj / 5) # E: numpy.poly1d -reveal_type(5 / poly_obj) # E: numpy.poly1d +reveal_type(-poly_obj) # E: poly1d +reveal_type(+poly_obj) # E: poly1d + +reveal_type(poly_obj * 5) # E: poly1d +reveal_type(5 * poly_obj) # E: poly1d +reveal_type(poly_obj + 5) # E: poly1d +reveal_type(5 + poly_obj) # E: poly1d +reveal_type(poly_obj - 5) # E: poly1d +reveal_type(5 - poly_obj) # E: poly1d +reveal_type(poly_obj**1) # E: poly1d +reveal_type(poly_obj**1.0) # E: poly1d +reveal_type(poly_obj / 5) # E: poly1d +reveal_type(5 / poly_obj) # E: poly1d reveal_type(poly_obj[0]) # E: Any poly_obj[0] = 5 reveal_type(iter(poly_obj)) # E: Iterator[Any] -reveal_type(poly_obj.deriv()) # E: numpy.poly1d -reveal_type(poly_obj.integ()) # E: numpy.poly1d - -reveal_type(np.poly(poly_obj)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.poly(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.poly(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] - -reveal_type(np.polyint(poly_obj)) # E: numpy.poly1d -reveal_type(np.polyint(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.polyint(AR_f8, k=AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.polyint(AR_O, m=2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] - -reveal_type(np.polyder(poly_obj)) # E: numpy.poly1d -reveal_type(np.polyder(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.polyder(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.polyder(AR_O, m=2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] - -reveal_type(np.polyfit(AR_f8, AR_f8, 2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.polyfit(AR_f8, AR_i8, 1, full=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]], numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]] -reveal_type(np.polyfit(AR_u4, AR_f8, 1.0, cov="unscaled")) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]] -reveal_type(np.polyfit(AR_c16, AR_f8, 2)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(np.polyfit(AR_f8, AR_c16, 1, full=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{complex128}]], numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]], numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]] -reveal_type(np.polyfit(AR_u4, AR_c16, 1.0, cov=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{complex128}]], numpy.ndarray[Any, numpy.dtype[{complex128}]]] - -reveal_type(np.polyval(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.polyval(AR_u4, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(np.polyval(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.polyval(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.polyval(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.polyval(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] - -reveal_type(np.polyadd(poly_obj, AR_i8)) # E: numpy.poly1d -reveal_type(np.polyadd(AR_f8, poly_obj)) # E: numpy.poly1d -reveal_type(np.polyadd(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.polyadd(AR_u4, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(np.polyadd(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.polyadd(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.polyadd(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.polyadd(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] - -reveal_type(np.polysub(poly_obj, AR_i8)) # E: numpy.poly1d -reveal_type(np.polysub(AR_f8, poly_obj)) # E: numpy.poly1d +reveal_type(poly_obj.deriv()) # E: poly1d +reveal_type(poly_obj.integ()) # E: poly1d + +reveal_type(np.poly(poly_obj)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.poly(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.poly(AR_c16)) # E: ndarray[Any, dtype[floating[Any]]] + +reveal_type(np.polyint(poly_obj)) # E: poly1d +reveal_type(np.polyint(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.polyint(AR_f8, k=AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.polyint(AR_O, m=2)) # E: ndarray[Any, dtype[object_]] + +reveal_type(np.polyder(poly_obj)) # E: poly1d +reveal_type(np.polyder(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.polyder(AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.polyder(AR_O, m=2)) # E: ndarray[Any, dtype[object_]] + +reveal_type(np.polyfit(AR_f8, AR_f8, 2)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.polyfit(AR_f8, AR_i8, 1, full=True)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[signedinteger[typing._32Bit]]], ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]]] +reveal_type(np.polyfit(AR_u4, AR_f8, 1.0, cov="unscaled")) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]]] +reveal_type(np.polyfit(AR_c16, AR_f8, 2)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(np.polyfit(AR_f8, AR_c16, 1, full=True)) # E: Tuple[ndarray[Any, dtype[{complex128}]], ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[signedinteger[typing._32Bit]]], ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]]] +reveal_type(np.polyfit(AR_u4, AR_c16, 1.0, cov=True)) # E: Tuple[ndarray[Any, dtype[{complex128}]], ndarray[Any, dtype[{complex128}]]] + +reveal_type(np.polyval(AR_b, AR_b)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.polyval(AR_u4, AR_b)) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(np.polyval(AR_i8, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.polyval(AR_f8, AR_i8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.polyval(AR_i8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.polyval(AR_O, AR_O)) # E: ndarray[Any, dtype[object_]] + +reveal_type(np.polyadd(poly_obj, AR_i8)) # E: poly1d +reveal_type(np.polyadd(AR_f8, poly_obj)) # E: poly1d +reveal_type(np.polyadd(AR_b, AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.polyadd(AR_u4, AR_b)) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(np.polyadd(AR_i8, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.polyadd(AR_f8, AR_i8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.polyadd(AR_i8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.polyadd(AR_O, AR_O)) # E: ndarray[Any, dtype[object_]] + +reveal_type(np.polysub(poly_obj, AR_i8)) # E: poly1d +reveal_type(np.polysub(AR_f8, poly_obj)) # E: poly1d reveal_type(np.polysub(AR_b, AR_b)) # E: -reveal_type(np.polysub(AR_u4, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(np.polysub(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.polysub(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.polysub(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.polysub(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] - -reveal_type(np.polymul(poly_obj, AR_i8)) # E: numpy.poly1d -reveal_type(np.polymul(AR_f8, poly_obj)) # E: numpy.poly1d -reveal_type(np.polymul(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.polymul(AR_u4, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(np.polymul(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.polymul(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.polymul(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.polymul(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] - -reveal_type(np.polydiv(poly_obj, AR_i8)) # E: numpy.poly1d -reveal_type(np.polydiv(AR_f8, poly_obj)) # E: numpy.poly1d -reveal_type(np.polydiv(AR_b, AR_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.polydiv(AR_u4, AR_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.polydiv(AR_i8, AR_i8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.polydiv(AR_f8, AR_i8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.polydiv(AR_i8, AR_c16)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]] -reveal_type(np.polydiv(AR_O, AR_O)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.polysub(AR_u4, AR_b)) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(np.polysub(AR_i8, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.polysub(AR_f8, AR_i8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.polysub(AR_i8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.polysub(AR_O, AR_O)) # E: ndarray[Any, dtype[object_]] + +reveal_type(np.polymul(poly_obj, AR_i8)) # E: poly1d +reveal_type(np.polymul(AR_f8, poly_obj)) # E: poly1d +reveal_type(np.polymul(AR_b, AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.polymul(AR_u4, AR_b)) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(np.polymul(AR_i8, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.polymul(AR_f8, AR_i8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.polymul(AR_i8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.polymul(AR_O, AR_O)) # E: ndarray[Any, dtype[object_]] + +reveal_type(np.polydiv(poly_obj, AR_i8)) # E: poly1d +reveal_type(np.polydiv(AR_f8, poly_obj)) # E: poly1d +reveal_type(np.polydiv(AR_b, AR_b)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.polydiv(AR_u4, AR_b)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.polydiv(AR_i8, AR_i8)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.polydiv(AR_f8, AR_i8)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.polydiv(AR_i8, AR_c16)) # E: Tuple[ndarray[Any, dtype[complexfloating[Any, Any]]], ndarray[Any, dtype[complexfloating[Any, Any]]]] +reveal_type(np.polydiv(AR_O, AR_O)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[Any]]] diff --git a/numpy/typing/tests/data/reveal/linalg.pyi b/numpy/typing/tests/data/reveal/linalg.pyi index fecdc0d37..19e13aed6 100644 --- a/numpy/typing/tests/data/reveal/linalg.pyi +++ b/numpy/typing/tests/data/reveal/linalg.pyi @@ -8,57 +8,57 @@ AR_O: npt.NDArray[np.object_] AR_m: npt.NDArray[np.timedelta64] AR_S: npt.NDArray[np.str_] -reveal_type(np.linalg.tensorsolve(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.linalg.tensorsolve(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.linalg.tensorsolve(AR_c16, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] - -reveal_type(np.linalg.solve(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.linalg.solve(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.linalg.solve(AR_c16, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] - -reveal_type(np.linalg.tensorinv(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.linalg.tensorinv(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.linalg.tensorinv(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] - -reveal_type(np.linalg.inv(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.linalg.inv(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.linalg.inv(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] - -reveal_type(np.linalg.matrix_power(AR_i8, -1)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.linalg.matrix_power(AR_f8, 0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.linalg.matrix_power(AR_c16, 1)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.linalg.matrix_power(AR_O, 2)) # E: numpy.ndarray[Any, numpy.dtype[Any]] - -reveal_type(np.linalg.cholesky(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.linalg.cholesky(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.linalg.cholesky(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] - -reveal_type(np.linalg.qr(AR_i8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]] -reveal_type(np.linalg.qr(AR_f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.linalg.qr(AR_c16)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]] - -reveal_type(np.linalg.eigvals(AR_i8)) # E: Union[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{complex128}]]] -reveal_type(np.linalg.eigvals(AR_f8)) # E: Union[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]] -reveal_type(np.linalg.eigvals(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] - -reveal_type(np.linalg.eigvalsh(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.linalg.eigvalsh(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.linalg.eigvalsh(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] - -reveal_type(np.linalg.eig(AR_i8)) # E: Union[Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]], Tuple[numpy.ndarray[Any, numpy.dtype[{complex128}]], numpy.ndarray[Any, numpy.dtype[{complex128}]]]] -reveal_type(np.linalg.eig(AR_f8)) # E: Union[Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]], Tuple[numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]]] -reveal_type(np.linalg.eig(AR_c16)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]] - -reveal_type(np.linalg.eigh(AR_i8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]] -reveal_type(np.linalg.eigh(AR_f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.linalg.eigh(AR_c16)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]] - -reveal_type(np.linalg.svd(AR_i8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]] -reveal_type(np.linalg.svd(AR_f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.linalg.svd(AR_c16)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]] -reveal_type(np.linalg.svd(AR_i8, compute_uv=False)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.linalg.svd(AR_f8, compute_uv=False)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.linalg.svd(AR_c16, compute_uv=False)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.linalg.tensorsolve(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.linalg.tensorsolve(AR_i8, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.linalg.tensorsolve(AR_c16, AR_f8)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] + +reveal_type(np.linalg.solve(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.linalg.solve(AR_i8, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.linalg.solve(AR_c16, AR_f8)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] + +reveal_type(np.linalg.tensorinv(AR_i8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.linalg.tensorinv(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.linalg.tensorinv(AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] + +reveal_type(np.linalg.inv(AR_i8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.linalg.inv(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.linalg.inv(AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] + +reveal_type(np.linalg.matrix_power(AR_i8, -1)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.linalg.matrix_power(AR_f8, 0)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.linalg.matrix_power(AR_c16, 1)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.linalg.matrix_power(AR_O, 2)) # E: ndarray[Any, dtype[Any]] + +reveal_type(np.linalg.cholesky(AR_i8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.linalg.cholesky(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.linalg.cholesky(AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] + +reveal_type(np.linalg.qr(AR_i8)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]]] +reveal_type(np.linalg.qr(AR_f8)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.linalg.qr(AR_c16)) # E: Tuple[ndarray[Any, dtype[complexfloating[Any, Any]]], ndarray[Any, dtype[complexfloating[Any, Any]]]] + +reveal_type(np.linalg.eigvals(AR_i8)) # E: Union[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{complex128}]]] +reveal_type(np.linalg.eigvals(AR_f8)) # E: Union[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[complexfloating[Any, Any]]]] +reveal_type(np.linalg.eigvals(AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] + +reveal_type(np.linalg.eigvalsh(AR_i8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.linalg.eigvalsh(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.linalg.eigvalsh(AR_c16)) # E: ndarray[Any, dtype[floating[Any]]] + +reveal_type(np.linalg.eig(AR_i8)) # E: Union[Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]]], Tuple[ndarray[Any, dtype[{complex128}]], ndarray[Any, dtype[{complex128}]]]] +reveal_type(np.linalg.eig(AR_f8)) # E: Union[Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]], Tuple[ndarray[Any, dtype[complexfloating[Any, Any]]], ndarray[Any, dtype[complexfloating[Any, Any]]]]] +reveal_type(np.linalg.eig(AR_c16)) # E: Tuple[ndarray[Any, dtype[complexfloating[Any, Any]]], ndarray[Any, dtype[complexfloating[Any, Any]]]] + +reveal_type(np.linalg.eigh(AR_i8)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]]] +reveal_type(np.linalg.eigh(AR_f8)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.linalg.eigh(AR_c16)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[complexfloating[Any, Any]]]] + +reveal_type(np.linalg.svd(AR_i8)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]]] +reveal_type(np.linalg.svd(AR_f8)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.linalg.svd(AR_c16)) # E: Tuple[ndarray[Any, dtype[complexfloating[Any, Any]]], ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[complexfloating[Any, Any]]]] +reveal_type(np.linalg.svd(AR_i8, compute_uv=False)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.linalg.svd(AR_f8, compute_uv=False)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.linalg.svd(AR_c16, compute_uv=False)) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(np.linalg.cond(AR_i8)) # E: Any reveal_type(np.linalg.cond(AR_f8)) # E: Any @@ -68,9 +68,9 @@ reveal_type(np.linalg.matrix_rank(AR_i8)) # E: Any reveal_type(np.linalg.matrix_rank(AR_f8)) # E: Any reveal_type(np.linalg.matrix_rank(AR_c16)) # E: Any -reveal_type(np.linalg.pinv(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.linalg.pinv(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.linalg.pinv(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] +reveal_type(np.linalg.pinv(AR_i8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.linalg.pinv(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.linalg.pinv(AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] reveal_type(np.linalg.slogdet(AR_i8)) # E: Tuple[Any, Any] reveal_type(np.linalg.slogdet(AR_f8)) # E: Tuple[Any, Any] @@ -80,14 +80,14 @@ reveal_type(np.linalg.det(AR_i8)) # E: Any reveal_type(np.linalg.det(AR_f8)) # E: Any reveal_type(np.linalg.det(AR_c16)) # E: Any -reveal_type(np.linalg.lstsq(AR_i8, AR_i8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]], {int32}, numpy.ndarray[Any, numpy.dtype[{float64}]]] -reveal_type(np.linalg.lstsq(AR_i8, AR_f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], {int32}, numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.linalg.lstsq(AR_f8, AR_c16)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], {int32}, numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] +reveal_type(np.linalg.lstsq(AR_i8, AR_i8)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{float64}]], {int32}, ndarray[Any, dtype[{float64}]]] +reveal_type(np.linalg.lstsq(AR_i8, AR_f8)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]], {int32}, ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.linalg.lstsq(AR_f8, AR_c16)) # E: Tuple[ndarray[Any, dtype[complexfloating[Any, Any]]], ndarray[Any, dtype[floating[Any]]], {int32}, ndarray[Any, dtype[floating[Any]]]] -reveal_type(np.linalg.norm(AR_i8)) # E: numpy.floating[Any] -reveal_type(np.linalg.norm(AR_f8)) # E: numpy.floating[Any] -reveal_type(np.linalg.norm(AR_c16)) # E: numpy.floating[Any] -reveal_type(np.linalg.norm(AR_S)) # E: numpy.floating[Any] +reveal_type(np.linalg.norm(AR_i8)) # E: floating[Any] +reveal_type(np.linalg.norm(AR_f8)) # E: floating[Any] +reveal_type(np.linalg.norm(AR_c16)) # E: floating[Any] +reveal_type(np.linalg.norm(AR_S)) # E: floating[Any] reveal_type(np.linalg.norm(AR_f8, axis=0)) # E: Any reveal_type(np.linalg.multi_dot([AR_i8, AR_i8])) # E: Any diff --git a/numpy/typing/tests/data/reveal/matrix.pyi b/numpy/typing/tests/data/reveal/matrix.pyi index def33f458..21c39067e 100644 --- a/numpy/typing/tests/data/reveal/matrix.pyi +++ b/numpy/typing/tests/data/reveal/matrix.pyi @@ -5,11 +5,11 @@ import numpy.typing as npt mat: np.matrix[Any, np.dtype[np.int64]] ar_f8: npt.NDArray[np.float64] -reveal_type(mat * 5) # E: numpy.matrix[Any, Any] -reveal_type(5 * mat) # E: numpy.matrix[Any, Any] +reveal_type(mat * 5) # E: matrix[Any, Any] +reveal_type(5 * mat) # E: matrix[Any, Any] mat *= 5 -reveal_type(mat**5) # E: numpy.matrix[Any, Any] +reveal_type(mat**5) # E: matrix[Any, Any] mat **= 5 reveal_type(mat.sum()) # E: Any @@ -17,53 +17,53 @@ reveal_type(mat.mean()) # E: Any reveal_type(mat.std()) # E: Any reveal_type(mat.var()) # E: Any reveal_type(mat.prod()) # E: Any -reveal_type(mat.any()) # E: numpy.bool_ -reveal_type(mat.all()) # E: numpy.bool_ +reveal_type(mat.any()) # E: bool_ +reveal_type(mat.all()) # E: bool_ reveal_type(mat.max()) # E: {int64} reveal_type(mat.min()) # E: {int64} reveal_type(mat.argmax()) # E: {intp} reveal_type(mat.argmin()) # E: {intp} reveal_type(mat.ptp()) # E: {int64} -reveal_type(mat.sum(axis=0)) # E: numpy.matrix[Any, Any] -reveal_type(mat.mean(axis=0)) # E: numpy.matrix[Any, Any] -reveal_type(mat.std(axis=0)) # E: numpy.matrix[Any, Any] -reveal_type(mat.var(axis=0)) # E: numpy.matrix[Any, Any] -reveal_type(mat.prod(axis=0)) # E: numpy.matrix[Any, Any] -reveal_type(mat.any(axis=0)) # E: numpy.matrix[Any, numpy.dtype[numpy.bool_]] -reveal_type(mat.all(axis=0)) # E: numpy.matrix[Any, numpy.dtype[numpy.bool_]] -reveal_type(mat.max(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{int64}]] -reveal_type(mat.min(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{int64}]] -reveal_type(mat.argmax(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{intp}]] -reveal_type(mat.argmin(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{intp}]] -reveal_type(mat.ptp(axis=0)) # E: numpy.matrix[Any, numpy.dtype[{int64}]] +reveal_type(mat.sum(axis=0)) # E: matrix[Any, Any] +reveal_type(mat.mean(axis=0)) # E: matrix[Any, Any] +reveal_type(mat.std(axis=0)) # E: matrix[Any, Any] +reveal_type(mat.var(axis=0)) # E: matrix[Any, Any] +reveal_type(mat.prod(axis=0)) # E: matrix[Any, Any] +reveal_type(mat.any(axis=0)) # E: matrix[Any, dtype[bool_]] +reveal_type(mat.all(axis=0)) # E: matrix[Any, dtype[bool_]] +reveal_type(mat.max(axis=0)) # E: matrix[Any, dtype[{int64}]] +reveal_type(mat.min(axis=0)) # E: matrix[Any, dtype[{int64}]] +reveal_type(mat.argmax(axis=0)) # E: matrix[Any, dtype[{intp}]] +reveal_type(mat.argmin(axis=0)) # E: matrix[Any, dtype[{intp}]] +reveal_type(mat.ptp(axis=0)) # E: matrix[Any, dtype[{int64}]] -reveal_type(mat.sum(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.mean(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.std(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.var(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.prod(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.any(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.all(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.max(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.min(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.argmax(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.argmin(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(mat.ptp(out=ar_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(mat.sum(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.mean(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.std(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.var(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.prod(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.any(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.all(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.max(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.min(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.argmax(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.argmin(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(mat.ptp(out=ar_f8)) # E: ndarray[Any, dtype[{float64}]] -reveal_type(mat.T) # E: numpy.matrix[Any, numpy.dtype[{int64}]] -reveal_type(mat.I) # E: numpy.matrix[Any, Any] -reveal_type(mat.A) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(mat.A1) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(mat.H) # E: numpy.matrix[Any, numpy.dtype[{int64}]] -reveal_type(mat.getT()) # E: numpy.matrix[Any, numpy.dtype[{int64}]] -reveal_type(mat.getI()) # E: numpy.matrix[Any, Any] -reveal_type(mat.getA()) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(mat.getA1()) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(mat.getH()) # E: numpy.matrix[Any, numpy.dtype[{int64}]] +reveal_type(mat.T) # E: matrix[Any, dtype[{int64}]] +reveal_type(mat.I) # E: matrix[Any, Any] +reveal_type(mat.A) # E: ndarray[Any, dtype[{int64}]] +reveal_type(mat.A1) # E: ndarray[Any, dtype[{int64}]] +reveal_type(mat.H) # E: matrix[Any, dtype[{int64}]] +reveal_type(mat.getT()) # E: matrix[Any, dtype[{int64}]] +reveal_type(mat.getI()) # E: matrix[Any, Any] +reveal_type(mat.getA()) # E: ndarray[Any, dtype[{int64}]] +reveal_type(mat.getA1()) # E: ndarray[Any, dtype[{int64}]] +reveal_type(mat.getH()) # E: matrix[Any, dtype[{int64}]] -reveal_type(np.bmat(ar_f8)) # E: numpy.matrix[Any, Any] -reveal_type(np.bmat([[0, 1, 2]])) # E: numpy.matrix[Any, Any] -reveal_type(np.bmat("mat")) # E: numpy.matrix[Any, Any] +reveal_type(np.bmat(ar_f8)) # E: matrix[Any, Any] +reveal_type(np.bmat([[0, 1, 2]])) # E: matrix[Any, Any] +reveal_type(np.bmat("mat")) # E: matrix[Any, Any] -reveal_type(np.asmatrix(ar_f8, dtype=np.int64)) # E: numpy.matrix[Any, Any] +reveal_type(np.asmatrix(ar_f8, dtype=np.int64)) # E: matrix[Any, Any] diff --git a/numpy/typing/tests/data/reveal/memmap.pyi b/numpy/typing/tests/data/reveal/memmap.pyi index c1d8edc67..86de8eb08 100644 --- a/numpy/typing/tests/data/reveal/memmap.pyi +++ b/numpy/typing/tests/data/reveal/memmap.pyi @@ -10,7 +10,7 @@ reveal_type(memmap_obj.offset) # E: int reveal_type(memmap_obj.mode) # E: str reveal_type(memmap_obj.flush()) # E: None -reveal_type(np.memmap("file.txt", offset=5)) # E: numpy.memmap[Any, numpy.dtype[{uint8}]] -reveal_type(np.memmap(b"file.txt", dtype=np.float64, shape=(10, 3))) # E: numpy.memmap[Any, numpy.dtype[{float64}]] +reveal_type(np.memmap("file.txt", offset=5)) # E: memmap[Any, dtype[{uint8}]] +reveal_type(np.memmap(b"file.txt", dtype=np.float64, shape=(10, 3))) # E: memmap[Any, dtype[{float64}]] with open("file.txt", "rb") as f: - reveal_type(np.memmap(f, dtype=float, order="K")) # E: numpy.memmap[Any, numpy.dtype[Any]] + reveal_type(np.memmap(f, dtype=float, order="K")) # E: memmap[Any, dtype[Any]] diff --git a/numpy/typing/tests/data/reveal/mod.pyi b/numpy/typing/tests/data/reveal/mod.pyi index bf45b8c58..b2790b7f3 100644 --- a/numpy/typing/tests/data/reveal/mod.pyi +++ b/numpy/typing/tests/data/reveal/mod.pyi @@ -21,13 +21,13 @@ AR_m: np.ndarray[Any, np.dtype[np.timedelta64]] # Time structures -reveal_type(td % td) # E: numpy.timedelta64 +reveal_type(td % td) # E: timedelta64 reveal_type(AR_m % td) # E: Any reveal_type(td % AR_m) # E: Any -reveal_type(divmod(td, td)) # E: Tuple[{int64}, numpy.timedelta64] -reveal_type(divmod(AR_m, td)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]], numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]]] -reveal_type(divmod(td, AR_m)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]], numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]]] +reveal_type(divmod(td, td)) # E: Tuple[{int64}, timedelta64] +reveal_type(divmod(AR_m, td)) # E: Tuple[ndarray[Any, dtype[signedinteger[typing._64Bit]]], ndarray[Any, dtype[timedelta64]]] +reveal_type(divmod(td, AR_m)) # E: Tuple[ndarray[Any, dtype[signedinteger[typing._64Bit]]], ndarray[Any, dtype[timedelta64]]] # Bool @@ -38,7 +38,7 @@ reveal_type(b_ % b_) # E: {int8} reveal_type(b_ % i8) # E: {int64} reveal_type(b_ % u8) # E: {uint64} reveal_type(b_ % f8) # E: {float64} -reveal_type(b_ % AR_b) # E: numpy.ndarray[Any, numpy.dtype[{int8}]] +reveal_type(b_ % AR_b) # E: ndarray[Any, dtype[{int8}]] reveal_type(divmod(b_, b)) # E: Tuple[{int8}, {int8}] reveal_type(divmod(b_, i)) # E: Tuple[{int_}, {int_}] @@ -47,7 +47,7 @@ reveal_type(divmod(b_, b_)) # E: Tuple[{int8}, {int8}] reveal_type(divmod(b_, i8)) # E: Tuple[{int64}, {int64}] reveal_type(divmod(b_, u8)) # E: Tuple[{uint64}, {uint64}] reveal_type(divmod(b_, f8)) # E: Tuple[{float64}, {float64}] -reveal_type(divmod(b_, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[{int8}]], numpy.ndarray[Any, numpy.dtype[{int8}]]] +reveal_type(divmod(b_, AR_b)) # E: ndarray[Any, dtype[{int8}]], ndarray[Any, dtype[{int8}]]] reveal_type(b % b_) # E: {int8} reveal_type(i % b_) # E: {int_} @@ -56,7 +56,7 @@ reveal_type(b_ % b_) # E: {int8} reveal_type(i8 % b_) # E: {int64} reveal_type(u8 % b_) # E: {uint64} reveal_type(f8 % b_) # E: {float64} -reveal_type(AR_b % b_) # E: numpy.ndarray[Any, numpy.dtype[{int8}]] +reveal_type(AR_b % b_) # E: ndarray[Any, dtype[{int8}]] reveal_type(divmod(b, b_)) # E: Tuple[{int8}, {int8}] reveal_type(divmod(i, b_)) # E: Tuple[{int_}, {int_}] @@ -65,7 +65,7 @@ reveal_type(divmod(b_, b_)) # E: Tuple[{int8}, {int8}] reveal_type(divmod(i8, b_)) # E: Tuple[{int64}, {int64}] reveal_type(divmod(u8, b_)) # E: Tuple[{uint64}, {uint64}] reveal_type(divmod(f8, b_)) # E: Tuple[{float64}, {float64}] -reveal_type(divmod(AR_b, b_)) # E: numpy.ndarray[Any, numpy.dtype[{int8}]], numpy.ndarray[Any, numpy.dtype[{int8}]]] +reveal_type(divmod(AR_b, b_)) # E: ndarray[Any, dtype[{int8}]], ndarray[Any, dtype[{int8}]]] # int @@ -78,7 +78,7 @@ reveal_type(i4 % i8) # E: {int64} reveal_type(i4 % f8) # E: {float64} reveal_type(i4 % i4) # E: {int32} reveal_type(i4 % f4) # E: {float32} -reveal_type(i8 % AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(i8 % AR_b) # E: ndarray[Any, dtype[signedinteger[Any]]] reveal_type(divmod(i8, b)) # E: Tuple[{int64}, {int64}] reveal_type(divmod(i8, i)) # E: Tuple[{int64}, {int64}] @@ -89,7 +89,7 @@ reveal_type(divmod(i8, i4)) # E: Tuple[{int64}, {int64}] reveal_type(divmod(i8, f4)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(i4, i4)) # E: Tuple[{int32}, {int32}] reveal_type(divmod(i4, f4)) # E: Tuple[{float32}, {float32}] -reveal_type(divmod(i8, AR_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]]] +reveal_type(divmod(i8, AR_b)) # E: Tuple[ndarray[Any, dtype[signedinteger[Any]]], ndarray[Any, dtype[signedinteger[Any]]]] reveal_type(b % i8) # E: {int64} reveal_type(i % i8) # E: {int64} @@ -100,7 +100,7 @@ reveal_type(i8 % i4) # E: {int64} reveal_type(f8 % i4) # E: {float64} reveal_type(i4 % i4) # E: {int32} reveal_type(f4 % i4) # E: {float32} -reveal_type(AR_b % i8) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(AR_b % i8) # E: ndarray[Any, dtype[signedinteger[Any]]] reveal_type(divmod(b, i8)) # E: Tuple[{int64}, {int64}] reveal_type(divmod(i, i8)) # E: Tuple[{int64}, {int64}] @@ -111,7 +111,7 @@ reveal_type(divmod(i4, i8)) # E: Tuple[{int64}, {int64}] reveal_type(divmod(f4, i8)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(i4, i4)) # E: Tuple[{int32}, {int32}] reveal_type(divmod(f4, i4)) # E: Tuple[{float32}, {float32}] -reveal_type(divmod(AR_b, i8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]]] +reveal_type(divmod(AR_b, i8)) # E: Tuple[ndarray[Any, dtype[signedinteger[Any]]], ndarray[Any, dtype[signedinteger[Any]]]] # float @@ -120,7 +120,7 @@ reveal_type(f8 % i) # E: {float64} reveal_type(f8 % f) # E: {float64} reveal_type(i8 % f4) # E: {float64} reveal_type(f4 % f4) # E: {float32} -reveal_type(f8 % AR_b) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(f8 % AR_b) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(divmod(f8, b)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(f8, i)) # E: Tuple[{float64}, {float64}] @@ -128,7 +128,7 @@ reveal_type(divmod(f8, f)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(f8, f8)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(f8, f4)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(f4, f4)) # E: Tuple[{float32}, {float32}] -reveal_type(divmod(f8, AR_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] +reveal_type(divmod(f8, AR_b)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] reveal_type(b % f8) # E: {float64} reveal_type(i % f8) # E: {float64} @@ -136,7 +136,7 @@ reveal_type(f % f8) # E: {float64} reveal_type(f8 % f8) # E: {float64} reveal_type(f8 % f8) # E: {float64} reveal_type(f4 % f4) # E: {float32} -reveal_type(AR_b % f8) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(AR_b % f8) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(divmod(b, f8)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(i, f8)) # E: Tuple[{float64}, {float64}] @@ -144,4 +144,4 @@ reveal_type(divmod(f, f8)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(f8, f8)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(f4, f8)) # E: Tuple[{float64}, {float64}] reveal_type(divmod(f4, f4)) # E: Tuple[{float32}, {float32}] -reveal_type(divmod(AR_b, f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] +reveal_type(divmod(AR_b, f8)) # E: Tuple[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] diff --git a/numpy/typing/tests/data/reveal/modules.pyi b/numpy/typing/tests/data/reveal/modules.pyi index 7e695433e..ba830eb0d 100644 --- a/numpy/typing/tests/data/reveal/modules.pyi +++ b/numpy/typing/tests/data/reveal/modules.pyi @@ -32,7 +32,7 @@ reveal_type(np.polynomial.polynomial) # E: ModuleType reveal_type(np.__path__) # E: list[builtins.str] reveal_type(np.__version__) # E: str reveal_type(np.__git_version__) # E: str -reveal_type(np.test) # E: numpy._pytesttester.PytestTester +reveal_type(np.test) # E: _pytesttester.PytestTester reveal_type(np.test.module_name) # E: str reveal_type(np.__all__) # E: list[builtins.str] diff --git a/numpy/typing/tests/data/reveal/multiarray.pyi b/numpy/typing/tests/data/reveal/multiarray.pyi index ee818c08a..0e91a7afd 100644 --- a/numpy/typing/tests/data/reveal/multiarray.pyi +++ b/numpy/typing/tests/data/reveal/multiarray.pyi @@ -32,7 +32,7 @@ def func(a: int) -> bool: ... reveal_type(next(b_f8)) # E: tuple[Any] reveal_type(b_f8.reset()) # E: None reveal_type(b_f8.index) # E: int -reveal_type(b_f8.iters) # E: tuple[numpy.flatiter[Any]] +reveal_type(b_f8.iters) # E: tuple[flatiter[Any]] reveal_type(b_f8.nd) # E: int reveal_type(b_f8.ndim) # E: int reveal_type(b_f8.numiter) # E: int @@ -42,7 +42,7 @@ reveal_type(b_f8.size) # E: int reveal_type(next(b_i8_f8_f8)) # E: tuple[Any] reveal_type(b_i8_f8_f8.reset()) # E: None reveal_type(b_i8_f8_f8.index) # E: int -reveal_type(b_i8_f8_f8.iters) # E: tuple[numpy.flatiter[Any]] +reveal_type(b_i8_f8_f8.iters) # E: tuple[flatiter[Any]] reveal_type(b_i8_f8_f8.nd) # E: int reveal_type(b_i8_f8_f8.ndim) # E: int reveal_type(b_i8_f8_f8.numiter) # E: int @@ -51,8 +51,8 @@ reveal_type(b_i8_f8_f8.size) # E: int reveal_type(np.inner(AR_f8, AR_i8)) # E: Any -reveal_type(np.where([True, True, False])) # E: tuple[numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.where([True, True, False], 1, 0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.where([True, True, False])) # E: tuple[ndarray[Any, dtype[{intp}]]] +reveal_type(np.where([True, True, False], 1, 0)) # E: ndarray[Any, dtype[Any]] reveal_type(np.lexsort([0, 1, 2])) # E: Any @@ -60,32 +60,32 @@ reveal_type(np.can_cast(np.dtype("i8"), int)) # E: bool reveal_type(np.can_cast(AR_f8, "f8")) # E: bool reveal_type(np.can_cast(AR_f8, np.complex128, casting="unsafe")) # E: bool -reveal_type(np.min_scalar_type([1])) # E: numpy.dtype[Any] -reveal_type(np.min_scalar_type(AR_f8)) # E: numpy.dtype[Any] +reveal_type(np.min_scalar_type([1])) # E: dtype[Any] +reveal_type(np.min_scalar_type(AR_f8)) # E: dtype[Any] -reveal_type(np.result_type(int, [1])) # E: numpy.dtype[Any] -reveal_type(np.result_type(AR_f8, AR_u1)) # E: numpy.dtype[Any] -reveal_type(np.result_type(AR_f8, np.complex128)) # E: numpy.dtype[Any] +reveal_type(np.result_type(int, [1])) # E: dtype[Any] +reveal_type(np.result_type(AR_f8, AR_u1)) # E: dtype[Any] +reveal_type(np.result_type(AR_f8, np.complex128)) # E: dtype[Any] reveal_type(np.dot(AR_LIKE_f, AR_i8)) # E: Any reveal_type(np.dot(AR_u1, 1)) # E: Any reveal_type(np.dot(1.5j, 1)) # E: Any -reveal_type(np.dot(AR_u1, 1, out=AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.dot(AR_u1, 1, out=AR_f8)) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.vdot(AR_LIKE_f, AR_i8)) # E: numpy.floating[Any] -reveal_type(np.vdot(AR_u1, 1)) # E: numpy.signedinteger[Any] -reveal_type(np.vdot(1.5j, 1)) # E: numpy.complexfloating[Any, Any] +reveal_type(np.vdot(AR_LIKE_f, AR_i8)) # E: floating[Any] +reveal_type(np.vdot(AR_u1, 1)) # E: signedinteger[Any] +reveal_type(np.vdot(1.5j, 1)) # E: complexfloating[Any, Any] -reveal_type(np.bincount(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] +reveal_type(np.bincount(AR_i8)) # E: ndarray[Any, dtype[{intp}]] reveal_type(np.copyto(AR_f8, [1., 1.5, 1.6])) # E: None reveal_type(np.putmask(AR_f8, [True, True, False], 1.5)) # E: None -reveal_type(np.packbits(AR_i8)) # numpy.ndarray[Any, numpy.dtype[{uint8}]] -reveal_type(np.packbits(AR_u1)) # numpy.ndarray[Any, numpy.dtype[{uint8}]] +reveal_type(np.packbits(AR_i8)) # ndarray[Any, dtype[{uint8}]] +reveal_type(np.packbits(AR_u1)) # ndarray[Any, dtype[{uint8}]] -reveal_type(np.unpackbits(AR_u1)) # numpy.ndarray[Any, numpy.dtype[{uint8}]] +reveal_type(np.unpackbits(AR_u1)) # ndarray[Any, dtype[{uint8}]] reveal_type(np.shares_memory(1, 2)) # E: bool reveal_type(np.shares_memory(AR_f8, AR_f8, max_work=1)) # E: bool @@ -97,36 +97,36 @@ reveal_type(np.geterrobj()) # E: list[Any] reveal_type(np.seterrobj([8192, 521, None])) # E: None -reveal_type(np.promote_types(np.int32, np.int64)) # E: numpy.dtype[Any] -reveal_type(np.promote_types("f4", float)) # E: numpy.dtype[Any] +reveal_type(np.promote_types(np.int32, np.int64)) # E: dtype[Any] +reveal_type(np.promote_types("f4", float)) # E: dtype[Any] -reveal_type(np.frompyfunc(func, 1, 1, identity=None)) # numpy.ufunc +reveal_type(np.frompyfunc(func, 1, 1, identity=None)) # ufunc reveal_type(np.datetime_data("m8[D]")) # E: Tuple[builtins.str, builtins.int] reveal_type(np.datetime_data(np.datetime64)) # E: Tuple[builtins.str, builtins.int] reveal_type(np.datetime_data(np.dtype(np.timedelta64))) # E: Tuple[builtins.str, builtins.int] reveal_type(np.busday_count("2011-01", "2011-02")) # E: {int_} -reveal_type(np.busday_count(["2011-01"], "2011-02")) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(np.busday_count(["2011-01"], "2011-02")) # E: ndarray[Any, dtype[{int_}]] -reveal_type(np.busday_offset(M, m)) # E: numpy.datetime64 -reveal_type(np.busday_offset(M, 5)) # E: numpy.datetime64 -reveal_type(np.busday_offset(AR_M, m)) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] -reveal_type(np.busday_offset("2011-01", "2011-02", roll="forward")) # E: numpy.datetime64 -reveal_type(np.busday_offset(["2011-01"], "2011-02", roll="forward")) # E: numpy.ndarray[Any, numpy.dtype[numpy.datetime64]] +reveal_type(np.busday_offset(M, m)) # E: datetime64 +reveal_type(np.busday_offset(M, 5)) # E: datetime64 +reveal_type(np.busday_offset(AR_M, m)) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.busday_offset("2011-01", "2011-02", roll="forward")) # E: datetime64 +reveal_type(np.busday_offset(["2011-01"], "2011-02", roll="forward")) # E: ndarray[Any, dtype[datetime64]] -reveal_type(np.is_busday("2012")) # E: numpy.bool_ -reveal_type(np.is_busday(["2012"])) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.is_busday("2012")) # E: bool_ +reveal_type(np.is_busday(["2012"])) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.datetime_as_string(M)) # E: numpy.str_ -reveal_type(np.datetime_as_string(AR_M)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.datetime_as_string(M)) # E: str_ +reveal_type(np.datetime_as_string(AR_M)) # E: ndarray[Any, dtype[str_]] -reveal_type(np.compare_chararrays("a", "b", "!=", rstrip=False)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.compare_chararrays(b"a", b"a", "==", True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.compare_chararrays("a", "b", "!=", rstrip=False)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.compare_chararrays(b"a", b"a", "==", True)) # E: ndarray[Any, dtype[bool_]] reveal_type(np.add_docstring(func, "test")) # E: None -reveal_type(np.nested_iters([AR_i8, AR_i8], [[0], [1]], flags=["c_index"])) # E: tuple[numpy.nditer] -reveal_type(np.nested_iters([AR_i8, AR_i8], [[0], [1]], op_flags=[["readonly", "readonly"]])) # E: tuple[numpy.nditer] -reveal_type(np.nested_iters([AR_i8, AR_i8], [[0], [1]], op_dtypes=np.int_)) # E: tuple[numpy.nditer] -reveal_type(np.nested_iters([AR_i8, AR_i8], [[0], [1]], order="C", casting="no")) # E: tuple[numpy.nditer] +reveal_type(np.nested_iters([AR_i8, AR_i8], [[0], [1]], flags=["c_index"])) # E: tuple[nditer] +reveal_type(np.nested_iters([AR_i8, AR_i8], [[0], [1]], op_flags=[["readonly", "readonly"]])) # E: tuple[nditer] +reveal_type(np.nested_iters([AR_i8, AR_i8], [[0], [1]], op_dtypes=np.int_)) # E: tuple[nditer] +reveal_type(np.nested_iters([AR_i8, AR_i8], [[0], [1]], order="C", casting="no")) # E: tuple[nditer] diff --git a/numpy/typing/tests/data/reveal/ndarray_conversion.pyi b/numpy/typing/tests/data/reveal/ndarray_conversion.pyi index 03f2faf43..6885d4fd6 100644 --- a/numpy/typing/tests/data/reveal/ndarray_conversion.pyi +++ b/numpy/typing/tests/data/reveal/ndarray_conversion.pyi @@ -20,32 +20,32 @@ reveal_type(nd.tolist()) # E: Any # dumps is pretty simple # astype -reveal_type(nd.astype("float")) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(nd.astype(float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(nd.astype(np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(nd.astype(np.float64, "K")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(nd.astype(np.float64, "K", "unsafe")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(nd.astype(np.float64, "K", "unsafe", True)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(nd.astype(np.float64, "K", "unsafe", True, True)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(nd.astype("float")) # E: ndarray[Any, dtype[Any]] +reveal_type(nd.astype(float)) # E: ndarray[Any, dtype[Any]] +reveal_type(nd.astype(np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(nd.astype(np.float64, "K")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(nd.astype(np.float64, "K", "unsafe")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(nd.astype(np.float64, "K", "unsafe", True)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(nd.astype(np.float64, "K", "unsafe", True, True)) # E: ndarray[Any, dtype[{float64}]] # byteswap -reveal_type(nd.byteswap()) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(nd.byteswap(True)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(nd.byteswap()) # E: ndarray[Any, dtype[{int_}]] +reveal_type(nd.byteswap(True)) # E: ndarray[Any, dtype[{int_}]] # copy -reveal_type(nd.copy()) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(nd.copy("C")) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(nd.copy()) # E: ndarray[Any, dtype[{int_}]] +reveal_type(nd.copy("C")) # E: ndarray[Any, dtype[{int_}]] -reveal_type(nd.view()) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(nd.view(np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(nd.view(float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(nd.view(np.float64, np.matrix)) # E: numpy.matrix[Any, Any] +reveal_type(nd.view()) # E: ndarray[Any, dtype[{int_}]] +reveal_type(nd.view(np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(nd.view(float)) # E: ndarray[Any, dtype[Any]] +reveal_type(nd.view(np.float64, np.matrix)) # E: matrix[Any, Any] # getfield -reveal_type(nd.getfield("float")) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(nd.getfield(float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(nd.getfield(np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(nd.getfield(np.float64, 8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(nd.getfield("float")) # E: ndarray[Any, dtype[Any]] +reveal_type(nd.getfield(float)) # E: ndarray[Any, dtype[Any]] +reveal_type(nd.getfield(np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(nd.getfield(np.float64, 8)) # E: ndarray[Any, dtype[{float64}]] # setflags does not return a value # fill does not return a value diff --git a/numpy/typing/tests/data/reveal/ndarray_misc.pyi b/numpy/typing/tests/data/reveal/ndarray_misc.pyi index 2d900c53d..cd1c3136f 100644 --- a/numpy/typing/tests/data/reveal/ndarray_misc.pyi +++ b/numpy/typing/tests/data/reveal/ndarray_misc.pyi @@ -33,14 +33,14 @@ reveal_type(ctypes_obj.data_as(ct.c_void_p)) # E: ctypes.c_void_p reveal_type(ctypes_obj.shape_as(ct.c_longlong)) # E: ctypes.Array[ctypes.c_longlong] reveal_type(ctypes_obj.strides_as(ct.c_ubyte)) # E: ctypes.Array[ctypes.c_ubyte] -reveal_type(f8.all()) # E: numpy.bool_ -reveal_type(AR_f8.all()) # E: numpy.bool_ +reveal_type(f8.all()) # E: bool_ +reveal_type(AR_f8.all()) # E: bool_ reveal_type(AR_f8.all(axis=0)) # E: Any reveal_type(AR_f8.all(keepdims=True)) # E: Any reveal_type(AR_f8.all(out=B)) # E: SubClass -reveal_type(f8.any()) # E: numpy.bool_ -reveal_type(AR_f8.any()) # E: numpy.bool_ +reveal_type(f8.any()) # E: bool_ +reveal_type(AR_f8.any()) # E: bool_ reveal_type(AR_f8.any(axis=0)) # E: Any reveal_type(AR_f8.any(keepdims=True)) # E: Any reveal_type(AR_f8.any(out=B)) # E: SubClass @@ -55,11 +55,11 @@ reveal_type(AR_f8.argmin()) # E: {intp} reveal_type(AR_f8.argmin(axis=0)) # E: Any reveal_type(AR_f8.argmin(out=B)) # E: SubClass -reveal_type(f8.argsort()) # E: numpy.ndarray[Any, Any] -reveal_type(AR_f8.argsort()) # E: numpy.ndarray[Any, Any] +reveal_type(f8.argsort()) # E: ndarray[Any, Any] +reveal_type(AR_f8.argsort()) # E: ndarray[Any, Any] -reveal_type(f8.astype(np.int64).choose([()])) # E: numpy.ndarray[Any, Any] -reveal_type(AR_f8.choose([0])) # E: numpy.ndarray[Any, Any] +reveal_type(f8.astype(np.int64).choose([()])) # E: ndarray[Any, Any] +reveal_type(AR_f8.choose([0])) # E: ndarray[Any, Any] reveal_type(AR_f8.choose([0], out=B)) # E: SubClass reveal_type(f8.clip(1)) # E: Any @@ -68,24 +68,24 @@ reveal_type(AR_f8.clip(None, 1)) # E: Any reveal_type(AR_f8.clip(1, out=B)) # E: SubClass reveal_type(AR_f8.clip(None, 1, out=B)) # E: SubClass -reveal_type(f8.compress([0])) # E: numpy.ndarray[Any, Any] -reveal_type(AR_f8.compress([0])) # E: numpy.ndarray[Any, Any] +reveal_type(f8.compress([0])) # E: ndarray[Any, Any] +reveal_type(AR_f8.compress([0])) # E: ndarray[Any, Any] reveal_type(AR_f8.compress([0], out=B)) # E: SubClass reveal_type(f8.conj()) # E: {float64} -reveal_type(AR_f8.conj()) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(AR_f8.conj()) # E: ndarray[Any, dtype[{float64}]] reveal_type(B.conj()) # E: SubClass reveal_type(f8.conjugate()) # E: {float64} -reveal_type(AR_f8.conjugate()) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(AR_f8.conjugate()) # E: ndarray[Any, dtype[{float64}]] reveal_type(B.conjugate()) # E: SubClass -reveal_type(f8.cumprod()) # E: numpy.ndarray[Any, Any] -reveal_type(AR_f8.cumprod()) # E: numpy.ndarray[Any, Any] +reveal_type(f8.cumprod()) # E: ndarray[Any, Any] +reveal_type(AR_f8.cumprod()) # E: ndarray[Any, Any] reveal_type(AR_f8.cumprod(out=B)) # E: SubClass -reveal_type(f8.cumsum()) # E: numpy.ndarray[Any, Any] -reveal_type(AR_f8.cumsum()) # E: numpy.ndarray[Any, Any] +reveal_type(f8.cumsum()) # E: ndarray[Any, Any] +reveal_type(AR_f8.cumsum()) # E: ndarray[Any, Any] reveal_type(AR_f8.cumsum(out=B)) # E: SubClass reveal_type(f8.max()) # E: Any @@ -107,7 +107,7 @@ reveal_type(AR_f8.min(keepdims=True)) # E: Any reveal_type(AR_f8.min(out=B)) # E: SubClass reveal_type(f8.newbyteorder()) # E: {float64} -reveal_type(AR_f8.newbyteorder()) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(AR_f8.newbyteorder()) # E: ndarray[Any, dtype[{float64}]] reveal_type(B.newbyteorder('|')) # E: SubClass reveal_type(f8.prod()) # E: Any @@ -123,12 +123,12 @@ reveal_type(AR_f8.ptp(keepdims=True)) # E: Any reveal_type(AR_f8.ptp(out=B)) # E: SubClass reveal_type(f8.round()) # E: {float64} -reveal_type(AR_f8.round()) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(AR_f8.round()) # E: ndarray[Any, dtype[{float64}]] reveal_type(AR_f8.round(out=B)) # E: SubClass -reveal_type(f8.repeat(1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(AR_f8.repeat(1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(B.repeat(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(f8.repeat(1)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(AR_f8.repeat(1)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(B.repeat(1)) # E: ndarray[Any, dtype[object_]] reveal_type(f8.std()) # E: Any reveal_type(AR_f8.std()) # E: Any @@ -144,7 +144,7 @@ reveal_type(AR_f8.sum(out=B)) # E: SubClass reveal_type(f8.take(0)) # E: {float64} reveal_type(AR_f8.take(0)) # E: {float64} -reveal_type(AR_f8.take([0])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(AR_f8.take([0])) # E: ndarray[Any, dtype[{float64}]] reveal_type(AR_f8.take(0, out=B)) # E: SubClass reveal_type(AR_f8.take([0], out=B)) # E: SubClass @@ -154,18 +154,18 @@ reveal_type(AR_f8.var(axis=0)) # E: Any reveal_type(AR_f8.var(keepdims=True)) # E: Any reveal_type(AR_f8.var(out=B)) # E: SubClass -reveal_type(AR_f8.argpartition([0])) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] +reveal_type(AR_f8.argpartition([0])) # E: ndarray[Any, dtype[{intp}]] -reveal_type(AR_f8.diagonal()) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(AR_f8.diagonal()) # E: ndarray[Any, dtype[{float64}]] -reveal_type(AR_f8.dot(1)) # E: numpy.ndarray[Any, Any] +reveal_type(AR_f8.dot(1)) # E: ndarray[Any, Any] reveal_type(AR_f8.dot([1])) # E: Any reveal_type(AR_f8.dot(1, out=B)) # E: SubClass -reveal_type(AR_f8.nonzero()) # E: tuple[numpy.ndarray[Any, numpy.dtype[{intp}]]] +reveal_type(AR_f8.nonzero()) # E: tuple[ndarray[Any, dtype[{intp}]]] reveal_type(AR_f8.searchsorted(1)) # E: {intp} -reveal_type(AR_f8.searchsorted([1])) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] +reveal_type(AR_f8.searchsorted([1])) # E: ndarray[Any, dtype[{intp}]] reveal_type(AR_f8.trace()) # E: Any reveal_type(AR_f8.trace(out=B)) # E: SubClass @@ -173,14 +173,14 @@ reveal_type(AR_f8.trace(out=B)) # E: SubClass reveal_type(AR_f8.item()) # E: float reveal_type(AR_U.item()) # E: str -reveal_type(AR_f8.ravel()) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(AR_U.ravel()) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_f8.ravel()) # E: ndarray[Any, dtype[{float64}]] +reveal_type(AR_U.ravel()) # E: ndarray[Any, dtype[str_]] -reveal_type(AR_f8.flatten()) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(AR_U.flatten()) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_f8.flatten()) # E: ndarray[Any, dtype[{float64}]] +reveal_type(AR_U.flatten()) # E: ndarray[Any, dtype[str_]] -reveal_type(AR_f8.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(AR_U.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(AR_f8.reshape(1)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(AR_U.reshape(1)) # E: ndarray[Any, dtype[str_]] reveal_type(int(AR_f8)) # E: int reveal_type(int(AR_U)) # E: int @@ -192,18 +192,18 @@ reveal_type(complex(AR_f8)) # E: complex reveal_type(operator.index(AR_i8)) # E: int -reveal_type(AR_f8.__array_prepare__(B)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] -reveal_type(AR_f8.__array_wrap__(B)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(AR_f8.__array_prepare__(B)) # E: ndarray[Any, dtype[object_]] +reveal_type(AR_f8.__array_wrap__(B)) # E: ndarray[Any, dtype[object_]] reveal_type(AR_V[0]) # E: Any reveal_type(AR_V[0, 0]) # E: Any reveal_type(AR_V[AR_i8]) # E: Any reveal_type(AR_V[AR_i8, AR_i8]) # E: Any -reveal_type(AR_V[AR_i8, None]) # E: numpy.ndarray[Any, numpy.dtype[numpy.void]] -reveal_type(AR_V[0, ...]) # E: numpy.ndarray[Any, numpy.dtype[numpy.void]] -reveal_type(AR_V[:]) # E: numpy.ndarray[Any, numpy.dtype[numpy.void]] -reveal_type(AR_V["a"]) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(AR_V[["a", "b"]]) # E: numpy.ndarray[Any, numpy.dtype[numpy.void]] +reveal_type(AR_V[AR_i8, None]) # E: ndarray[Any, dtype[void]] +reveal_type(AR_V[0, ...]) # E: ndarray[Any, dtype[void]] +reveal_type(AR_V[:]) # E: ndarray[Any, dtype[void]] +reveal_type(AR_V["a"]) # E: ndarray[Any, dtype[Any]] +reveal_type(AR_V[["a", "b"]]) # E: ndarray[Any, dtype[void]] reveal_type(AR_f8.dump("test_file")) # E: None reveal_type(AR_f8.dump(b"test_file")) # E: None diff --git a/numpy/typing/tests/data/reveal/ndarray_shape_manipulation.pyi b/numpy/typing/tests/data/reveal/ndarray_shape_manipulation.pyi index a44e1cfa1..c000bf45c 100644 --- a/numpy/typing/tests/data/reveal/ndarray_shape_manipulation.pyi +++ b/numpy/typing/tests/data/reveal/ndarray_shape_manipulation.pyi @@ -3,33 +3,33 @@ import numpy as np nd = np.array([[1, 2], [3, 4]]) # reshape -reveal_type(nd.reshape()) # E: numpy.ndarray -reveal_type(nd.reshape(4)) # E: numpy.ndarray -reveal_type(nd.reshape(2, 2)) # E: numpy.ndarray -reveal_type(nd.reshape((2, 2))) # E: numpy.ndarray +reveal_type(nd.reshape()) # E: ndarray +reveal_type(nd.reshape(4)) # E: ndarray +reveal_type(nd.reshape(2, 2)) # E: ndarray +reveal_type(nd.reshape((2, 2))) # E: ndarray -reveal_type(nd.reshape((2, 2), order="C")) # E: numpy.ndarray -reveal_type(nd.reshape(4, order="C")) # E: numpy.ndarray +reveal_type(nd.reshape((2, 2), order="C")) # E: ndarray +reveal_type(nd.reshape(4, order="C")) # E: ndarray # resize does not return a value # transpose -reveal_type(nd.transpose()) # E: numpy.ndarray -reveal_type(nd.transpose(1, 0)) # E: numpy.ndarray -reveal_type(nd.transpose((1, 0))) # E: numpy.ndarray +reveal_type(nd.transpose()) # E: ndarray +reveal_type(nd.transpose(1, 0)) # E: ndarray +reveal_type(nd.transpose((1, 0))) # E: ndarray # swapaxes -reveal_type(nd.swapaxes(0, 1)) # E: numpy.ndarray +reveal_type(nd.swapaxes(0, 1)) # E: ndarray # flatten -reveal_type(nd.flatten()) # E: numpy.ndarray -reveal_type(nd.flatten("C")) # E: numpy.ndarray +reveal_type(nd.flatten()) # E: ndarray +reveal_type(nd.flatten("C")) # E: ndarray # ravel -reveal_type(nd.ravel()) # E: numpy.ndarray -reveal_type(nd.ravel("C")) # E: numpy.ndarray +reveal_type(nd.ravel()) # E: ndarray +reveal_type(nd.ravel("C")) # E: ndarray # squeeze -reveal_type(nd.squeeze()) # E: numpy.ndarray -reveal_type(nd.squeeze(0)) # E: numpy.ndarray -reveal_type(nd.squeeze((0, 2))) # E: numpy.ndarray +reveal_type(nd.squeeze()) # E: ndarray +reveal_type(nd.squeeze(0)) # E: ndarray +reveal_type(nd.squeeze((0, 2))) # E: ndarray diff --git a/numpy/typing/tests/data/reveal/nditer.pyi b/numpy/typing/tests/data/reveal/nditer.pyi index 473e922a2..65861da54 100644 --- a/numpy/typing/tests/data/reveal/nditer.pyi +++ b/numpy/typing/tests/data/reveal/nditer.pyi @@ -2,12 +2,12 @@ import numpy as np nditer_obj: np.nditer -reveal_type(np.nditer([0, 1], flags=["c_index"])) # E: numpy.nditer -reveal_type(np.nditer([0, 1], op_flags=[["readonly", "readonly"]])) # E: numpy.nditer -reveal_type(np.nditer([0, 1], op_dtypes=np.int_)) # E: numpy.nditer -reveal_type(np.nditer([0, 1], order="C", casting="no")) # E: numpy.nditer +reveal_type(np.nditer([0, 1], flags=["c_index"])) # E: nditer +reveal_type(np.nditer([0, 1], op_flags=[["readonly", "readonly"]])) # E: nditer +reveal_type(np.nditer([0, 1], op_dtypes=np.int_)) # E: nditer +reveal_type(np.nditer([0, 1], order="C", casting="no")) # E: nditer -reveal_type(nditer_obj.dtypes) # E: tuple[numpy.dtype[Any]] +reveal_type(nditer_obj.dtypes) # E: tuple[dtype[Any]] reveal_type(nditer_obj.finished) # E: bool reveal_type(nditer_obj.has_delayed_bufalloc) # E: bool reveal_type(nditer_obj.has_index) # E: bool @@ -17,16 +17,16 @@ reveal_type(nditer_obj.iterationneedsapi) # E: bool reveal_type(nditer_obj.iterindex) # E: int reveal_type(nditer_obj.iterrange) # E: tuple[builtins.int] reveal_type(nditer_obj.itersize) # E: int -reveal_type(nditer_obj.itviews) # E: tuple[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(nditer_obj.itviews) # E: tuple[ndarray[Any, dtype[Any]]] reveal_type(nditer_obj.multi_index) # E: tuple[builtins.int] reveal_type(nditer_obj.ndim) # E: int reveal_type(nditer_obj.nop) # E: int -reveal_type(nditer_obj.operands) # E: tuple[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(nditer_obj.operands) # E: tuple[ndarray[Any, dtype[Any]]] reveal_type(nditer_obj.shape) # E: tuple[builtins.int] -reveal_type(nditer_obj.value) # E: tuple[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(nditer_obj.value) # E: tuple[ndarray[Any, dtype[Any]]] reveal_type(nditer_obj.close()) # E: None -reveal_type(nditer_obj.copy()) # E: numpy.nditer +reveal_type(nditer_obj.copy()) # E: nditer reveal_type(nditer_obj.debug_print()) # E: None reveal_type(nditer_obj.enable_external_loop()) # E: None reveal_type(nditer_obj.iternext()) # E: bool @@ -35,12 +35,12 @@ reveal_type(nditer_obj.remove_multi_index()) # E: None reveal_type(nditer_obj.reset()) # E: None reveal_type(len(nditer_obj)) # E: int -reveal_type(iter(nditer_obj)) # E: Iterator[builtins.tuple[numpy.ndarray[Any, numpy.dtype[Any]]]] -reveal_type(next(nditer_obj)) # E: tuple[numpy.ndarray[Any, numpy.dtype[Any]]] -reveal_type(nditer_obj.__copy__()) # E: numpy.nditer +reveal_type(iter(nditer_obj)) # E: Iterator[builtins.tuple[ndarray[Any, dtype[Any]]]] +reveal_type(next(nditer_obj)) # E: tuple[ndarray[Any, dtype[Any]]] +reveal_type(nditer_obj.__copy__()) # E: nditer with nditer_obj as f: - reveal_type(f) # E: numpy.nditer -reveal_type(nditer_obj[0]) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(nditer_obj[:]) # E: tuple[numpy.ndarray[Any, numpy.dtype[Any]]] + reveal_type(f) # E: nditer +reveal_type(nditer_obj[0]) # E: ndarray[Any, dtype[Any]] +reveal_type(nditer_obj[:]) # E: tuple[ndarray[Any, dtype[Any]]] nditer_obj[0] = 0 nditer_obj[:] = [0, 1] diff --git a/numpy/typing/tests/data/reveal/npyio.pyi b/numpy/typing/tests/data/reveal/npyio.pyi index bee97a8e1..f54fbf610 100644 --- a/numpy/typing/tests/data/reveal/npyio.pyi +++ b/numpy/typing/tests/data/reveal/npyio.pyi @@ -34,11 +34,11 @@ reveal_type(npz_file.fid) # E: Union[None, typing.IO[builtins.str]] reveal_type(npz_file.files) # E: list[builtins.str] reveal_type(npz_file.allow_pickle) # E: bool reveal_type(npz_file.pickle_kwargs) # E: Union[None, typing.Mapping[builtins.str, Any]] -reveal_type(npz_file.f) # E: numpy.lib.npyio.BagObj[numpy.lib.npyio.NpzFile] -reveal_type(npz_file["test"]) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(npz_file.f) # E: lib.npyio.BagObj[lib.npyio.NpzFile] +reveal_type(npz_file["test"]) # E: ndarray[Any, dtype[Any]] reveal_type(len(npz_file)) # E: int with npz_file as f: - reveal_type(f) # E: numpy.lib.npyio.NpzFile + reveal_type(f) # E: lib.npyio.NpzFile reveal_type(np.load(bytes_file)) # E: Any reveal_type(np.load(pathlib_path, allow_pickle=True)) # E: Any @@ -60,32 +60,32 @@ reveal_type(np.savez_compressed(pathlib_path, ar1=AR_i8, ar2=AR_i8)) # E: None reveal_type(np.savez_compressed(str_path, AR_LIKE_f8, ar1=AR_i8)) # E: None reveal_type(np.savez_compressed(bytes_writer, AR_LIKE_f8, ar1=AR_i8)) # E: None -reveal_type(np.loadtxt(bytes_file)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.loadtxt(pathlib_path, dtype=np.str_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.loadtxt(str_path, dtype=str, skiprows=2)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.loadtxt(str_file, comments="test")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.loadtxt(str_path, delimiter="\n")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.loadtxt(str_path, ndmin=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.loadtxt(["1", "2", "3"])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] - -reveal_type(np.fromregex(bytes_file, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fromregex(str_file, b"test", dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.fromregex(str_path, re.compile("test"), dtype=np.str_, encoding="utf8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.fromregex(pathlib_path, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.fromregex(bytes_reader, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] - -reveal_type(np.genfromtxt(bytes_file)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.genfromtxt(pathlib_path, dtype=np.str_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(np.genfromtxt(str_path, dtype=str, skiprows=2)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.genfromtxt(str_file, comments="test")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.genfromtxt(str_path, delimiter="\n")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.genfromtxt(str_path, ndmin=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.genfromtxt(["1", "2", "3"], ndmin=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] - -reveal_type(np.recfromtxt(bytes_file)) # E: numpy.recarray[Any, numpy.dtype[numpy.record]] -reveal_type(np.recfromtxt(pathlib_path, usemask=True)) # E: numpy.ma.mrecords.MaskedRecords[Any, numpy.dtype[numpy.void]] -reveal_type(np.recfromtxt(["1", "2", "3"])) # E: numpy.recarray[Any, numpy.dtype[numpy.record]] - -reveal_type(np.recfromcsv(bytes_file)) # E: numpy.recarray[Any, numpy.dtype[numpy.record]] -reveal_type(np.recfromcsv(pathlib_path, usemask=True)) # E: numpy.ma.mrecords.MaskedRecords[Any, numpy.dtype[numpy.void]] -reveal_type(np.recfromcsv(["1", "2", "3"])) # E: numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.loadtxt(bytes_file)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.loadtxt(pathlib_path, dtype=np.str_)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.loadtxt(str_path, dtype=str, skiprows=2)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.loadtxt(str_file, comments="test")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.loadtxt(str_path, delimiter="\n")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.loadtxt(str_path, ndmin=2)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.loadtxt(["1", "2", "3"])) # E: ndarray[Any, dtype[{float64}]] + +reveal_type(np.fromregex(bytes_file, "test", np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fromregex(str_file, b"test", dtype=float)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.fromregex(str_path, re.compile("test"), dtype=np.str_, encoding="utf8")) # E: ndarray[Any, dtype[str_]] +reveal_type(np.fromregex(pathlib_path, "test", np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.fromregex(bytes_reader, "test", np.float64)) # E: ndarray[Any, dtype[{float64}]] + +reveal_type(np.genfromtxt(bytes_file)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.genfromtxt(pathlib_path, dtype=np.str_)) # E: ndarray[Any, dtype[str_]] +reveal_type(np.genfromtxt(str_path, dtype=str, skiprows=2)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.genfromtxt(str_file, comments="test")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.genfromtxt(str_path, delimiter="\n")) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.genfromtxt(str_path, ndmin=2)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.genfromtxt(["1", "2", "3"], ndmin=2)) # E: ndarray[Any, dtype[{float64}]] + +reveal_type(np.recfromtxt(bytes_file)) # E: recarray[Any, dtype[record]] +reveal_type(np.recfromtxt(pathlib_path, usemask=True)) # E: ma.mrecords.MaskedRecords[Any, dtype[void]] +reveal_type(np.recfromtxt(["1", "2", "3"])) # E: recarray[Any, dtype[record]] + +reveal_type(np.recfromcsv(bytes_file)) # E: recarray[Any, dtype[record]] +reveal_type(np.recfromcsv(pathlib_path, usemask=True)) # E: ma.mrecords.MaskedRecords[Any, dtype[void]] +reveal_type(np.recfromcsv(["1", "2", "3"])) # E: recarray[Any, dtype[record]] diff --git a/numpy/typing/tests/data/reveal/numeric.pyi b/numpy/typing/tests/data/reveal/numeric.pyi index 9b3b1419d..bf5653937 100644 --- a/numpy/typing/tests/data/reveal/numeric.pyi +++ b/numpy/typing/tests/data/reveal/numeric.pyi @@ -1,5 +1,5 @@ """ -Tests for :mod:`numpy.core.numeric`. +Tests for :mod:`core.numeric`. Does not include tests which fall under ``array_constructors``. @@ -34,83 +34,83 @@ reveal_type(np.count_nonzero(AR_i8, axis=0)) # E: Any reveal_type(np.isfortran(i8)) # E: bool reveal_type(np.isfortran(AR_i8)) # E: bool -reveal_type(np.argwhere(i8)) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] -reveal_type(np.argwhere(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] - -reveal_type(np.flatnonzero(i8)) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] -reveal_type(np.flatnonzero(AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[{intp}]] - -reveal_type(np.correlate(B, AR_i8, mode="valid")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.correlate(AR_i8, AR_i8, mode="same")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.correlate(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.correlate(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(np.correlate(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.correlate(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.correlate(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.correlate(AR_i8, AR_m)) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(np.correlate(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] - -reveal_type(np.convolve(B, AR_i8, mode="valid")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.convolve(AR_i8, AR_i8, mode="same")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.convolve(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.convolve(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(np.convolve(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.convolve(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.convolve(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.convolve(AR_i8, AR_m)) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(np.convolve(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] - -reveal_type(np.outer(i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.outer(B, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.outer(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] +reveal_type(np.argwhere(i8)) # E: ndarray[Any, dtype[{intp}]] +reveal_type(np.argwhere(AR_i8)) # E: ndarray[Any, dtype[{intp}]] + +reveal_type(np.flatnonzero(i8)) # E: ndarray[Any, dtype[{intp}]] +reveal_type(np.flatnonzero(AR_i8)) # E: ndarray[Any, dtype[{intp}]] + +reveal_type(np.correlate(B, AR_i8, mode="valid")) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.correlate(AR_i8, AR_i8, mode="same")) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.correlate(AR_b, AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.correlate(AR_b, AR_u8)) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(np.correlate(AR_i8, AR_b)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.correlate(AR_i8, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.correlate(AR_i8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.correlate(AR_i8, AR_m)) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(np.correlate(AR_O, AR_O)) # E: ndarray[Any, dtype[object_]] + +reveal_type(np.convolve(B, AR_i8, mode="valid")) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.convolve(AR_i8, AR_i8, mode="same")) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.convolve(AR_b, AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.convolve(AR_b, AR_u8)) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(np.convolve(AR_i8, AR_b)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.convolve(AR_i8, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.convolve(AR_i8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.convolve(AR_i8, AR_m)) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(np.convolve(AR_O, AR_O)) # E: ndarray[Any, dtype[object_]] + +reveal_type(np.outer(i8, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.outer(B, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.outer(AR_i8, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] reveal_type(np.outer(AR_i8, AR_i8, out=C)) # E: SubClass -reveal_type(np.outer(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.outer(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(np.outer(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.convolve(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.outer(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.outer(AR_i8, AR_m)) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(np.outer(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] - -reveal_type(np.tensordot(B, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.tensordot(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.tensordot(AR_i8, AR_i8, axes=0)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.tensordot(AR_i8, AR_i8, axes=(0, 1))) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.tensordot(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.tensordot(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(np.tensordot(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.tensordot(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.tensordot(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.tensordot(AR_i8, AR_m)) # E: numpy.ndarray[Any, numpy.dtype[numpy.timedelta64]] -reveal_type(np.tensordot(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(np.outer(AR_b, AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.outer(AR_b, AR_u8)) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(np.outer(AR_i8, AR_b)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.convolve(AR_i8, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.outer(AR_i8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.outer(AR_i8, AR_m)) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(np.outer(AR_O, AR_O)) # E: ndarray[Any, dtype[object_]] + +reveal_type(np.tensordot(B, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_i8, axes=0)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_i8, axes=(0, 1))) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.tensordot(AR_b, AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.tensordot(AR_b, AR_u8)) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_b)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.tensordot(AR_i8, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.tensordot(AR_i8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.tensordot(AR_i8, AR_m)) # E: ndarray[Any, dtype[timedelta64]] +reveal_type(np.tensordot(AR_O, AR_O)) # E: ndarray[Any, dtype[object_]] reveal_type(np.isscalar(i8)) # E: bool reveal_type(np.isscalar(AR_i8)) # E: bool reveal_type(np.isscalar(B)) # E: bool -reveal_type(np.roll(AR_i8, 1)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.roll(AR_i8, (1, 2))) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.roll(B, 1)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.roll(AR_i8, 1)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.roll(AR_i8, (1, 2))) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.roll(B, 1)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.rollaxis(AR_i8, 0, 1)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.rollaxis(AR_i8, 0, 1)) # E: ndarray[Any, dtype[{int64}]] -reveal_type(np.moveaxis(AR_i8, 0, 1)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.moveaxis(AR_i8, (0, 1), (1, 2))) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] +reveal_type(np.moveaxis(AR_i8, 0, 1)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.moveaxis(AR_i8, (0, 1), (1, 2))) # E: ndarray[Any, dtype[{int64}]] -reveal_type(np.cross(B, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.cross(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.cross(AR_b, AR_u8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]] -reveal_type(np.cross(AR_i8, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.cross(AR_i8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.cross(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.cross(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(np.cross(B, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.cross(AR_i8, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.cross(AR_b, AR_u8)) # E: ndarray[Any, dtype[unsignedinteger[Any]]] +reveal_type(np.cross(AR_i8, AR_b)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.cross(AR_i8, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.cross(AR_i8, AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.cross(AR_O, AR_O)) # E: ndarray[Any, dtype[object_]] -reveal_type(np.indices([0, 1, 2])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(np.indices([0, 1, 2], sparse=True)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{int_}]]] -reveal_type(np.indices([0, 1, 2], dtype=np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.indices([0, 1, 2], sparse=True, dtype=np.float64)) # E: tuple[numpy.ndarray[Any, numpy.dtype[{float64}]]] -reveal_type(np.indices([0, 1, 2], dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.indices([0, 1, 2], sparse=True, dtype=float)) # E: tuple[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.indices([0, 1, 2])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(np.indices([0, 1, 2], sparse=True)) # E: tuple[ndarray[Any, dtype[{int_}]]] +reveal_type(np.indices([0, 1, 2], dtype=np.float64)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.indices([0, 1, 2], sparse=True, dtype=np.float64)) # E: tuple[ndarray[Any, dtype[{float64}]]] +reveal_type(np.indices([0, 1, 2], dtype=float)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.indices([0, 1, 2], sparse=True, dtype=float)) # E: tuple[ndarray[Any, dtype[Any]]] reveal_type(np.binary_repr(1)) # E: str @@ -120,10 +120,10 @@ reveal_type(np.allclose(i8, AR_i8)) # E: bool reveal_type(np.allclose(B, AR_i8)) # E: bool reveal_type(np.allclose(AR_i8, AR_i8)) # E: bool -reveal_type(np.isclose(i8, i8)) # E: numpy.bool_ -reveal_type(np.isclose(i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isclose(B, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isclose(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.isclose(i8, i8)) # E: bool_ +reveal_type(np.isclose(i8, AR_i8)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isclose(B, AR_i8)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isclose(AR_i8, AR_i8)) # E: ndarray[Any, dtype[bool_]] reveal_type(np.array_equal(i8, AR_i8)) # E: bool reveal_type(np.array_equal(B, AR_i8)) # E: bool diff --git a/numpy/typing/tests/data/reveal/numerictypes.pyi b/numpy/typing/tests/data/reveal/numerictypes.pyi index c50a3a3d6..cc2335264 100644 --- a/numpy/typing/tests/data/reveal/numerictypes.pyi +++ b/numpy/typing/tests/data/reveal/numerictypes.pyi @@ -21,7 +21,7 @@ reveal_type(np.issubclass_(1, 1)) # E: Literal[False] reveal_type(np.sctype2char("S8")) # E: str reveal_type(np.sctype2char(list)) # E: str -reveal_type(np.find_common_type([np.int64], [np.int64])) # E: numpy.dtype[Any] +reveal_type(np.find_common_type([np.int64], [np.int64])) # E: dtype[Any] reveal_type(np.cast[int]) # E: _CastFunc reveal_type(np.cast["i8"]) # E: _CastFunc diff --git a/numpy/typing/tests/data/reveal/random.pyi b/numpy/typing/tests/data/reveal/random.pyi index 6fc35aced..4e06aa7d5 100644 --- a/numpy/typing/tests/data/reveal/random.pyi +++ b/numpy/typing/tests/data/reveal/random.pyi @@ -12,23 +12,23 @@ sfc64 = np.random.SFC64() philox = np.random.Philox() seedless_seq = np.random.bit_generator.SeedlessSeedSequence() -reveal_type(def_rng) # E: numpy.random._generator.Generator -reveal_type(mt19937) # E: numpy.random._mt19937.MT19937 -reveal_type(pcg64) # E: numpy.random._pcg64.PCG64 -reveal_type(sfc64) # E: numpy.random._sfc64.SFC64 -reveal_type(philox) # E: numpy.random._philox.Philox -reveal_type(seed_seq) # E: numpy.random.bit_generator.SeedSequence -reveal_type(seedless_seq) # E: numpy.random.bit_generator.SeedlessSeedSequence +reveal_type(def_rng) # E: random._generator.Generator +reveal_type(mt19937) # E: random._mt19937.MT19937 +reveal_type(pcg64) # E: random._pcg64.PCG64 +reveal_type(sfc64) # E: random._sfc64.SFC64 +reveal_type(philox) # E: random._philox.Philox +reveal_type(seed_seq) # E: random.bit_generator.SeedSequence +reveal_type(seedless_seq) # E: random.bit_generator.SeedlessSeedSequence mt19937_jumped = mt19937.jumped() mt19937_jumped3 = mt19937.jumped(3) mt19937_raw = mt19937.random_raw() mt19937_raw_arr = mt19937.random_raw(5) -reveal_type(mt19937_jumped) # E: numpy.random._mt19937.MT19937 -reveal_type(mt19937_jumped3) # E: numpy.random._mt19937.MT19937 +reveal_type(mt19937_jumped) # E: random._mt19937.MT19937 +reveal_type(mt19937_jumped3) # E: random._mt19937.MT19937 reveal_type(mt19937_raw) # E: int -reveal_type(mt19937_raw_arr) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(mt19937_raw_arr) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] reveal_type(mt19937.lock) # E: threading.Lock pcg64_jumped = pcg64.jumped() @@ -37,11 +37,11 @@ pcg64_adv = pcg64.advance(3) pcg64_raw = pcg64.random_raw() pcg64_raw_arr = pcg64.random_raw(5) -reveal_type(pcg64_jumped) # E: numpy.random._pcg64.PCG64 -reveal_type(pcg64_jumped3) # E: numpy.random._pcg64.PCG64 -reveal_type(pcg64_adv) # E: numpy.random._pcg64.PCG64 +reveal_type(pcg64_jumped) # E: random._pcg64.PCG64 +reveal_type(pcg64_jumped3) # E: random._pcg64.PCG64 +reveal_type(pcg64_adv) # E: random._pcg64.PCG64 reveal_type(pcg64_raw) # E: int -reveal_type(pcg64_raw_arr) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(pcg64_raw_arr) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] reveal_type(pcg64.lock) # E: threading.Lock philox_jumped = philox.jumped() @@ -50,25 +50,25 @@ philox_adv = philox.advance(3) philox_raw = philox.random_raw() philox_raw_arr = philox.random_raw(5) -reveal_type(philox_jumped) # E: numpy.random._philox.Philox -reveal_type(philox_jumped3) # E: numpy.random._philox.Philox -reveal_type(philox_adv) # E: numpy.random._philox.Philox +reveal_type(philox_jumped) # E: random._philox.Philox +reveal_type(philox_jumped3) # E: random._philox.Philox +reveal_type(philox_adv) # E: random._philox.Philox reveal_type(philox_raw) # E: int -reveal_type(philox_raw_arr) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(philox_raw_arr) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] reveal_type(philox.lock) # E: threading.Lock sfc64_raw = sfc64.random_raw() sfc64_raw_arr = sfc64.random_raw(5) reveal_type(sfc64_raw) # E: int -reveal_type(sfc64_raw_arr) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(sfc64_raw_arr) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] reveal_type(sfc64.lock) # E: threading.Lock -reveal_type(seed_seq.pool) # numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] +reveal_type(seed_seq.pool) # ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] reveal_type(seed_seq.entropy) # E:Union[None, int, Sequence[int]] -reveal_type(seed_seq.spawn(1)) # E: list[numpy.random.bit_generator.SeedSequence] -reveal_type(seed_seq.generate_state(8, "uint32")) # E: numpy.ndarray[Any, numpy.dtype[Union[numpy.unsignedinteger[numpy.typing._32Bit], numpy.unsignedinteger[numpy.typing._64Bit]]]] -reveal_type(seed_seq.generate_state(8, "uint64")) # E: numpy.ndarray[Any, numpy.dtype[Union[numpy.unsignedinteger[numpy.typing._32Bit], numpy.unsignedinteger[numpy.typing._64Bit]]]] +reveal_type(seed_seq.spawn(1)) # E: list[random.bit_generator.SeedSequence] +reveal_type(seed_seq.generate_state(8, "uint32")) # E: ndarray[Any, dtype[Union[unsignedinteger[typing._32Bit], unsignedinteger[typing._64Bit]]]] +reveal_type(seed_seq.generate_state(8, "uint64")) # E: ndarray[Any, dtype[Union[unsignedinteger[typing._32Bit], unsignedinteger[typing._64Bit]]]] def_gen: np.random.Generator = np.random.default_rng() @@ -96,17 +96,17 @@ reveal_type(def_gen.standard_normal(dtype="float32")) # E: float reveal_type(def_gen.standard_normal(dtype="double")) # E: float reveal_type(def_gen.standard_normal(dtype=np.float64)) # E: float reveal_type(def_gen.standard_normal(size=None)) # E: float -reveal_type(def_gen.standard_normal(size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_normal(size=1, dtype=np.float32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_normal(size=1, dtype="f4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_normal(size=1, dtype="float32", out=S_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_normal(dtype=np.float32, out=S_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_normal(size=1, dtype=np.float64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_normal(size=1, dtype="float64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_normal(size=1, dtype="f8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_normal(out=D_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_normal(size=1, dtype="float64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_normal(size=1, dtype="float64", out=D_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] +reveal_type(def_gen.standard_normal(size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_normal(size=1, dtype=np.float32)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_normal(size=1, dtype="f4")) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_normal(size=1, dtype="float32", out=S_out)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_normal(dtype=np.float32, out=S_out)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_normal(size=1, dtype=np.float64)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_normal(size=1, dtype="float64")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_normal(size=1, dtype="f8")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_normal(out=D_out)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_normal(size=1, dtype="float64")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_normal(size=1, dtype="float64", out=D_out)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] reveal_type(def_gen.random()) # E: float reveal_type(def_gen.random(dtype=np.float32)) # E: float @@ -114,21 +114,21 @@ reveal_type(def_gen.random(dtype="float32")) # E: float reveal_type(def_gen.random(dtype="double")) # E: float reveal_type(def_gen.random(dtype=np.float64)) # E: float reveal_type(def_gen.random(size=None)) # E: float -reveal_type(def_gen.random(size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.random(size=1, dtype=np.float32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.random(size=1, dtype="f4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.random(size=1, dtype="float32", out=S_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.random(dtype=np.float32, out=S_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.random(size=1, dtype=np.float64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.random(size=1, dtype="float64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.random(size=1, dtype="f8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.random(out=D_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.random(size=1, dtype="float64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.random(size=1, dtype="float64", out=D_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] +reveal_type(def_gen.random(size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.random(size=1, dtype=np.float32)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.random(size=1, dtype="f4")) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.random(size=1, dtype="float32", out=S_out)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.random(dtype=np.float32, out=S_out)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.random(size=1, dtype=np.float64)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.random(size=1, dtype="float64")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.random(size=1, dtype="f8")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.random(out=D_out)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.random(size=1, dtype="float64")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.random(size=1, dtype="float64", out=D_out)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] reveal_type(def_gen.standard_cauchy()) # E: float reveal_type(def_gen.standard_cauchy(size=None)) # E: float -reveal_type(def_gen.standard_cauchy(size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.standard_cauchy(size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.standard_exponential()) # E: float reveal_type(def_gen.standard_exponential(method="inv")) # E: float @@ -138,367 +138,367 @@ reveal_type(def_gen.standard_exponential(dtype="double")) # E: float reveal_type(def_gen.standard_exponential(dtype=np.float64)) # E: float reveal_type(def_gen.standard_exponential(size=None)) # E: float reveal_type(def_gen.standard_exponential(size=None, method="inv")) # E: float -reveal_type(def_gen.standard_exponential(size=1, method="inv")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_exponential(size=1, dtype=np.float32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_exponential(size=1, dtype="f4", method="inv")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_exponential(size=1, dtype="float32", out=S_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_exponential(dtype=np.float32, out=S_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_exponential(size=1, dtype=np.float64, method="inv")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_exponential(size=1, dtype="float64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_exponential(size=1, dtype="f8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_exponential(out=D_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_exponential(size=1, dtype="float64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_exponential(size=1, dtype="float64", out=D_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] +reveal_type(def_gen.standard_exponential(size=1, method="inv")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_exponential(size=1, dtype=np.float32)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_exponential(size=1, dtype="f4", method="inv")) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_exponential(size=1, dtype="float32", out=S_out)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_exponential(dtype=np.float32, out=S_out)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_exponential(size=1, dtype=np.float64, method="inv")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_exponential(size=1, dtype="float64")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_exponential(size=1, dtype="f8")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_exponential(out=D_out)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_exponential(size=1, dtype="float64")) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_exponential(size=1, dtype="float64", out=D_out)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] reveal_type(def_gen.zipf(1.5)) # E: int reveal_type(def_gen.zipf(1.5, size=None)) # E: int -reveal_type(def_gen.zipf(1.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.zipf(D_arr_1p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.zipf(D_arr_1p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.zipf(D_arr_like_1p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.zipf(D_arr_like_1p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.zipf(1.5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.zipf(D_arr_1p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.zipf(D_arr_1p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.zipf(D_arr_like_1p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.zipf(D_arr_like_1p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.weibull(0.5)) # E: float reveal_type(def_gen.weibull(0.5, size=None)) # E: float -reveal_type(def_gen.weibull(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.weibull(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.weibull(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.weibull(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.weibull(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.weibull(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.weibull(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.weibull(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.weibull(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.weibull(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.standard_t(0.5)) # E: float reveal_type(def_gen.standard_t(0.5, size=None)) # E: float -reveal_type(def_gen.standard_t(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.standard_t(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.standard_t(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.standard_t(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.standard_t(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.standard_t(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.standard_t(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.standard_t(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.standard_t(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.standard_t(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.poisson(0.5)) # E: int reveal_type(def_gen.poisson(0.5, size=None)) # E: int -reveal_type(def_gen.poisson(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.poisson(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.poisson(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.poisson(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.poisson(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.poisson(0.5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.poisson(D_arr_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.poisson(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.poisson(D_arr_like_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.poisson(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.power(0.5)) # E: float reveal_type(def_gen.power(0.5, size=None)) # E: float -reveal_type(def_gen.power(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.power(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.power(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.power(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.power(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.power(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.power(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.power(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.power(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.power(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.pareto(0.5)) # E: float reveal_type(def_gen.pareto(0.5, size=None)) # E: float -reveal_type(def_gen.pareto(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.pareto(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.pareto(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.pareto(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.pareto(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.pareto(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.pareto(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.pareto(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.pareto(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.pareto(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.chisquare(0.5)) # E: float reveal_type(def_gen.chisquare(0.5, size=None)) # E: float -reveal_type(def_gen.chisquare(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.chisquare(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.chisquare(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.chisquare(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.chisquare(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.chisquare(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.chisquare(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.chisquare(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.chisquare(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.chisquare(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.exponential(0.5)) # E: float reveal_type(def_gen.exponential(0.5, size=None)) # E: float -reveal_type(def_gen.exponential(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.exponential(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.exponential(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.exponential(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.exponential(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.exponential(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.exponential(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.exponential(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.exponential(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.exponential(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.geometric(0.5)) # E: int reveal_type(def_gen.geometric(0.5, size=None)) # E: int -reveal_type(def_gen.geometric(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.geometric(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.geometric(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.geometric(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.geometric(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.geometric(0.5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.geometric(D_arr_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.geometric(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.geometric(D_arr_like_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.geometric(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.logseries(0.5)) # E: int reveal_type(def_gen.logseries(0.5, size=None)) # E: int -reveal_type(def_gen.logseries(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.logseries(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.logseries(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.logseries(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.logseries(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.logseries(0.5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.logseries(D_arr_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.logseries(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.logseries(D_arr_like_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.logseries(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.rayleigh(0.5)) # E: float reveal_type(def_gen.rayleigh(0.5, size=None)) # E: float -reveal_type(def_gen.rayleigh(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.rayleigh(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.rayleigh(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.rayleigh(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.rayleigh(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.rayleigh(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.rayleigh(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.rayleigh(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.rayleigh(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.rayleigh(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.standard_gamma(0.5)) # E: float reveal_type(def_gen.standard_gamma(0.5, size=None)) # E: float reveal_type(def_gen.standard_gamma(0.5, dtype="float32")) # E: float reveal_type(def_gen.standard_gamma(0.5, size=None, dtype="float32")) # E: float -reveal_type(def_gen.standard_gamma(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_gamma(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_gamma(D_arr_0p5, dtype="f4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_gamma(0.5, size=1, dtype="float32", out=S_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_gamma(D_arr_0p5, dtype=np.float32, out=S_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._32Bit]]] -reveal_type(def_gen.standard_gamma(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_gamma(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_gamma(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_gamma(0.5, out=D_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_gamma(D_arr_like_0p5, out=D_out)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_gamma(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(def_gen.standard_gamma(D_arr_like_0p5, size=1, out=D_out, dtype=np.float64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] +reveal_type(def_gen.standard_gamma(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_gamma(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_gamma(D_arr_0p5, dtype="f4")) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_gamma(0.5, size=1, dtype="float32", out=S_out)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_gamma(D_arr_0p5, dtype=np.float32, out=S_out)) # E: ndarray[Any, dtype[floating[typing._32Bit]]] +reveal_type(def_gen.standard_gamma(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_gamma(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_gamma(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_gamma(0.5, out=D_out)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_gamma(D_arr_like_0p5, out=D_out)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_gamma(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(def_gen.standard_gamma(D_arr_like_0p5, size=1, out=D_out, dtype=np.float64)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] reveal_type(def_gen.vonmises(0.5, 0.5)) # E: float reveal_type(def_gen.vonmises(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.vonmises(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.vonmises(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.vonmises(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.vonmises(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.wald(0.5, 0.5)) # E: float reveal_type(def_gen.wald(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.wald(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.wald(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.wald(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.wald(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.uniform(0.5, 0.5)) # E: float reveal_type(def_gen.uniform(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.uniform(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.uniform(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.uniform(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.uniform(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.beta(0.5, 0.5)) # E: float reveal_type(def_gen.beta(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.beta(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.beta(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.beta(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.beta(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.f(0.5, 0.5)) # E: float reveal_type(def_gen.f(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.f(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.f(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.f(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.f(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.gamma(0.5, 0.5)) # E: float reveal_type(def_gen.gamma(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.gamma(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gamma(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.gamma(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gamma(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.gumbel(0.5, 0.5)) # E: float reveal_type(def_gen.gumbel(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.gumbel(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.gumbel(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.gumbel(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.gumbel(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.laplace(0.5, 0.5)) # E: float reveal_type(def_gen.laplace(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.laplace(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.laplace(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.laplace(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.laplace(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.logistic(0.5, 0.5)) # E: float reveal_type(def_gen.logistic(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.logistic(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.logistic(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.logistic(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.logistic(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.lognormal(0.5, 0.5)) # E: float reveal_type(def_gen.lognormal(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.lognormal(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.lognormal(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.lognormal(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.lognormal(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.noncentral_chisquare(0.5, 0.5)) # E: float reveal_type(def_gen.noncentral_chisquare(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.noncentral_chisquare(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_chisquare(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_chisquare(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.normal(0.5, 0.5)) # E: float reveal_type(def_gen.normal(0.5, 0.5, size=None)) # E: float -reveal_type(def_gen.normal(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.normal(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.normal(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.normal(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.triangular(0.1, 0.5, 0.9)) # E: float reveal_type(def_gen.triangular(0.1, 0.5, 0.9, size=None)) # E: float -reveal_type(def_gen.triangular(0.1, 0.5, 0.9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(D_arr_0p1, 0.5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(0.1, D_arr_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(D_arr_0p1, 0.5, D_arr_like_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(0.1, D_arr_0p5, 0.9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(D_arr_like_0p1, 0.5, D_arr_0p9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(0.5, D_arr_like_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(D_arr_0p1, D_arr_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(D_arr_like_0p1, D_arr_like_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(D_arr_0p1, D_arr_0p5, D_arr_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.triangular(D_arr_like_0p1, D_arr_like_0p5, D_arr_like_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.triangular(0.1, 0.5, 0.9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(D_arr_0p1, 0.5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(0.1, D_arr_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(D_arr_0p1, 0.5, D_arr_like_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(0.1, D_arr_0p5, 0.9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(D_arr_like_0p1, 0.5, D_arr_0p9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(0.5, D_arr_like_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(D_arr_0p1, D_arr_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(D_arr_like_0p1, D_arr_like_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(D_arr_0p1, D_arr_0p5, D_arr_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.triangular(D_arr_like_0p1, D_arr_like_0p5, D_arr_like_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.noncentral_f(0.1, 0.5, 0.9)) # E: float reveal_type(def_gen.noncentral_f(0.1, 0.5, 0.9, size=None)) # E: float -reveal_type(def_gen.noncentral_f(0.1, 0.5, 0.9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(D_arr_0p1, 0.5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(0.1, D_arr_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(D_arr_0p1, 0.5, D_arr_like_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(0.1, D_arr_0p5, 0.9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(D_arr_like_0p1, 0.5, D_arr_0p9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(0.5, D_arr_like_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(D_arr_0p1, D_arr_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(D_arr_like_0p1, D_arr_like_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(D_arr_0p1, D_arr_0p5, D_arr_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.noncentral_f(D_arr_like_0p1, D_arr_like_0p5, D_arr_like_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(def_gen.noncentral_f(0.1, 0.5, 0.9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(D_arr_0p1, 0.5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(0.1, D_arr_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(D_arr_0p1, 0.5, D_arr_like_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(0.1, D_arr_0p5, 0.9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(D_arr_like_0p1, 0.5, D_arr_0p9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(0.5, D_arr_like_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(D_arr_0p1, D_arr_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(D_arr_like_0p1, D_arr_like_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(D_arr_0p1, D_arr_0p5, D_arr_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.noncentral_f(D_arr_like_0p1, D_arr_like_0p5, D_arr_like_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(def_gen.binomial(10, 0.5)) # E: int reveal_type(def_gen.binomial(10, 0.5, size=None)) # E: int -reveal_type(def_gen.binomial(10, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(I_arr_10, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(10, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(I_arr_10, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(10, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(I_arr_like_10, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(10, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(I_arr_10, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(I_arr_like_10, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(I_arr_10, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.binomial(I_arr_like_10, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.binomial(10, 0.5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(I_arr_10, 0.5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(10, D_arr_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(I_arr_10, 0.5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(10, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(I_arr_like_10, 0.5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(10, D_arr_like_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(I_arr_10, D_arr_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(I_arr_like_10, D_arr_like_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(I_arr_10, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.binomial(I_arr_like_10, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.negative_binomial(10, 0.5)) # E: int reveal_type(def_gen.negative_binomial(10, 0.5, size=None)) # E: int -reveal_type(def_gen.negative_binomial(10, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(I_arr_10, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(10, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(I_arr_10, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(10, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(I_arr_like_10, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(10, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(I_arr_10, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(I_arr_like_10, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(I_arr_10, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.negative_binomial(I_arr_like_10, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.negative_binomial(10, 0.5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(I_arr_10, 0.5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(10, D_arr_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(I_arr_10, 0.5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(10, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(I_arr_like_10, 0.5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(10, D_arr_like_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(I_arr_10, D_arr_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(I_arr_like_10, D_arr_like_0p5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(I_arr_10, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.negative_binomial(I_arr_like_10, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.hypergeometric(20, 20, 10)) # E: int reveal_type(def_gen.hypergeometric(20, 20, 10, size=None)) # E: int -reveal_type(def_gen.hypergeometric(20, 20, 10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(I_arr_20, 20, 10)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(20, I_arr_20, 10)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(I_arr_20, 20, I_arr_like_10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(20, I_arr_20, 10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(I_arr_like_20, 20, I_arr_10)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(20, I_arr_like_20, 10)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(I_arr_20, I_arr_20, 10)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(I_arr_like_20, I_arr_like_20, 10)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(I_arr_20, I_arr_20, I_arr_10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.hypergeometric(I_arr_like_20, I_arr_like_20, I_arr_like_10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.hypergeometric(20, 20, 10, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(I_arr_20, 20, 10)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(20, I_arr_20, 10)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(I_arr_20, 20, I_arr_like_10, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(20, I_arr_20, 10, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(I_arr_like_20, 20, I_arr_10)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(20, I_arr_like_20, 10)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(I_arr_20, I_arr_20, 10)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(I_arr_like_20, I_arr_like_20, 10)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(I_arr_20, I_arr_20, I_arr_10, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.hypergeometric(I_arr_like_20, I_arr_like_20, I_arr_like_10, size=1)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] I_int64_100: np.ndarray[Any, np.dtype[np.int64]] = np.array([100], dtype=np.int64) reveal_type(def_gen.integers(0, 100)) # E: int reveal_type(def_gen.integers(100)) # E: int -reveal_type(def_gen.integers([100])) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(0, [100])) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.integers([100])) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(0, [100])) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] I_bool_low: np.ndarray[Any, np.dtype[np.bool_]] = np.array([0], dtype=np.bool_) I_bool_low_like: List[int] = [0] @@ -509,25 +509,25 @@ reveal_type(def_gen.integers(2, dtype=bool)) # E: builtins.bool reveal_type(def_gen.integers(0, 2, dtype=bool)) # E: builtins.bool reveal_type(def_gen.integers(1, dtype=bool, endpoint=True)) # E: builtins.bool reveal_type(def_gen.integers(0, 1, dtype=bool, endpoint=True)) # E: builtins.bool -reveal_type(def_gen.integers(I_bool_low_like, 1, dtype=bool, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(I_bool_high_open, dtype=bool)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(I_bool_low, I_bool_high_open, dtype=bool)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(0, I_bool_high_open, dtype=bool)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(I_bool_high_closed, dtype=bool, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(I_bool_low, I_bool_high_closed, dtype=bool, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(0, I_bool_high_closed, dtype=bool, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] +reveal_type(def_gen.integers(I_bool_low_like, 1, dtype=bool, endpoint=True)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(I_bool_high_open, dtype=bool)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(I_bool_low, I_bool_high_open, dtype=bool)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(0, I_bool_high_open, dtype=bool)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(I_bool_high_closed, dtype=bool, endpoint=True)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(I_bool_low, I_bool_high_closed, dtype=bool, endpoint=True)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(0, I_bool_high_closed, dtype=bool, endpoint=True)) # E: ndarray[Any, dtype[bool_] reveal_type(def_gen.integers(2, dtype=np.bool_)) # E: builtins.bool reveal_type(def_gen.integers(0, 2, dtype=np.bool_)) # E: builtins.bool reveal_type(def_gen.integers(1, dtype=np.bool_, endpoint=True)) # E: builtins.bool reveal_type(def_gen.integers(0, 1, dtype=np.bool_, endpoint=True)) # E: builtins.bool -reveal_type(def_gen.integers(I_bool_low_like, 1, dtype=np.bool_, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(I_bool_high_open, dtype=np.bool_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(I_bool_low, I_bool_high_open, dtype=np.bool_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(0, I_bool_high_open, dtype=np.bool_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(I_bool_high_closed, dtype=np.bool_, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(I_bool_low, I_bool_high_closed, dtype=np.bool_, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(def_gen.integers(0, I_bool_high_closed, dtype=np.bool_, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] +reveal_type(def_gen.integers(I_bool_low_like, 1, dtype=np.bool_, endpoint=True)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(I_bool_high_open, dtype=np.bool_)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(I_bool_low, I_bool_high_open, dtype=np.bool_)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(0, I_bool_high_open, dtype=np.bool_)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(I_bool_high_closed, dtype=np.bool_, endpoint=True)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(I_bool_low, I_bool_high_closed, dtype=np.bool_, endpoint=True)) # E: ndarray[Any, dtype[bool_] +reveal_type(def_gen.integers(0, I_bool_high_closed, dtype=np.bool_, endpoint=True)) # E: ndarray[Any, dtype[bool_] I_u1_low: np.ndarray[Any, np.dtype[np.uint8]] = np.array([0], dtype=np.uint8) I_u1_low_like: List[int] = [0] @@ -538,37 +538,37 @@ reveal_type(def_gen.integers(256, dtype="u1")) # E: int reveal_type(def_gen.integers(0, 256, dtype="u1")) # E: int reveal_type(def_gen.integers(255, dtype="u1", endpoint=True)) # E: int reveal_type(def_gen.integers(0, 255, dtype="u1", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u1_low_like, 255, dtype="u1", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_high_open, dtype="u1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_low, I_u1_high_open, dtype="u1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(0, I_u1_high_open, dtype="u1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_high_closed, dtype="u1", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_low, I_u1_high_closed, dtype="u1", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(0, I_u1_high_closed, dtype="u1", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_low_like, 255, dtype="u1", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_high_open, dtype="u1")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_low, I_u1_high_open, dtype="u1")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(0, I_u1_high_open, dtype="u1")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_high_closed, dtype="u1", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_low, I_u1_high_closed, dtype="u1", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(0, I_u1_high_closed, dtype="u1", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] reveal_type(def_gen.integers(256, dtype="uint8")) # E: int reveal_type(def_gen.integers(0, 256, dtype="uint8")) # E: int reveal_type(def_gen.integers(255, dtype="uint8", endpoint=True)) # E: int reveal_type(def_gen.integers(0, 255, dtype="uint8", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u1_low_like, 255, dtype="uint8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_high_open, dtype="uint8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_low, I_u1_high_open, dtype="uint8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(0, I_u1_high_open, dtype="uint8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_high_closed, dtype="uint8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_low, I_u1_high_closed, dtype="uint8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(0, I_u1_high_closed, dtype="uint8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_low_like, 255, dtype="uint8", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_high_open, dtype="uint8")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_low, I_u1_high_open, dtype="uint8")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(0, I_u1_high_open, dtype="uint8")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_high_closed, dtype="uint8", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_low, I_u1_high_closed, dtype="uint8", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(0, I_u1_high_closed, dtype="uint8", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] reveal_type(def_gen.integers(256, dtype=np.uint8)) # E: int reveal_type(def_gen.integers(0, 256, dtype=np.uint8)) # E: int reveal_type(def_gen.integers(255, dtype=np.uint8, endpoint=True)) # E: int reveal_type(def_gen.integers(0, 255, dtype=np.uint8, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u1_low_like, 255, dtype=np.uint8, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_high_open, dtype=np.uint8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_low, I_u1_high_open, dtype=np.uint8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(0, I_u1_high_open, dtype=np.uint8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_high_closed, dtype=np.uint8, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_u1_low, I_u1_high_closed, dtype=np.uint8, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(0, I_u1_high_closed, dtype=np.uint8, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_low_like, 255, dtype=np.uint8, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_high_open, dtype=np.uint8)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_low, I_u1_high_open, dtype=np.uint8)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(0, I_u1_high_open, dtype=np.uint8)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_high_closed, dtype=np.uint8, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_u1_low, I_u1_high_closed, dtype=np.uint8, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(0, I_u1_high_closed, dtype=np.uint8, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] I_u2_low: np.ndarray[Any, np.dtype[np.uint16]] = np.array([0], dtype=np.uint16) I_u2_low_like: List[int] = [0] @@ -579,37 +579,37 @@ reveal_type(def_gen.integers(65536, dtype="u2")) # E: int reveal_type(def_gen.integers(0, 65536, dtype="u2")) # E: int reveal_type(def_gen.integers(65535, dtype="u2", endpoint=True)) # E: int reveal_type(def_gen.integers(0, 65535, dtype="u2", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u2_low_like, 65535, dtype="u2", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_high_open, dtype="u2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_low, I_u2_high_open, dtype="u2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(0, I_u2_high_open, dtype="u2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_high_closed, dtype="u2", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_low, I_u2_high_closed, dtype="u2", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(0, I_u2_high_closed, dtype="u2", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_low_like, 65535, dtype="u2", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_high_open, dtype="u2")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_low, I_u2_high_open, dtype="u2")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(0, I_u2_high_open, dtype="u2")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_high_closed, dtype="u2", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_low, I_u2_high_closed, dtype="u2", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(0, I_u2_high_closed, dtype="u2", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] reveal_type(def_gen.integers(65536, dtype="uint16")) # E: int reveal_type(def_gen.integers(0, 65536, dtype="uint16")) # E: int reveal_type(def_gen.integers(65535, dtype="uint16", endpoint=True)) # E: int reveal_type(def_gen.integers(0, 65535, dtype="uint16", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u2_low_like, 65535, dtype="uint16", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_high_open, dtype="uint16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_low, I_u2_high_open, dtype="uint16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(0, I_u2_high_open, dtype="uint16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_high_closed, dtype="uint16", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_low, I_u2_high_closed, dtype="uint16", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(0, I_u2_high_closed, dtype="uint16", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_low_like, 65535, dtype="uint16", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_high_open, dtype="uint16")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_low, I_u2_high_open, dtype="uint16")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(0, I_u2_high_open, dtype="uint16")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_high_closed, dtype="uint16", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_low, I_u2_high_closed, dtype="uint16", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(0, I_u2_high_closed, dtype="uint16", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] reveal_type(def_gen.integers(65536, dtype=np.uint16)) # E: int reveal_type(def_gen.integers(0, 65536, dtype=np.uint16)) # E: int reveal_type(def_gen.integers(65535, dtype=np.uint16, endpoint=True)) # E: int reveal_type(def_gen.integers(0, 65535, dtype=np.uint16, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u2_low_like, 65535, dtype=np.uint16, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_high_open, dtype=np.uint16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_low, I_u2_high_open, dtype=np.uint16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(0, I_u2_high_open, dtype=np.uint16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_high_closed, dtype=np.uint16, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_u2_low, I_u2_high_closed, dtype=np.uint16, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(0, I_u2_high_closed, dtype=np.uint16, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_low_like, 65535, dtype=np.uint16, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_high_open, dtype=np.uint16)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_low, I_u2_high_open, dtype=np.uint16)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(0, I_u2_high_open, dtype=np.uint16)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_high_closed, dtype=np.uint16, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_u2_low, I_u2_high_closed, dtype=np.uint16, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(0, I_u2_high_closed, dtype=np.uint16, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] I_u4_low: np.ndarray[Any, np.dtype[np.uint32]] = np.array([0], dtype=np.uint32) I_u4_low_like: List[int] = [0] @@ -620,62 +620,62 @@ reveal_type(def_gen.integers(4294967296, dtype=np.int_)) # E: int reveal_type(def_gen.integers(0, 4294967296, dtype=np.int_)) # E: int reveal_type(def_gen.integers(4294967295, dtype=np.int_, endpoint=True)) # E: int reveal_type(def_gen.integers(0, 4294967295, dtype=np.int_, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype=np.int_, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(def_gen.integers(I_u4_high_open, dtype=np.int_)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype=np.int_)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(def_gen.integers(0, I_u4_high_open, dtype=np.int_)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(def_gen.integers(I_u4_high_closed, dtype=np.int_, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype=np.int_, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(def_gen.integers(0, I_u4_high_closed, dtype=np.int_, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype=np.int_, endpoint=True)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(def_gen.integers(I_u4_high_open, dtype=np.int_)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype=np.int_)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(def_gen.integers(0, I_u4_high_open, dtype=np.int_)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(def_gen.integers(I_u4_high_closed, dtype=np.int_, endpoint=True)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype=np.int_, endpoint=True)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(def_gen.integers(0, I_u4_high_closed, dtype=np.int_, endpoint=True)) # E: ndarray[Any, dtype[{int_}]] reveal_type(def_gen.integers(4294967296, dtype="u4")) # E: int reveal_type(def_gen.integers(0, 4294967296, dtype="u4")) # E: int reveal_type(def_gen.integers(4294967295, dtype="u4", endpoint=True)) # E: int reveal_type(def_gen.integers(0, 4294967295, dtype="u4", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype="u4", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_high_open, dtype="u4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype="u4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(0, I_u4_high_open, dtype="u4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_high_closed, dtype="u4", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype="u4", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(0, I_u4_high_closed, dtype="u4", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype="u4", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_high_open, dtype="u4")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype="u4")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(0, I_u4_high_open, dtype="u4")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_high_closed, dtype="u4", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype="u4", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(0, I_u4_high_closed, dtype="u4", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] reveal_type(def_gen.integers(4294967296, dtype="uint32")) # E: int reveal_type(def_gen.integers(0, 4294967296, dtype="uint32")) # E: int reveal_type(def_gen.integers(4294967295, dtype="uint32", endpoint=True)) # E: int reveal_type(def_gen.integers(0, 4294967295, dtype="uint32", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype="uint32", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_high_open, dtype="uint32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype="uint32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(0, I_u4_high_open, dtype="uint32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_high_closed, dtype="uint32", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype="uint32", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(0, I_u4_high_closed, dtype="uint32", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype="uint32", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_high_open, dtype="uint32")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype="uint32")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(0, I_u4_high_open, dtype="uint32")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_high_closed, dtype="uint32", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype="uint32", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(0, I_u4_high_closed, dtype="uint32", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] reveal_type(def_gen.integers(4294967296, dtype=np.uint32)) # E: int reveal_type(def_gen.integers(0, 4294967296, dtype=np.uint32)) # E: int reveal_type(def_gen.integers(4294967295, dtype=np.uint32, endpoint=True)) # E: int reveal_type(def_gen.integers(0, 4294967295, dtype=np.uint32, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype=np.uint32, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_high_open, dtype=np.uint32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype=np.uint32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(0, I_u4_high_open, dtype=np.uint32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_high_closed, dtype=np.uint32, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype=np.uint32, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(0, I_u4_high_closed, dtype=np.uint32, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype=np.uint32, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_high_open, dtype=np.uint32)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype=np.uint32)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(0, I_u4_high_open, dtype=np.uint32)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_high_closed, dtype=np.uint32, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype=np.uint32, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(0, I_u4_high_closed, dtype=np.uint32, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] reveal_type(def_gen.integers(4294967296, dtype=np.uint)) # E: int reveal_type(def_gen.integers(0, 4294967296, dtype=np.uint)) # E: int reveal_type(def_gen.integers(4294967295, dtype=np.uint, endpoint=True)) # E: int reveal_type(def_gen.integers(0, 4294967295, dtype=np.uint, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype=np.uint, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] -reveal_type(def_gen.integers(I_u4_high_open, dtype=np.uint)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype=np.uint)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] -reveal_type(def_gen.integers(0, I_u4_high_open, dtype=np.uint)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] -reveal_type(def_gen.integers(I_u4_high_closed, dtype=np.uint, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] -reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype=np.uint, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] -reveal_type(def_gen.integers(0, I_u4_high_closed, dtype=np.uint, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] +reveal_type(def_gen.integers(I_u4_low_like, 4294967295, dtype=np.uint, endpoint=True)) # E: ndarray[Any, dtype[{uint}]] +reveal_type(def_gen.integers(I_u4_high_open, dtype=np.uint)) # E: ndarray[Any, dtype[{uint}]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_open, dtype=np.uint)) # E: ndarray[Any, dtype[{uint}]] +reveal_type(def_gen.integers(0, I_u4_high_open, dtype=np.uint)) # E: ndarray[Any, dtype[{uint}]] +reveal_type(def_gen.integers(I_u4_high_closed, dtype=np.uint, endpoint=True)) # E: ndarray[Any, dtype[{uint}]] +reveal_type(def_gen.integers(I_u4_low, I_u4_high_closed, dtype=np.uint, endpoint=True)) # E: ndarray[Any, dtype[{uint}]] +reveal_type(def_gen.integers(0, I_u4_high_closed, dtype=np.uint, endpoint=True)) # E: ndarray[Any, dtype[{uint}]] I_u8_low: np.ndarray[Any, np.dtype[np.uint64]] = np.array([0], dtype=np.uint64) I_u8_low_like: List[int] = [0] @@ -686,37 +686,37 @@ reveal_type(def_gen.integers(18446744073709551616, dtype="u8")) # E: int reveal_type(def_gen.integers(0, 18446744073709551616, dtype="u8")) # E: int reveal_type(def_gen.integers(18446744073709551615, dtype="u8", endpoint=True)) # E: int reveal_type(def_gen.integers(0, 18446744073709551615, dtype="u8", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u8_low_like, 18446744073709551615, dtype="u8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_high_open, dtype="u8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_low, I_u8_high_open, dtype="u8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(0, I_u8_high_open, dtype="u8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_high_closed, dtype="u8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_low, I_u8_high_closed, dtype="u8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(0, I_u8_high_closed, dtype="u8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_low_like, 18446744073709551615, dtype="u8", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_high_open, dtype="u8")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_low, I_u8_high_open, dtype="u8")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(0, I_u8_high_open, dtype="u8")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_high_closed, dtype="u8", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_low, I_u8_high_closed, dtype="u8", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(0, I_u8_high_closed, dtype="u8", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] reveal_type(def_gen.integers(18446744073709551616, dtype="uint64")) # E: int reveal_type(def_gen.integers(0, 18446744073709551616, dtype="uint64")) # E: int reveal_type(def_gen.integers(18446744073709551615, dtype="uint64", endpoint=True)) # E: int reveal_type(def_gen.integers(0, 18446744073709551615, dtype="uint64", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u8_low_like, 18446744073709551615, dtype="uint64", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_high_open, dtype="uint64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_low, I_u8_high_open, dtype="uint64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(0, I_u8_high_open, dtype="uint64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_high_closed, dtype="uint64", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_low, I_u8_high_closed, dtype="uint64", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(0, I_u8_high_closed, dtype="uint64", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_low_like, 18446744073709551615, dtype="uint64", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_high_open, dtype="uint64")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_low, I_u8_high_open, dtype="uint64")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(0, I_u8_high_open, dtype="uint64")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_high_closed, dtype="uint64", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_low, I_u8_high_closed, dtype="uint64", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(0, I_u8_high_closed, dtype="uint64", endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] reveal_type(def_gen.integers(18446744073709551616, dtype=np.uint64)) # E: int reveal_type(def_gen.integers(0, 18446744073709551616, dtype=np.uint64)) # E: int reveal_type(def_gen.integers(18446744073709551615, dtype=np.uint64, endpoint=True)) # E: int reveal_type(def_gen.integers(0, 18446744073709551615, dtype=np.uint64, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_u8_low_like, 18446744073709551615, dtype=np.uint64, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_high_open, dtype=np.uint64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_low, I_u8_high_open, dtype=np.uint64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(0, I_u8_high_open, dtype=np.uint64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_high_closed, dtype=np.uint64, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_u8_low, I_u8_high_closed, dtype=np.uint64, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(0, I_u8_high_closed, dtype=np.uint64, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_low_like, 18446744073709551615, dtype=np.uint64, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_high_open, dtype=np.uint64)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_low, I_u8_high_open, dtype=np.uint64)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(0, I_u8_high_open, dtype=np.uint64)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_high_closed, dtype=np.uint64, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_u8_low, I_u8_high_closed, dtype=np.uint64, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(0, I_u8_high_closed, dtype=np.uint64, endpoint=True)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] I_i1_low: np.ndarray[Any, np.dtype[np.int8]] = np.array([-128], dtype=np.int8) I_i1_low_like: List[int] = [-128] @@ -727,37 +727,37 @@ reveal_type(def_gen.integers(128, dtype="i1")) # E: int reveal_type(def_gen.integers(-128, 128, dtype="i1")) # E: int reveal_type(def_gen.integers(127, dtype="i1", endpoint=True)) # E: int reveal_type(def_gen.integers(-128, 127, dtype="i1", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i1_low_like, 127, dtype="i1", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_high_open, dtype="i1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_low, I_i1_high_open, dtype="i1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(-128, I_i1_high_open, dtype="i1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_high_closed, dtype="i1", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_low, I_i1_high_closed, dtype="i1", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(-128, I_i1_high_closed, dtype="i1", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_low_like, 127, dtype="i1", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_high_open, dtype="i1")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_low, I_i1_high_open, dtype="i1")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(-128, I_i1_high_open, dtype="i1")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_high_closed, dtype="i1", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_low, I_i1_high_closed, dtype="i1", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(-128, I_i1_high_closed, dtype="i1", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] reveal_type(def_gen.integers(128, dtype="int8")) # E: int reveal_type(def_gen.integers(-128, 128, dtype="int8")) # E: int reveal_type(def_gen.integers(127, dtype="int8", endpoint=True)) # E: int reveal_type(def_gen.integers(-128, 127, dtype="int8", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i1_low_like, 127, dtype="int8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_high_open, dtype="int8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_low, I_i1_high_open, dtype="int8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(-128, I_i1_high_open, dtype="int8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_high_closed, dtype="int8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_low, I_i1_high_closed, dtype="int8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(-128, I_i1_high_closed, dtype="int8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_low_like, 127, dtype="int8", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_high_open, dtype="int8")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_low, I_i1_high_open, dtype="int8")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(-128, I_i1_high_open, dtype="int8")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_high_closed, dtype="int8", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_low, I_i1_high_closed, dtype="int8", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(-128, I_i1_high_closed, dtype="int8", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] reveal_type(def_gen.integers(128, dtype=np.int8)) # E: int reveal_type(def_gen.integers(-128, 128, dtype=np.int8)) # E: int reveal_type(def_gen.integers(127, dtype=np.int8, endpoint=True)) # E: int reveal_type(def_gen.integers(-128, 127, dtype=np.int8, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i1_low_like, 127, dtype=np.int8, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_high_open, dtype=np.int8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_low, I_i1_high_open, dtype=np.int8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(-128, I_i1_high_open, dtype=np.int8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_high_closed, dtype=np.int8, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(I_i1_low, I_i1_high_closed, dtype=np.int8, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(def_gen.integers(-128, I_i1_high_closed, dtype=np.int8, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_low_like, 127, dtype=np.int8, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_high_open, dtype=np.int8)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_low, I_i1_high_open, dtype=np.int8)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(-128, I_i1_high_open, dtype=np.int8)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_high_closed, dtype=np.int8, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(I_i1_low, I_i1_high_closed, dtype=np.int8, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(def_gen.integers(-128, I_i1_high_closed, dtype=np.int8, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] I_i2_low: np.ndarray[Any, np.dtype[np.int16]] = np.array([-32768], dtype=np.int16) I_i2_low_like: List[int] = [-32768] @@ -768,37 +768,37 @@ reveal_type(def_gen.integers(32768, dtype="i2")) # E: int reveal_type(def_gen.integers(-32768, 32768, dtype="i2")) # E: int reveal_type(def_gen.integers(32767, dtype="i2", endpoint=True)) # E: int reveal_type(def_gen.integers(-32768, 32767, dtype="i2", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i2_low_like, 32767, dtype="i2", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_high_open, dtype="i2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_low, I_i2_high_open, dtype="i2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(-32768, I_i2_high_open, dtype="i2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_high_closed, dtype="i2", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_low, I_i2_high_closed, dtype="i2", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(-32768, I_i2_high_closed, dtype="i2", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_low_like, 32767, dtype="i2", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_high_open, dtype="i2")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_low, I_i2_high_open, dtype="i2")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(-32768, I_i2_high_open, dtype="i2")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_high_closed, dtype="i2", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_low, I_i2_high_closed, dtype="i2", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(-32768, I_i2_high_closed, dtype="i2", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] reveal_type(def_gen.integers(32768, dtype="int16")) # E: int reveal_type(def_gen.integers(-32768, 32768, dtype="int16")) # E: int reveal_type(def_gen.integers(32767, dtype="int16", endpoint=True)) # E: int reveal_type(def_gen.integers(-32768, 32767, dtype="int16", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i2_low_like, 32767, dtype="int16", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_high_open, dtype="int16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_low, I_i2_high_open, dtype="int16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(-32768, I_i2_high_open, dtype="int16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_high_closed, dtype="int16", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_low, I_i2_high_closed, dtype="int16", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(-32768, I_i2_high_closed, dtype="int16", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_low_like, 32767, dtype="int16", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_high_open, dtype="int16")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_low, I_i2_high_open, dtype="int16")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(-32768, I_i2_high_open, dtype="int16")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_high_closed, dtype="int16", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_low, I_i2_high_closed, dtype="int16", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(-32768, I_i2_high_closed, dtype="int16", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] reveal_type(def_gen.integers(32768, dtype=np.int16)) # E: int reveal_type(def_gen.integers(-32768, 32768, dtype=np.int16)) # E: int reveal_type(def_gen.integers(32767, dtype=np.int16, endpoint=True)) # E: int reveal_type(def_gen.integers(-32768, 32767, dtype=np.int16, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i2_low_like, 32767, dtype=np.int16, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_high_open, dtype=np.int16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_low, I_i2_high_open, dtype=np.int16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(-32768, I_i2_high_open, dtype=np.int16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_high_closed, dtype=np.int16, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(I_i2_low, I_i2_high_closed, dtype=np.int16, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(def_gen.integers(-32768, I_i2_high_closed, dtype=np.int16, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_low_like, 32767, dtype=np.int16, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_high_open, dtype=np.int16)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_low, I_i2_high_open, dtype=np.int16)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(-32768, I_i2_high_open, dtype=np.int16)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_high_closed, dtype=np.int16, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(I_i2_low, I_i2_high_closed, dtype=np.int16, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(def_gen.integers(-32768, I_i2_high_closed, dtype=np.int16, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] I_i4_low: np.ndarray[Any, np.dtype[np.int32]] = np.array([-2147483648], dtype=np.int32) I_i4_low_like: List[int] = [-2147483648] @@ -809,37 +809,37 @@ reveal_type(def_gen.integers(2147483648, dtype="i4")) # E: int reveal_type(def_gen.integers(-2147483648, 2147483648, dtype="i4")) # E: int reveal_type(def_gen.integers(2147483647, dtype="i4", endpoint=True)) # E: int reveal_type(def_gen.integers(-2147483648, 2147483647, dtype="i4", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i4_low_like, 2147483647, dtype="i4", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_high_open, dtype="i4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_low, I_i4_high_open, dtype="i4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(-2147483648, I_i4_high_open, dtype="i4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_high_closed, dtype="i4", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_low, I_i4_high_closed, dtype="i4", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(-2147483648, I_i4_high_closed, dtype="i4", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_low_like, 2147483647, dtype="i4", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_high_open, dtype="i4")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_low, I_i4_high_open, dtype="i4")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(-2147483648, I_i4_high_open, dtype="i4")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_high_closed, dtype="i4", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_low, I_i4_high_closed, dtype="i4", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(-2147483648, I_i4_high_closed, dtype="i4", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] reveal_type(def_gen.integers(2147483648, dtype="int32")) # E: int reveal_type(def_gen.integers(-2147483648, 2147483648, dtype="int32")) # E: int reveal_type(def_gen.integers(2147483647, dtype="int32", endpoint=True)) # E: int reveal_type(def_gen.integers(-2147483648, 2147483647, dtype="int32", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i4_low_like, 2147483647, dtype="int32", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_high_open, dtype="int32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_low, I_i4_high_open, dtype="int32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(-2147483648, I_i4_high_open, dtype="int32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_high_closed, dtype="int32", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_low, I_i4_high_closed, dtype="int32", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(-2147483648, I_i4_high_closed, dtype="int32", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_low_like, 2147483647, dtype="int32", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_high_open, dtype="int32")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_low, I_i4_high_open, dtype="int32")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(-2147483648, I_i4_high_open, dtype="int32")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_high_closed, dtype="int32", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_low, I_i4_high_closed, dtype="int32", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(-2147483648, I_i4_high_closed, dtype="int32", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] reveal_type(def_gen.integers(2147483648, dtype=np.int32)) # E: int reveal_type(def_gen.integers(-2147483648, 2147483648, dtype=np.int32)) # E: int reveal_type(def_gen.integers(2147483647, dtype=np.int32, endpoint=True)) # E: int reveal_type(def_gen.integers(-2147483648, 2147483647, dtype=np.int32, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i4_low_like, 2147483647, dtype=np.int32, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_high_open, dtype=np.int32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_low, I_i4_high_open, dtype=np.int32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(-2147483648, I_i4_high_open, dtype=np.int32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_high_closed, dtype=np.int32, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(I_i4_low, I_i4_high_closed, dtype=np.int32, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(def_gen.integers(-2147483648, I_i4_high_closed, dtype=np.int32, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_low_like, 2147483647, dtype=np.int32, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_high_open, dtype=np.int32)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_low, I_i4_high_open, dtype=np.int32)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(-2147483648, I_i4_high_open, dtype=np.int32)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_high_closed, dtype=np.int32, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(I_i4_low, I_i4_high_closed, dtype=np.int32, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(def_gen.integers(-2147483648, I_i4_high_closed, dtype=np.int32, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] I_i8_low: np.ndarray[Any, np.dtype[np.int64]] = np.array([-9223372036854775808], dtype=np.int64) I_i8_low_like: List[int] = [-9223372036854775808] @@ -850,37 +850,37 @@ reveal_type(def_gen.integers(9223372036854775808, dtype="i8")) # E: int reveal_type(def_gen.integers(-9223372036854775808, 9223372036854775808, dtype="i8")) # E: int reveal_type(def_gen.integers(9223372036854775807, dtype="i8", endpoint=True)) # E: int reveal_type(def_gen.integers(-9223372036854775808, 9223372036854775807, dtype="i8", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i8_low_like, 9223372036854775807, dtype="i8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_high_open, dtype="i8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_low, I_i8_high_open, dtype="i8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_open, dtype="i8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_high_closed, dtype="i8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_low, I_i8_high_closed, dtype="i8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_closed, dtype="i8", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_low_like, 9223372036854775807, dtype="i8", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_high_open, dtype="i8")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_low, I_i8_high_open, dtype="i8")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_open, dtype="i8")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_high_closed, dtype="i8", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_low, I_i8_high_closed, dtype="i8", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_closed, dtype="i8", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.integers(9223372036854775808, dtype="int64")) # E: int reveal_type(def_gen.integers(-9223372036854775808, 9223372036854775808, dtype="int64")) # E: int reveal_type(def_gen.integers(9223372036854775807, dtype="int64", endpoint=True)) # E: int reveal_type(def_gen.integers(-9223372036854775808, 9223372036854775807, dtype="int64", endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i8_low_like, 9223372036854775807, dtype="int64", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_high_open, dtype="int64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_low, I_i8_high_open, dtype="int64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_open, dtype="int64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_high_closed, dtype="int64", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_low, I_i8_high_closed, dtype="int64", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_closed, dtype="int64", endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_low_like, 9223372036854775807, dtype="int64", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_high_open, dtype="int64")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_low, I_i8_high_open, dtype="int64")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_open, dtype="int64")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_high_closed, dtype="int64", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_low, I_i8_high_closed, dtype="int64", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_closed, dtype="int64", endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.integers(9223372036854775808, dtype=np.int64)) # E: int reveal_type(def_gen.integers(-9223372036854775808, 9223372036854775808, dtype=np.int64)) # E: int reveal_type(def_gen.integers(9223372036854775807, dtype=np.int64, endpoint=True)) # E: int reveal_type(def_gen.integers(-9223372036854775808, 9223372036854775807, dtype=np.int64, endpoint=True)) # E: int -reveal_type(def_gen.integers(I_i8_low_like, 9223372036854775807, dtype=np.int64, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_high_open, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_low, I_i8_high_open, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_open, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_high_closed, dtype=np.int64, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(I_i8_low, I_i8_high_closed, dtype=np.int64, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_closed, dtype=np.int64, endpoint=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_low_like, 9223372036854775807, dtype=np.int64, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_high_open, dtype=np.int64)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_low, I_i8_high_open, dtype=np.int64)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_open, dtype=np.int64)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_high_closed, dtype=np.int64, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(I_i8_low, I_i8_high_closed, dtype=np.int64, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.integers(-9223372036854775808, I_i8_high_closed, dtype=np.int64, endpoint=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.bit_generator) # E: BitGenerator @@ -888,50 +888,50 @@ reveal_type(def_gen.bit_generator) # E: BitGenerator reveal_type(def_gen.bytes(2)) # E: bytes reveal_type(def_gen.choice(5)) # E: int -reveal_type(def_gen.choice(5, 3)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.choice(5, 3, replace=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.choice(5, 3, p=[1 / 5] * 5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.choice(5, 3, p=[1 / 5] * 5, replace=False)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(def_gen.choice(5, 3)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.choice(5, 3, replace=True)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.choice(5, 3, p=[1 / 5] * 5)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.choice(5, 3, p=[1 / 5] * 5, replace=False)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(def_gen.choice(["pooh", "rabbit", "piglet", "Christopher"])) # E: Any -reveal_type(def_gen.choice(["pooh", "rabbit", "piglet", "Christopher"], 3)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, p=[1 / 4] * 4)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, replace=True)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, replace=False, p=np.array([1 / 8, 1 / 8, 1 / 2, 1 / 4]))) # E: numpy.ndarray[Any, Any] - -reveal_type(def_gen.dirichlet([0.5, 0.5])) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.dirichlet(np.array([0.5, 0.5]))) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.dirichlet(np.array([0.5, 0.5]), size=3)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] - -reveal_type(def_gen.multinomial(20, [1 / 6.0] * 6)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.multinomial(20, np.array([0.5, 0.5]))) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.multinomial(20, [1 / 6.0] * 6, size=2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.multinomial([[10], [20]], [1 / 6.0] * 6, size=(2, 2))) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.multinomial(np.array([[10], [20]]), np.array([0.5, 0.5]), size=(2, 2))) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] - -reveal_type(def_gen.multivariate_hypergeometric([3, 5, 7], 2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.multivariate_hypergeometric(np.array([3, 5, 7]), 2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.multivariate_hypergeometric(np.array([3, 5, 7]), 2, size=4)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.multivariate_hypergeometric(np.array([3, 5, 7]), 2, size=(4, 7))) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.multivariate_hypergeometric([3, 5, 7], 2, method="count")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.multivariate_hypergeometric(np.array([3, 5, 7]), 2, method="marginals")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] - -reveal_type(def_gen.multivariate_normal([0.0], [[1.0]])) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.multivariate_normal([0.0], np.array([[1.0]]))) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.multivariate_normal(np.array([0.0]), [[1.0]])) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(def_gen.multivariate_normal([0.0], np.array([[1.0]]))) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] - -reveal_type(def_gen.permutation(10)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(def_gen.permutation([1, 2, 3, 4])) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.permutation(np.array([1, 2, 3, 4]))) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.permutation(D_2D, axis=1)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.permuted(D_2D)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.permuted(D_2D_like)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.permuted(D_2D, axis=1)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.permuted(D_2D, out=D_2D)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.permuted(D_2D_like, out=D_2D)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.permuted(D_2D_like, out=D_2D)) # E: numpy.ndarray[Any, Any] -reveal_type(def_gen.permuted(D_2D, axis=1, out=D_2D)) # E: numpy.ndarray[Any, Any] +reveal_type(def_gen.choice(["pooh", "rabbit", "piglet", "Christopher"], 3)) # E: ndarray[Any, Any] +reveal_type(def_gen.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, p=[1 / 4] * 4)) # E: ndarray[Any, Any] +reveal_type(def_gen.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, replace=True)) # E: ndarray[Any, Any] +reveal_type(def_gen.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, replace=False, p=np.array([1 / 8, 1 / 8, 1 / 2, 1 / 4]))) # E: ndarray[Any, Any] + +reveal_type(def_gen.dirichlet([0.5, 0.5])) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.dirichlet(np.array([0.5, 0.5]))) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.dirichlet(np.array([0.5, 0.5]), size=3)) # E: ndarray[Any, dtype[floating[typing._64Bit]] + +reveal_type(def_gen.multinomial(20, [1 / 6.0] * 6)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.multinomial(20, np.array([0.5, 0.5]))) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.multinomial(20, [1 / 6.0] * 6, size=2)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.multinomial([[10], [20]], [1 / 6.0] * 6, size=(2, 2))) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.multinomial(np.array([[10], [20]]), np.array([0.5, 0.5]), size=(2, 2))) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] + +reveal_type(def_gen.multivariate_hypergeometric([3, 5, 7], 2)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.multivariate_hypergeometric(np.array([3, 5, 7]), 2)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.multivariate_hypergeometric(np.array([3, 5, 7]), 2, size=4)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.multivariate_hypergeometric(np.array([3, 5, 7]), 2, size=(4, 7))) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.multivariate_hypergeometric([3, 5, 7], 2, method="count")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.multivariate_hypergeometric(np.array([3, 5, 7]), 2, method="marginals")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] + +reveal_type(def_gen.multivariate_normal([0.0], [[1.0]])) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.multivariate_normal([0.0], np.array([[1.0]]))) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.multivariate_normal(np.array([0.0]), [[1.0]])) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(def_gen.multivariate_normal([0.0], np.array([[1.0]]))) # E: ndarray[Any, dtype[floating[typing._64Bit]] + +reveal_type(def_gen.permutation(10)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(def_gen.permutation([1, 2, 3, 4])) # E: ndarray[Any, Any] +reveal_type(def_gen.permutation(np.array([1, 2, 3, 4]))) # E: ndarray[Any, Any] +reveal_type(def_gen.permutation(D_2D, axis=1)) # E: ndarray[Any, Any] +reveal_type(def_gen.permuted(D_2D)) # E: ndarray[Any, Any] +reveal_type(def_gen.permuted(D_2D_like)) # E: ndarray[Any, Any] +reveal_type(def_gen.permuted(D_2D, axis=1)) # E: ndarray[Any, Any] +reveal_type(def_gen.permuted(D_2D, out=D_2D)) # E: ndarray[Any, Any] +reveal_type(def_gen.permuted(D_2D_like, out=D_2D)) # E: ndarray[Any, Any] +reveal_type(def_gen.permuted(D_2D_like, out=D_2D)) # E: ndarray[Any, Any] +reveal_type(def_gen.permuted(D_2D, axis=1, out=D_2D)) # E: ndarray[Any, Any] reveal_type(def_gen.shuffle(np.arange(10))) # E: None reveal_type(def_gen.shuffle([1, 2, 3, 4, 5])) # E: None @@ -949,559 +949,559 @@ random_st: np.random.RandomState = np.random.RandomState() reveal_type(random_st.standard_normal()) # E: float reveal_type(random_st.standard_normal(size=None)) # E: float -reveal_type(random_st.standard_normal(size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] +reveal_type(random_st.standard_normal(size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] reveal_type(random_st.random()) # E: float reveal_type(random_st.random(size=None)) # E: float -reveal_type(random_st.random(size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] +reveal_type(random_st.random(size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] reveal_type(random_st.standard_cauchy()) # E: float reveal_type(random_st.standard_cauchy(size=None)) # E: float -reveal_type(random_st.standard_cauchy(size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.standard_cauchy(size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.standard_exponential()) # E: float reveal_type(random_st.standard_exponential(size=None)) # E: float -reveal_type(random_st.standard_exponential(size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] +reveal_type(random_st.standard_exponential(size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] reveal_type(random_st.zipf(1.5)) # E: int reveal_type(random_st.zipf(1.5, size=None)) # E: int -reveal_type(random_st.zipf(1.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.zipf(D_arr_1p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.zipf(D_arr_1p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.zipf(D_arr_like_1p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.zipf(D_arr_like_1p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.zipf(1.5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.zipf(D_arr_1p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.zipf(D_arr_1p5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.zipf(D_arr_like_1p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.zipf(D_arr_like_1p5, size=1)) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.weibull(0.5)) # E: float reveal_type(random_st.weibull(0.5, size=None)) # E: float -reveal_type(random_st.weibull(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.weibull(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.weibull(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.weibull(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.weibull(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.weibull(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.weibull(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.weibull(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.weibull(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.weibull(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.standard_t(0.5)) # E: float reveal_type(random_st.standard_t(0.5, size=None)) # E: float -reveal_type(random_st.standard_t(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.standard_t(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.standard_t(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.standard_t(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.standard_t(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.standard_t(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.standard_t(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.standard_t(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.standard_t(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.standard_t(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.poisson(0.5)) # E: int reveal_type(random_st.poisson(0.5, size=None)) # E: int -reveal_type(random_st.poisson(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.poisson(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.poisson(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.poisson(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.poisson(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.poisson(0.5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.poisson(D_arr_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.poisson(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.poisson(D_arr_like_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.poisson(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.power(0.5)) # E: float reveal_type(random_st.power(0.5, size=None)) # E: float -reveal_type(random_st.power(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.power(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.power(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.power(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.power(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.power(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.power(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.power(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.power(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.power(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.pareto(0.5)) # E: float reveal_type(random_st.pareto(0.5, size=None)) # E: float -reveal_type(random_st.pareto(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.pareto(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.pareto(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.pareto(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.pareto(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.pareto(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.pareto(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.pareto(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.pareto(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.pareto(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.chisquare(0.5)) # E: float reveal_type(random_st.chisquare(0.5, size=None)) # E: float -reveal_type(random_st.chisquare(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.chisquare(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.chisquare(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.chisquare(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.chisquare(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.chisquare(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.chisquare(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.chisquare(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.chisquare(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.chisquare(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.exponential(0.5)) # E: float reveal_type(random_st.exponential(0.5, size=None)) # E: float -reveal_type(random_st.exponential(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.exponential(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.exponential(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.exponential(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.exponential(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.exponential(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.exponential(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.exponential(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.exponential(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.exponential(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.geometric(0.5)) # E: int reveal_type(random_st.geometric(0.5, size=None)) # E: int -reveal_type(random_st.geometric(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.geometric(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.geometric(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.geometric(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.geometric(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.geometric(0.5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.geometric(D_arr_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.geometric(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.geometric(D_arr_like_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.geometric(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.logseries(0.5)) # E: int reveal_type(random_st.logseries(0.5, size=None)) # E: int -reveal_type(random_st.logseries(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.logseries(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.logseries(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.logseries(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.logseries(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.logseries(0.5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.logseries(D_arr_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.logseries(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.logseries(D_arr_like_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.logseries(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.rayleigh(0.5)) # E: float reveal_type(random_st.rayleigh(0.5, size=None)) # E: float -reveal_type(random_st.rayleigh(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.rayleigh(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.rayleigh(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.rayleigh(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.rayleigh(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.rayleigh(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.rayleigh(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.rayleigh(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.rayleigh(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.rayleigh(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.standard_gamma(0.5)) # E: float reveal_type(random_st.standard_gamma(0.5, size=None)) # E: float -reveal_type(random_st.standard_gamma(0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(random_st.standard_gamma(D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(random_st.standard_gamma(D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(random_st.standard_gamma(D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(random_st.standard_gamma(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] -reveal_type(random_st.standard_gamma(D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]]] +reveal_type(random_st.standard_gamma(0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(random_st.standard_gamma(D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(random_st.standard_gamma(D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(random_st.standard_gamma(D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(random_st.standard_gamma(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] +reveal_type(random_st.standard_gamma(D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]]] reveal_type(random_st.vonmises(0.5, 0.5)) # E: float reveal_type(random_st.vonmises(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.vonmises(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.vonmises(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.vonmises(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.vonmises(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.wald(0.5, 0.5)) # E: float reveal_type(random_st.wald(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.wald(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.wald(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.wald(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.wald(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.uniform(0.5, 0.5)) # E: float reveal_type(random_st.uniform(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.uniform(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.uniform(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.uniform(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.uniform(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.beta(0.5, 0.5)) # E: float reveal_type(random_st.beta(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.beta(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.beta(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.beta(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.beta(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.f(0.5, 0.5)) # E: float reveal_type(random_st.f(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.f(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.f(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.f(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.f(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.gamma(0.5, 0.5)) # E: float reveal_type(random_st.gamma(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.gamma(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gamma(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.gamma(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gamma(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.gumbel(0.5, 0.5)) # E: float reveal_type(random_st.gumbel(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.gumbel(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.gumbel(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.gumbel(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.gumbel(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.laplace(0.5, 0.5)) # E: float reveal_type(random_st.laplace(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.laplace(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.laplace(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.laplace(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.laplace(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.logistic(0.5, 0.5)) # E: float reveal_type(random_st.logistic(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.logistic(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.logistic(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.logistic(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.logistic(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.lognormal(0.5, 0.5)) # E: float reveal_type(random_st.lognormal(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.lognormal(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.lognormal(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.lognormal(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.lognormal(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.noncentral_chisquare(0.5, 0.5)) # E: float reveal_type(random_st.noncentral_chisquare(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.noncentral_chisquare(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_chisquare(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_chisquare(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.normal(0.5, 0.5)) # E: float reveal_type(random_st.normal(0.5, 0.5, size=None)) # E: float -reveal_type(random_st.normal(0.5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(D_arr_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(0.5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(D_arr_0p5, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(0.5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(D_arr_like_0p5, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(0.5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(D_arr_0p5, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(D_arr_like_0p5, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(D_arr_0p5, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.normal(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.normal(0.5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(D_arr_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(0.5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(D_arr_0p5, 0.5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(0.5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(D_arr_like_0p5, 0.5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(0.5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(D_arr_0p5, D_arr_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(D_arr_like_0p5, D_arr_like_0p5)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(D_arr_0p5, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.normal(D_arr_like_0p5, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.triangular(0.1, 0.5, 0.9)) # E: float reveal_type(random_st.triangular(0.1, 0.5, 0.9, size=None)) # E: float -reveal_type(random_st.triangular(0.1, 0.5, 0.9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(D_arr_0p1, 0.5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(0.1, D_arr_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(D_arr_0p1, 0.5, D_arr_like_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(0.1, D_arr_0p5, 0.9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(D_arr_like_0p1, 0.5, D_arr_0p9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(0.5, D_arr_like_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(D_arr_0p1, D_arr_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(D_arr_like_0p1, D_arr_like_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(D_arr_0p1, D_arr_0p5, D_arr_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.triangular(D_arr_like_0p1, D_arr_like_0p5, D_arr_like_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.triangular(0.1, 0.5, 0.9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(D_arr_0p1, 0.5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(0.1, D_arr_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(D_arr_0p1, 0.5, D_arr_like_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(0.1, D_arr_0p5, 0.9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(D_arr_like_0p1, 0.5, D_arr_0p9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(0.5, D_arr_like_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(D_arr_0p1, D_arr_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(D_arr_like_0p1, D_arr_like_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(D_arr_0p1, D_arr_0p5, D_arr_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.triangular(D_arr_like_0p1, D_arr_like_0p5, D_arr_like_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.noncentral_f(0.1, 0.5, 0.9)) # E: float reveal_type(random_st.noncentral_f(0.1, 0.5, 0.9, size=None)) # E: float -reveal_type(random_st.noncentral_f(0.1, 0.5, 0.9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(D_arr_0p1, 0.5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(0.1, D_arr_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(D_arr_0p1, 0.5, D_arr_like_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(0.1, D_arr_0p5, 0.9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(D_arr_like_0p1, 0.5, D_arr_0p9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(0.5, D_arr_like_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(D_arr_0p1, D_arr_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(D_arr_like_0p1, D_arr_like_0p5, 0.9)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(D_arr_0p1, D_arr_0p5, D_arr_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.noncentral_f(D_arr_like_0p1, D_arr_like_0p5, D_arr_like_0p9, size=1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.noncentral_f(0.1, 0.5, 0.9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(D_arr_0p1, 0.5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(0.1, D_arr_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(D_arr_0p1, 0.5, D_arr_like_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(0.1, D_arr_0p5, 0.9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(D_arr_like_0p1, 0.5, D_arr_0p9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(0.5, D_arr_like_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(D_arr_0p1, D_arr_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(D_arr_like_0p1, D_arr_like_0p5, 0.9)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(D_arr_0p1, D_arr_0p5, D_arr_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.noncentral_f(D_arr_like_0p1, D_arr_like_0p5, D_arr_like_0p9, size=1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.binomial(10, 0.5)) # E: int reveal_type(random_st.binomial(10, 0.5, size=None)) # E: int -reveal_type(random_st.binomial(10, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(I_arr_10, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(10, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(I_arr_10, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(10, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(I_arr_like_10, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(10, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(I_arr_10, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(I_arr_like_10, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(I_arr_10, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.binomial(I_arr_like_10, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.binomial(10, 0.5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(I_arr_10, 0.5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(10, D_arr_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(I_arr_10, 0.5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(10, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(I_arr_like_10, 0.5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(10, D_arr_like_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(I_arr_10, D_arr_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(I_arr_like_10, D_arr_like_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(I_arr_10, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.binomial(I_arr_like_10, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.negative_binomial(10, 0.5)) # E: int reveal_type(random_st.negative_binomial(10, 0.5, size=None)) # E: int -reveal_type(random_st.negative_binomial(10, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(I_arr_10, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(10, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(I_arr_10, 0.5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(10, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(I_arr_like_10, 0.5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(10, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(I_arr_10, D_arr_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(I_arr_like_10, D_arr_like_0p5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(I_arr_10, D_arr_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.negative_binomial(I_arr_like_10, D_arr_like_0p5, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.negative_binomial(10, 0.5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(I_arr_10, 0.5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(10, D_arr_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(I_arr_10, 0.5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(10, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(I_arr_like_10, 0.5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(10, D_arr_like_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(I_arr_10, D_arr_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(I_arr_like_10, D_arr_like_0p5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(I_arr_10, D_arr_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.negative_binomial(I_arr_like_10, D_arr_like_0p5, size=1)) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.hypergeometric(20, 20, 10)) # E: int reveal_type(random_st.hypergeometric(20, 20, 10, size=None)) # E: int -reveal_type(random_st.hypergeometric(20, 20, 10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(I_arr_20, 20, 10)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(20, I_arr_20, 10)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(I_arr_20, 20, I_arr_like_10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(20, I_arr_20, 10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(I_arr_like_20, 20, I_arr_10)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(20, I_arr_like_20, 10)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(I_arr_20, I_arr_20, 10)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(I_arr_like_20, I_arr_like_20, 10)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(I_arr_20, I_arr_20, I_arr_10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.hypergeometric(I_arr_like_20, I_arr_like_20, I_arr_like_10, size=1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.hypergeometric(20, 20, 10, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(I_arr_20, 20, 10)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(20, I_arr_20, 10)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(I_arr_20, 20, I_arr_like_10, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(20, I_arr_20, 10, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(I_arr_like_20, 20, I_arr_10)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(20, I_arr_like_20, 10)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(I_arr_20, I_arr_20, 10)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(I_arr_like_20, I_arr_like_20, 10)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(I_arr_20, I_arr_20, I_arr_10, size=1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.hypergeometric(I_arr_like_20, I_arr_like_20, I_arr_like_10, size=1)) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.randint(0, 100)) # E: int reveal_type(random_st.randint(100)) # E: int -reveal_type(random_st.randint([100])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.randint(0, [100])) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.randint([100])) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.randint(0, [100])) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.randint(2, dtype=bool)) # E: builtins.bool reveal_type(random_st.randint(0, 2, dtype=bool)) # E: builtins.bool -reveal_type(random_st.randint(I_bool_high_open, dtype=bool)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(random_st.randint(I_bool_low, I_bool_high_open, dtype=bool)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(random_st.randint(0, I_bool_high_open, dtype=bool)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] +reveal_type(random_st.randint(I_bool_high_open, dtype=bool)) # E: ndarray[Any, dtype[bool_] +reveal_type(random_st.randint(I_bool_low, I_bool_high_open, dtype=bool)) # E: ndarray[Any, dtype[bool_] +reveal_type(random_st.randint(0, I_bool_high_open, dtype=bool)) # E: ndarray[Any, dtype[bool_] reveal_type(random_st.randint(2, dtype=np.bool_)) # E: builtins.bool reveal_type(random_st.randint(0, 2, dtype=np.bool_)) # E: builtins.bool -reveal_type(random_st.randint(I_bool_high_open, dtype=np.bool_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(random_st.randint(I_bool_low, I_bool_high_open, dtype=np.bool_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] -reveal_type(random_st.randint(0, I_bool_high_open, dtype=np.bool_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_] +reveal_type(random_st.randint(I_bool_high_open, dtype=np.bool_)) # E: ndarray[Any, dtype[bool_] +reveal_type(random_st.randint(I_bool_low, I_bool_high_open, dtype=np.bool_)) # E: ndarray[Any, dtype[bool_] +reveal_type(random_st.randint(0, I_bool_high_open, dtype=np.bool_)) # E: ndarray[Any, dtype[bool_] reveal_type(random_st.randint(256, dtype="u1")) # E: int reveal_type(random_st.randint(0, 256, dtype="u1")) # E: int -reveal_type(random_st.randint(I_u1_high_open, dtype="u1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(I_u1_low, I_u1_high_open, dtype="u1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(0, I_u1_high_open, dtype="u1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] +reveal_type(random_st.randint(I_u1_high_open, dtype="u1")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(random_st.randint(I_u1_low, I_u1_high_open, dtype="u1")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(random_st.randint(0, I_u1_high_open, dtype="u1")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] reveal_type(random_st.randint(256, dtype="uint8")) # E: int reveal_type(random_st.randint(0, 256, dtype="uint8")) # E: int -reveal_type(random_st.randint(I_u1_high_open, dtype="uint8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(I_u1_low, I_u1_high_open, dtype="uint8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(0, I_u1_high_open, dtype="uint8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] +reveal_type(random_st.randint(I_u1_high_open, dtype="uint8")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(random_st.randint(I_u1_low, I_u1_high_open, dtype="uint8")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(random_st.randint(0, I_u1_high_open, dtype="uint8")) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] reveal_type(random_st.randint(256, dtype=np.uint8)) # E: int reveal_type(random_st.randint(0, 256, dtype=np.uint8)) # E: int -reveal_type(random_st.randint(I_u1_high_open, dtype=np.uint8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(I_u1_low, I_u1_high_open, dtype=np.uint8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(0, I_u1_high_open, dtype=np.uint8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._8Bit]]] +reveal_type(random_st.randint(I_u1_high_open, dtype=np.uint8)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(random_st.randint(I_u1_low, I_u1_high_open, dtype=np.uint8)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] +reveal_type(random_st.randint(0, I_u1_high_open, dtype=np.uint8)) # E: ndarray[Any, dtype[unsignedinteger[typing._8Bit]]] reveal_type(random_st.randint(65536, dtype="u2")) # E: int reveal_type(random_st.randint(0, 65536, dtype="u2")) # E: int -reveal_type(random_st.randint(I_u2_high_open, dtype="u2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(I_u2_low, I_u2_high_open, dtype="u2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(0, I_u2_high_open, dtype="u2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] +reveal_type(random_st.randint(I_u2_high_open, dtype="u2")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(random_st.randint(I_u2_low, I_u2_high_open, dtype="u2")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(random_st.randint(0, I_u2_high_open, dtype="u2")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] reveal_type(random_st.randint(65536, dtype="uint16")) # E: int reveal_type(random_st.randint(0, 65536, dtype="uint16")) # E: int -reveal_type(random_st.randint(I_u2_high_open, dtype="uint16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(I_u2_low, I_u2_high_open, dtype="uint16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(0, I_u2_high_open, dtype="uint16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] +reveal_type(random_st.randint(I_u2_high_open, dtype="uint16")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(random_st.randint(I_u2_low, I_u2_high_open, dtype="uint16")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(random_st.randint(0, I_u2_high_open, dtype="uint16")) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] reveal_type(random_st.randint(65536, dtype=np.uint16)) # E: int reveal_type(random_st.randint(0, 65536, dtype=np.uint16)) # E: int -reveal_type(random_st.randint(I_u2_high_open, dtype=np.uint16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(I_u2_low, I_u2_high_open, dtype=np.uint16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(0, I_u2_high_open, dtype=np.uint16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._16Bit]]] +reveal_type(random_st.randint(I_u2_high_open, dtype=np.uint16)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(random_st.randint(I_u2_low, I_u2_high_open, dtype=np.uint16)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] +reveal_type(random_st.randint(0, I_u2_high_open, dtype=np.uint16)) # E: ndarray[Any, dtype[unsignedinteger[typing._16Bit]]] reveal_type(random_st.randint(4294967296, dtype="u4")) # E: int reveal_type(random_st.randint(0, 4294967296, dtype="u4")) # E: int -reveal_type(random_st.randint(I_u4_high_open, dtype="u4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(I_u4_low, I_u4_high_open, dtype="u4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(0, I_u4_high_open, dtype="u4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] +reveal_type(random_st.randint(I_u4_high_open, dtype="u4")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(random_st.randint(I_u4_low, I_u4_high_open, dtype="u4")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(random_st.randint(0, I_u4_high_open, dtype="u4")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] reveal_type(random_st.randint(4294967296, dtype="uint32")) # E: int reveal_type(random_st.randint(0, 4294967296, dtype="uint32")) # E: int -reveal_type(random_st.randint(I_u4_high_open, dtype="uint32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(I_u4_low, I_u4_high_open, dtype="uint32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(0, I_u4_high_open, dtype="uint32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] +reveal_type(random_st.randint(I_u4_high_open, dtype="uint32")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(random_st.randint(I_u4_low, I_u4_high_open, dtype="uint32")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(random_st.randint(0, I_u4_high_open, dtype="uint32")) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] reveal_type(random_st.randint(4294967296, dtype=np.uint32)) # E: int reveal_type(random_st.randint(0, 4294967296, dtype=np.uint32)) # E: int -reveal_type(random_st.randint(I_u4_high_open, dtype=np.uint32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(I_u4_low, I_u4_high_open, dtype=np.uint32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(0, I_u4_high_open, dtype=np.uint32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]] +reveal_type(random_st.randint(I_u4_high_open, dtype=np.uint32)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(random_st.randint(I_u4_low, I_u4_high_open, dtype=np.uint32)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] +reveal_type(random_st.randint(0, I_u4_high_open, dtype=np.uint32)) # E: ndarray[Any, dtype[unsignedinteger[typing._32Bit]]] reveal_type(random_st.randint(4294967296, dtype=np.uint)) # E: int reveal_type(random_st.randint(0, 4294967296, dtype=np.uint)) # E: int -reveal_type(random_st.randint(I_u4_high_open, dtype=np.uint)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] -reveal_type(random_st.randint(I_u4_low, I_u4_high_open, dtype=np.uint)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] -reveal_type(random_st.randint(0, I_u4_high_open, dtype=np.uint)) # E: numpy.ndarray[Any, numpy.dtype[{uint}]] +reveal_type(random_st.randint(I_u4_high_open, dtype=np.uint)) # E: ndarray[Any, dtype[{uint}]] +reveal_type(random_st.randint(I_u4_low, I_u4_high_open, dtype=np.uint)) # E: ndarray[Any, dtype[{uint}]] +reveal_type(random_st.randint(0, I_u4_high_open, dtype=np.uint)) # E: ndarray[Any, dtype[{uint}]] reveal_type(random_st.randint(18446744073709551616, dtype="u8")) # E: int reveal_type(random_st.randint(0, 18446744073709551616, dtype="u8")) # E: int -reveal_type(random_st.randint(I_u8_high_open, dtype="u8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(I_u8_low, I_u8_high_open, dtype="u8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(0, I_u8_high_open, dtype="u8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(random_st.randint(I_u8_high_open, dtype="u8")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(random_st.randint(I_u8_low, I_u8_high_open, dtype="u8")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(random_st.randint(0, I_u8_high_open, dtype="u8")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] reveal_type(random_st.randint(18446744073709551616, dtype="uint64")) # E: int reveal_type(random_st.randint(0, 18446744073709551616, dtype="uint64")) # E: int -reveal_type(random_st.randint(I_u8_high_open, dtype="uint64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(I_u8_low, I_u8_high_open, dtype="uint64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(0, I_u8_high_open, dtype="uint64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(random_st.randint(I_u8_high_open, dtype="uint64")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(random_st.randint(I_u8_low, I_u8_high_open, dtype="uint64")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(random_st.randint(0, I_u8_high_open, dtype="uint64")) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] reveal_type(random_st.randint(18446744073709551616, dtype=np.uint64)) # E: int reveal_type(random_st.randint(0, 18446744073709551616, dtype=np.uint64)) # E: int -reveal_type(random_st.randint(I_u8_high_open, dtype=np.uint64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(I_u8_low, I_u8_high_open, dtype=np.uint64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(0, I_u8_high_open, dtype=np.uint64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._64Bit]]] +reveal_type(random_st.randint(I_u8_high_open, dtype=np.uint64)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(random_st.randint(I_u8_low, I_u8_high_open, dtype=np.uint64)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] +reveal_type(random_st.randint(0, I_u8_high_open, dtype=np.uint64)) # E: ndarray[Any, dtype[unsignedinteger[typing._64Bit]]] reveal_type(random_st.randint(128, dtype="i1")) # E: int reveal_type(random_st.randint(-128, 128, dtype="i1")) # E: int -reveal_type(random_st.randint(I_i1_high_open, dtype="i1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(I_i1_low, I_i1_high_open, dtype="i1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(-128, I_i1_high_open, dtype="i1")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] +reveal_type(random_st.randint(I_i1_high_open, dtype="i1")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(random_st.randint(I_i1_low, I_i1_high_open, dtype="i1")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(random_st.randint(-128, I_i1_high_open, dtype="i1")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] reveal_type(random_st.randint(128, dtype="int8")) # E: int reveal_type(random_st.randint(-128, 128, dtype="int8")) # E: int -reveal_type(random_st.randint(I_i1_high_open, dtype="int8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(I_i1_low, I_i1_high_open, dtype="int8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(-128, I_i1_high_open, dtype="int8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] +reveal_type(random_st.randint(I_i1_high_open, dtype="int8")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(random_st.randint(I_i1_low, I_i1_high_open, dtype="int8")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(random_st.randint(-128, I_i1_high_open, dtype="int8")) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] reveal_type(random_st.randint(128, dtype=np.int8)) # E: int reveal_type(random_st.randint(-128, 128, dtype=np.int8)) # E: int -reveal_type(random_st.randint(I_i1_high_open, dtype=np.int8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(I_i1_low, I_i1_high_open, dtype=np.int8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] -reveal_type(random_st.randint(-128, I_i1_high_open, dtype=np.int8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._8Bit]]] +reveal_type(random_st.randint(I_i1_high_open, dtype=np.int8)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(random_st.randint(I_i1_low, I_i1_high_open, dtype=np.int8)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] +reveal_type(random_st.randint(-128, I_i1_high_open, dtype=np.int8)) # E: ndarray[Any, dtype[signedinteger[typing._8Bit]]] reveal_type(random_st.randint(32768, dtype="i2")) # E: int reveal_type(random_st.randint(-32768, 32768, dtype="i2")) # E: int -reveal_type(random_st.randint(I_i2_high_open, dtype="i2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(I_i2_low, I_i2_high_open, dtype="i2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(-32768, I_i2_high_open, dtype="i2")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] +reveal_type(random_st.randint(I_i2_high_open, dtype="i2")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(random_st.randint(I_i2_low, I_i2_high_open, dtype="i2")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(random_st.randint(-32768, I_i2_high_open, dtype="i2")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] reveal_type(random_st.randint(32768, dtype="int16")) # E: int reveal_type(random_st.randint(-32768, 32768, dtype="int16")) # E: int -reveal_type(random_st.randint(I_i2_high_open, dtype="int16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(I_i2_low, I_i2_high_open, dtype="int16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(-32768, I_i2_high_open, dtype="int16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] +reveal_type(random_st.randint(I_i2_high_open, dtype="int16")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(random_st.randint(I_i2_low, I_i2_high_open, dtype="int16")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(random_st.randint(-32768, I_i2_high_open, dtype="int16")) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] reveal_type(random_st.randint(32768, dtype=np.int16)) # E: int reveal_type(random_st.randint(-32768, 32768, dtype=np.int16)) # E: int -reveal_type(random_st.randint(I_i2_high_open, dtype=np.int16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(I_i2_low, I_i2_high_open, dtype=np.int16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] -reveal_type(random_st.randint(-32768, I_i2_high_open, dtype=np.int16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._16Bit]]] +reveal_type(random_st.randint(I_i2_high_open, dtype=np.int16)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(random_st.randint(I_i2_low, I_i2_high_open, dtype=np.int16)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] +reveal_type(random_st.randint(-32768, I_i2_high_open, dtype=np.int16)) # E: ndarray[Any, dtype[signedinteger[typing._16Bit]]] reveal_type(random_st.randint(2147483648, dtype="i4")) # E: int reveal_type(random_st.randint(-2147483648, 2147483648, dtype="i4")) # E: int -reveal_type(random_st.randint(I_i4_high_open, dtype="i4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(I_i4_low, I_i4_high_open, dtype="i4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(-2147483648, I_i4_high_open, dtype="i4")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] +reveal_type(random_st.randint(I_i4_high_open, dtype="i4")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(random_st.randint(I_i4_low, I_i4_high_open, dtype="i4")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(random_st.randint(-2147483648, I_i4_high_open, dtype="i4")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] reveal_type(random_st.randint(2147483648, dtype="int32")) # E: int reveal_type(random_st.randint(-2147483648, 2147483648, dtype="int32")) # E: int -reveal_type(random_st.randint(I_i4_high_open, dtype="int32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(I_i4_low, I_i4_high_open, dtype="int32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(-2147483648, I_i4_high_open, dtype="int32")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] +reveal_type(random_st.randint(I_i4_high_open, dtype="int32")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(random_st.randint(I_i4_low, I_i4_high_open, dtype="int32")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(random_st.randint(-2147483648, I_i4_high_open, dtype="int32")) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] reveal_type(random_st.randint(2147483648, dtype=np.int32)) # E: int reveal_type(random_st.randint(-2147483648, 2147483648, dtype=np.int32)) # E: int -reveal_type(random_st.randint(I_i4_high_open, dtype=np.int32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(I_i4_low, I_i4_high_open, dtype=np.int32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] -reveal_type(random_st.randint(-2147483648, I_i4_high_open, dtype=np.int32)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]] +reveal_type(random_st.randint(I_i4_high_open, dtype=np.int32)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(random_st.randint(I_i4_low, I_i4_high_open, dtype=np.int32)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] +reveal_type(random_st.randint(-2147483648, I_i4_high_open, dtype=np.int32)) # E: ndarray[Any, dtype[signedinteger[typing._32Bit]]] reveal_type(random_st.randint(2147483648, dtype=np.int_)) # E: int reveal_type(random_st.randint(-2147483648, 2147483648, dtype=np.int_)) # E: int -reveal_type(random_st.randint(I_i4_high_open, dtype=np.int_)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.randint(I_i4_low, I_i4_high_open, dtype=np.int_)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.randint(-2147483648, I_i4_high_open, dtype=np.int_)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.randint(I_i4_high_open, dtype=np.int_)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.randint(I_i4_low, I_i4_high_open, dtype=np.int_)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.randint(-2147483648, I_i4_high_open, dtype=np.int_)) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.randint(9223372036854775808, dtype="i8")) # E: int reveal_type(random_st.randint(-9223372036854775808, 9223372036854775808, dtype="i8")) # E: int -reveal_type(random_st.randint(I_i8_high_open, dtype="i8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(I_i8_low, I_i8_high_open, dtype="i8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(-9223372036854775808, I_i8_high_open, dtype="i8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(random_st.randint(I_i8_high_open, dtype="i8")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(random_st.randint(I_i8_low, I_i8_high_open, dtype="i8")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(random_st.randint(-9223372036854775808, I_i8_high_open, dtype="i8")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(random_st.randint(9223372036854775808, dtype="int64")) # E: int reveal_type(random_st.randint(-9223372036854775808, 9223372036854775808, dtype="int64")) # E: int -reveal_type(random_st.randint(I_i8_high_open, dtype="int64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(I_i8_low, I_i8_high_open, dtype="int64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(-9223372036854775808, I_i8_high_open, dtype="int64")) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(random_st.randint(I_i8_high_open, dtype="int64")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(random_st.randint(I_i8_low, I_i8_high_open, dtype="int64")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(random_st.randint(-9223372036854775808, I_i8_high_open, dtype="int64")) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(random_st.randint(9223372036854775808, dtype=np.int64)) # E: int reveal_type(random_st.randint(-9223372036854775808, 9223372036854775808, dtype=np.int64)) # E: int -reveal_type(random_st.randint(I_i8_high_open, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(I_i8_low, I_i8_high_open, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] -reveal_type(random_st.randint(-9223372036854775808, I_i8_high_open, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]] +reveal_type(random_st.randint(I_i8_high_open, dtype=np.int64)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(random_st.randint(I_i8_low, I_i8_high_open, dtype=np.int64)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] +reveal_type(random_st.randint(-9223372036854775808, I_i8_high_open, dtype=np.int64)) # E: ndarray[Any, dtype[signedinteger[typing._64Bit]]] reveal_type(random_st._bit_generator) # E: BitGenerator reveal_type(random_st.bytes(2)) # E: bytes reveal_type(random_st.choice(5)) # E: int -reveal_type(random_st.choice(5, 3)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.choice(5, 3, replace=True)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.choice(5, 3, p=[1 / 5] * 5)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.choice(5, 3, p=[1 / 5] * 5, replace=False)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.choice(5, 3)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.choice(5, 3, replace=True)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.choice(5, 3, p=[1 / 5] * 5)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.choice(5, 3, p=[1 / 5] * 5, replace=False)) # E: ndarray[Any, dtype[{int_}]] reveal_type(random_st.choice(["pooh", "rabbit", "piglet", "Christopher"])) # E: Any -reveal_type(random_st.choice(["pooh", "rabbit", "piglet", "Christopher"], 3)) # E: numpy.ndarray[Any, Any] -reveal_type(random_st.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, p=[1 / 4] * 4)) # E: numpy.ndarray[Any, Any] -reveal_type(random_st.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, replace=True)) # E: numpy.ndarray[Any, Any] -reveal_type(random_st.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, replace=False, p=np.array([1 / 8, 1 / 8, 1 / 2, 1 / 4]))) # E: numpy.ndarray[Any, Any] +reveal_type(random_st.choice(["pooh", "rabbit", "piglet", "Christopher"], 3)) # E: ndarray[Any, Any] +reveal_type(random_st.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, p=[1 / 4] * 4)) # E: ndarray[Any, Any] +reveal_type(random_st.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, replace=True)) # E: ndarray[Any, Any] +reveal_type(random_st.choice(["pooh", "rabbit", "piglet", "Christopher"], 3, replace=False, p=np.array([1 / 8, 1 / 8, 1 / 2, 1 / 4]))) # E: ndarray[Any, Any] -reveal_type(random_st.dirichlet([0.5, 0.5])) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.dirichlet(np.array([0.5, 0.5]))) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.dirichlet(np.array([0.5, 0.5]), size=3)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.dirichlet([0.5, 0.5])) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.dirichlet(np.array([0.5, 0.5]))) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.dirichlet(np.array([0.5, 0.5]), size=3)) # E: ndarray[Any, dtype[floating[typing._64Bit]] -reveal_type(random_st.multinomial(20, [1 / 6.0] * 6)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.multinomial(20, np.array([0.5, 0.5]))) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.multinomial(20, [1 / 6.0] * 6, size=2)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.multinomial(20, [1 / 6.0] * 6)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.multinomial(20, np.array([0.5, 0.5]))) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.multinomial(20, [1 / 6.0] * 6, size=2)) # E: ndarray[Any, dtype[{int_}]] -reveal_type(random_st.multivariate_normal([0.0], [[1.0]])) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.multivariate_normal([0.0], np.array([[1.0]]))) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.multivariate_normal(np.array([0.0]), [[1.0]])) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.multivariate_normal([0.0], np.array([[1.0]]))) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.multivariate_normal([0.0], [[1.0]])) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.multivariate_normal([0.0], np.array([[1.0]]))) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.multivariate_normal(np.array([0.0]), [[1.0]])) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.multivariate_normal([0.0], np.array([[1.0]]))) # E: ndarray[Any, dtype[floating[typing._64Bit]] -reveal_type(random_st.permutation(10)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.permutation([1, 2, 3, 4])) # E: numpy.ndarray[Any, Any] -reveal_type(random_st.permutation(np.array([1, 2, 3, 4]))) # E: numpy.ndarray[Any, Any] -reveal_type(random_st.permutation(D_2D)) # E: numpy.ndarray[Any, Any] +reveal_type(random_st.permutation(10)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.permutation([1, 2, 3, 4])) # E: ndarray[Any, Any] +reveal_type(random_st.permutation(np.array([1, 2, 3, 4]))) # E: ndarray[Any, Any] +reveal_type(random_st.permutation(D_2D)) # E: ndarray[Any, Any] reveal_type(random_st.shuffle(np.arange(10))) # E: None reveal_type(random_st.shuffle([1, 2, 3, 4, 5])) # E: None @@ -1521,19 +1521,19 @@ reveal_type(random_st.seed([0, 1])) # E: None random_st_get_state = random_st.get_state() reveal_type(random_st_state) # E: builtins.dict[builtins.str, Any] random_st_get_state_legacy = random_st.get_state(legacy=True) -reveal_type(random_st_get_state_legacy) # E: Union[builtins.dict[builtins.str, Any], Tuple[builtins.str, numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[numpy.typing._32Bit]]], builtins.int, builtins.int, builtins.float]] +reveal_type(random_st_get_state_legacy) # E: Union[builtins.dict[builtins.str, Any], Tuple[builtins.str, ndarray[Any, dtype[unsignedinteger[typing._32Bit]]], builtins.int, builtins.int, builtins.float]] reveal_type(random_st.set_state(random_st_get_state)) # E: None reveal_type(random_st.rand()) # E: float -reveal_type(random_st.rand(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.rand(1, 2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.rand(1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.rand(1, 2)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.randn()) # E: float -reveal_type(random_st.randn(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.randn(1, 2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.randn(1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.randn(1, 2)) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.random_sample()) # E: float -reveal_type(random_st.random_sample(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] -reveal_type(random_st.random_sample(size=(1, 2))) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy.typing._64Bit]] +reveal_type(random_st.random_sample(1)) # E: ndarray[Any, dtype[floating[typing._64Bit]] +reveal_type(random_st.random_sample(size=(1, 2))) # E: ndarray[Any, dtype[floating[typing._64Bit]] reveal_type(random_st.tomaxint()) # E: int -reveal_type(random_st.tomaxint(1)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -reveal_type(random_st.tomaxint((1,))) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(random_st.tomaxint(1)) # E: ndarray[Any, dtype[{int_}]] +reveal_type(random_st.tomaxint((1,))) # E: ndarray[Any, dtype[{int_}]] diff --git a/numpy/typing/tests/data/reveal/rec.pyi b/numpy/typing/tests/data/reveal/rec.pyi index 2fa8cc7b9..bf51c82a3 100644 --- a/numpy/typing/tests/data/reveal/rec.pyi +++ b/numpy/typing/tests/data/reveal/rec.pyi @@ -12,13 +12,13 @@ format_parser: np.format_parser record: np.record file_obj: io.BufferedIOBase -reveal_type(np.format_parser( # E: numpy.format_parser +reveal_type(np.format_parser( # E: format_parser formats=[np.float64, np.int64, np.bool_], names=["f8", "i8", "?"], titles=None, aligned=True, )) -reveal_type(format_parser.dtype) # E: numpy.dtype[numpy.void] +reveal_type(format_parser.dtype) # E: dtype[void] reveal_type(record.field_a) # E: Any reveal_type(record.field_b) # E: Any @@ -34,72 +34,72 @@ reveal_type(REC_AR_V.field("field_a", AR_i8)) # E: None reveal_type(REC_AR_V["field_a"]) # E: Any reveal_type(REC_AR_V.field_a) # E: Any -reveal_type(np.recarray( # numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.recarray( # recarray[Any, dtype[record]] shape=(10, 5), formats=[np.float64, np.int64, np.bool_], order="K", byteorder="|", )) -reveal_type(np.recarray( # numpy.recarray[Any, numpy.dtype[Any]] +reveal_type(np.recarray( # recarray[Any, dtype[Any]] shape=(10, 5), dtype=[("f8", np.float64), ("i8", np.int64)], strides=(5, 5), )) -reveal_type(np.rec.fromarrays( # numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.rec.fromarrays( # recarray[Any, dtype[record]] AR_LIST, )) -reveal_type(np.rec.fromarrays( # numpy.recarray[Any, numpy.dtype[Any]] +reveal_type(np.rec.fromarrays( # recarray[Any, dtype[Any]] AR_LIST, dtype=np.int64, )) -reveal_type(np.rec.fromarrays( # numpy.recarray[Any, numpy.dtype[Any]] +reveal_type(np.rec.fromarrays( # recarray[Any, dtype[Any]] AR_LIST, formats=[np.int64, np.float64], names=["i8", "f8"] )) -reveal_type(np.rec.fromrecords( # numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.rec.fromrecords( # recarray[Any, dtype[record]] (1, 1.5), )) -reveal_type(np.rec.fromrecords( # numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.rec.fromrecords( # recarray[Any, dtype[record]] [(1, 1.5)], dtype=[("i8", np.int64), ("f8", np.float64)], )) -reveal_type(np.rec.fromrecords( # numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.rec.fromrecords( # recarray[Any, dtype[record]] REC_AR_V, formats=[np.int64, np.float64], names=["i8", "f8"] )) -reveal_type(np.rec.fromstring( # numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.rec.fromstring( # recarray[Any, dtype[record]] b"(1, 1.5)", dtype=[("i8", np.int64), ("f8", np.float64)], )) -reveal_type(np.rec.fromstring( # numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.rec.fromstring( # recarray[Any, dtype[record]] REC_AR_V, formats=[np.int64, np.float64], names=["i8", "f8"] )) -reveal_type(np.rec.fromfile( # numpy.recarray[Any, numpy.dtype[Any]] +reveal_type(np.rec.fromfile( # recarray[Any, dtype[Any]] "test_file.txt", dtype=[("i8", np.int64), ("f8", np.float64)], )) -reveal_type(np.rec.fromfile( # numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.rec.fromfile( # recarray[Any, dtype[record]] file_obj, formats=[np.int64, np.float64], names=["i8", "f8"] )) -reveal_type(np.rec.array( # numpy.recarray[Any, numpy.dtype[{int64}]] +reveal_type(np.rec.array( # recarray[Any, dtype[{int64}]] AR_i8, )) -reveal_type(np.rec.array( # numpy.recarray[Any, numpy.dtype[Any]] +reveal_type(np.rec.array( # recarray[Any, dtype[Any]] [(1, 1.5)], dtype=[("i8", np.int64), ("f8", np.float64)], )) -reveal_type(np.rec.array( # numpy.recarray[Any, numpy.dtype[numpy.record]] +reveal_type(np.rec.array( # recarray[Any, dtype[record]] [(1, 1.5)], formats=[np.int64, np.float64], names=["i8", "f8"] diff --git a/numpy/typing/tests/data/reveal/scalars.pyi b/numpy/typing/tests/data/reveal/scalars.pyi index a95f8f6f2..383e40ef0 100644 --- a/numpy/typing/tests/data/reveal/scalars.pyi +++ b/numpy/typing/tests/data/reveal/scalars.pyi @@ -29,27 +29,27 @@ reveal_type(c8.squeeze()) # E: {complex64} reveal_type(c8.byteswap()) # E: {complex64} reveal_type(c8.transpose()) # E: {complex64} -reveal_type(c8.dtype) # E: numpy.dtype[{complex64}] +reveal_type(c8.dtype) # E: dtype[{complex64}] reveal_type(c8.real) # E: {float32} reveal_type(c16.imag) # E: {float64} -reveal_type(np.unicode_('foo')) # E: numpy.str_ -reveal_type(np.str0('foo')) # E: numpy.str_ +reveal_type(np.unicode_('foo')) # E: str_ +reveal_type(np.str0('foo')) # E: str_ reveal_type(V[0]) # E: Any reveal_type(V["field1"]) # E: Any -reveal_type(V[["field1", "field2"]]) # E: numpy.void +reveal_type(V[["field1", "field2"]]) # E: void V[0] = 5 # Aliases -reveal_type(np.unicode_()) # E: numpy.str_ -reveal_type(np.str0()) # E: numpy.str_ -reveal_type(np.bool8()) # E: numpy.bool_ -reveal_type(np.bytes0()) # E: numpy.bytes_ -reveal_type(np.string_()) # E: numpy.bytes_ -reveal_type(np.object0()) # E: numpy.object_ -reveal_type(np.void0(0)) # E: numpy.void +reveal_type(np.unicode_()) # E: str_ +reveal_type(np.str0()) # E: str_ +reveal_type(np.bool8()) # E: bool_ +reveal_type(np.bytes0()) # E: bytes_ +reveal_type(np.string_()) # E: bytes_ +reveal_type(np.object0()) # E: object_ +reveal_type(np.void0(0)) # E: void reveal_type(np.byte()) # E: {byte} reveal_type(np.short()) # E: {short} @@ -99,29 +99,29 @@ reveal_type(c16.tolist()) # E: complex reveal_type(U.tolist()) # E: str reveal_type(S.tolist()) # E: bytes -reveal_type(b.ravel()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(i8.ravel()) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(u8.ravel()) # E: numpy.ndarray[Any, numpy.dtype[{uint64}]] -reveal_type(f8.ravel()) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(c16.ravel()) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(U.ravel()) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(S.ravel()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] - -reveal_type(b.flatten()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(i8.flatten()) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(u8.flatten()) # E: numpy.ndarray[Any, numpy.dtype[{uint64}]] -reveal_type(f8.flatten()) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(c16.flatten()) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(U.flatten()) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(S.flatten()) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] - -reveal_type(b.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(i8.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(u8.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[{uint64}]] -reveal_type(f8.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(c16.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] -reveal_type(U.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(S.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] +reveal_type(b.ravel()) # E: ndarray[Any, dtype[bool_]] +reveal_type(i8.ravel()) # E: ndarray[Any, dtype[{int64}]] +reveal_type(u8.ravel()) # E: ndarray[Any, dtype[{uint64}]] +reveal_type(f8.ravel()) # E: ndarray[Any, dtype[{float64}]] +reveal_type(c16.ravel()) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(U.ravel()) # E: ndarray[Any, dtype[str_]] +reveal_type(S.ravel()) # E: ndarray[Any, dtype[bytes_]] + +reveal_type(b.flatten()) # E: ndarray[Any, dtype[bool_]] +reveal_type(i8.flatten()) # E: ndarray[Any, dtype[{int64}]] +reveal_type(u8.flatten()) # E: ndarray[Any, dtype[{uint64}]] +reveal_type(f8.flatten()) # E: ndarray[Any, dtype[{float64}]] +reveal_type(c16.flatten()) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(U.flatten()) # E: ndarray[Any, dtype[str_]] +reveal_type(S.flatten()) # E: ndarray[Any, dtype[bytes_]] + +reveal_type(b.reshape(1)) # E: ndarray[Any, dtype[bool_]] +reveal_type(i8.reshape(1)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(u8.reshape(1)) # E: ndarray[Any, dtype[{uint64}]] +reveal_type(f8.reshape(1)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(c16.reshape(1)) # E: ndarray[Any, dtype[{complex128}]] +reveal_type(U.reshape(1)) # E: ndarray[Any, dtype[str_]] +reveal_type(S.reshape(1)) # E: ndarray[Any, dtype[bytes_]] reveal_type(i8.astype(float)) # E: Any reveal_type(i8.astype(np.float64)) # E: {float64} @@ -149,7 +149,7 @@ reveal_type(i8.numerator) # E: {int64} reveal_type(i8.denominator) # E: Literal[1] reveal_type(u8.numerator) # E: {uint64} reveal_type(u8.denominator) # E: Literal[1] -reveal_type(m.numerator) # E: numpy.timedelta64 +reveal_type(m.numerator) # E: timedelta64 reveal_type(m.denominator) # E: Literal[1] reveal_type(round(i8)) # E: int diff --git a/numpy/typing/tests/data/reveal/shape_base.pyi b/numpy/typing/tests/data/reveal/shape_base.pyi index 57633defb..f13678c3a 100644 --- a/numpy/typing/tests/data/reveal/shape_base.pyi +++ b/numpy/typing/tests/data/reveal/shape_base.pyi @@ -11,47 +11,47 @@ AR_f8: NDArray[np.float64] AR_LIKE_f8: List[float] -reveal_type(np.take_along_axis(AR_f8, AR_i8, axis=1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.take_along_axis(f8, AR_i8, axis=None)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.take_along_axis(AR_f8, AR_i8, axis=1)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.take_along_axis(f8, AR_i8, axis=None)) # E: ndarray[Any, dtype[{float64}]] reveal_type(np.put_along_axis(AR_f8, AR_i8, "1.0", axis=1)) # E: None -reveal_type(np.expand_dims(AR_i8, 2)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.expand_dims(AR_LIKE_f8, 2)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.expand_dims(AR_i8, 2)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.expand_dims(AR_LIKE_f8, 2)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.column_stack([AR_i8])) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.column_stack([AR_LIKE_f8])) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.column_stack([AR_i8])) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.column_stack([AR_LIKE_f8])) # E: ndarray[Any, dtype[Any]] -reveal_type(np.dstack([AR_i8])) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.dstack([AR_LIKE_f8])) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.dstack([AR_i8])) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.dstack([AR_LIKE_f8])) # E: ndarray[Any, dtype[Any]] -reveal_type(np.row_stack([AR_i8])) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.row_stack([AR_LIKE_f8])) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.row_stack([AR_i8])) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.row_stack([AR_LIKE_f8])) # E: ndarray[Any, dtype[Any]] -reveal_type(np.array_split(AR_i8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[{int64}]]] -reveal_type(np.array_split(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.array_split(AR_i8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[{int64}]]] +reveal_type(np.array_split(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[Any]]] -reveal_type(np.split(AR_i8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[{int64}]]] -reveal_type(np.split(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.split(AR_i8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[{int64}]]] +reveal_type(np.split(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[Any]]] -reveal_type(np.hsplit(AR_i8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[{int64}]]] -reveal_type(np.hsplit(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.hsplit(AR_i8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[{int64}]]] +reveal_type(np.hsplit(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[Any]]] -reveal_type(np.vsplit(AR_i8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[{int64}]]] -reveal_type(np.vsplit(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.vsplit(AR_i8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[{int64}]]] +reveal_type(np.vsplit(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[Any]]] -reveal_type(np.dsplit(AR_i8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[{int64}]]] -reveal_type(np.dsplit(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.dsplit(AR_i8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[{int64}]]] +reveal_type(np.dsplit(AR_LIKE_f8, [3, 5, 6, 10])) # E: list[ndarray[Any, dtype[Any]]] -reveal_type(np.lib.shape_base.get_array_prepare(AR_i8)) # E: numpy.lib.shape_base._ArrayPrepare -reveal_type(np.lib.shape_base.get_array_prepare(AR_i8, 1)) # E: Union[None, numpy.lib.shape_base._ArrayPrepare] +reveal_type(np.lib.shape_base.get_array_prepare(AR_i8)) # E: lib.shape_base._ArrayPrepare +reveal_type(np.lib.shape_base.get_array_prepare(AR_i8, 1)) # E: Union[None, lib.shape_base._ArrayPrepare] -reveal_type(np.get_array_wrap(AR_i8)) # E: numpy.lib.shape_base._ArrayWrap -reveal_type(np.get_array_wrap(AR_i8, 1)) # E: Union[None, numpy.lib.shape_base._ArrayWrap] +reveal_type(np.get_array_wrap(AR_i8)) # E: lib.shape_base._ArrayWrap +reveal_type(np.get_array_wrap(AR_i8, 1)) # E: Union[None, lib.shape_base._ArrayWrap] -reveal_type(np.kron(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.kron(AR_b, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.kron(AR_f8, AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.kron(AR_b, AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.kron(AR_b, AR_i8)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.kron(AR_f8, AR_f8)) # E: ndarray[Any, dtype[floating[Any]]] -reveal_type(np.tile(AR_i8, 5)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.tile(AR_LIKE_f8, [2, 2])) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.tile(AR_i8, 5)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.tile(AR_LIKE_f8, [2, 2])) # E: ndarray[Any, dtype[Any]] diff --git a/numpy/typing/tests/data/reveal/stride_tricks.pyi b/numpy/typing/tests/data/reveal/stride_tricks.pyi index 152d9cea6..0d6dcd388 100644 --- a/numpy/typing/tests/data/reveal/stride_tricks.pyi +++ b/numpy/typing/tests/data/reveal/stride_tricks.pyi @@ -6,23 +6,23 @@ AR_f8: npt.NDArray[np.float64] AR_LIKE_f: List[float] interface_dict: Dict[str, Any] -reveal_type(np.lib.stride_tricks.DummyArray(interface_dict)) # E: numpy.lib.stride_tricks.DummyArray +reveal_type(np.lib.stride_tricks.DummyArray(interface_dict)) # E: lib.stride_tricks.DummyArray -reveal_type(np.lib.stride_tricks.as_strided(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.lib.stride_tricks.as_strided(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.lib.stride_tricks.as_strided(AR_f8, strides=(1, 5))) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.lib.stride_tricks.as_strided(AR_f8, shape=[9, 20])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.lib.stride_tricks.as_strided(AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.lib.stride_tricks.as_strided(AR_LIKE_f)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.lib.stride_tricks.as_strided(AR_f8, strides=(1, 5))) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.lib.stride_tricks.as_strided(AR_f8, shape=[9, 20])) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.lib.stride_tricks.sliding_window_view(AR_f8, 5)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.lib.stride_tricks.sliding_window_view(AR_LIKE_f, (1, 5))) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.lib.stride_tricks.sliding_window_view(AR_f8, [9], axis=1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.lib.stride_tricks.sliding_window_view(AR_f8, 5)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.lib.stride_tricks.sliding_window_view(AR_LIKE_f, (1, 5))) # E: ndarray[Any, dtype[Any]] +reveal_type(np.lib.stride_tricks.sliding_window_view(AR_f8, [9], axis=1)) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.broadcast_to(AR_f8, 5)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.broadcast_to(AR_LIKE_f, (1, 5))) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.broadcast_to(AR_f8, [4, 6], subok=True)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.broadcast_to(AR_f8, 5)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.broadcast_to(AR_LIKE_f, (1, 5))) # E: ndarray[Any, dtype[Any]] +reveal_type(np.broadcast_to(AR_f8, [4, 6], subok=True)) # E: ndarray[Any, dtype[{float64}]] reveal_type(np.broadcast_shapes((1, 2), [3, 1], (3, 2))) # E: tuple[builtins.int] reveal_type(np.broadcast_shapes((6, 7), (5, 6, 1), 7, (5, 1, 7))) # E: tuple[builtins.int] -reveal_type(np.broadcast_arrays(AR_f8, AR_f8)) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] -reveal_type(np.broadcast_arrays(AR_f8, AR_LIKE_f)) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.broadcast_arrays(AR_f8, AR_f8)) # E: list[ndarray[Any, dtype[Any]]] +reveal_type(np.broadcast_arrays(AR_f8, AR_LIKE_f)) # E: list[ndarray[Any, dtype[Any]]] diff --git a/numpy/typing/tests/data/reveal/testing.pyi b/numpy/typing/tests/data/reveal/testing.pyi index 2b040ff60..9813dc723 100644 --- a/numpy/typing/tests/data/reveal/testing.pyi +++ b/numpy/typing/tests/data/reveal/testing.pyi @@ -147,8 +147,8 @@ reveal_type(np.testing.assert_allclose(AR_i8, AR_f8, verbose=False)) # E: None reveal_type(np.testing.assert_array_almost_equal_nulp(AR_i8, AR_f8, nulp=2)) # E: None -reveal_type(np.testing.assert_array_max_ulp(AR_i8, AR_f8, maxulp=2)) # E: numpy.ndarray[Any, numpy.dtype[Any]] -reveal_type(np.testing.assert_array_max_ulp(AR_i8, AR_f8, dtype=np.float32)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.testing.assert_array_max_ulp(AR_i8, AR_f8, maxulp=2)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.testing.assert_array_max_ulp(AR_i8, AR_f8, dtype=np.float32)) # E: ndarray[Any, dtype[Any]] reveal_type(np.testing.assert_warns(RuntimeWarning)) # E: _GeneratorContextManager[None] reveal_type(np.testing.assert_warns(RuntimeWarning, func3, 5)) # E: bool diff --git a/numpy/typing/tests/data/reveal/twodim_base.pyi b/numpy/typing/tests/data/reveal/twodim_base.pyi index b95fbc71e..0318c3cf1 100644 --- a/numpy/typing/tests/data/reveal/twodim_base.pyi +++ b/numpy/typing/tests/data/reveal/twodim_base.pyi @@ -23,50 +23,50 @@ AR_O: npt.NDArray[np.object_] AR_LIKE_b: List[bool] -reveal_type(np.fliplr(AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.fliplr(AR_LIKE_b)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.fliplr(AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.fliplr(AR_LIKE_b)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.flipud(AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.flipud(AR_LIKE_b)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.flipud(AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.flipud(AR_LIKE_b)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.eye(10)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.eye(10, M=20, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.eye(10, k=2, dtype=int)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.eye(10)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.eye(10, M=20, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.eye(10, k=2, dtype=int)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.diag(AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.diag(AR_LIKE_b, k=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.diag(AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.diag(AR_LIKE_b, k=0)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.diagflat(AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.diagflat(AR_LIKE_b, k=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.diagflat(AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.diagflat(AR_LIKE_b, k=0)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.tri(10)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.tri(10, M=20, dtype=np.int64)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]] -reveal_type(np.tri(10, k=2, dtype=int)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.tri(10)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.tri(10, M=20, dtype=np.int64)) # E: ndarray[Any, dtype[{int64}]] +reveal_type(np.tri(10, k=2, dtype=int)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.tril(AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.tril(AR_LIKE_b, k=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.tril(AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.tril(AR_LIKE_b, k=0)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.triu(AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.triu(AR_LIKE_b, k=0)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.triu(AR_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.triu(AR_LIKE_b, k=0)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.vander(AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.vander(AR_u)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.vander(AR_i, N=2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]] -reveal_type(np.vander(AR_f, increasing=True)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.vander(AR_c)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.vander(AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]] +reveal_type(np.vander(AR_b)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.vander(AR_u)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.vander(AR_i, N=2)) # E: ndarray[Any, dtype[signedinteger[Any]]] +reveal_type(np.vander(AR_f, increasing=True)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.vander(AR_c)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.vander(AR_O)) # E: ndarray[Any, dtype[object_]] -reveal_type(np.histogram2d(AR_i, AR_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.histogram2d(AR_f, AR_f)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]] -reveal_type(np.histogram2d(AR_f, AR_c, weights=AR_LIKE_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]] +reveal_type(np.histogram2d(AR_i, AR_b)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.histogram2d(AR_f, AR_f)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[floating[Any]]]] +reveal_type(np.histogram2d(AR_f, AR_c, weights=AR_LIKE_b)) # E: Tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[complexfloating[Any, Any]]], ndarray[Any, dtype[complexfloating[Any, Any]]]] -reveal_type(np.mask_indices(10, func1)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] -reveal_type(np.mask_indices(8, func2, "0")) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{intp}]], numpy.ndarray[Any, numpy.dtype[{intp}]]] +reveal_type(np.mask_indices(10, func1)) # E: Tuple[ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] +reveal_type(np.mask_indices(8, func2, "0")) # E: Tuple[ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]] -reveal_type(np.tril_indices(10)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{int_}]], numpy.ndarray[Any, numpy.dtype[{int_}]]] +reveal_type(np.tril_indices(10)) # E: Tuple[ndarray[Any, dtype[{int_}]], ndarray[Any, dtype[{int_}]]] -reveal_type(np.tril_indices_from(AR_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{int_}]], numpy.ndarray[Any, numpy.dtype[{int_}]]] +reveal_type(np.tril_indices_from(AR_b)) # E: Tuple[ndarray[Any, dtype[{int_}]], ndarray[Any, dtype[{int_}]]] -reveal_type(np.triu_indices(10)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{int_}]], numpy.ndarray[Any, numpy.dtype[{int_}]]] +reveal_type(np.triu_indices(10)) # E: Tuple[ndarray[Any, dtype[{int_}]], ndarray[Any, dtype[{int_}]]] -reveal_type(np.triu_indices_from(AR_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{int_}]], numpy.ndarray[Any, numpy.dtype[{int_}]]] +reveal_type(np.triu_indices_from(AR_b)) # E: Tuple[ndarray[Any, dtype[{int_}]], ndarray[Any, dtype[{int_}]]] diff --git a/numpy/typing/tests/data/reveal/type_check.pyi b/numpy/typing/tests/data/reveal/type_check.pyi index 416dd42a8..13d41d844 100644 --- a/numpy/typing/tests/data/reveal/type_check.pyi +++ b/numpy/typing/tests/data/reveal/type_check.pyi @@ -24,41 +24,41 @@ class ImagObj: reveal_type(np.mintypecode(["f8"], typeset="qfQF")) -reveal_type(np.asfarray(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.asfarray(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.asfarray(AR_f8, dtype="c16")) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]] -reveal_type(np.asfarray(AR_f8, dtype="i8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.asfarray(AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.asfarray(AR_LIKE_f)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.asfarray(AR_f8, dtype="c16")) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] +reveal_type(np.asfarray(AR_f8, dtype="i8")) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(np.real(RealObj())) # E: slice -reveal_type(np.real(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.real(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.real(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.real(AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.real(AR_c16)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.real(AR_LIKE_f)) # E: ndarray[Any, dtype[Any]] reveal_type(np.imag(ImagObj())) # E: slice -reveal_type(np.imag(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.imag(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.imag(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.imag(AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.imag(AR_c16)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.imag(AR_LIKE_f)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.iscomplex(f8)) # E: numpy.bool_ -reveal_type(np.iscomplex(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.iscomplex(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.iscomplex(f8)) # E: bool_ +reveal_type(np.iscomplex(AR_f8)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.iscomplex(AR_LIKE_f)) # E: ndarray[Any, dtype[bool_]] -reveal_type(np.isreal(f8)) # E: numpy.bool_ -reveal_type(np.isreal(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isreal(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] +reveal_type(np.isreal(f8)) # E: bool_ +reveal_type(np.isreal(AR_f8)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isreal(AR_LIKE_f)) # E: ndarray[Any, dtype[bool_]] reveal_type(np.iscomplexobj(f8)) # E: bool reveal_type(np.isrealobj(f8)) # E: bool reveal_type(np.nan_to_num(f8)) # E: {float64} reveal_type(np.nan_to_num(f, copy=True)) # E: Any -reveal_type(np.nan_to_num(AR_f8, nan=1.5)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.nan_to_num(AR_LIKE_f, posinf=9999)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.nan_to_num(AR_f8, nan=1.5)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.nan_to_num(AR_LIKE_f, posinf=9999)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.real_if_close(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] -reveal_type(np.real_if_close(AR_c16)) # E: Union[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{complex128}]]] -reveal_type(np.real_if_close(AR_c8)) # E: Union[numpy.ndarray[Any, numpy.dtype[{float32}]], numpy.ndarray[Any, numpy.dtype[{complex64}]]] -reveal_type(np.real_if_close(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.real_if_close(AR_f8)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.real_if_close(AR_c16)) # E: Union[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{complex128}]]] +reveal_type(np.real_if_close(AR_c8)) # E: Union[ndarray[Any, dtype[{float32}]], ndarray[Any, dtype[{complex64}]]] +reveal_type(np.real_if_close(AR_LIKE_f)) # E: ndarray[Any, dtype[Any]] reveal_type(np.typename("h")) # E: Literal['short'] reveal_type(np.typename("B")) # E: Literal['unsigned char'] diff --git a/numpy/typing/tests/data/reveal/ufunc_config.pyi b/numpy/typing/tests/data/reveal/ufunc_config.pyi index 6848a3cb5..2c6fadf92 100644 --- a/numpy/typing/tests/data/reveal/ufunc_config.pyi +++ b/numpy/typing/tests/data/reveal/ufunc_config.pyi @@ -1,4 +1,4 @@ -"""Typing tests for `numpy.core._ufunc_config`.""" +"""Typing tests for `core._ufunc_config`.""" import numpy as np @@ -7,19 +7,19 @@ def func(a: str, b: int) -> None: ... class Write: def write(self, value: str) -> None: ... -reveal_type(np.seterr(all=None)) # E: TypedDict('numpy.core._ufunc_config._ErrDict' -reveal_type(np.seterr(divide="ignore")) # E: TypedDict('numpy.core._ufunc_config._ErrDict' -reveal_type(np.seterr(over="warn")) # E: TypedDict('numpy.core._ufunc_config._ErrDict' -reveal_type(np.seterr(under="call")) # E: TypedDict('numpy.core._ufunc_config._ErrDict' -reveal_type(np.seterr(invalid="raise")) # E: TypedDict('numpy.core._ufunc_config._ErrDict' -reveal_type(np.geterr()) # E: TypedDict('numpy.core._ufunc_config._ErrDict' +reveal_type(np.seterr(all=None)) # E: TypedDict('core._ufunc_config._ErrDict' +reveal_type(np.seterr(divide="ignore")) # E: TypedDict('core._ufunc_config._ErrDict' +reveal_type(np.seterr(over="warn")) # E: TypedDict('core._ufunc_config._ErrDict' +reveal_type(np.seterr(under="call")) # E: TypedDict('core._ufunc_config._ErrDict' +reveal_type(np.seterr(invalid="raise")) # E: TypedDict('core._ufunc_config._ErrDict' +reveal_type(np.geterr()) # E: TypedDict('core._ufunc_config._ErrDict' reveal_type(np.setbufsize(4096)) # E: int reveal_type(np.getbufsize()) # E: int -reveal_type(np.seterrcall(func)) # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy._SupportsWrite[builtins.str]] -reveal_type(np.seterrcall(Write())) # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy._SupportsWrite[builtins.str]] -reveal_type(np.geterrcall()) # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy._SupportsWrite[builtins.str]] +reveal_type(np.seterrcall(func)) # E: Union[None, def (builtins.str, builtins.int) -> Any, _SupportsWrite[builtins.str]] +reveal_type(np.seterrcall(Write())) # E: Union[None, def (builtins.str, builtins.int) -> Any, _SupportsWrite[builtins.str]] +reveal_type(np.geterrcall()) # E: Union[None, def (builtins.str, builtins.int) -> Any, _SupportsWrite[builtins.str]] -reveal_type(np.errstate(call=func, all="call")) # E: numpy.errstate[def (a: builtins.str, b: builtins.int)] -reveal_type(np.errstate(call=Write(), divide="log", over="log")) # E: numpy.errstate[ufunc_config.Write] +reveal_type(np.errstate(call=func, all="call")) # E: errstate[def (a: builtins.str, b: builtins.int)] +reveal_type(np.errstate(call=Write(), divide="log", over="log")) # E: errstate[ufunc_config.Write] diff --git a/numpy/typing/tests/data/reveal/ufunclike.pyi b/numpy/typing/tests/data/reveal/ufunclike.pyi index 8b3aea7ce..2d67c923f 100644 --- a/numpy/typing/tests/data/reveal/ufunclike.pyi +++ b/numpy/typing/tests/data/reveal/ufunclike.pyi @@ -9,21 +9,21 @@ AR_LIKE_O: List[np.object_] AR_U: np.ndarray[Any, np.dtype[np.str_]] -reveal_type(np.fix(AR_LIKE_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.fix(AR_LIKE_u)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.fix(AR_LIKE_i)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] -reveal_type(np.fix(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]] +reveal_type(np.fix(AR_LIKE_b)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.fix(AR_LIKE_u)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.fix(AR_LIKE_i)) # E: ndarray[Any, dtype[floating[Any]]] +reveal_type(np.fix(AR_LIKE_f)) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(np.fix(AR_LIKE_O)) # E: Any -reveal_type(np.fix(AR_LIKE_f, out=AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.fix(AR_LIKE_f, out=AR_U)) # E: ndarray[Any, dtype[str_]] -reveal_type(np.isposinf(AR_LIKE_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isposinf(AR_LIKE_u)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isposinf(AR_LIKE_i)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isposinf(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isposinf(AR_LIKE_f, out=AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.isposinf(AR_LIKE_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isposinf(AR_LIKE_u)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isposinf(AR_LIKE_i)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isposinf(AR_LIKE_f)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isposinf(AR_LIKE_f, out=AR_U)) # E: ndarray[Any, dtype[str_]] -reveal_type(np.isneginf(AR_LIKE_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isneginf(AR_LIKE_u)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isneginf(AR_LIKE_i)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isneginf(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]] -reveal_type(np.isneginf(AR_LIKE_f, out=AR_U)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.isneginf(AR_LIKE_b)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isneginf(AR_LIKE_u)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isneginf(AR_LIKE_i)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isneginf(AR_LIKE_f)) # E: ndarray[Any, dtype[bool_]] +reveal_type(np.isneginf(AR_LIKE_f, out=AR_U)) # E: ndarray[Any, dtype[str_]] diff --git a/numpy/typing/tests/data/reveal/ufuncs.pyi b/numpy/typing/tests/data/reveal/ufuncs.pyi index ade45577c..3bf83c820 100644 --- a/numpy/typing/tests/data/reveal/ufuncs.pyi +++ b/numpy/typing/tests/data/reveal/ufuncs.pyi @@ -17,7 +17,7 @@ reveal_type(np.absolute.nout) # E: Literal[1] reveal_type(np.absolute.nargs) # E: Literal[2] reveal_type(np.absolute.signature) # E: None reveal_type(np.absolute(f8)) # E: Any -reveal_type(np.absolute(AR_f8)) # E: numpy.ndarray +reveal_type(np.absolute(AR_f8)) # E: ndarray reveal_type(np.absolute.at(AR_f8, AR_i8)) # E: None reveal_type(np.add.__name__) # E: Literal['add'] @@ -28,13 +28,13 @@ reveal_type(np.add.nout) # E: Literal[1] reveal_type(np.add.nargs) # E: Literal[3] reveal_type(np.add.signature) # E: None reveal_type(np.add(f8, f8)) # E: Any -reveal_type(np.add(AR_f8, f8)) # E: numpy.ndarray +reveal_type(np.add(AR_f8, f8)) # E: ndarray reveal_type(np.add.at(AR_f8, AR_i8, f8)) # E: None reveal_type(np.add.reduce(AR_f8, axis=0)) # E: Any -reveal_type(np.add.accumulate(AR_f8)) # E: numpy.ndarray -reveal_type(np.add.reduceat(AR_f8, AR_i8)) # E: numpy.ndarray +reveal_type(np.add.accumulate(AR_f8)) # E: ndarray +reveal_type(np.add.reduceat(AR_f8, AR_i8)) # E: ndarray reveal_type(np.add.outer(f8, f8)) # E: Any -reveal_type(np.add.outer(AR_f8, f8)) # E: numpy.ndarray +reveal_type(np.add.outer(AR_f8, f8)) # E: ndarray reveal_type(np.frexp.__name__) # E: Literal['frexp'] reveal_type(np.frexp.ntypes) # E: Literal[4] @@ -44,7 +44,7 @@ reveal_type(np.frexp.nout) # E: Literal[2] reveal_type(np.frexp.nargs) # E: Literal[3] reveal_type(np.frexp.signature) # E: None reveal_type(np.frexp(f8)) # E: Tuple[Any, Any] -reveal_type(np.frexp(AR_f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.frexp(AR_f8)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[Any]]] reveal_type(np.divmod.__name__) # E: Literal['divmod'] reveal_type(np.divmod.ntypes) # E: Literal[15] @@ -54,7 +54,7 @@ reveal_type(np.divmod.nout) # E: Literal[2] reveal_type(np.divmod.nargs) # E: Literal[4] reveal_type(np.divmod.signature) # E: None reveal_type(np.divmod(f8, f8)) # E: Tuple[Any, Any] -reveal_type(np.divmod(AR_f8, f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.divmod(AR_f8, f8)) # E: Tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[Any]]] reveal_type(np.matmul.__name__) # E: Literal['matmul'] reveal_type(np.matmul.ntypes) # E: Literal[19] diff --git a/numpy/typing/tests/data/reveal/warnings_and_errors.pyi b/numpy/typing/tests/data/reveal/warnings_and_errors.pyi index 3f20a0135..d5c50448a 100644 --- a/numpy/typing/tests/data/reveal/warnings_and_errors.pyi +++ b/numpy/typing/tests/data/reveal/warnings_and_errors.pyi @@ -2,10 +2,10 @@ from typing import Type import numpy as np -reveal_type(np.ModuleDeprecationWarning()) # E: numpy.ModuleDeprecationWarning -reveal_type(np.VisibleDeprecationWarning()) # E: numpy.VisibleDeprecationWarning -reveal_type(np.ComplexWarning()) # E: numpy.ComplexWarning -reveal_type(np.RankWarning()) # E: numpy.RankWarning -reveal_type(np.TooHardError()) # E: numpy.TooHardError -reveal_type(np.AxisError("test")) # E: numpy.AxisError -reveal_type(np.AxisError(5, 1)) # E: numpy.AxisError +reveal_type(np.ModuleDeprecationWarning()) # E: ModuleDeprecationWarning +reveal_type(np.VisibleDeprecationWarning()) # E: VisibleDeprecationWarning +reveal_type(np.ComplexWarning()) # E: ComplexWarning +reveal_type(np.RankWarning()) # E: RankWarning +reveal_type(np.TooHardError()) # E: TooHardError +reveal_type(np.AxisError("test")) # E: AxisError +reveal_type(np.AxisError(5, 1)) # E: AxisError diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index 2dcfd6082..fe58a8f4c 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -58,6 +58,11 @@ def _strip_filename(msg: str) -> str: return tail.split(":", 1)[-1] +def strip_func(match: re.Match[str]) -> str: + """`re.sub` helper function for stripping module names.""" + return match.groups()[1] + + @pytest.mark.slow @pytest.mark.skipif(NO_MYPY, reason="Mypy is not installed") @pytest.fixture(scope="module", autouse=True) @@ -373,9 +378,15 @@ def _test_reveal( lineno: int, ) -> None: """Error-reporting helper function for `test_reveal`.""" - if reveal not in expected_reveal: + strip_pattern = re.compile(r"(\w+\.)+(\w+)") + stripped_reveal = strip_pattern.sub(strip_func, reveal) + stripped_expected_reveal = strip_pattern.sub(strip_func, expected_reveal) + if stripped_reveal not in stripped_expected_reveal: raise AssertionError( - _REVEAL_MSG.format(lineno, expression, expected_reveal, reveal) + _REVEAL_MSG.format(lineno, + expression, + stripped_expected_reveal, + stripped_reveal) ) -- cgit v1.2.1 From 167539e4c2fdc6b77d6c73c9eebfa77e1507423e Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Wed, 3 Nov 2021 09:43:53 -0700 Subject: DOC: Do not leave space between directive name and double colon. From my regular expression foo, those are the only 9 case whereas there are about ~2000 usage that do not have spaces. While this is ok with docutils/sphinx, it does not seem to be documented, and that means that other parsers will see that as comments, leading to for example improper syntax highlighting. This make it also a tiny bit harder to develop alternative rst parsers. --- numpy/lib/polynomial.py | 2 +- numpy/polynomial/chebyshev.py | 4 ++-- numpy/polynomial/polynomial.py | 4 ++-- numpy/polynomial/polyutils.py | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index 1cbb3cd88..f824c4c5e 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -550,7 +550,7 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False): ----- The solution minimizes the squared error - .. math :: + .. math:: E = \\sum_{j=0}^k |p(x_j) - y_j|^2 in the equations:: diff --git a/numpy/polynomial/chebyshev.py b/numpy/polynomial/chebyshev.py index 2b3268aeb..89ce815d5 100644 --- a/numpy/polynomial/chebyshev.py +++ b/numpy/polynomial/chebyshev.py @@ -88,13 +88,13 @@ Notes The implementations of multiplication, division, integration, and differentiation use the algebraic identities [1]_: -.. math :: +.. math:: T_n(x) = \\frac{z^n + z^{-n}}{2} \\\\ z\\frac{dx}{dz} = \\frac{z - z^{-1}}{2}. where -.. math :: x = \\frac{z + z^{-1}}{2}. +.. math:: x = \\frac{z + z^{-1}}{2}. These identities allow a Chebyshev series to be expressed as a finite, symmetric Laurent series. In this module, this sort of Laurent series diff --git a/numpy/polynomial/polynomial.py b/numpy/polynomial/polynomial.py index 2fead88ab..3c2663b6c 100644 --- a/numpy/polynomial/polynomial.py +++ b/numpy/polynomial/polynomial.py @@ -1304,12 +1304,12 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None): The solution is the coefficients of the polynomial `p` that minimizes the sum of the weighted squared errors - .. math :: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2, + .. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2, where the :math:`w_j` are the weights. This problem is solved by setting up the (typically) over-determined matrix equation: - .. math :: V(x) * c = w * y, + .. math:: V(x) * c = w * y, where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the coefficients to be solved for, `w` are the weights, and `y` are the diff --git a/numpy/polynomial/polyutils.py b/numpy/polynomial/polyutils.py index 3b0f0a9e5..a2bc75a4d 100644 --- a/numpy/polynomial/polyutils.py +++ b/numpy/polynomial/polyutils.py @@ -330,12 +330,12 @@ def mapdomain(x, old, new): ----- Effectively, this implements: - .. math :: + .. math:: x\\_out = new[0] + m(x - old[0]) where - .. math :: + .. math:: m = \\frac{new[1]-new[0]}{old[1]-old[0]} Examples -- cgit v1.2.1 From c38b7f328b87d626be544c5ffb542f2b6a97adee Mon Sep 17 00:00:00 2001 From: mattip Date: Thu, 4 Nov 2021 00:11:04 +0200 Subject: make a.__dlpack__() fail if a is readonly --- numpy/core/src/multiarray/dlpack.c | 6 ++++++ numpy/core/tests/test_dlpack.py | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c index 21930b0ef..f061a6bf9 100644 --- a/numpy/core/src/multiarray/dlpack.c +++ b/numpy/core/src/multiarray/dlpack.c @@ -131,6 +131,12 @@ array_dlpack(PyArrayObject *self, return NULL; } + if ( !(PyArray_FLAGS(self) & NPY_ARRAY_WRITEABLE)) { + PyErr_SetString(PyExc_TypeError, "NumPy currently only supports " + "dlpack for writeable arrays"); + return NULL; + } + npy_intp itemsize = PyArray_ITEMSIZE(self); int ndim = PyArray_NDIM(self); npy_intp *strides = PyArray_STRIDES(self); diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 06fc042ec..f848b2008 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -78,7 +78,7 @@ class TestDLPack: y4 = x[1] assert_array_equal(y4, np._from_dlpack(y4)) - y5 = np.diagonal(x) + y5 = np.diagonal(x).copy() assert_array_equal(y5, np._from_dlpack(y5)) @pytest.mark.parametrize("ndim", range(33)) @@ -101,3 +101,9 @@ class TestDLPack: def test_dlpack_destructor_exception(self): with pytest.raises(RuntimeError): self.dlpack_deleter_exception() + + def test_readonly(self): + x = np.arange(5) + x.flags.writeable = False + with pytest.raises(TypeError): + x.__dlpack__() -- cgit v1.2.1 From d9a9785ada0b4a2b8694511dd854121f618f4f56 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Thu, 4 Nov 2021 14:11:54 +0530 Subject: Addressed reviews and increased code coverage --- numpy/_globals.py | 12 ++++++++++++ numpy/core/src/multiarray/_multiarray_tests.c.src | 14 ++++++++++++++ numpy/core/src/multiarray/array_coercion.c | 1 - numpy/core/src/multiarray/conversion_utils.c | 6 +++--- numpy/core/src/multiarray/ctors.c | 5 +++-- numpy/core/tests/test_multiarray.py | 13 +++++++++++-- 6 files changed, 43 insertions(+), 8 deletions(-) (limited to 'numpy') diff --git a/numpy/_globals.py b/numpy/_globals.py index d458fc9c4..b17ca1979 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -95,6 +95,18 @@ _NoValue = _NoValueType() class _CopyMode(enum.Enum): + """ + An enumeration for the copy modes supported + by numpy. The following three modes are supported, + + - ALWAYS: This means that a deep copy of the input + array will always be taken. + - IF_NEEDED: This means that a deep copy of the input + array will be taken only if necessary. + - NEVER: This means that the deep copy will never be taken. + If a copy cannot be avoided then a `ValueError` will be + raised. + """ ALWAYS = True IF_NEEDED = False diff --git a/numpy/core/src/multiarray/_multiarray_tests.c.src b/numpy/core/src/multiarray/_multiarray_tests.c.src index e945d0771..f21a901c1 100644 --- a/numpy/core/src/multiarray/_multiarray_tests.c.src +++ b/numpy/core/src/multiarray/_multiarray_tests.c.src @@ -2363,6 +2363,17 @@ run_intp_converter(PyObject* NPY_UNUSED(self), PyObject *args) return tup; } +/* used to test _NPY_ARRAY_ENSURENOCOPY raises ValueError */ +static PyObject* +npy_ensurenocopy(PyObject* NPY_UNUSED(self), PyObject* args) +{ + int flags = _NPY_ARRAY_ENSURENOCOPY; + if (!PyArray_CheckFromAny(args, NULL, 0, 0, flags, NULL)) { + return NULL; + } + Py_RETURN_NONE; +} + static PyMethodDef Multiarray_TestsMethods[] = { {"argparse_example_function", (PyCFunction)argparse_example_function, @@ -2424,6 +2435,9 @@ static PyMethodDef Multiarray_TestsMethods[] = { {"npy_discard", npy_discard, METH_O, NULL}, + {"npy_ensurenocopy", + npy_ensurenocopy, + METH_O, NULL}, {"get_buffer_info", get_buffer_info, METH_VARARGS, NULL}, diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index b335b64a0..847bdafc3 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -1163,7 +1163,6 @@ PyArray_DiscoverDTypeAndShape_Recursive( * It might be nice to deprecate this? But it allows things such as * `arr1d[...] = np.array([[1,2,3,4]])` */ -// Here we check whether a copy is being made or not. Check this function. NPY_NO_EXPORT int PyArray_DiscoverDTypeAndShape( PyObject *obj, int max_dims, diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index d8b5ea6b4..4df46cffa 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -18,8 +18,6 @@ #include "alloc.h" #include "npy_buffer.h" -#include "npy_argparse.h" - static int PyArray_PyIntAsInt_ErrMsg(PyObject *o, const char * msg) NPY_GCC_NONNULL(2); static npy_intp @@ -182,7 +180,9 @@ PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) { if (mode_value == NULL) { return NPY_FAIL; } - if (!PyArray_PythonPyIntFromInt(mode_value, &int_copymode)) { + + int_copymode = PyLong_AsLong(mode_value); + if (int_copymode < 0 || PyErr_Occurred()) { return NPY_FAIL; } } diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index fdc393b97..a8f41f582 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1802,12 +1802,13 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, * NPY_ARRAY_ALIGNED, * NPY_ARRAY_WRITEABLE, * NPY_ARRAY_NOTSWAPPED, - * _NPY_ARRAY_ENSURECOPY, + * NPY_ARRAY_ENSURECOPY, * NPY_ARRAY_UPDATEIFCOPY, * NPY_ARRAY_WRITEBACKIFCOPY, * NPY_ARRAY_FORCECAST, * NPY_ARRAY_ENSUREARRAY, - * NPY_ARRAY_ELEMENTSTRIDES + * NPY_ARRAY_ELEMENTSTRIDES, + * _NPY_ARRAY_ENSURENOCOPY * * or'd (|) together * diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 3919f31d8..50133798c 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7836,10 +7836,10 @@ class TestArrayCreationCopyArgument(object): copy=np._CopyMode.NEVER) assert_raises(ValueError, np.array, pyscalar, copy=np._CopyMode.NEVER) - assert_raises(ValueError, np.array, pyscalar, - copy=None) assert_raises(ValueError, np.array, pyscalar, copy=self.RaiseOnBool()) + assert_raises(ValueError, _multiarray_tests.npy_ensurenocopy, + [1]) def test_compatible_cast(self): @@ -8004,6 +8004,15 @@ class TestArrayCreationCopyArgument(object): view, copy=None, order=order2) + def test_striding_not_ok(self): + arr = np.array([[1, 2, 4], [3, 4, 5]]) + assert_raises(ValueError, np.array, + arr.T, copy=np._CopyMode.NEVER, + order='C') + assert_raises(ValueError, np.array, + arr, copy=np._CopyMode.NEVER, + order='F') + class TestArrayAttributeDeletion: -- cgit v1.2.1 From f7a392d43286c39013d98b6bdeb93def645fa8ab Mon Sep 17 00:00:00 2001 From: Matthew Barber Date: Tue, 31 Aug 2021 19:05:21 +0100 Subject: Test array_api entry point exists and points to numpy.array_api --- numpy/tests/test_public_api.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'numpy') diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index 1e7d389d9..81de66670 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -458,3 +458,19 @@ def test_api_importable(): raise AssertionError("Modules that are not really public but looked " "public and can not be imported: " "{}".format(module_names)) + + +def test_array_api_entry_point(): + """ + Entry point for Array API implementation can be found with importlib and + returns the numpy.array_api namespace. + """ + from numpy import array_api + + eps = importlib.metadata.entry_points() + assert "array_api" in eps.keys() + xp_eps = eps["array_api"] + assert "numpy" in (ep.name for ep in xp_eps) + ep = next(ep for ep in xp_eps if ep.name == "numpy") + xp = importlib.import_module(ep.value) + assert xp is array_api -- cgit v1.2.1 From 0ae911c9246a1366a60ed45ac0c28fd828498211 Mon Sep 17 00:00:00 2001 From: Matthew Barber Date: Wed, 1 Sep 2021 09:47:19 +0100 Subject: Make test compatible with Python >=3.10 --- numpy/tests/test_public_api.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'numpy') diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index 81de66670..d8ebf32ea 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -465,12 +465,18 @@ def test_array_api_entry_point(): Entry point for Array API implementation can be found with importlib and returns the numpy.array_api namespace. """ - from numpy import array_api - eps = importlib.metadata.entry_points() - assert "array_api" in eps.keys() - xp_eps = eps["array_api"] + try: + xp_eps = eps.select(group="array_api") + except AttributeError: + # The select interface for entry_points was introduced in py3.10, + # deprecating its dict interface. We fallback to dict keys for finding + # Array API entry points so that running this test in <=3.9 will + # still work - see https://github.com/numpy/numpy/pull/19800. + assert "array_api" in eps.keys() + xp_eps = eps["array_api"] + assert len(xp_eps) > 0 assert "numpy" in (ep.name for ep in xp_eps) ep = next(ep for ep in xp_eps if ep.name == "numpy") xp = importlib.import_module(ep.value) - assert xp is array_api + assert xp is numpy.array_api -- cgit v1.2.1 From 2d601825d4bcb447911d063519c78bb5d5101dec Mon Sep 17 00:00:00 2001 From: Matthew Barber Date: Wed, 1 Sep 2021 09:59:34 +0100 Subject: Clearer test logic --- numpy/tests/test_public_api.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'numpy') diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index d8ebf32ea..26402fa2a 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -473,10 +473,17 @@ def test_array_api_entry_point(): # deprecating its dict interface. We fallback to dict keys for finding # Array API entry points so that running this test in <=3.9 will # still work - see https://github.com/numpy/numpy/pull/19800. - assert "array_api" in eps.keys() - xp_eps = eps["array_api"] - assert len(xp_eps) > 0 - assert "numpy" in (ep.name for ep in xp_eps) - ep = next(ep for ep in xp_eps if ep.name == "numpy") + xp_eps = eps.get("array_api", []) + assert len(xp_eps) > 0, "No entry points for 'array_api' found" + + try: + ep = next(ep for ep in xp_eps if ep.name == "numpy") + except StopIteration: + raise AssertionError("'numpy' not in array_api entry points") from None + xp = importlib.import_module(ep.value) - assert xp is numpy.array_api + msg = ( + f"numpy entry point value '{ep.value}' " + "does not point to our Array API implementation" + ) + assert xp is numpy.array_api, msg -- cgit v1.2.1 From d887db45be0ede22afcfb4fb02aa4570ec6befe0 Mon Sep 17 00:00:00 2001 From: Matthew Barber Date: Wed, 1 Sep 2021 12:13:02 +0100 Subject: Use ep.load() instead of import_module() to get xp Co-authored-by: Zac Hatfield-Dodds --- numpy/tests/test_public_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index 26402fa2a..1eca25afb 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -481,7 +481,7 @@ def test_array_api_entry_point(): except StopIteration: raise AssertionError("'numpy' not in array_api entry points") from None - xp = importlib.import_module(ep.value) + xp = ep.load() msg = ( f"numpy entry point value '{ep.value}' " "does not point to our Array API implementation" -- cgit v1.2.1 From 5702b51bd22b2a96a87bebb3518a57a2ad6904ca Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 2 Nov 2021 17:08:21 +0000 Subject: xfail `test_array_api_entry_point` on python-dbg --- numpy/tests/test_public_api.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'numpy') diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index 1eca25afb..fa29c75b5 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -1,4 +1,5 @@ import sys +import sysconfig import subprocess import pkgutil import types @@ -460,6 +461,14 @@ def test_api_importable(): "{}".format(module_names)) +@pytest.mark.xfail( + sysconfig.get_config_var("Py_DEBUG") is not None, + reason=( + "NumPy possibly built with `USE_DEBUG=True ./tools/travis-test.sh`, " + "which does not expose the `array_api` entry point. " + "See https://github.com/numpy/numpy/pull/19800" + ), +) def test_array_api_entry_point(): """ Entry point for Array API implementation can be found with importlib and -- cgit v1.2.1 From 3cb20a03f5337fe5f038f1593508458849612ba2 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Thu, 4 Nov 2021 14:03:30 -0500 Subject: BUG: Fix requirement that user DTypes had to be heaptypes Requiring heap-types currently does not make sense, because the way to create a new DType is clunky if you were to make a heaptype. This was an initial thought, which panned out inconvenient now, so changing. This has no effect, except for experimental user DTypes when `NUMPY_EXPERIMENTAL_DTYPE_API=1` is used. --- numpy/core/src/multiarray/descriptor.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index 6a09f92ac..fd2577bc6 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -2305,8 +2305,9 @@ arraydescr_new(PyTypeObject *subtype, { if (subtype != &PyArrayDescr_Type) { if (Py_TYPE(subtype) == &PyArrayDTypeMeta_Type && - !(PyType_GetFlags(Py_TYPE(subtype)) & Py_TPFLAGS_HEAPTYPE) && - (NPY_DT_SLOTS((PyArray_DTypeMeta *)subtype)) != NULL) { + (NPY_DT_SLOTS((PyArray_DTypeMeta *)subtype)) != NULL && + !NPY_DT_is_legacy((PyArray_DTypeMeta *)subtype) && + subtype->tp_new != PyArrayDescr_Type.tp_new) { /* * Appears to be a properly initialized user DType. Allocate * it and initialize the main part as best we can. @@ -2333,7 +2334,9 @@ arraydescr_new(PyTypeObject *subtype, } /* The DTypeMeta class should prevent this from happening. */ PyErr_Format(PyExc_SystemError, - "'%S' must not inherit np.dtype.__new__().", subtype); + "'%S' must not inherit np.dtype.__new__(). User DTypes should " + "currently call `PyArrayDescr_Type.tp_new` from their new.", + subtype); return NULL; } -- cgit v1.2.1 From cd7a02a4db7e760b881f3feeb832ffd84fa8645a Mon Sep 17 00:00:00 2001 From: abel Date: Thu, 2 Sep 2021 16:34:42 +0200 Subject: MAINT, ENH [#10736] Add interpolation methods to quantile - Added the missing linear interpolation methods. - Updated the existing unit tests. - Added pytest.mark.xfail for boolean arrays See - https://github.com/numpy/numpy/pull/19857#issuecomment-919258693 - https://github.com/numpy/numpy/issues/19154 --- numpy/lib/function_base.py | 593 +++++++++++++++++++++++++++------- numpy/lib/nanfunctions.py | 212 ++++++++++-- numpy/lib/tests/test_function_base.py | 177 +++++++--- 3 files changed, 788 insertions(+), 194 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 20e32a78d..67a34f6e1 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -52,6 +52,89 @@ __all__ = [ ] +_QuantileInterpolation = dict( + # --- HYNDMAN and FAN methods + # Discrete methods + inverted_cdf=dict( + get_virtual_index=lambda n, quantiles: _inverted_cdf(n, quantiles), + fix_gamma=lambda gamma, _: gamma, # should never be called + ), + averaged_inverted_cdf=dict( + get_virtual_index=lambda n, quantiles: (n * quantiles) - 1, + fix_gamma=lambda gamma, _: _get_gamma_mask( + shape=gamma.shape, + default_value=1., + conditioned_value=0.5, + where=gamma == 0), + ), + closest_observation=dict( + get_virtual_index=lambda n, quantiles: _closest_observation(n, + quantiles), + fix_gamma=lambda gamma, _: gamma, # should never be called + ), + # Continuous methods + interpolated_inverted_cdf=dict( + get_virtual_index=lambda n, quantiles: + _virtual_index_formula(n, quantiles, 0, 1), + fix_gamma=lambda gamma, _: gamma, + ), + hazen=dict( + get_virtual_index=lambda n, quantiles: + _virtual_index_formula(n, quantiles, 0.5, 0.5), + fix_gamma=lambda gamma, _: gamma, + ), + weibull=dict( + get_virtual_index=lambda n, quantiles: + _virtual_index_formula(n, quantiles, 0, 0), + fix_gamma=lambda gamma, _: gamma, + ), + # Default value + linear=dict( + get_virtual_index=lambda n, quantiles: + _virtual_index_formula(n, quantiles, 1, 1), + fix_gamma=lambda gamma, _: gamma, + ), + median_unbiased=dict( + get_virtual_index=lambda n, quantiles: + _virtual_index_formula(n, quantiles, 1 / 3.0, 1 / 3.0), + fix_gamma=lambda gamma, _: gamma, + ), + normal_unbiased=dict( + get_virtual_index=lambda n, quantiles: + _virtual_index_formula(n, quantiles, 3 / 8.0, 3 / 8.0), + fix_gamma=lambda gamma, _: gamma, + ), + # --- OTHER METHODS fixme add deprecated ? + lower=dict( + get_virtual_index=lambda n, quantiles: np.floor( + (n - 1) * quantiles).astype(np.intp), + fix_gamma=lambda gamma, _: gamma, + # should never be called, index dtype is int + ), + higher=dict( + get_virtual_index=lambda n, quantiles: np.ceil( + (n - 1) * quantiles).astype(np.intp), + fix_gamma=lambda gamma, _: gamma, + # should never be called, index dtype is int + ), + midpoint=dict( + get_virtual_index=lambda n, quantiles: 0.5 * ( + np.floor((n - 1) * quantiles) + + np.ceil((n - 1) * quantiles)), + fix_gamma=lambda gamma, index: _get_gamma_mask( + shape=gamma.shape, + default_value=0.5, + conditioned_value=0., + where=index % 1 == 0), + ), + nearest=dict( + get_virtual_index=lambda n, quantiles: np.around( + (n - 1) * quantiles).astype(np.intp), + fix_gamma=lambda gamma, _: gamma, + # should never be called, index dtype is int + )) + + def _rot90_dispatcher(m, k=None, axes=None): return (m,) @@ -3760,8 +3843,13 @@ def _percentile_dispatcher(a, q, axis=None, out=None, overwrite_input=None, @array_function_dispatch(_percentile_dispatcher) -def percentile(a, q, axis=None, out=None, - overwrite_input=False, interpolation='linear', keepdims=False): +def percentile(a, + q, + axis=None, + out=None, + overwrite_input=False, + interpolation="linear", + keepdims=False): """ Compute the q-th percentile of the data along the specified axis. @@ -3790,20 +3878,75 @@ def percentile(a, q, axis=None, out=None, calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'} + interpolation : str + Possible values: 'linear' (default), + 'inverted_cdf', 'averaged_inverted_cdf', + 'closest_observation', 'interpolated_inverted_cdf', + 'hazen', 'weibull', + 'median_unbiased', 'normal_unbiased', + 'lower', 'higher', + 'midpoint', 'nearest'. This optional parameter specifies the interpolation method to - use when the desired percentile lies between two data points - ``i < j``: + use when the desired quantile lies between two data points ``i < j``. + g is the fractional part of the index surrounded by ``i``. + alpha and beta are correction constants modifying i and j: + i + g = (q - alpha) / ( n - alpha - beta + 1 ) + * inverted_cdf: + method 1 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 ; then take i + * averaged_inverted_cdf: + method 2 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 ; then average between bounds + * closest_observation: + method 3 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 and index is odd ; then take j + if g = 0 and index is even ; then take i + * interpolated_inverted_cdf: + method 4 of H&F. + This method give continuous results using: + alpha = 0 + beta = 1 + * hazen: + method 5 of H&F. + This method give continuous results using: + alpha = 1/2 + beta = 1/2 + * weibull: + method 6 of H&F. + This method give continuous results using: + alpha = 0 + beta = 0 + * inclusive: + Default method, aliased with "linear". + method 7 of H&F. + This method give continuous results using: + alpha = 1 + beta = 1 + * median_unbiased: + method 8 of H&F. + This method is probably the best method if the sample distribution + function is unknown (see reference). + This method give continuous results using: + alpha = 1/3 + beta = 1/3 + * normal_unbiased: + method 9 of H&F. + This method is probably the best method if the sample distribution + function is known to be normal. + This method give continuous results using: + alpha = 3/8 + beta = 3/8 + * lower: ``i``. + * higher: ``j``. + * nearest: ``i`` or ``j``, whichever is nearest. + * midpoint: ``(i + j) / 2``. - * 'linear': ``i + (j - i) * fraction``, where ``fraction`` - is the fractional part of the index surrounded by ``i`` - and ``j``. - * 'lower': ``i``. - * 'higher': ``j``. - * 'nearest': ``i`` or ``j``, whichever is nearest. - * 'midpoint': ``(i + j) / 2``. - - .. versionadded:: 1.9.0 keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the @@ -3897,6 +4040,11 @@ def percentile(a, q, axis=None, out=None, ax.legend() plt.show() + References + ---------- + [Hyndman&Fan] Hyndman, R. J., & Fan, Y. (1996). + Sample quantiles in statistical packages. + The American Statistician, 50(4), 361-365. """ q = np.true_divide(q, 100) q = asanyarray(q) # undo any decay that the ufunc performed (see gh-13105) @@ -3912,8 +4060,13 @@ def _quantile_dispatcher(a, q, axis=None, out=None, overwrite_input=None, @array_function_dispatch(_quantile_dispatcher) -def quantile(a, q, axis=None, out=None, - overwrite_input=False, interpolation='linear', keepdims=False): +def quantile(a, + q, + axis=None, + out=None, + overwrite_input=False, + interpolation="linear", + keepdims=False): """ Compute the q-th quantile of the data along the specified axis. @@ -3938,18 +4091,75 @@ def quantile(a, q, axis=None, out=None, If True, then allow the input array `a` to be modified by intermediate calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'} + interpolation : str + Possible values: 'linear' (default), + 'inverted_cdf', 'averaged_inverted_cdf', + 'closest_observation', 'interpolated_inverted_cdf', + 'hazen', 'weibull', + 'median_unbiased', 'normal_unbiased', + 'lower', 'higher', + 'midpoint', 'nearest'. This optional parameter specifies the interpolation method to - use when the desired quantile lies between two data points - ``i < j``: - - * linear: ``i + (j - i) * fraction``, where ``fraction`` - is the fractional part of the index surrounded by ``i`` - and ``j``. - * lower: ``i``. - * higher: ``j``. - * nearest: ``i`` or ``j``, whichever is nearest. - * midpoint: ``(i + j) / 2``. + use when the desired quantile lies between two data points ``i < j``. + g is the fractional part of the index surrounded by ``i``. + alpha and beta are correction constants modifying i and j: + i + g = (q - alpha) / ( n - alpha - beta + 1 ) + * inverted_cdf: + method 1 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 ; then take i + * averaged_inverted_cdf: + method 2 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 ; then average between bounds + * closest_observation: + method 3 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 and index is odd ; then take j + if g = 0 and index is even ; then take i + * interpolated_inverted_cdf: + method 4 of H&F. + This method give continuous results using: + alpha = 0 + beta = 1 + * hazen: + method 5 of H&F. + This method give continuous results using: + alpha = 1/2 + beta = 1/2 + * weibull: + method 6 of H&F. + This method give continuous results using: + alpha = 0 + beta = 0 + * linear: + Default method. + method 7 of H&F. + This method give continuous results using: + alpha = 1 + beta = 1 + * median_unbiased: + method 8 of H&F. + This method is probably the best method if the sample distribution + function is unknown (see reference). + This method give continuous results using: + alpha = 1/3 + beta = 1/3 + * normal_unbiased: + method 9 of H&F. + This method is probably the best method if the sample distribution + function is known to be normal. + This method give continuous results using: + alpha = 3/8 + beta = 3/8 + * lower: ``i``. + * higher: ``j``. + * nearest: ``i`` or ``j``, whichever is nearest. + * midpoint: ``(i + j) / 2``. + keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the @@ -4010,6 +4220,12 @@ def quantile(a, q, axis=None, out=None, >>> np.quantile(b, 0.5, axis=1, overwrite_input=True) array([7., 2.]) >>> assert not np.all(a == b) + + References + ---------- + [Hyndman&Fan] Hyndman, R. J., & Fan, Y. (1996). + Sample quantiles in statistical packages. + The American Statistician, 50(4), 361-365. """ q = np.asanyarray(q) if not _quantile_is_valid(q): @@ -4018,10 +4234,19 @@ def quantile(a, q, axis=None, out=None, a, q, axis, out, overwrite_input, interpolation, keepdims) -def _quantile_unchecked(a, q, axis=None, out=None, overwrite_input=False, - interpolation='linear', keepdims=False): +def _quantile_unchecked(a, + q, + axis=None, + out=None, + overwrite_input=False, + interpolation="linear", + keepdims=False): """Assumes that q is in [0, 1], and is an ndarray""" - r, k = _ureduce(a, func=_quantile_ureduce_func, q=q, axis=axis, out=out, + r, k = _ureduce(a, + func=_quantiles_ureduce_func, + q=q, + axis=axis, + out=out, overwrite_input=overwrite_input, interpolation=interpolation) if keepdims: @@ -4042,122 +4267,242 @@ def _quantile_is_valid(q): return True -def _lerp(a, b, t, out=None): - """ Linearly interpolate from a to b by a factor of t """ - diff_b_a = subtract(b, a) - # asanyarray is a stop-gap until gh-13105 - lerp_interpolation = asanyarray(add(a, diff_b_a*t, out=out)) - subtract(b, diff_b_a * (1 - t), out=lerp_interpolation, where=t>=0.5) - if lerp_interpolation.ndim == 0 and out is None: - lerp_interpolation = lerp_interpolation[()] # unpack 0d arrays - return lerp_interpolation +def _virtual_index_formula(n: np.array, + quantiles: np.array, + alpha: float, + beta: float) -> np.array: + """ + Compute the floating point indexes of an array for the linear + interpolation of quantiles. + Reference: + Hyndman&Fan paper "Sample Quantiles in Statistical Packages", + DOI: 10.1080/00031305.1996.10473566 + """ + return n * quantiles + ( + alpha + quantiles * (1 - alpha - beta) + ) - 1 -def _quantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, - interpolation='linear', keepdims=False): - a = asarray(a) +def _get_gamma(virtual_indexes: np.array, + previous_indexes: np.array, + interpolation: _QuantileInterpolation) -> np.array: + """ + Compute the gamma (a.k.a 'm' or weight) for the linear interpolation + of quantiles. + When gamma == 0 the left bound will be picked, + When gamma == 1 the right bound will be. + """ + gamma = np.asanyarray(virtual_indexes - previous_indexes) + gamma = interpolation["fix_gamma"](gamma, virtual_indexes) + return np.asanyarray(gamma) - # ufuncs cause 0d array results to decay to scalars (see gh-13105), which - # makes them problematic for __setitem__ and attribute access. As a - # workaround, we call this on the result of every ufunc on a possibly-0d - # array. - not_scalar = np.asanyarray - # prepare a for partitioning - if overwrite_input: - if axis is None: - ap = a.ravel() - else: - ap = a - else: - if axis is None: - ap = a.flatten() - else: - ap = a.copy() +def _linear_interpolation_formula( + left: np.array, right: np.array, gamma: np.array, out: np.array = None +) -> np.array: + """ + Compute the linear interpolation weighted by gamma on each point of + two same shape array. + """ + # Equivalent to gamma * right + (1 - gamma) * left + # see gh-14685 + diff_right_left = subtract(right, left) + result = asanyarray(add(left, diff_right_left * gamma, out=out)) + result = subtract(right, + diff_right_left * (1 - gamma), + out=result, + where=gamma >= 0.5) + return result - if axis is None: - axis = 0 +def _get_gamma_mask(shape, default_value, conditioned_value, where): + out = np.full(shape, default_value) + out[where] = conditioned_value + return out + + +def _discret_interpolation_to_boundaries(index, gamma_condition_fun): + previous = np.floor(index) + next = previous + 1 + gamma = index - previous + return _get_gamma_mask(shape=index.shape, + default_value=next, + conditioned_value=previous, + where=gamma_condition_fun(gamma, index) + ).astype(np.intp) + + +def _closest_observation(n, quantiles): + gamma_fun = lambda gamma, index: (gamma == 0) & (np.floor(index) % 2 == 0) + return _discret_interpolation_to_boundaries((n * quantiles) - 1 - 0.5, + gamma_fun) + + +def _inverted_cdf(n, quantiles): + gamma_fun = lambda gamma, _: (gamma == 0) + return _discret_interpolation_to_boundaries((n * quantiles) - 1, + gamma_fun) + + +def _quantiles_ureduce_func( + a: np.array, + q: np.array, + axis: int = None, + out=None, + overwrite_input: bool = False, + interpolation="linear", +) -> np.array: if q.ndim > 2: # The code below works fine for nd, but it might not have useful # semantics. For now, keep the supported dimensions the same as it was # before. raise ValueError("q must be a scalar or 1d") - - Nx = ap.shape[axis] - indices = not_scalar(q * (Nx - 1)) - # round fractional indices according to interpolation method - if interpolation == 'lower': - indices = floor(indices).astype(intp) - elif interpolation == 'higher': - indices = ceil(indices).astype(intp) - elif interpolation == 'midpoint': - indices = 0.5 * (floor(indices) + ceil(indices)) - elif interpolation == 'nearest': - indices = around(indices).astype(intp) - elif interpolation == 'linear': - pass # keep index as fraction and interpolate + if overwrite_input: + if axis is None: + axis = 0 + arr = a.ravel() + else: + arr = a else: - raise ValueError( - "interpolation can only be 'linear', 'lower' 'higher', " - "'midpoint', or 'nearest'") + if axis is None: + axis = 0 + arr = a.flatten() + else: + arr = a.copy() + result = _quantile(arr, + quantiles=q, + axis=axis, + interpolation=interpolation, + out=out) + if result.ndim == 0 and out is None: + result = result[()] # unpack 0d arrays + elif result.size == 1 and out is None and q.ndim == 0: + result = result[0] + return result - # The dimensions of `q` are prepended to the output shape, so we need the - # axis being sampled from `ap` to be first. - ap = np.moveaxis(ap, axis, 0) - del axis - if np.issubdtype(indices.dtype, np.integer): - # take the points along axis +def _get_indexes(arr, virtual_indexes, valid_values_count): + """ + Get the valid indexes of arr neighbouring virtual_indexes. + Note + This is a companion function to linear interpolation of + Quantiles - if np.issubdtype(a.dtype, np.inexact): + Returns + ------- + (previous_indexes, next_indexes): Tuple + A Tuple of virtual_indexes neighbouring indexes + """ + previous_indexes = np.asanyarray(np.floor(virtual_indexes)) + next_indexes = np.asanyarray(previous_indexes + 1) + indexes_above_bounds = virtual_indexes >= valid_values_count - 1 + # When indexes is above max index, take the max value of the array + if indexes_above_bounds.any(): + previous_indexes[indexes_above_bounds] = -1 + next_indexes[indexes_above_bounds] = -1 + # When indexes is below min index, take the min value of the array + indexes_below_bounds = virtual_indexes < 0 + if indexes_below_bounds.any(): + previous_indexes[indexes_below_bounds] = 0 + next_indexes[indexes_below_bounds] = 0 + if np.issubdtype(arr.dtype, np.inexact): + # After the sort, slices having NaNs will have for last element a NaN + virtual_indexes_nans = np.isnan(virtual_indexes) + if virtual_indexes_nans.any(): + previous_indexes[virtual_indexes_nans] = -1 + next_indexes[virtual_indexes_nans] = -1 + previous_indexes = previous_indexes.astype(np.intp) + next_indexes = next_indexes.astype(np.intp) + return previous_indexes, next_indexes + + +def _quantile( + arr: np.array, + quantiles: np.array, + axis: int = -1, + interpolation="linear", + out=None, +): + """ + Private function that doesn't support extended axis or keepdims. + These methods are extended to this function using _ureduce + See nanpercentile for parameter usage + It computes the quantiles of the array for the given axis. + A linear interpolation is performed based on the `interpolation`. + + By default, the interpolation is "linear" where + alpha == beta == 1 which performs the 7th method of Hyndman&Fan. + With "median_unbiased" we get alpha == beta == 1/3 + thus the 8th method of Hyndman&Fan. + """ + # --- Setup + arr = np.asanyarray(arr) + values_count = arr.shape[axis] + # The dimensions of `q` are prepended to the output shape, so we need the + # axis being sampled from `arr` to be last. + DATA_AXIS = 0 + arr = np.moveaxis(arr, axis, destination=DATA_AXIS) + # --- Computation of indexes + # Index where to find the value in the sorted array. + # Virtual because it is a floating point value, not an valid index. + # The nearest neighbours are used for interpolation + try: + interpolation = _QuantileInterpolation[interpolation] + except KeyError: + raise ValueError( + f"{interpolation!r} is not a valid interpolation. Use one of: " + f"{_QuantileInterpolation.keys()}") + virtual_indexes = interpolation["get_virtual_index"](values_count, + quantiles) + virtual_indexes = np.asanyarray(virtual_indexes) + if np.issubdtype(virtual_indexes.dtype, np.integer): + # No interpolation needed, take the points along axis + if np.issubdtype(arr.dtype, np.inexact): # may contain nan, which would sort to the end - ap.partition(concatenate((indices.ravel(), [-1])), axis=0) - n = np.isnan(ap[-1]) + arr.partition(concatenate((virtual_indexes.ravel(), [-1])), axis=0) + slices_having_nans = np.isnan(arr[-1]) else: # cannot contain nan - ap.partition(indices.ravel(), axis=0) - n = np.array(False, dtype=bool) - - r = take(ap, indices, axis=0, out=out) - + arr.partition(virtual_indexes.ravel(), axis=0) + slices_having_nans = np.array(False, dtype=bool) + result = np.asanyarray(take(arr, virtual_indexes, axis=0, out=out)) else: - # weight the points above and below the indices - - indices_below = not_scalar(floor(indices)).astype(intp) - indices_above = not_scalar(indices_below + 1) - indices_above[indices_above > Nx - 1] = Nx - 1 - - if np.issubdtype(a.dtype, np.inexact): - # may contain nan, which would sort to the end - ap.partition(concatenate(( - indices_below.ravel(), indices_above.ravel(), [-1] - )), axis=0) - n = np.isnan(ap[-1]) + previous_indexes, next_indexes = _get_indexes(arr, + virtual_indexes, + values_count) + # --- Sorting + arr.partition( + np.unique(np.concatenate(([0, -1], + previous_indexes.ravel(), + next_indexes.ravel(), + ))), + axis=DATA_AXIS) + if np.issubdtype(arr.dtype, np.inexact): + slices_having_nans = np.isnan( + take(arr, indices=-1, axis=DATA_AXIS) + ) else: - # cannot contain nan - ap.partition(concatenate(( - indices_below.ravel(), indices_above.ravel() - )), axis=0) - n = np.array(False, dtype=bool) - - weights_shape = indices.shape + (1,) * (ap.ndim - 1) - weights_above = not_scalar(indices - indices_below).reshape(weights_shape) - - x_below = take(ap, indices_below, axis=0) - x_above = take(ap, indices_above, axis=0) - - r = _lerp(x_below, x_above, weights_above, out=out) - - # if any slice contained a nan, then all results on that slice are also nan - if np.any(n): - if r.ndim == 0 and out is None: + slices_having_nans = None + # --- Get values from indexes + previous = np.take(arr, previous_indexes, axis=DATA_AXIS) + next = np.take(arr, next_indexes, axis=DATA_AXIS) + # --- Linear interpolation + gamma = _get_gamma(virtual_indexes, + previous_indexes, + interpolation) + result_shape = virtual_indexes.shape + (1,) * (arr.ndim - 1) + gamma = gamma.reshape(result_shape) + result = _linear_interpolation_formula(previous, + next, + gamma, + out=out) + if np.any(slices_having_nans): + if result.ndim == 0 and out is None: # can't write to a scalar - r = a.dtype.type(np.nan) + result = np.array(np.nan, dtype=arr.dtype) else: - r[..., n] = a.dtype.type(np.nan) - - return r + result[..., slices_having_nans] = np.nan + return result def _trapz_dispatcher(y, x=None, dx=None, axis=None): diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 08d9b42bb..e90c19b4a 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -23,6 +23,7 @@ Functions import functools import warnings import numpy as np +from numpy.lib.function_base import _QuantileInterpolation from numpy.lib import function_base from numpy.core import overrides @@ -1229,8 +1230,15 @@ def _nanpercentile_dispatcher(a, q, axis=None, out=None, overwrite_input=None, @array_function_dispatch(_nanpercentile_dispatcher) -def nanpercentile(a, q, axis=None, out=None, overwrite_input=False, - interpolation='linear', keepdims=np._NoValue): +def nanpercentile( + a, + q, + axis=None, + out=None, + overwrite_input=False, + interpolation="linear", + keepdims=np._NoValue, +): """ Compute the qth percentile of the data along the specified axis, while ignoring nan values. @@ -1259,18 +1267,74 @@ def nanpercentile(a, q, axis=None, out=None, overwrite_input=False, If True, then allow the input array `a` to be modified by intermediate calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'} + interpolation : str + Possible values: 'linear' (default), + 'inverted_cdf', 'averaged_inverted_cdf', + 'closest_observation', 'interpolated_inverted_cdf', + 'hazen', 'weibull', + 'median_unbiased', 'normal_unbiased', + 'lower', 'higher', + 'midpoint', 'nearest'. This optional parameter specifies the interpolation method to - use when the desired percentile lies between two data points - ``i < j``: - - * 'linear': ``i + (j - i) * fraction``, where ``fraction`` - is the fractional part of the index surrounded by ``i`` - and ``j``. - * 'lower': ``i``. - * 'higher': ``j``. - * 'nearest': ``i`` or ``j``, whichever is nearest. - * 'midpoint': ``(i + j) / 2``. + use when the desired quantile lies between two data points ``i < j``. + g is the fractional part of the index surrounded by ``i``. + alpha and beta are correction constants modifying i and j: + i + g = (q - alpha) / ( n - alpha - beta + 1 ) + * inverted_cdf: + method 1 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 ; then take i + * averaged_inverted_cdf: + method 2 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 ; then average between bounds + * closest_observation: + method 3 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 and index is odd ; then take j + if g = 0 and index is even ; then take i + * interpolated_inverted_cdf: + method 4 of H&F. + This method give continuous results using: + alpha = 0 + beta = 1 + * hazen: + method 5 of H&F. + This method give continuous results using: + alpha = 1/2 + beta = 1/2 + * weibull: + method 6 of H&F. + This method give continuous results using: + alpha = 0 + beta = 0 + * linear: + Default method. + method 7 of H&F. + This method give continuous results using: + alpha = 1 + beta = 1 + * median_unbiased: + method 8 of H&F. + This method is probably the best method if the sample distribution + function is unknown (see reference). + This method give continuous results using: + alpha = 1/3 + beta = 1/3 + * normal_unbiased: + method 9 of H&F. + This method is probably the best method if the sample distribution + function is known to be normal. + This method give continuous results using: + alpha = 3/8 + beta = 3/8 + * lower: ``i``. + * higher: ``j``. + * nearest: ``i`` or ``j``, whichever is nearest. + * midpoint: ``(i + j) / 2``. keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the @@ -1342,7 +1406,9 @@ def nanpercentile(a, q, axis=None, out=None, overwrite_input=False, """ a = np.asanyarray(a) - q = np.true_divide(q, 100.0) # handles the asarray for us too + q = np.true_divide(q, 100.0) + # undo any decay that the ufunc performed (see gh-13105) + q = np.asanyarray(q) if not function_base._quantile_is_valid(q): raise ValueError("Percentiles must be in the range [0, 100]") return _nanquantile_unchecked( @@ -1355,8 +1421,15 @@ def _nanquantile_dispatcher(a, q, axis=None, out=None, overwrite_input=None, @array_function_dispatch(_nanquantile_dispatcher) -def nanquantile(a, q, axis=None, out=None, overwrite_input=False, - interpolation='linear', keepdims=np._NoValue): +def nanquantile( + a, + q, + axis=None, + out=None, + overwrite_input=False, + interpolation="linear", + keepdims=np._NoValue, +): """ Compute the qth quantile of the data along the specified axis, while ignoring nan values. @@ -1384,19 +1457,74 @@ def nanquantile(a, q, axis=None, out=None, overwrite_input=False, If True, then allow the input array `a` to be modified by intermediate calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'} + interpolation : str + Possible values: 'linear' (default), + 'inverted_cdf', 'averaged_inverted_cdf', + 'closest_observation', 'interpolated_inverted_cdf', + 'hazen', 'weibull', + 'median_unbiased', 'normal_unbiased', + 'lower', 'higher', + 'midpoint', 'nearest'. This optional parameter specifies the interpolation method to - use when the desired quantile lies between two data points - ``i < j``: - - * linear: ``i + (j - i) * fraction``, where ``fraction`` - is the fractional part of the index surrounded by ``i`` - and ``j``. + use when the desired quantile lies between two data points ``i < j``. + g is the fractional part of the index surrounded by ``i``. + alpha and beta are correction constants modifying i and j: + i + g = (q - alpha) / ( n - alpha - beta + 1 ) + * inverted_cdf: + method 1 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 ; then take i + * averaged_inverted_cdf: + method 2 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 ; then average between bounds + * closest_observation: + method 3 of H&F. + This method give discontinuous results: + if g > 0 ; then take j + if g = 0 and index is odd ; then take j + if g = 0 and index is even ; then take i + * interpolated_inverted_cdf: + method 4 of H&F. + This method give continuous results using: + alpha = 0 + beta = 1 + * hazen: + method 5 of H&F. + This method give continuous results using: + alpha = 1/2 + beta = 1/2 + * weibull: + method 6 of H&F. + This method give continuous results using: + alpha = 0 + beta = 0 + * linear: + Default method. + method 7 of H&F. + This method give continuous results using: + alpha = 1 + beta = 1 + * median_unbiased: + method 8 of H&F. + This method is probably the best method if the sample distribution + function is unknown (see reference). + This method give continuous results using: + alpha = 1/3 + beta = 1/3 + * normal_unbiased: + method 9 of H&F. + This method is probably the best method if the sample distribution + function is known to be normal. + This method give continuous results using: + alpha = 3/8 + beta = 3/8 * lower: ``i``. * higher: ``j``. * nearest: ``i`` or ``j``, whichever is nearest. * midpoint: ``(i + j) / 2``. - keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the @@ -1462,26 +1590,39 @@ def nanquantile(a, q, axis=None, out=None, overwrite_input=False, a, q, axis, out, overwrite_input, interpolation, keepdims) -def _nanquantile_unchecked(a, q, axis=None, out=None, overwrite_input=False, - interpolation='linear', keepdims=np._NoValue): +def _nanquantile_unchecked( + a, + q, + axis=None, + out=None, + overwrite_input=False, + interpolation="linear", + keepdims=np._NoValue, +): """Assumes that q is in [0, 1], and is an ndarray""" # apply_along_axis in _nanpercentile doesn't handle empty arrays well, # so deal them upfront if a.size == 0: return np.nanmean(a, axis, out=out, keepdims=keepdims) - - r, k = function_base._ureduce( - a, func=_nanquantile_ureduce_func, q=q, axis=axis, out=out, - overwrite_input=overwrite_input, interpolation=interpolation - ) + r, k = function_base._ureduce(a, + func=_nanquantile_ureduce_func, + q=q, + axis=axis, + out=out, + overwrite_input=overwrite_input, + interpolation=interpolation) if keepdims and keepdims is not np._NoValue: return r.reshape(q.shape + k) else: return r -def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, - interpolation='linear'): +def _nanquantile_ureduce_func(a, + q, + axis=None, + out=None, + overwrite_input=False, + interpolation= "linear"): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce @@ -1504,7 +1645,10 @@ def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, return result -def _nanquantile_1d(arr1d, q, overwrite_input=False, interpolation='linear'): +def _nanquantile_1d(arr1d, + q, + overwrite_input=False, + interpolation= "linear"): """ Private function for rank 1 arrays. Compute quantile ignoring NaNs. See nanpercentile for parameter usage diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index c7dfe5673..4f0db7bdb 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2903,36 +2903,134 @@ class TestPercentile: [1, 1, 1]]) assert_array_equal(np.percentile(x, 50, axis=0), [1, 1, 1]) - def test_linear(self): - + @pytest.mark.parametrize("dtype", np.typecodes["AllFloat"]) + def test_linear_nan_1D(self, dtype): + # METHOD 1 of H&F + arr = np.asarray([15.0, np.NAN, 35.0, 40.0, 50.0], dtype=dtype) + res = np.percentile( + arr, + 40.0, + interpolation="linear") + np.testing.assert_equal(res, np.NAN) + np.testing.assert_equal(res.dtype, arr.dtype) + + TYPE_CODES = np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O" + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_linear_inverted_cdf(self, dtype): + # METHOD 1 of H&F + arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) + res = np.percentile( + arr, + 40.0, + interpolation="inverted_cdf") + np.testing.assert_almost_equal(res, 20, 15) + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_linear_averaged_inverted_cdf(self, dtype): + # METHOD 2 of H&F + arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) + res = np.percentile( + arr, + 40.0, + interpolation="averaged_inverted_cdf") + np.testing.assert_almost_equal(res, 27.5, 15) + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_linear_closest_observation(self, dtype): + # METHOD 3 of H&F + arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) + res = np.percentile( + arr, + 40.0, + interpolation="closest_observation") + np.testing.assert_almost_equal(res, 20, 15) + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_linear_interpolated_inverted_cdf(self, dtype): + # METHOD 4 of H&F + arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) + res = np.percentile( + arr, + 40.0, + interpolation="interpolated_inverted_cdf") + np.testing.assert_almost_equal(res, 20, 15) + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_linear_hazen(self, dtype): + # METHOD 5 of H&F + arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) + res = np.percentile( + arr, + 40.0, + interpolation="hazen") + np.testing.assert_almost_equal(res, 27.5, 15) + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_linear_weibull(self, dtype): + # METHOD 6 of H&F + arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) + res = np.percentile( + arr, + 40.0, + interpolation="weibull") + np.testing.assert_almost_equal(res, 26, 15) + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_linear_linear(self, dtype): + # METHOD 7 of H&F # Test defaults assert_equal(np.percentile(range(10), 50), 4.5) - - # explicitly specify interpolation_method 'linear' (the default) - assert_equal(np.percentile(range(10), 50, - interpolation='linear'), 4.5) - - def test_lower_higher(self): - - # interpolation_method 'lower'/'higher' - assert_equal(np.percentile(range(10), 50, + # explicit interpolation_method (the default) + res = np.percentile([15.0, 20.0, 35.0, 40.0, 50.0], + 40, + interpolation="linear") + np.testing.assert_almost_equal(res, 29, 15) + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_linear_median_unbiased(self, dtype): + # METHOD 8 of H&F + arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) + res = np.percentile( + arr, + 40.0, + interpolation="median_unbiased") + np.testing.assert_almost_equal(res, 27, 14) + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_linear_normal_unbiased(self, dtype): + # METHOD 9 of H&F + arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) + res = np.percentile( + arr, + 40.0, + interpolation="normal_unbiased") + np.testing.assert_almost_equal(res, 27.125, 15) + + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_lower_higher(self, dtype): + assert_equal(np.percentile(np.arange(10, dtype=dtype), 50, interpolation='lower'), 4) - assert_equal(np.percentile(range(10), 50, + assert_equal(np.percentile(np.arange(10, dtype=dtype), 50, interpolation='higher'), 5) - def test_midpoint(self): - assert_equal(np.percentile(range(10), 51, + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_midpoint(self, dtype): + assert_equal(np.percentile(np.arange(10, dtype=dtype), 51, interpolation='midpoint'), 4.5) - assert_equal(np.percentile(range(11), 51, + assert_equal(np.percentile(np.arange(9, dtype=dtype) + 1, 50, + interpolation='midpoint'), 5) + assert_equal(np.percentile(np.arange(11, dtype=dtype), 51, interpolation='midpoint'), 5.5) - assert_equal(np.percentile(range(11), 50, + assert_equal(np.percentile(np.arange(11, dtype=dtype), 50, interpolation='midpoint'), 5) - def test_nearest(self): - assert_equal(np.percentile(range(10), 51, + @pytest.mark.parametrize("dtype", TYPE_CODES) + def test_nearest(self, dtype): + assert_equal(np.percentile(np.arange(10, dtype=dtype), 51, interpolation='nearest'), 5) - assert_equal(np.percentile(range(10), 49, - interpolation='nearest'), 4) + assert_equal(np.percentile(np.arange(10, dtype=dtype), 49, + interpolation='nearest'), 4) def test_sequence(self): x = np.arange(8) * 0.5 @@ -3038,18 +3136,18 @@ class TestPercentile: y = np.zeros((3,)) p = (1, 2, 3) np.percentile(x, p, out=y) - assert_equal(y, np.percentile(x, p)) + assert_equal(np.percentile(x, p), y) x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.zeros((3, 3)) np.percentile(x, p, axis=0, out=y) - assert_equal(y, np.percentile(x, p, axis=0)) + assert_equal(np.percentile(x, p, axis=0), y) y = np.zeros((3, 2)) np.percentile(x, p, axis=1, out=y) - assert_equal(y, np.percentile(x, p, axis=1)) + assert_equal(np.percentile(x, p, axis=1), y) x = np.arange(12).reshape(3, 4) # q.dim > 1, float @@ -3293,6 +3391,7 @@ class TestPercentile: with pytest.raises(ValueError, match="Percentiles must be in"): np.percentile([1, 2, 3, 4.0], q) + class TestQuantile: # most of this is already tested by TestPercentile @@ -3302,6 +3401,7 @@ class TestQuantile: assert_equal(np.quantile(x, 1), 3.5) assert_equal(np.quantile(x, 0.5), 1.75) + @pytest.mark.xfail(reason="See gh-19154") def test_correct_quantile_value(self): a = np.array([True]) tf_quant = np.quantile(True, False) @@ -3310,12 +3410,11 @@ class TestQuantile: a = np.array([False, True, True]) quant_res = np.quantile(a, a) assert_array_equal(quant_res, a) - assert_equal(a.dtype, quant_res.dtype) + assert_equal(quant_res.dtype, a.dtype) def test_fraction(self): # fractional input, integral quantile x = [Fraction(i, 2) for i in range(8)] - q = np.quantile(x, 0) assert_equal(q, 0) assert_equal(type(q), Fraction) @@ -3352,6 +3451,12 @@ class TestQuantile: np.quantile(np.arange(100.), p, interpolation="midpoint") assert_array_equal(p, p0) + @pytest.mark.parametrize("dtype", np.typecodes["AllInteger"]) + def test_quantile_preserve_int_type(self, dtype): + res = np.quantile(np.array([1, 2], dtype=dtype), [0.5], + interpolation="nearest") + assert res.dtype == dtype + def test_quantile_monotonic(self): # GH 14685 # test that the return value of quantile is monotonic if p0 is ordered @@ -3380,9 +3485,9 @@ class TestLerp: min_value=-1e300, max_value=1e300), b = st.floats(allow_nan=False, allow_infinity=False, min_value=-1e300, max_value=1e300)) - def test_lerp_monotonic(self, t0, t1, a, b): - l0 = np.lib.function_base._lerp(a, b, t0) - l1 = np.lib.function_base._lerp(a, b, t1) + def test_linear_interpolation_formula_monotonic(self, t0, t1, a, b): + l0 = nfb._linear_interpolation_formula(a, b, t0) + l1 = nfb._linear_interpolation_formula(a, b, t1) if t0 == t1 or a == b: assert l0 == l1 # uninteresting elif (t0 < t1) == (a < b): @@ -3396,11 +3501,11 @@ class TestLerp: min_value=-1e300, max_value=1e300), b=st.floats(allow_nan=False, allow_infinity=False, min_value=-1e300, max_value=1e300)) - def test_lerp_bounded(self, t, a, b): + def test_linear_interpolation_formula_bounded(self, t, a, b): if a <= b: - assert a <= np.lib.function_base._lerp(a, b, t) <= b + assert a <= nfb._linear_interpolation_formula(a, b, t) <= b else: - assert b <= np.lib.function_base._lerp(a, b, t) <= a + assert b <= nfb._linear_interpolation_formula(a, b, t) <= a @hypothesis.given(t=st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=1), @@ -3408,17 +3513,17 @@ class TestLerp: min_value=-1e300, max_value=1e300), b=st.floats(allow_nan=False, allow_infinity=False, min_value=-1e300, max_value=1e300)) - def test_lerp_symmetric(self, t, a, b): + def test_linear_interpolation_formula_symmetric(self, t, a, b): # double subtraction is needed to remove the extra precision of t < 0.5 - left = np.lib.function_base._lerp(a, b, 1 - (1 - t)) - right = np.lib.function_base._lerp(b, a, 1 - t) + left = nfb._linear_interpolation_formula(a, b, 1 - (1 - t)) + right = nfb._linear_interpolation_formula(b, a, 1 - t) assert left == right - def test_lerp_0d_inputs(self): + def test_linear_interpolation_formula_0d_inputs(self): a = np.array(2) b = np.array(5) t = np.array(0.2) - assert np.lib.function_base._lerp(a, b, t) == 2.6 + assert nfb._linear_interpolation_formula(a, b, t) == 2.6 class TestMedian: -- cgit v1.2.1 From 303c12cfe7ad1b8b6ed5417c126857b29355b1fb Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Fri, 8 Oct 2021 17:09:51 -0600 Subject: DOC: fix docstrings. Hopefully fix the docstrings of percentile, nanpercentile, quantile, and nanquantile so that CircleCI passes. --- numpy/lib/function_base.py | 424 +++++++++++++++++++++++++++------------------ numpy/lib/nanfunctions.py | 424 ++++++++++++++++++++++++++++----------------- 2 files changed, 523 insertions(+), 325 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 67a34f6e1..dbaba87f9 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3877,75 +3877,31 @@ def percentile(a, If True, then allow the input array `a` to be modified by intermediate calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - - interpolation : str - Possible values: 'linear' (default), - 'inverted_cdf', 'averaged_inverted_cdf', - 'closest_observation', 'interpolated_inverted_cdf', - 'hazen', 'weibull', - 'median_unbiased', 'normal_unbiased', - 'lower', 'higher', - 'midpoint', 'nearest'. - This optional parameter specifies the interpolation method to - use when the desired quantile lies between two data points ``i < j``. - g is the fractional part of the index surrounded by ``i``. - alpha and beta are correction constants modifying i and j: - i + g = (q - alpha) / ( n - alpha - beta + 1 ) - * inverted_cdf: - method 1 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 ; then take i - * averaged_inverted_cdf: - method 2 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 ; then average between bounds - * closest_observation: - method 3 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 and index is odd ; then take j - if g = 0 and index is even ; then take i - * interpolated_inverted_cdf: - method 4 of H&F. - This method give continuous results using: - alpha = 0 - beta = 1 - * hazen: - method 5 of H&F. - This method give continuous results using: - alpha = 1/2 - beta = 1/2 - * weibull: - method 6 of H&F. - This method give continuous results using: - alpha = 0 - beta = 0 - * inclusive: - Default method, aliased with "linear". - method 7 of H&F. - This method give continuous results using: - alpha = 1 - beta = 1 - * median_unbiased: - method 8 of H&F. - This method is probably the best method if the sample distribution - function is unknown (see reference). - This method give continuous results using: - alpha = 1/3 - beta = 1/3 - * normal_unbiased: - method 9 of H&F. - This method is probably the best method if the sample distribution - function is known to be normal. - This method give continuous results using: - alpha = 3/8 - beta = 3/8 - * lower: ``i``. - * higher: ``j``. - * nearest: ``i`` or ``j``, whichever is nearest. - * midpoint: ``(i + j) / 2``. + interpolation : str, optional + This parameter specifies the interpolation method to + use when the desired quantile lies between two data points + There are many different methods, some unique to NumPy. See the + notes for explanation. Options + + * (NPY 1): 'lower' + * (NPY 2): 'higher', + * (NPY 3): 'midpoint' + * (NPY 4): 'nearest' + * (NPY 5): 'linear', aliased with 'inclusive' (default) + + New options: + + * (H&F 1): 'inverted_cdf' + * (H&F 2): 'averaged_inverted_cdf' + * (H&F 3): 'closest_observation' + * (H&F 4): 'interpolated_inverted_cdf' + * (H&F 5): 'hazen' + * (H&F 6): 'weibull' + * (H&F 7): 'inclusive', aliased with 'linear' (default) + * (H&F 8): 'median_unbiased' + * (H&F 9): 'normal_unbiased' + + .. versionadded;: 1.22.0 keepdims : bool, optional If this is set to True, the axes which are reduced are left in @@ -3971,18 +3927,104 @@ def percentile(a, mean median : equivalent to ``percentile(..., 50)`` nanpercentile - quantile : equivalent to percentile, except with q in the range [0, 1]. + quantile : equivalent to percentile, except q in the range [0, 1]. Notes ----- - Given a vector ``V`` of length ``N``, the q-th percentile of - ``V`` is the value ``q/100`` of the way from the minimum to the - maximum in a sorted copy of ``V``. The values and distances of - the two nearest neighbors as well as the `interpolation` parameter - will determine the percentile if the normalized ranking does not - match the location of ``q`` exactly. This function is the same as - the median if ``q=50``, the same as the minimum if ``q=0`` and the - same as the maximum if ``q=100``. + Given a vector ``V`` of length ``N``, the q-th percentile of ``V`` is + the value ``q/100`` of the way from the minimum to the maximum in a + sorted copy of ``V``. The values and distances of the two nearest + neighbors as well as the `interpolation` parameter will determine the + percentile if the normalized ranking does not match the location of + ``q`` exactly. This function is the same as the median if ``q=50``, the + same as the minimum if ``q=0`` and the same as the maximum if + ``q=100``. + + This optional `interpolation` parameter specifies the interpolation + method to use when the desired quantile lies between two data points + ``i < j``. If ``g`` is the fractional part of the index surrounded by + ``i`` and alpha and beta are correction constants modifying i and j. + + .. math:: + i + g = (q - alpha) / ( n - alpha - beta + 1 ) + + The different interpolation methods then work as follows + + inverted_cdf: + method 1 of H&F [1]_. + This method gives discontinuous results: + * if g > 0 ; then take j + * if g = 0 ; then take i + + averaged_inverted_cdf: + method 2 of H&F [1]_. + This method give discontinuous results: + * if g > 0 ; then take j + * if g = 0 ; then average between bounds + + closest_observation: + method 3 of H&F [1]_. + This method give discontinuous results: + * if g > 0 ; then take j + * if g = 0 and index is odd ; then take j + * if g = 0 and index is even ; then take i + + interpolated_inverted_cdf: + method 4 of H&F [1]_. + This method give continuous results using: + * alpha = 0 + * beta = 1 + + hazen: + method 5 of H&F [1]_. + This method give continuous results using: + * alpha = 1/2 + * beta = 1/2 + + weibull: + method 6 of H&F [1]_. + This method give continuous results using: + * alpha = 0 + * beta = 0 + + inclusive: + Default method, aliased with "linear". + method 7 of H&F [1]_. + This method give continuous results using: + * alpha = 1 + * beta = 1 + + median_unbiased: + method 8 of H&F [1]_. + This method is probably the best method if the sample + distribution function is unknown (see reference). + This method give continuous results using: + * alpha = 1/3 + * beta = 1/3 + + normal_unbiased: + method 9 of H&F [1]_. + This method is probably the best method if the sample + distribution function is known to be normal. + This method give continuous results using: + * alpha = 3/8 + * beta = 3/8 + + lower: + NumPy method kept for backwards compatibility. + Takes ``i`` as the interpolation point. + + higher: + NumPy method kept for backwards compatibility. + Takes ``j`` as the interpolation point. + + nearest: + NumPy method kept for backwards compatibility. + Takes ``i`` or ``j``, whichever is nearest. + + midpoint: + NumPy method kept for backwards compatibility. + Uses ``(i + j) / 2``. Examples -------- @@ -4042,9 +4084,10 @@ def percentile(a, References ---------- - [Hyndman&Fan] Hyndman, R. J., & Fan, Y. (1996). - Sample quantiles in statistical packages. - The American Statistician, 50(4), 361-365. + .. [1] R. J. Hyndman and Y. Fan, + "Sample quantiles in statistical packages," + The American Statistician, 50(4), pp. 361-365, 1996 + """ q = np.true_divide(q, 100) q = asanyarray(q) # undo any decay that the ufunc performed (see gh-13105) @@ -4080,85 +4123,42 @@ def quantile(a, Quantile or sequence of quantiles to compute, which must be between 0 and 1 inclusive. axis : {int, tuple of int, None}, optional - Axis or axes along which the quantiles are computed. The - default is to compute the quantile(s) along a flattened - version of the array. + Axis or axes along which the quantiles are computed. The default is + to compute the quantile(s) along a flattened version of the array. out : ndarray, optional - Alternative output array in which to place the result. It must - have the same shape and buffer length as the expected output, - but the type (of the output) will be cast if necessary. + Alternative output array in which to place the result. It must have + the same shape and buffer length as the expected output, but the + type (of the output) will be cast if necessary. overwrite_input : bool, optional - If True, then allow the input array `a` to be modified by intermediate - calculations, to save memory. In this case, the contents of the input - `a` after this function completes is undefined. - interpolation : str - Possible values: 'linear' (default), - 'inverted_cdf', 'averaged_inverted_cdf', - 'closest_observation', 'interpolated_inverted_cdf', - 'hazen', 'weibull', - 'median_unbiased', 'normal_unbiased', - 'lower', 'higher', - 'midpoint', 'nearest'. - This optional parameter specifies the interpolation method to - use when the desired quantile lies between two data points ``i < j``. - g is the fractional part of the index surrounded by ``i``. - alpha and beta are correction constants modifying i and j: - i + g = (q - alpha) / ( n - alpha - beta + 1 ) - * inverted_cdf: - method 1 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 ; then take i - * averaged_inverted_cdf: - method 2 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 ; then average between bounds - * closest_observation: - method 3 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 and index is odd ; then take j - if g = 0 and index is even ; then take i - * interpolated_inverted_cdf: - method 4 of H&F. - This method give continuous results using: - alpha = 0 - beta = 1 - * hazen: - method 5 of H&F. - This method give continuous results using: - alpha = 1/2 - beta = 1/2 - * weibull: - method 6 of H&F. - This method give continuous results using: - alpha = 0 - beta = 0 - * linear: - Default method. - method 7 of H&F. - This method give continuous results using: - alpha = 1 - beta = 1 - * median_unbiased: - method 8 of H&F. - This method is probably the best method if the sample distribution - function is unknown (see reference). - This method give continuous results using: - alpha = 1/3 - beta = 1/3 - * normal_unbiased: - method 9 of H&F. - This method is probably the best method if the sample distribution - function is known to be normal. - This method give continuous results using: - alpha = 3/8 - beta = 3/8 - * lower: ``i``. - * higher: ``j``. - * nearest: ``i`` or ``j``, whichever is nearest. - * midpoint: ``(i + j) / 2``. + If True, then allow the input array `a` to be modified by + intermediate calculations, to save memory. In this case, the + contents of the input `a` after this function completes is + undefined. + interpolation : str, optional + This parameter specifies the interpolation method to use when the + desired quantile lies between two data points There are many + different methods, some unique to NumPy. See the notes for + explanation. Options: + + * (NPY 1): 'lower' + * (NPY 2): 'higher', + * (NPY 3): 'midpoint' + * (NPY 4): 'nearest' + * (NPY 5): 'linear', aliased with 'inclusive' (default) + + New options: + + * (H&F 1): 'inverted_cdf' + * (H&F 2): 'averaged_inverted_cdf' + * (H&F 3): 'closest_observation' + * (H&F 4): 'interpolated_inverted_cdf' + * (H&F 5): 'hazen' + * (H&F 6): 'weibull' + * (H&F 7): 'inclusive', aliased with 'linear' (default) + * (H&F 8): 'median_unbiased' + * (H&F 9): 'normal_unbiased' + + .. versionadded:: 1.22.0 keepdims : bool, optional If this is set to True, the axes which are reduced are left in @@ -4186,14 +4186,99 @@ def quantile(a, Notes ----- - Given a vector ``V`` of length ``N``, the q-th quantile of - ``V`` is the value ``q`` of the way from the minimum to the - maximum in a sorted copy of ``V``. The values and distances of - the two nearest neighbors as well as the `interpolation` parameter - will determine the quantile if the normalized ranking does not - match the location of ``q`` exactly. This function is the same as - the median if ``q=0.5``, the same as the minimum if ``q=0.0`` and the - same as the maximum if ``q=1.0``. + Given a vector ``V`` of length ``N``, the q-th quantile of ``V`` is the + value ``q`` of the way from the minimum to the maximum in a sorted copy of + ``V``. The values and distances of the two nearest neighbors as well as the + `interpolation` parameter will determine the quantile if the normalized + ranking does not match the location of ``q`` exactly. This function is the + same as the median if ``q=0.5``, the same as the minimum if ``q=0.0`` and + the same as the maximum if ``q=1.0``. + + This optional `interpolation` parameter specifies the interpolation method + to use when the desired quantile lies between two data points ``i < j``. If + ``g`` is the fractional part of the index surrounded by ``i`` and alpha + and beta are correction constants modifying i and j. + + .. math:: + i + g = (q - alpha) / ( n - alpha - beta + 1 ) + + The different interpolation methods then work as follows + + inverted_cdf: + method 1 of H&F [1]_. + This method gives discontinuous results: + * if g > 0 ; then take j + * if g = 0 ; then take i + + averaged_inverted_cdf: + method 2 of H&F [1]_. + This method give discontinuous results: + * if g > 0 ; then take j + * if g = 0 ; then average between bounds + + closest_observation: + method 3 of H&F [1]_. + This method give discontinuous results: + * if g > 0 ; then take j + * if g = 0 and index is odd ; then take j + * if g = 0 and index is even ; then take i + + interpolated_inverted_cdf: + method 4 of H&F [1]_. + This method give continuous results using: + * alpha = 0 + * beta = 1 + + hazen: + method 5 of H&F [1]_. + This method give continuous results using: + * alpha = 1/2 + * beta = 1/2 + + weibull: + method 6 of H&F [1]_. + This method give continuous results using: + * alpha = 0 + * beta = 0 + + inclusive: + Default method, aliased with "linear". + method 7 of H&F [1]_. + This method give continuous results using: + * alpha = 1 + * beta = 1 + + median_unbiased: + method 8 of H&F [1]_. + This method is probably the best method if the sample + distribution function is unknown (see reference). + This method give continuous results using: + * alpha = 1/3 + * beta = 1/3 + + normal_unbiased: + method 9 of H&F [1]_. + This method is probably the best method if the sample + distribution function is known to be normal. + This method give continuous results using: + * alpha = 3/8 + * beta = 3/8 + + lower: + NumPy method kept for backwards compatibility. + Takes ``i`` as the interpolation point. + + higher: + NumPy method kept for backwards compatibility. + Takes ``j`` as the interpolation point. + + nearest: + NumPy method kept for backwards compatibility. + Takes ``i`` or ``j``, whichever is nearest. + + midpoint: + NumPy method kept for backwards compatibility. + Uses ``(i + j) / 2``. Examples -------- @@ -4223,9 +4308,10 @@ def quantile(a, References ---------- - [Hyndman&Fan] Hyndman, R. J., & Fan, Y. (1996). - Sample quantiles in statistical packages. - The American Statistician, 50(4), 361-365. + .. [1] R. J. Hyndman and Y. Fan, + "Sample quantiles in statistical packages," + The American Statistician, 50(4), pp. 361-365, 1996 + """ q = np.asanyarray(q) if not _quantile_is_valid(q): diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index e90c19b4a..710242d59 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -1253,88 +1253,47 @@ def nanpercentile( Input array or object that can be converted to an array, containing nan values to be ignored. q : array_like of float - Percentile or sequence of percentiles to compute, which must be between - 0 and 100 inclusive. + Percentile or sequence of percentiles to compute, which must be + between 0 and 100 inclusive. axis : {int, tuple of int, None}, optional - Axis or axes along which the percentiles are computed. The - default is to compute the percentile(s) along a flattened - version of the array. + Axis or axes along which the percentiles are computed. The default + is to compute the percentile(s) along a flattened version of the + array. out : ndarray, optional - Alternative output array in which to place the result. It must - have the same shape and buffer length as the expected output, - but the type (of the output) will be cast if necessary. + Alternative output array in which to place the result. It must have + the same shape and buffer length as the expected output, but the + type (of the output) will be cast if necessary. overwrite_input : bool, optional - If True, then allow the input array `a` to be modified by intermediate - calculations, to save memory. In this case, the contents of the input - `a` after this function completes is undefined. - interpolation : str - Possible values: 'linear' (default), - 'inverted_cdf', 'averaged_inverted_cdf', - 'closest_observation', 'interpolated_inverted_cdf', - 'hazen', 'weibull', - 'median_unbiased', 'normal_unbiased', - 'lower', 'higher', - 'midpoint', 'nearest'. - This optional parameter specifies the interpolation method to - use when the desired quantile lies between two data points ``i < j``. - g is the fractional part of the index surrounded by ``i``. - alpha and beta are correction constants modifying i and j: - i + g = (q - alpha) / ( n - alpha - beta + 1 ) - * inverted_cdf: - method 1 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 ; then take i - * averaged_inverted_cdf: - method 2 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 ; then average between bounds - * closest_observation: - method 3 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 and index is odd ; then take j - if g = 0 and index is even ; then take i - * interpolated_inverted_cdf: - method 4 of H&F. - This method give continuous results using: - alpha = 0 - beta = 1 - * hazen: - method 5 of H&F. - This method give continuous results using: - alpha = 1/2 - beta = 1/2 - * weibull: - method 6 of H&F. - This method give continuous results using: - alpha = 0 - beta = 0 - * linear: - Default method. - method 7 of H&F. - This method give continuous results using: - alpha = 1 - beta = 1 - * median_unbiased: - method 8 of H&F. - This method is probably the best method if the sample distribution - function is unknown (see reference). - This method give continuous results using: - alpha = 1/3 - beta = 1/3 - * normal_unbiased: - method 9 of H&F. - This method is probably the best method if the sample distribution - function is known to be normal. - This method give continuous results using: - alpha = 3/8 - beta = 3/8 - * lower: ``i``. - * higher: ``j``. - * nearest: ``i`` or ``j``, whichever is nearest. - * midpoint: ``(i + j) / 2``. + If True, then allow the input array `a` to be modified by + intermediate calculations, to save memory. In this case, the + contents of the input `a` after this function completes is + undefined. + interpolation : str, optional + This parameter specifies the interpolation method to use when the + desired quantile lies between two data points There are many + different methods, some unique to NumPy. See the notes for + explanation. Options: + + * (NPY 1): 'lower' + * (NPY 2): 'higher', + * (NPY 3): 'midpoint' + * (NPY 4): 'nearest' + * (NPY 5): 'linear', aliased with 'inclusive' (default) + + New options: + + * (H&F 1): 'inverted_cdf' + * (H&F 2): 'averaged_inverted_cdf' + * (H&F 3): 'closest_observation' + * (H&F 4): 'interpolated_inverted_cdf' + * (H&F 5): 'hazen' + * (H&F 6): 'weibull' + * (H&F 7): 'inclusive', aliased with 'linear' (default) + * (H&F 8): 'median_unbiased' + * (H&F 9): 'normal_unbiased' + + .. versionadded:: 1.22.0 + keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the @@ -1363,18 +1322,104 @@ def nanpercentile( nanmean nanmedian : equivalent to ``nanpercentile(..., 50)`` percentile, median, mean - nanquantile : equivalent to nanpercentile, but with q in the range [0, 1]. + nanquantile : equivalent to nanpercentile, except q in range [0, 1]. Notes ----- - Given a vector ``V`` of length ``N``, the ``q``-th percentile of - ``V`` is the value ``q/100`` of the way from the minimum to the - maximum in a sorted copy of ``V``. The values and distances of - the two nearest neighbors as well as the `interpolation` parameter - will determine the percentile if the normalized ranking does not - match the location of ``q`` exactly. This function is the same as - the median if ``q=50``, the same as the minimum if ``q=0`` and the - same as the maximum if ``q=100``. + Given a vector ``V`` of length ``N``, the ``q``-th percentile of ``V`` + is the value ``q/100`` of the way from the minimum to the maximum in a + sorted copy of ``V``. The values and distances of the two nearest + neighbors as well as the `interpolation` parameter will determine the + percentile if the normalized ranking does not match the location of + ``q`` exactly. This function is the same as the median if ``q=50``, the + same as the minimum if ``q=0`` and the same as the maximum if + ``q=100``. + + This optional `interpolation` parameter specifies the interpolation + method to use when the desired quantile lies between two data points + ``i < j``. If ``g`` is the fractional part of the index surrounded by + ``i`` and alpha and beta are correction constants modifying i and j. + + .. math:: + i + g = (q - alpha) / ( n - alpha - beta + 1 ) + + The different interpolation methods then work as follows + + inverted_cdf: + method 1 of H&F [1]_. + This method gives discontinuous results: + * if g > 0 ; then take j + * if g = 0 ; then take i + + averaged_inverted_cdf: + method 2 of H&F [1]_. + This method give discontinuous results: + * if g > 0 ; then take j + * if g = 0 ; then average between bounds + + closest_observation: + method 3 of H&F [1]_. + This method give discontinuous results: + * if g > 0 ; then take j + * if g = 0 and index is odd ; then take j + * if g = 0 and index is even ; then take i + + interpolated_inverted_cdf: + method 4 of H&F [1]_. + This method give continuous results using: + * alpha = 0 + * beta = 1 + + hazen: + method 5 of H&F [1]_. + This method give continuous results using: + * alpha = 1/2 + * beta = 1/2 + + weibull: + method 6 of H&F [1]_. + This method give continuous results using: + * alpha = 0 + * beta = 0 + + inclusive: + Default method, aliased with "linear". + method 7 of H&F [1]_. + This method give continuous results using: + * alpha = 1 + * beta = 1 + + median_unbiased: + method 8 of H&F [1]_. + This method is probably the best method if the sample + distribution function is unknown (see reference). + This method give continuous results using: + * alpha = 1/3 + * beta = 1/3 + + normal_unbiased: + method 9 of H&F [1]_. + This method is probably the best method if the sample + distribution function is known to be normal. + This method give continuous results using: + * alpha = 3/8 + * beta = 3/8 + + lower: + NumPy method kept for backwards compatibility. + Takes ``i`` as the interpolation point. + + higher: + NumPy method kept for backwards compatibility. + Takes ``j`` as the interpolation point. + + nearest: + NumPy method kept for backwards compatibility. + Takes ``i`` or ``j``, whichever is nearest. + + midpoint: + NumPy method kept for backwards compatibility. + Uses ``(i + j) / 2``. Examples -------- @@ -1404,6 +1449,12 @@ def nanpercentile( array([7., 2.]) >>> assert not np.all(a==b) + References + ---------- + .. [1] R. J. Hyndman and Y. Fan, + "Sample quantiles in statistical packages," + The American Statistician, 50(4), pp. 361-365, 1996 + """ a = np.asanyarray(a) q = np.true_divide(q, 100.0) @@ -1457,74 +1508,32 @@ def nanquantile( If True, then allow the input array `a` to be modified by intermediate calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - interpolation : str - Possible values: 'linear' (default), - 'inverted_cdf', 'averaged_inverted_cdf', - 'closest_observation', 'interpolated_inverted_cdf', - 'hazen', 'weibull', - 'median_unbiased', 'normal_unbiased', - 'lower', 'higher', - 'midpoint', 'nearest'. - This optional parameter specifies the interpolation method to - use when the desired quantile lies between two data points ``i < j``. - g is the fractional part of the index surrounded by ``i``. - alpha and beta are correction constants modifying i and j: - i + g = (q - alpha) / ( n - alpha - beta + 1 ) - * inverted_cdf: - method 1 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 ; then take i - * averaged_inverted_cdf: - method 2 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 ; then average between bounds - * closest_observation: - method 3 of H&F. - This method give discontinuous results: - if g > 0 ; then take j - if g = 0 and index is odd ; then take j - if g = 0 and index is even ; then take i - * interpolated_inverted_cdf: - method 4 of H&F. - This method give continuous results using: - alpha = 0 - beta = 1 - * hazen: - method 5 of H&F. - This method give continuous results using: - alpha = 1/2 - beta = 1/2 - * weibull: - method 6 of H&F. - This method give continuous results using: - alpha = 0 - beta = 0 - * linear: - Default method. - method 7 of H&F. - This method give continuous results using: - alpha = 1 - beta = 1 - * median_unbiased: - method 8 of H&F. - This method is probably the best method if the sample distribution - function is unknown (see reference). - This method give continuous results using: - alpha = 1/3 - beta = 1/3 - * normal_unbiased: - method 9 of H&F. - This method is probably the best method if the sample distribution - function is known to be normal. - This method give continuous results using: - alpha = 3/8 - beta = 3/8 - * lower: ``i``. - * higher: ``j``. - * nearest: ``i`` or ``j``, whichever is nearest. - * midpoint: ``(i + j) / 2``. + interpolation : str, optional + This parameter specifies the interpolation method to + use when the desired quantile lies between two data points + There are many different methods, some unique to NumPy. See the + notes for explanation. Options: + + * (NPY 1): 'lower' + * (NPY 2): 'higher', + * (NPY 3): 'midpoint' + * (NPY 4): 'nearest' + * (NPY 5): 'linear', aliased with 'inclusive' (default) + + New options: + + * (H&F 1): 'inverted_cdf' + * (H&F 2): 'averaged_inverted_cdf' + * (H&F 3): 'closest_observation' + * (H&F 4): 'interpolated_inverted_cdf' + * (H&F 5): 'hazen' + * (H&F 6): 'weibull' + * (H&F 7): 'inclusive', aliased with 'linear' (default) + * (H&F 8): 'median_unbiased' + * (H&F 9): 'normal_unbiased' + + .. versionadded;: 1.22.0 + keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the @@ -1555,6 +1564,102 @@ def nanquantile( nanmedian : equivalent to ``nanquantile(..., 0.5)`` nanpercentile : same as nanquantile, but with q in the range [0, 100]. + Notes + ----- + Given a vector ``V`` of length ``N``, the q-th quantile of ``V`` is the + value ``q`` of the way from the minimum to the maximum in a sorted copy of + ``V``. The values and distances of the two nearest neighbors as well as the + `interpolation` parameter will determine the quantile if the normalized + ranking does not match the location of ``q`` exactly. This function is the + same as the median if ``q=0.5``, the same as the minimum if ``q=0.0`` and + the same as the maximum if ``q=1.0``. + + This optional `interpolation` parameter specifies the interpolation method + to use when the desired quantile lies between two data points ``i < j``. If + ``g`` is the fractional part of the index surrounded by ``i`` and alpha + and beta are correction constants modifying i and j. + + .. math:: + i + g = (q - alpha) / ( n - alpha - beta + 1 ) + + The different interpolation methods then work as follows + + inverted_cdf: + method 1 of H&F [1]_. + This method gives discontinuous results: + * if g > 0 ; then take j + * if g = 0 ; then take i + + averaged_inverted_cdf: + method 2 of H&F [1]_. + This method give discontinuous results: + * if g > 0 ; then take j + * if g = 0 ; then average between bounds + + closest_observation: + method 3 of H&F [1]_. + This method give discontinuous results: + * if g > 0 ; then take j + * if g = 0 and index is odd ; then take j + * if g = 0 and index is even ; then take i + + interpolated_inverted_cdf: + method 4 of H&F [1]_. + This method give continuous results using: + * alpha = 0 + * beta = 1 + + hazen: + method 5 of H&F [1]_. + This method give continuous results using: + * alpha = 1/2 + * beta = 1/2 + + weibull: + method 6 of H&F [1]_. + This method give continuous results using: + * alpha = 0 + * beta = 0 + + inclusive: + Default method, aliased with "linear". + method 7 of H&F [1]_. + This method give continuous results using: + * alpha = 1 + * beta = 1 + + median_unbiased: + method 8 of H&F [1]_. + This method is probably the best method if the sample + distribution function is unknown (see reference). + This method give continuous results using: + * alpha = 1/3 + * beta = 1/3 + + normal_unbiased: + method 9 of H&F [1]_. + This method is probably the best method if the sample + distribution function is known to be normal. + This method give continuous results using: + * alpha = 3/8 + * beta = 3/8 + + lower: + NumPy method kept for backwards compatibility. + Takes ``i`` as the interpolation point. + + higher: + NumPy method kept for backwards compatibility. + Takes ``j`` as the interpolation point. + + nearest: + NumPy method kept for backwards compatibility. + Takes ``i`` or ``j``, whichever is nearest. + + midpoint: + NumPy method kept for backwards compatibility. + Uses ``(i + j) / 2``. + Examples -------- >>> a = np.array([[10., 7., 4.], [3., 2., 1.]]) @@ -1581,6 +1686,13 @@ def nanquantile( >>> np.nanquantile(b, 0.5, axis=1, overwrite_input=True) array([7., 2.]) >>> assert not np.all(a==b) + + References + ---------- + .. [1] R. J. Hyndman and Y. Fan, + "Sample quantiles in statistical packages," + The American Statistician, 50(4), pp. 361-365, 1996 + """ a = np.asanyarray(a) q = np.asanyarray(q) -- cgit v1.2.1 From ab19ed256bf9b20340c92cebcfd6158241122c88 Mon Sep 17 00:00:00 2001 From: abel Date: Tue, 19 Oct 2021 12:10:16 +0200 Subject: Fix _lerp - some changes were unrelated to the PR and have been reverted, including, renaming and moving the logic around. - Also renamed _quantile_ureduce_func to its original name --- numpy/lib/function_base.py | 42 ++++++++++++++++------------------- numpy/lib/tests/test_function_base.py | 14 ++++++------ 2 files changed, 26 insertions(+), 30 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index dbaba87f9..353490fc2 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4329,7 +4329,7 @@ def _quantile_unchecked(a, keepdims=False): """Assumes that q is in [0, 1], and is an ndarray""" r, k = _ureduce(a, - func=_quantiles_ureduce_func, + func=_quantile_ureduce_func, q=q, axis=axis, out=out, @@ -4383,22 +4383,22 @@ def _get_gamma(virtual_indexes: np.array, return np.asanyarray(gamma) -def _linear_interpolation_formula( - left: np.array, right: np.array, gamma: np.array, out: np.array = None -) -> np.array: +def _lerp(a, b, t, out=None): """ Compute the linear interpolation weighted by gamma on each point of two same shape array. """ - # Equivalent to gamma * right + (1 - gamma) * left - # see gh-14685 - diff_right_left = subtract(right, left) - result = asanyarray(add(left, diff_right_left * gamma, out=out)) - result = subtract(right, - diff_right_left * (1 - gamma), - out=result, - where=gamma >= 0.5) - return result + # Equivalent to gamma * right + (1 - gamma) * left, see gh-14685 + diff_b_a = subtract(b, a) + # asanyarray is a stop-gap until gh-13105 + lerp_interpolation = asanyarray(add(a, diff_b_a * t, out=out)) + lerp_interpolation = subtract(b, + diff_b_a * (1 - t), + out=lerp_interpolation, + where=t >= 0.5) + if lerp_interpolation.ndim == 0 and out is None: + lerp_interpolation = lerp_interpolation[()] # unpack 0d arrays + return lerp_interpolation def _get_gamma_mask(shape, default_value, conditioned_value, where): @@ -4430,7 +4430,7 @@ def _inverted_cdf(n, quantiles): gamma_fun) -def _quantiles_ureduce_func( +def _quantile_ureduce_func( a: np.array, q: np.array, axis: int = None, @@ -4460,10 +4460,6 @@ def _quantiles_ureduce_func( axis=axis, interpolation=interpolation, out=out) - if result.ndim == 0 and out is None: - result = result[()] # unpack 0d arrays - elif result.size == 1 and out is None and q.ndim == 0: - result = result[0] return result @@ -4551,7 +4547,7 @@ def _quantile( # cannot contain nan arr.partition(virtual_indexes.ravel(), axis=0) slices_having_nans = np.array(False, dtype=bool) - result = np.asanyarray(take(arr, virtual_indexes, axis=0, out=out)) + result = take(arr, virtual_indexes, axis=0, out=out) else: previous_indexes, next_indexes = _get_indexes(arr, virtual_indexes, @@ -4578,10 +4574,10 @@ def _quantile( interpolation) result_shape = virtual_indexes.shape + (1,) * (arr.ndim - 1) gamma = gamma.reshape(result_shape) - result = _linear_interpolation_formula(previous, - next, - gamma, - out=out) + result = _lerp(previous, + next, + gamma, + out=out) if np.any(slices_having_nans): if result.ndim == 0 and out is None: # can't write to a scalar diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 4f0db7bdb..d59f3a85d 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -3486,8 +3486,8 @@ class TestLerp: b = st.floats(allow_nan=False, allow_infinity=False, min_value=-1e300, max_value=1e300)) def test_linear_interpolation_formula_monotonic(self, t0, t1, a, b): - l0 = nfb._linear_interpolation_formula(a, b, t0) - l1 = nfb._linear_interpolation_formula(a, b, t1) + l0 = nfb._lerp(a, b, t0) + l1 = nfb._lerp(a, b, t1) if t0 == t1 or a == b: assert l0 == l1 # uninteresting elif (t0 < t1) == (a < b): @@ -3503,9 +3503,9 @@ class TestLerp: min_value=-1e300, max_value=1e300)) def test_linear_interpolation_formula_bounded(self, t, a, b): if a <= b: - assert a <= nfb._linear_interpolation_formula(a, b, t) <= b + assert a <= nfb._lerp(a, b, t) <= b else: - assert b <= nfb._linear_interpolation_formula(a, b, t) <= a + assert b <= nfb._lerp(a, b, t) <= a @hypothesis.given(t=st.floats(allow_nan=False, allow_infinity=False, min_value=0, max_value=1), @@ -3515,15 +3515,15 @@ class TestLerp: min_value=-1e300, max_value=1e300)) def test_linear_interpolation_formula_symmetric(self, t, a, b): # double subtraction is needed to remove the extra precision of t < 0.5 - left = nfb._linear_interpolation_formula(a, b, 1 - (1 - t)) - right = nfb._linear_interpolation_formula(b, a, 1 - t) + left = nfb._lerp(a, b, 1 - (1 - t)) + right = nfb._lerp(b, a, 1 - t) assert left == right def test_linear_interpolation_formula_0d_inputs(self): a = np.array(2) b = np.array(5) t = np.array(0.2) - assert nfb._linear_interpolation_formula(a, b, t) == 2.6 + assert nfb._lerp(a, b, t) == 2.6 class TestMedian: -- cgit v1.2.1 From d5e275b2bf65c1848203f6bff7606623793daeed Mon Sep 17 00:00:00 2001 From: abel Date: Tue, 19 Oct 2021 15:22:05 +0200 Subject: DOC: Improve quantile documentation Also removed unused imports --- numpy/lib/function_base.py | 87 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 20 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 353490fc2..a5ccc1189 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -9,8 +9,7 @@ import numpy.core.numeric as _nx from numpy.core import transpose from numpy.core.numeric import ( ones, zeros_like, arange, concatenate, array, asarray, asanyarray, empty, - ndarray, around, floor, ceil, take, dot, where, intp, - integer, isscalar, absolute + ndarray, take, dot, where, intp, integer, isscalar, absolute ) from numpy.core.umath import ( pi, add, arctan2, frompyfunc, cos, less_equal, sqrt, sin, @@ -51,7 +50,22 @@ __all__ = [ 'quantile' ] - +# _QuantileInterpolation is a dictionary listing all the supported +# interpolation methods to compute quantile/percentile. +# +# Below virtual_index refer to the index of the element where the percentile +# would be found in the sorted sample. +# When the sample contains exactly the percentile wanted, the virtual_index is +# an integer to the index of this element. +# When the percentile wanted is in between two elements, the virtual_index +# is made of a integer part (a.k.a 'i' or 'left') and a fractional part +# (a.k.a 'g' or 'gamma') +# +# Each _QuantileInterpolation have two properties +# get_virtual_index : Callable +# The function used to compute the virtual_index. +# fix_gamma : Callable +# A function used for discret methods to force the index to a specific value. _QuantileInterpolation = dict( # --- HYNDMAN and FAN methods # Discrete methods @@ -75,33 +89,33 @@ _QuantileInterpolation = dict( # Continuous methods interpolated_inverted_cdf=dict( get_virtual_index=lambda n, quantiles: - _virtual_index_formula(n, quantiles, 0, 1), + _compute_virtual_index(n, quantiles, 0, 1), fix_gamma=lambda gamma, _: gamma, ), hazen=dict( get_virtual_index=lambda n, quantiles: - _virtual_index_formula(n, quantiles, 0.5, 0.5), + _compute_virtual_index(n, quantiles, 0.5, 0.5), fix_gamma=lambda gamma, _: gamma, ), weibull=dict( get_virtual_index=lambda n, quantiles: - _virtual_index_formula(n, quantiles, 0, 0), + _compute_virtual_index(n, quantiles, 0, 0), fix_gamma=lambda gamma, _: gamma, ), # Default value linear=dict( get_virtual_index=lambda n, quantiles: - _virtual_index_formula(n, quantiles, 1, 1), + _compute_virtual_index(n, quantiles, 1, 1), fix_gamma=lambda gamma, _: gamma, ), median_unbiased=dict( get_virtual_index=lambda n, quantiles: - _virtual_index_formula(n, quantiles, 1 / 3.0, 1 / 3.0), + _compute_virtual_index(n, quantiles, 1 / 3.0, 1 / 3.0), fix_gamma=lambda gamma, _: gamma, ), normal_unbiased=dict( get_virtual_index=lambda n, quantiles: - _virtual_index_formula(n, quantiles, 3 / 8.0, 3 / 8.0), + _compute_virtual_index(n, quantiles, 3 / 8.0, 3 / 8.0), fix_gamma=lambda gamma, _: gamma, ), # --- OTHER METHODS fixme add deprecated ? @@ -3945,6 +3959,12 @@ def percentile(a, ``i < j``. If ``g`` is the fractional part of the index surrounded by ``i`` and alpha and beta are correction constants modifying i and j. + Below, 'q' is the quantile value, 'n' is the samle size and + alpha and beta are constants. + The following formula gives an interpolation "i + g" of where the quantile + would be in the sorted sample. + With 'i' being the floor and 'g' the fractional part of the result. + .. math:: i + g = (q - alpha) / ( n - alpha - beta + 1 ) @@ -4353,13 +4373,22 @@ def _quantile_is_valid(q): return True -def _virtual_index_formula(n: np.array, - quantiles: np.array, - alpha: float, - beta: float) -> np.array: +def _compute_virtual_index(n, quantiles, alpha: float, beta: float): """ Compute the floating point indexes of an array for the linear interpolation of quantiles. + n : array_like + The sample sizes. + quantiles : array_like + The quantiles values. + alpha : float + A constant used to correct the index computed. + beta : float + A constant used to correct the index computed. + + alpha and beta values depend on the chosen method + (see quantile documentation) + Reference: Hyndman&Fan paper "Sample Quantiles in Statistical Packages", DOI: 10.1080/00031305.1996.10473566 @@ -4369,14 +4398,24 @@ def _virtual_index_formula(n: np.array, ) - 1 -def _get_gamma(virtual_indexes: np.array, - previous_indexes: np.array, - interpolation: _QuantileInterpolation) -> np.array: +def _get_gamma(virtual_indexes, + previous_indexes, + interpolation: _QuantileInterpolation): """ - Compute the gamma (a.k.a 'm' or weight) for the linear interpolation + Compute gamma (a.k.a 'm' or 'weight') for the linear interpolation of quantiles. - When gamma == 0 the left bound will be picked, - When gamma == 1 the right bound will be. + + virtual_indexes : array_like + The indexes where the percentile is supposed to be found in the sorted + sample. + previous_indexes : array_like + The floor values of virtual_indexes. + interpolation : _QuantileInterpolation + The interpolation method chosen, which may have a specific rule + modifying gamma. + + gamma is usually the fractional part of virtual_indexes but can be modified + by the interpolation method. """ gamma = np.asanyarray(virtual_indexes - previous_indexes) gamma = interpolation["fix_gamma"](gamma, virtual_indexes) @@ -4387,8 +4426,16 @@ def _lerp(a, b, t, out=None): """ Compute the linear interpolation weighted by gamma on each point of two same shape array. + + a : array_like + Left bound. + b : array_like + Right bound. + t : array_like + The interpolation weight. + out : array_like + Output array. """ - # Equivalent to gamma * right + (1 - gamma) * left, see gh-14685 diff_b_a = subtract(b, a) # asanyarray is a stop-gap until gh-13105 lerp_interpolation = asanyarray(add(a, diff_b_a * t, out=out)) -- cgit v1.2.1 From 2a5422da7cb6759d75477738cc192fee3ca2a19c Mon Sep 17 00:00:00 2001 From: abel Date: Tue, 19 Oct 2021 17:55:06 +0200 Subject: Fix issue with nan scalar Also added unit test for it. --- numpy/lib/function_base.py | 4 ++-- numpy/lib/tests/test_function_base.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index a5ccc1189..a16781f53 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3959,7 +3959,7 @@ def percentile(a, ``i < j``. If ``g`` is the fractional part of the index surrounded by ``i`` and alpha and beta are correction constants modifying i and j. - Below, 'q' is the quantile value, 'n' is the samle size and + Below, 'q' is the quantile value, 'n' is the sample size and alpha and beta are constants. The following formula gives an interpolation "i + g" of where the quantile would be in the sorted sample. @@ -4628,7 +4628,7 @@ def _quantile( if np.any(slices_having_nans): if result.ndim == 0 and out is None: # can't write to a scalar - result = np.array(np.nan, dtype=arr.dtype) + result = arr.dtype.type(np.nan) else: result[..., slices_having_nans] = np.nan return result diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index d59f3a85d..f9854c568 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -3475,6 +3475,12 @@ class TestQuantile: quantile = np.quantile(arr, p0) assert_equal(np.sort(quantile), quantile) + def test_quantile_scalar_nan(self): + a = np.array([[10., 7., 4.], [3., 2., 1.]]) + a[0][1] = np.nan + actual = np.quantile(a, 0.5) + assert np.isscalar(actual) + assert_equal(np.quantile(a, 0.5), np.nan) class TestLerp: @hypothesis.given(t0=st.floats(allow_nan=False, allow_infinity=False, -- cgit v1.2.1 From 8413b5abf27221fb2bea070871c7cd8f8da5519c Mon Sep 17 00:00:00 2001 From: abel Date: Thu, 21 Oct 2021 09:59:53 +0200 Subject: MAINT: Clean following PR comments --- numpy/lib/function_base.py | 9 +++------ numpy/lib/nanfunctions.py | 15 ++++----------- 2 files changed, 7 insertions(+), 17 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index a16781f53..c33626df4 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3915,7 +3915,7 @@ def percentile(a, * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' - .. versionadded;: 1.22.0 + .. versionchanged:: 1.22.0 keepdims : bool, optional If this is set to True, the axes which are reduced are left in @@ -4439,10 +4439,7 @@ def _lerp(a, b, t, out=None): diff_b_a = subtract(b, a) # asanyarray is a stop-gap until gh-13105 lerp_interpolation = asanyarray(add(a, diff_b_a * t, out=out)) - lerp_interpolation = subtract(b, - diff_b_a * (1 - t), - out=lerp_interpolation, - where=t >= 0.5) + subtract(b, diff_b_a * (1 - t), out=lerp_interpolation, where=t >= 0.5) if lerp_interpolation.ndim == 0 and out is None: lerp_interpolation = lerp_interpolation[()] # unpack 0d arrays return lerp_interpolation @@ -4580,7 +4577,7 @@ def _quantile( except KeyError: raise ValueError( f"{interpolation!r} is not a valid interpolation. Use one of: " - f"{_QuantileInterpolation.keys()}") + f"{_QuantileInterpolation.keys()}") from None virtual_indexes = interpolation["get_virtual_index"](values_count, quantiles) virtual_indexes = np.asanyarray(virtual_indexes) diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 710242d59..c3333a83a 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -1532,7 +1532,7 @@ def nanquantile( * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' - .. versionadded;: 1.22.0 + .. versionchanged:: 1.22.0 keepdims : bool, optional If this is set to True, the axes which are reduced are left in @@ -1729,12 +1729,8 @@ def _nanquantile_unchecked( return r -def _nanquantile_ureduce_func(a, - q, - axis=None, - out=None, - overwrite_input=False, - interpolation= "linear"): +def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, + interpolation="linear"): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce @@ -1757,10 +1753,7 @@ def _nanquantile_ureduce_func(a, return result -def _nanquantile_1d(arr1d, - q, - overwrite_input=False, - interpolation= "linear"): +def _nanquantile_1d(arr1d, q, overwrite_input=False, interpolation="linear"): """ Private function for rank 1 arrays. Compute quantile ignoring NaNs. See nanpercentile for parameter usage -- cgit v1.2.1 From 98cf811e27138c41e365222cb70f06d70c0db4ee Mon Sep 17 00:00:00 2001 From: abel Date: Thu, 21 Oct 2021 12:31:06 +0200 Subject: TST: Add extrapolation tests --- numpy/lib/tests/test_function_base.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'numpy') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index f9854c568..53254c3e5 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -3032,6 +3032,15 @@ class TestPercentile: assert_equal(np.percentile(np.arange(10, dtype=dtype), 49, interpolation='nearest'), 4) + def test_linear_interpolation_extrapolation(self): + arr = np.random.rand(5) + + actual = np.percentile(arr, 100) + np.testing.assert_equal(actual, arr.max()) + + actual = np.percentile(arr, 0) + np.testing.assert_equal(actual, arr.min()) + def test_sequence(self): x = np.arange(8) * 0.5 assert_equal(np.percentile(x, [0, 100, 50]), [0, 3.5, 1.75]) -- cgit v1.2.1 From 2faf8edd635c8c700a3f5215cc747e268541b6bc Mon Sep 17 00:00:00 2001 From: abel Date: Thu, 21 Oct 2021 16:13:29 +0200 Subject: TST: Add parametrize for interpolation methods --- numpy/lib/tests/test_function_base.py | 134 +++++++++++----------------------- 1 file changed, 43 insertions(+), 91 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 53254c3e5..22228405a 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2914,98 +2914,50 @@ class TestPercentile: np.testing.assert_equal(res, np.NAN) np.testing.assert_equal(res.dtype, arr.dtype) - TYPE_CODES = np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O" - - @pytest.mark.parametrize("dtype", TYPE_CODES) - def test_linear_inverted_cdf(self, dtype): - # METHOD 1 of H&F - arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) - res = np.percentile( - arr, - 40.0, - interpolation="inverted_cdf") - np.testing.assert_almost_equal(res, 20, 15) - - @pytest.mark.parametrize("dtype", TYPE_CODES) - def test_linear_averaged_inverted_cdf(self, dtype): - # METHOD 2 of H&F - arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) - res = np.percentile( - arr, - 40.0, - interpolation="averaged_inverted_cdf") - np.testing.assert_almost_equal(res, 27.5, 15) - - @pytest.mark.parametrize("dtype", TYPE_CODES) - def test_linear_closest_observation(self, dtype): - # METHOD 3 of H&F - arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) - res = np.percentile( - arr, - 40.0, - interpolation="closest_observation") - np.testing.assert_almost_equal(res, 20, 15) - - @pytest.mark.parametrize("dtype", TYPE_CODES) - def test_linear_interpolated_inverted_cdf(self, dtype): - # METHOD 4 of H&F - arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) - res = np.percentile( - arr, - 40.0, - interpolation="interpolated_inverted_cdf") - np.testing.assert_almost_equal(res, 20, 15) - - @pytest.mark.parametrize("dtype", TYPE_CODES) - def test_linear_hazen(self, dtype): - # METHOD 5 of H&F - arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) - res = np.percentile( - arr, - 40.0, - interpolation="hazen") - np.testing.assert_almost_equal(res, 27.5, 15) - - @pytest.mark.parametrize("dtype", TYPE_CODES) - def test_linear_weibull(self, dtype): - # METHOD 6 of H&F - arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) - res = np.percentile( - arr, - 40.0, - interpolation="weibull") - np.testing.assert_almost_equal(res, 26, 15) - - @pytest.mark.parametrize("dtype", TYPE_CODES) - def test_linear_linear(self, dtype): - # METHOD 7 of H&F - # Test defaults - assert_equal(np.percentile(range(10), 50), 4.5) - # explicit interpolation_method (the default) - res = np.percentile([15.0, 20.0, 35.0, 40.0, 50.0], - 40, - interpolation="linear") - np.testing.assert_almost_equal(res, 29, 15) - - @pytest.mark.parametrize("dtype", TYPE_CODES) - def test_linear_median_unbiased(self, dtype): - # METHOD 8 of H&F - arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) - res = np.percentile( - arr, - 40.0, - interpolation="median_unbiased") - np.testing.assert_almost_equal(res, 27, 14) + H_F_TYPE_CODES = [(int_type, np.float64) + for int_type in np.typecodes["AllInteger"] + ] + [(np.float16, np.float64), + (np.float32, np.float64), + (np.float64, np.float64), + (np.float128, np.float128), + (np.complex64, np.complex128), + (np.complex128, np.complex128), + (np.complex256, np.complex256), + (np.dtype("O"), np.float64)] + + @pytest.mark.parametrize(["input_dtype", "expected_dtype"], H_F_TYPE_CODES) + @pytest.mark.parametrize(["interpolation", "expected"], + [("inverted_cdf", 20), + ("averaged_inverted_cdf", 27.5), + ("closest_observation", 20), + ("interpolated_inverted_cdf", 20), + ("hazen", 27.5), + ("weibull", 26), + ("linear", 29), + ("median_unbiased", 27), + ("normal_unbiased", 27.125), + ]) + def test_linear_interpolation(self, + interpolation, + expected, + input_dtype, + expected_dtype): + arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=input_dtype) + actual = np.percentile(arr, 40.0, interpolation=interpolation) + + np.testing.assert_almost_equal(actual, expected, 14) + + if interpolation in ["inverted_cdf", "closest_observation"]: + if input_dtype == "O": + np.testing.assert_equal(np.asarray(actual).dtype, np.float64) + else: + np.testing.assert_equal(np.asarray(actual).dtype, + np.dtype(input_dtype)) + else: + np.testing.assert_equal(np.asarray(actual).dtype, + np.dtype(expected_dtype)) - @pytest.mark.parametrize("dtype", TYPE_CODES) - def test_linear_normal_unbiased(self, dtype): - # METHOD 9 of H&F - arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype) - res = np.percentile( - arr, - 40.0, - interpolation="normal_unbiased") - np.testing.assert_almost_equal(res, 27.125, 15) + TYPE_CODES = np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O" @pytest.mark.parametrize("dtype", TYPE_CODES) def test_lower_higher(self, dtype): -- cgit v1.2.1 From f48e40636879d0f33c0cda8f76db06335043344b Mon Sep 17 00:00:00 2001 From: abel Date: Thu, 21 Oct 2021 16:26:10 +0200 Subject: DOC: Update _InterpolationKind --- numpy/lib/function_base.pyi | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.pyi b/numpy/lib/function_base.pyi index 9a53b24f2..82c625fed 100644 --- a/numpy/lib/function_base.pyi +++ b/numpy/lib/function_base.pyi @@ -501,11 +501,19 @@ def median( ) -> _ArrayType: ... _InterpolationKind = L[ + "inverted_cdf", + "averaged_inverted_cdf", + "closest_observation", + "interpolated_inverted_cdf", + "hazen", + "weibull", + "linear", + "median_unbiased", + "normal_unbiased", "lower", "higher", "midpoint", "nearest", - "linear", ] @overload -- cgit v1.2.1 From a8218af306e7639f2136c8e915a5b9ea1f34356a Mon Sep 17 00:00:00 2001 From: abel Date: Fri, 22 Oct 2021 13:35:40 +0200 Subject: TST: Make use of clongdouble and longdouble On some platforms float128 and complex256 do not exist. Using (c)longdouble aliases should work on all platforms. --- numpy/lib/tests/test_function_base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 22228405a..d5fa012f1 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2919,10 +2919,10 @@ class TestPercentile: ] + [(np.float16, np.float64), (np.float32, np.float64), (np.float64, np.float64), - (np.float128, np.float128), + (np.longdouble, np.longdouble), (np.complex64, np.complex128), (np.complex128, np.complex128), - (np.complex256, np.complex256), + (np.clongdouble, np.clongdouble), (np.dtype("O"), np.float64)] @pytest.mark.parametrize(["input_dtype", "expected_dtype"], H_F_TYPE_CODES) -- cgit v1.2.1 From 1574011425611a0e43715f81c494004c26b95e92 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 1 Nov 2021 18:00:20 -0500 Subject: MAINT: Remove unnecessary import of _QuantileInterpolation --- numpy/lib/nanfunctions.py | 1 - 1 file changed, 1 deletion(-) (limited to 'numpy') diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index c3333a83a..3189d2369 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -23,7 +23,6 @@ Functions import functools import warnings import numpy as np -from numpy.lib.function_base import _QuantileInterpolation from numpy.lib import function_base from numpy.core import overrides -- cgit v1.2.1 From f7911c67176c1d370be27726e87195699e4b581e Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Thu, 4 Nov 2021 13:26:37 -0500 Subject: DOC: Refer to the quantile/percentile notes for nan versions --- numpy/lib/function_base.py | 2 +- numpy/lib/nanfunctions.py | 203 +-------------------------------------------- 2 files changed, 4 insertions(+), 201 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index c33626df4..07d8a4269 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3893,7 +3893,7 @@ def percentile(a, `a` after this function completes is undefined. interpolation : str, optional This parameter specifies the interpolation method to - use when the desired quantile lies between two data points + use when the desired percentile lies between two data points There are many different methods, some unique to NumPy. See the notes for explanation. Options diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 3189d2369..9fab77f45 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -1269,7 +1269,7 @@ def nanpercentile( undefined. interpolation : str, optional This parameter specifies the interpolation method to use when the - desired quantile lies between two data points There are many + desired percentile lies between two data points There are many different methods, some unique to NumPy. See the notes for explanation. Options: @@ -1325,100 +1325,7 @@ def nanpercentile( Notes ----- - Given a vector ``V`` of length ``N``, the ``q``-th percentile of ``V`` - is the value ``q/100`` of the way from the minimum to the maximum in a - sorted copy of ``V``. The values and distances of the two nearest - neighbors as well as the `interpolation` parameter will determine the - percentile if the normalized ranking does not match the location of - ``q`` exactly. This function is the same as the median if ``q=50``, the - same as the minimum if ``q=0`` and the same as the maximum if - ``q=100``. - - This optional `interpolation` parameter specifies the interpolation - method to use when the desired quantile lies between two data points - ``i < j``. If ``g`` is the fractional part of the index surrounded by - ``i`` and alpha and beta are correction constants modifying i and j. - - .. math:: - i + g = (q - alpha) / ( n - alpha - beta + 1 ) - - The different interpolation methods then work as follows - - inverted_cdf: - method 1 of H&F [1]_. - This method gives discontinuous results: - * if g > 0 ; then take j - * if g = 0 ; then take i - - averaged_inverted_cdf: - method 2 of H&F [1]_. - This method give discontinuous results: - * if g > 0 ; then take j - * if g = 0 ; then average between bounds - - closest_observation: - method 3 of H&F [1]_. - This method give discontinuous results: - * if g > 0 ; then take j - * if g = 0 and index is odd ; then take j - * if g = 0 and index is even ; then take i - - interpolated_inverted_cdf: - method 4 of H&F [1]_. - This method give continuous results using: - * alpha = 0 - * beta = 1 - - hazen: - method 5 of H&F [1]_. - This method give continuous results using: - * alpha = 1/2 - * beta = 1/2 - - weibull: - method 6 of H&F [1]_. - This method give continuous results using: - * alpha = 0 - * beta = 0 - - inclusive: - Default method, aliased with "linear". - method 7 of H&F [1]_. - This method give continuous results using: - * alpha = 1 - * beta = 1 - - median_unbiased: - method 8 of H&F [1]_. - This method is probably the best method if the sample - distribution function is unknown (see reference). - This method give continuous results using: - * alpha = 1/3 - * beta = 1/3 - - normal_unbiased: - method 9 of H&F [1]_. - This method is probably the best method if the sample - distribution function is known to be normal. - This method give continuous results using: - * alpha = 3/8 - * beta = 3/8 - - lower: - NumPy method kept for backwards compatibility. - Takes ``i`` as the interpolation point. - - higher: - NumPy method kept for backwards compatibility. - Takes ``j`` as the interpolation point. - - nearest: - NumPy method kept for backwards compatibility. - Takes ``i`` or ``j``, whichever is nearest. - - midpoint: - NumPy method kept for backwards compatibility. - Uses ``(i + j) / 2``. + For more information please see `numpy.percentile` Examples -------- @@ -1448,12 +1355,6 @@ def nanpercentile( array([7., 2.]) >>> assert not np.all(a==b) - References - ---------- - .. [1] R. J. Hyndman and Y. Fan, - "Sample quantiles in statistical packages," - The American Statistician, 50(4), pp. 361-365, 1996 - """ a = np.asanyarray(a) q = np.true_divide(q, 100.0) @@ -1565,99 +1466,7 @@ def nanquantile( Notes ----- - Given a vector ``V`` of length ``N``, the q-th quantile of ``V`` is the - value ``q`` of the way from the minimum to the maximum in a sorted copy of - ``V``. The values and distances of the two nearest neighbors as well as the - `interpolation` parameter will determine the quantile if the normalized - ranking does not match the location of ``q`` exactly. This function is the - same as the median if ``q=0.5``, the same as the minimum if ``q=0.0`` and - the same as the maximum if ``q=1.0``. - - This optional `interpolation` parameter specifies the interpolation method - to use when the desired quantile lies between two data points ``i < j``. If - ``g`` is the fractional part of the index surrounded by ``i`` and alpha - and beta are correction constants modifying i and j. - - .. math:: - i + g = (q - alpha) / ( n - alpha - beta + 1 ) - - The different interpolation methods then work as follows - - inverted_cdf: - method 1 of H&F [1]_. - This method gives discontinuous results: - * if g > 0 ; then take j - * if g = 0 ; then take i - - averaged_inverted_cdf: - method 2 of H&F [1]_. - This method give discontinuous results: - * if g > 0 ; then take j - * if g = 0 ; then average between bounds - - closest_observation: - method 3 of H&F [1]_. - This method give discontinuous results: - * if g > 0 ; then take j - * if g = 0 and index is odd ; then take j - * if g = 0 and index is even ; then take i - - interpolated_inverted_cdf: - method 4 of H&F [1]_. - This method give continuous results using: - * alpha = 0 - * beta = 1 - - hazen: - method 5 of H&F [1]_. - This method give continuous results using: - * alpha = 1/2 - * beta = 1/2 - - weibull: - method 6 of H&F [1]_. - This method give continuous results using: - * alpha = 0 - * beta = 0 - - inclusive: - Default method, aliased with "linear". - method 7 of H&F [1]_. - This method give continuous results using: - * alpha = 1 - * beta = 1 - - median_unbiased: - method 8 of H&F [1]_. - This method is probably the best method if the sample - distribution function is unknown (see reference). - This method give continuous results using: - * alpha = 1/3 - * beta = 1/3 - - normal_unbiased: - method 9 of H&F [1]_. - This method is probably the best method if the sample - distribution function is known to be normal. - This method give continuous results using: - * alpha = 3/8 - * beta = 3/8 - - lower: - NumPy method kept for backwards compatibility. - Takes ``i`` as the interpolation point. - - higher: - NumPy method kept for backwards compatibility. - Takes ``j`` as the interpolation point. - - nearest: - NumPy method kept for backwards compatibility. - Takes ``i`` or ``j``, whichever is nearest. - - midpoint: - NumPy method kept for backwards compatibility. - Uses ``(i + j) / 2``. + For more information please see `numpy.quantile` Examples -------- @@ -1686,12 +1495,6 @@ def nanquantile( array([7., 2.]) >>> assert not np.all(a==b) - References - ---------- - .. [1] R. J. Hyndman and Y. Fan, - "Sample quantiles in statistical packages," - The American Statistician, 50(4), pp. 361-365, 1996 - """ a = np.asanyarray(a) q = np.asanyarray(q) -- cgit v1.2.1 From 6f6802b685b254a0c244ec24244b0a61f4a3c91c Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Thu, 4 Nov 2021 15:07:42 -0500 Subject: ENH: Check for `__repr__` and `__str__` implementation This adds a check whether `__repr__` or `__str__` appear to be inherited. If they are, it currently leads to cryptic errors adding an unnecessary difficulty to creating a custom user DType. --- numpy/core/src/multiarray/experimental_public_dtype_api.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/experimental_public_dtype_api.c b/numpy/core/src/multiarray/experimental_public_dtype_api.c index ef5030471..4b9c7199b 100644 --- a/numpy/core/src/multiarray/experimental_public_dtype_api.c +++ b/numpy/core/src/multiarray/experimental_public_dtype_api.c @@ -131,6 +131,14 @@ PyArrayInitDTypeMeta_FromSpec( return -1; } + if (((PyTypeObject *)DType)->tp_repr == PyArrayDescr_Type.tp_repr + || ((PyTypeObject *)DType)->tp_str == PyArrayDescr_Type.tp_str) { + PyErr_SetString(PyExc_TypeError, + "A custom DType must implement `__repr__` and `__str__` since " + "the default inherited version (currently) fails."); + return -1; + } + if (spec->typeobj == NULL || !PyType_Check(spec->typeobj)) { PyErr_SetString(PyExc_TypeError, "Not giving a type object is currently not supported, but " -- cgit v1.2.1 From f9c2573898899ee3809dfd4ebba113f6de2d528b Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 5 Nov 2021 00:05:04 +0100 Subject: PERF: Speedup np.quantile. Avoiding the unnecessary calls to moveaxis() speed up `np.quantile(x, .5)` (`x = np.random.rand(1000)`) by ~10% (although there's a lot of variability) for me. --- numpy/lib/function_base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 07d8a4269..4d57acdb2 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4567,7 +4567,8 @@ def _quantile( # The dimensions of `q` are prepended to the output shape, so we need the # axis being sampled from `arr` to be last. DATA_AXIS = 0 - arr = np.moveaxis(arr, axis, destination=DATA_AXIS) + if axis != DATA_AXIS: # But moveaxis is slow, so only call it if axis!=0. + arr = np.moveaxis(arr, axis, destination=DATA_AXIS) # --- Computation of indexes # Index where to find the value in the sorted array. # Virtual because it is a floating point value, not an valid index. -- cgit v1.2.1 From b9c0a231665a30836964e8c94ba2f83a08255be2 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 5 Nov 2021 11:11:12 +0100 Subject: MAINT: Add `IS_PYSTON` to `np.testing.__all__` --- numpy/testing/__init__.py | 3 +-- numpy/testing/_private/utils.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'numpy') diff --git a/numpy/testing/__init__.py b/numpy/testing/__init__.py index a008f5828..6e06c5b49 100644 --- a/numpy/testing/__init__.py +++ b/numpy/testing/__init__.py @@ -8,8 +8,7 @@ away. from unittest import TestCase from ._private.utils import * -from ._private.utils import (_assert_valid_refcount, _gen_alignment_data, - IS_PYSTON) +from ._private.utils import (_assert_valid_refcount, _gen_alignment_data) from ._private import extbuild, decorators as dec from ._private.nosetester import ( run_module_suite, NoseTester as Tester diff --git a/numpy/testing/_private/utils.py b/numpy/testing/_private/utils.py index 3d52f74b2..4c6b64bc9 100644 --- a/numpy/testing/_private/utils.py +++ b/numpy/testing/_private/utils.py @@ -35,7 +35,7 @@ __all__ = [ 'assert_allclose', 'IgnoreException', 'clear_and_catch_warnings', 'SkipTest', 'KnownFailureException', 'temppath', 'tempdir', 'IS_PYPY', 'HAS_REFCOUNT', 'suppress_warnings', 'assert_array_compare', - 'assert_no_gc_cycles', 'break_cycles', 'HAS_LAPACK64' + 'assert_no_gc_cycles', 'break_cycles', 'HAS_LAPACK64', 'IS_PYSTON', ] -- cgit v1.2.1 From 3930aea7cb333d043f955b35d8d5c0645dfe45ae Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 5 Nov 2021 11:11:28 +0100 Subject: MAINT: Add missing annotations for `IS_PYSTON` --- numpy/testing/_private/utils.pyi | 1 + 1 file changed, 1 insertion(+) (limited to 'numpy') diff --git a/numpy/testing/_private/utils.pyi b/numpy/testing/_private/utils.pyi index 26ce52e40..4ba5d82ee 100644 --- a/numpy/testing/_private/utils.pyi +++ b/numpy/testing/_private/utils.pyi @@ -133,6 +133,7 @@ class suppress_warnings: verbose: int IS_PYPY: Final[bool] +IS_PYSTON: Final[bool] HAS_REFCOUNT: Final[bool] HAS_LAPACK64: Final[bool] -- cgit v1.2.1 From 57610d2e01fbc0915b79ef803247a308ad3a9117 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 5 Nov 2021 11:25:49 +0100 Subject: MAINT: Add annotations for a missing `percentile` interpolation kind: `"inclusive"` --- numpy/lib/function_base.pyi | 1 + 1 file changed, 1 insertion(+) (limited to 'numpy') diff --git a/numpy/lib/function_base.pyi b/numpy/lib/function_base.pyi index 82c625fed..4bbd873a3 100644 --- a/numpy/lib/function_base.pyi +++ b/numpy/lib/function_base.pyi @@ -514,6 +514,7 @@ _InterpolationKind = L[ "higher", "midpoint", "nearest", + "inclusive", ] @overload -- cgit v1.2.1 From 4b9e56921e6343b980b27e308534ca433a4a1fe8 Mon Sep 17 00:00:00 2001 From: warren Date: Sat, 6 Nov 2021 02:59:05 -0400 Subject: BUG: Get full precision for 32 bit floating point random values. The formula to convert a 32 bit random integer to a random float32, (next_uint32(bitgen_state) >> 9) * (1.0f / 8388608.0f) shifts by one bit too many, resulting in uniform float32 samples always having a 0 in the least significant bit. The formula is corrected to (next_uint32(bitgen_state) >> 8) * (1.0f / 16777216.0f) Occurrences of the incorrect formula in numpy/random/tests/test_direct.py were also corrected. Closes gh-17478. --- numpy/random/src/distributions/distributions.c | 2 +- numpy/random/tests/test_direct.py | 13 ++++++++----- numpy/random/tests/test_generator_mt19937.py | 12 ++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) (limited to 'numpy') diff --git a/numpy/random/src/distributions/distributions.c b/numpy/random/src/distributions/distributions.c index adf4db4a7..bd1e1faa4 100644 --- a/numpy/random/src/distributions/distributions.c +++ b/numpy/random/src/distributions/distributions.c @@ -17,7 +17,7 @@ static NPY_INLINE uint64_t next_uint64(bitgen_t *bitgen_state) { } static NPY_INLINE float next_float(bitgen_t *bitgen_state) { - return (next_uint32(bitgen_state) >> 9) * (1.0f / 8388608.0f); + return (next_uint32(bitgen_state) >> 8) * (1.0f / 16777216.0f); } /* Random generators for external use */ diff --git a/numpy/random/tests/test_direct.py b/numpy/random/tests/test_direct.py index ea1ebacb6..58d966adf 100644 --- a/numpy/random/tests/test_direct.py +++ b/numpy/random/tests/test_direct.py @@ -46,25 +46,27 @@ def assert_state_equal(actual, target): assert actual[key] == target[key] +def uint32_to_float32(u): + return ((u >> np.uint32(8)) * (1.0 / 2**24)).astype(np.float32) + + def uniform32_from_uint64(x): x = np.uint64(x) upper = np.array(x >> np.uint64(32), dtype=np.uint32) lower = np.uint64(0xffffffff) lower = np.array(x & lower, dtype=np.uint32) joined = np.column_stack([lower, upper]).ravel() - out = (joined >> np.uint32(9)) * (1.0 / 2 ** 23) - return out.astype(np.float32) + return uint32_to_float32(joined) def uniform32_from_uint53(x): x = np.uint64(x) >> np.uint64(16) x = np.uint32(x & np.uint64(0xffffffff)) - out = (x >> np.uint32(9)) * (1.0 / 2 ** 23) - return out.astype(np.float32) + return uint32_to_float32(x) def uniform32_from_uint32(x): - return (x >> np.uint32(9)) * (1.0 / 2 ** 23) + return uint32_to_float32(x) def uniform32_from_uint(x, bits): @@ -126,6 +128,7 @@ def gauss_from_uint(x, n, bits): return gauss[:n] + def test_seedsequence(): from numpy.random.bit_generator import (ISeedSequence, ISpawnableSeedSequence, diff --git a/numpy/random/tests/test_generator_mt19937.py b/numpy/random/tests/test_generator_mt19937.py index 7ddccaf86..d057122f1 100644 --- a/numpy/random/tests/test_generator_mt19937.py +++ b/numpy/random/tests/test_generator_mt19937.py @@ -774,6 +774,18 @@ class TestRandomDist: desired = 0.0969992 assert_array_almost_equal(actual, desired, decimal=7) + @pytest.mark.parametrize('dtype, uint_view_type', + [(np.float32, np.uint32), + (np.float64, np.uint64)]) + def test_random_distribution_of_lsb(self, dtype, uint_view_type): + random = Generator(MT19937(self.seed)) + sample = random.random(100000, dtype=dtype) + num_ones_in_lsb = np.count_nonzero(sample.view(uint_view_type) & 1) + # The probability of a 1 in the least significant bit is 0.25. + # With a sample size of 100000, the probability that num_ones_in_lsb + # is outside the following range is less than 5e-11. + assert 24100 < num_ones_in_lsb < 25900 + def test_random_unsupported_type(self): assert_raises(TypeError, random.random, dtype='int32') -- cgit v1.2.1 From 035ecde53bf595c2b94849a248c6cf21221e2c6a Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Wed, 3 Nov 2021 22:08:09 +0200 Subject: SIMD: replace raw SIMD of ceil with universal intrinsics --- numpy/core/code_generators/generate_umath.py | 2 +- numpy/core/src/umath/loops.c.src | 8 ++++---- numpy/core/src/umath/loops.h.src | 6 +++--- numpy/core/src/umath/loops_unary_fp.dispatch.c.src | 21 +++++++++++++-------- numpy/core/src/umath/simd.inc.src | 22 +++++----------------- 5 files changed, 26 insertions(+), 33 deletions(-) (limited to 'numpy') diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py index 3a27a34cd..292d9e0d3 100644 --- a/numpy/core/code_generators/generate_umath.py +++ b/numpy/core/code_generators/generate_umath.py @@ -827,7 +827,7 @@ defdict = { docstrings.get('numpy.core.umath.ceil'), None, TD('e', f='ceil', astype={'e': 'f'}), - TD(inexactvec, simd=[('fma', 'fd'), ('avx512f', 'fd')]), + TD(inexactvec, dispatch=[('loops_unary_fp', 'fd')]), TD('fdg', f='ceil'), TD(O, f='npy_ObjectCeil'), ), diff --git a/numpy/core/src/umath/loops.c.src b/numpy/core/src/umath/loops.c.src index 7c0710819..aaa694f34 100644 --- a/numpy/core/src/umath/loops.c.src +++ b/numpy/core/src/umath/loops.c.src @@ -1532,8 +1532,8 @@ TIMEDELTA_mm_qm_divmod(char **args, npy_intp const *dimensions, npy_intp const * */ /**begin repeat - * #func = rint, ceil, floor, trunc# - * #scalarf = npy_rint, npy_ceil, npy_floor, npy_trunc# + * #func = rint, floor, trunc# + * #scalarf = npy_rint, npy_floor, npy_trunc# */ /**begin repeat1 @@ -1568,8 +1568,8 @@ NPY_NO_EXPORT NPY_GCC_OPT_3 void */ /**begin repeat2 - * #func = rint, ceil, floor, trunc# - * #scalarf = npy_rint, npy_ceil, npy_floor, npy_trunc# + * #func = rint, floor, trunc# + * #scalarf = npy_rint, npy_floor, npy_trunc# */ NPY_NO_EXPORT NPY_GCC_OPT_3 void diff --git a/numpy/core/src/umath/loops.h.src b/numpy/core/src/umath/loops.h.src index 0938cd050..081ca9957 100644 --- a/numpy/core/src/umath/loops.h.src +++ b/numpy/core/src/umath/loops.h.src @@ -187,7 +187,7 @@ NPY_NO_EXPORT void * #TYPE = FLOAT, DOUBLE# */ /**begin repeat1 - * #kind = sqrt, absolute, square, reciprocal# + * #kind = ceil, sqrt, absolute, square, reciprocal# */ NPY_CPU_DISPATCH_DECLARE(NPY_NO_EXPORT void @TYPE@_@kind@, (char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(data))) @@ -228,7 +228,7 @@ NPY_CPU_DISPATCH_DECLARE(NPY_NO_EXPORT void @TYPE@_@func@, /**end repeat**/ /**begin repeat - * #func = sin, cos# + * #func = sin, cos# */ NPY_CPU_DISPATCH_DECLARE(NPY_NO_EXPORT void DOUBLE_@func@, @@ -275,7 +275,7 @@ NPY_CPU_DISPATCH_DECLARE(NPY_NO_EXPORT void @TYPE@_@kind@, ( /**end repeat**/ /**begin repeat - * #func = rint, ceil, floor, trunc# + * #func = rint, floor, trunc# */ /**begin repeat1 diff --git a/numpy/core/src/umath/loops_unary_fp.dispatch.c.src b/numpy/core/src/umath/loops_unary_fp.dispatch.c.src index 2d5917282..789733fb6 100644 --- a/numpy/core/src/umath/loops_unary_fp.dispatch.c.src +++ b/numpy/core/src/umath/loops_unary_fp.dispatch.c.src @@ -1,6 +1,8 @@ /*@targets ** $maxopt baseline - ** sse2 vsx2 neon + ** sse2 sse41 + ** vsx2 + ** neon asimd **/ /** * Force use SSE only on x86, even if AVX2 or AVX512F are enabled @@ -65,6 +67,9 @@ NPY_FINLINE double c_square_f64(double a) #define c_sqrt_f64 npy_sqrt #endif +#define c_ceil_f32 npy_ceilf +#define c_ceil_f64 npy_ceil + /******************************************************************************** ** Defining the SIMD kernels ********************************************************************************/ @@ -134,10 +139,10 @@ NPY_FINLINE double c_square_f64(double a) */ #if @VCHK@ /**begin repeat1 - * #kind = sqrt, absolute, square, reciprocal# - * #intr = sqrt, abs, square, recip# - * #repl_0w1 = 0, 0, 0, 1# - * #RECIP_WORKAROUND = 0, 0, 0, WORKAROUND_CLANG_RECIPROCAL_BUG# + * #kind = ceil, sqrt, absolute, square, reciprocal# + * #intr = ceil, sqrt, abs, square, recip# + * #repl_0w1 = 0, 0, 0, 0, 1# + * #RECIP_WORKAROUND = 0, 0, 0, 0, WORKAROUND_CLANG_RECIPROCAL_BUG# */ /**begin repeat2 * #STYPE = CONTIG, NCONTIG, CONTIG, NCONTIG# @@ -245,9 +250,9 @@ static void simd_@TYPE@_@kind@_@STYPE@_@DTYPE@ * #VCHK = NPY_SIMD, NPY_SIMD_F64# */ /**begin repeat1 - * #kind = sqrt, absolute, square, reciprocal# - * #intr = sqrt, abs, square, recip# - * #clear = 0, 1, 0, 0# + * #kind = ceil, sqrt, absolute, square, reciprocal# + * #intr = ceil, sqrt, abs, square, recip# + * #clear = 0, 0, 1, 0, 0# */ NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(@TYPE@_@kind@) (char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func)) diff --git a/numpy/core/src/umath/simd.inc.src b/numpy/core/src/umath/simd.inc.src index d47be9a30..0e2c1ab8b 100644 --- a/numpy/core/src/umath/simd.inc.src +++ b/numpy/core/src/umath/simd.inc.src @@ -169,7 +169,7 @@ run_@func@_avx512_skx_@TYPE@(char **args, npy_intp const *dimensions, npy_intp c */ /**begin repeat2 - * #func = rint, floor, ceil, trunc# + * #func = rint, floor, trunc# */ #if defined @CHK@ && defined NPY_HAVE_SSE2_INTRINSICS @@ -849,12 +849,6 @@ fma_floor_@vsub@(@vtype@ x) return _mm256_round_@vsub@(x, _MM_FROUND_TO_NEG_INF); } -NPY_FINLINE NPY_GCC_OPT_3 NPY_GCC_TARGET_FMA @vtype@ -fma_ceil_@vsub@(@vtype@ x) -{ - return _mm256_round_@vsub@(x, _MM_FROUND_TO_POS_INF); -} - NPY_FINLINE NPY_GCC_OPT_3 NPY_GCC_TARGET_FMA @vtype@ fma_trunc_@vsub@(@vtype@ x) { @@ -987,12 +981,6 @@ avx512_floor_@vsub@(@vtype@ x) return _mm512_roundscale_@vsub@(x, 0x09); } -NPY_FINLINE NPY_GCC_OPT_3 NPY_GCC_TARGET_AVX512F @vtype@ -avx512_ceil_@vsub@(@vtype@ x) -{ - return _mm512_roundscale_@vsub@(x, 0x0A); -} - NPY_FINLINE NPY_GCC_OPT_3 NPY_GCC_TARGET_AVX512F @vtype@ avx512_trunc_@vsub@(@vtype@ x) { @@ -1327,8 +1315,8 @@ AVX512F_@func@_@TYPE@(char **args, npy_intp const *dimensions, npy_intp const *s */ /**begin repeat1 - * #func = rint, ceil, floor, trunc# - * #vectorf = rint, ceil, floor, trunc# + * #func = rint, floor, trunc# + * #vectorf = rint, floor, trunc# */ #if defined @CHK@ @@ -1398,8 +1386,8 @@ static NPY_INLINE NPY_GCC_OPT_3 NPY_GCC_TARGET_@ISA@ void */ /**begin repeat1 - * #func = rint, ceil, floor, trunc# - * #vectorf = rint, ceil, floor, trunc# + * #func = rint, floor, trunc# + * #vectorf = rint, floor, trunc# */ #if defined @CHK@ -- cgit v1.2.1 From abca5ca34595fda048e16760cb6355a67220f5db Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Sat, 6 Nov 2021 16:30:36 -0500 Subject: MAINT: Remove Python <3.8 support from C Remove version switches for support of Python versions before 3.8. The biggest deletion is that we can use Pythons `PyVectorcall_Call` to support the old `*args, **kwargs` style call convention in ufuncs. --- numpy/core/include/numpy/ufuncobject.h | 5 +-- numpy/core/src/multiarray/compiled_base.c | 2 +- numpy/core/src/multiarray/methods.c | 14 ------- numpy/core/src/umath/ufunc_object.c | 70 +------------------------------ 4 files changed, 3 insertions(+), 88 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/ufuncobject.h b/numpy/core/include/numpy/ufuncobject.h index 3f184bd45..1d7050bbe 100644 --- a/numpy/core/include/numpy/ufuncobject.h +++ b/numpy/core/include/numpy/ufuncobject.h @@ -173,11 +173,8 @@ typedef struct _tagPyUFuncObject { * but this was never implemented. (This is also why the above * selector is called the "legacy" selector.) */ - #if PY_VERSION_HEX >= 0x03080000 vectorcallfunc vectorcall; - #else - void *reserved2; - #endif + /* Was previously the `PyUFunc_MaskedInnerLoopSelectionFunc` */ void *_always_null_previously_masked_innerloop_selector; diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c index 9910fffe6..5853e068b 100644 --- a/numpy/core/src/multiarray/compiled_base.c +++ b/numpy/core/src/multiarray/compiled_base.c @@ -1393,7 +1393,7 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *args) { PyObject *obj; PyObject *str; - #if PY_VERSION_HEX >= 0x030700A2 && (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM > 0x07030300) + #if !defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM > 0x07030300 const char *docstr; #else char *docstr; diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 2d66c77dc..8e2cd09eb 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -1821,22 +1821,8 @@ array_reduce_ex_picklebuffer(PyArrayObject *self, int protocol) descr = PyArray_DESCR(self); - /* if the python version is below 3.8, the pickle module does not provide - * built-in support for protocol 5. We try importing the pickle5 - * backport instead */ -#if PY_VERSION_HEX >= 0x03080000 /* we expect protocol 5 to be available in Python 3.8 */ pickle_module = PyImport_ImportModule("pickle"); -#else - pickle_module = PyImport_ImportModule("pickle5"); - if (pickle_module == NULL) { - /* for protocol 5, raise a clear ImportError if pickle5 is not found - */ - PyErr_SetString(PyExc_ImportError, "Using pickle protocol 5 " - "requires the pickle5 module for Python >=3.6 and <3.8"); - return NULL; - } -#endif if (pickle_module == NULL){ return NULL; } diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 237af81b2..186f18a62 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -4925,65 +4925,6 @@ fail: } -/* - * TODO: The implementation below can be replaced with PyVectorcall_Call - * when available (should be Python 3.8+). - */ -static PyObject * -ufunc_generic_call( - PyUFuncObject *ufunc, PyObject *args, PyObject *kwds) -{ - Py_ssize_t len_args = PyTuple_GET_SIZE(args); - /* - * Wrapper for tp_call to tp_fastcall, to support both on older versions - * of Python. (and generally simplifying support of both versions in the - * same codebase. - */ - if (kwds == NULL) { - return ufunc_generic_fastcall(ufunc, - PySequence_Fast_ITEMS(args), len_args, NULL, NPY_FALSE); - } - - PyObject *new_args[NPY_MAXARGS]; - Py_ssize_t len_kwds = PyDict_Size(kwds); - - if (NPY_UNLIKELY(len_args + len_kwds > NPY_MAXARGS)) { - /* - * We do not have enough scratch-space, so we have to abort; - * In practice this error should not be seen by users. - */ - PyErr_Format(PyExc_ValueError, - "%s() takes from %d to %d positional arguments but " - "%zd were given", - ufunc_get_name_cstr(ufunc) , ufunc->nin, ufunc->nargs, len_args); - return NULL; - } - - /* Copy args into the scratch space */ - for (Py_ssize_t i = 0; i < len_args; i++) { - new_args[i] = PyTuple_GET_ITEM(args, i); - } - - PyObject *kwnames = PyTuple_New(len_kwds); - - PyObject *key, *value; - Py_ssize_t pos = 0; - Py_ssize_t i = 0; - while (PyDict_Next(kwds, &pos, &key, &value)) { - Py_INCREF(key); - PyTuple_SET_ITEM(kwnames, i, key); - new_args[i + len_args] = value; - i++; - } - - PyObject *res = ufunc_generic_fastcall(ufunc, - new_args, len_args, kwnames, NPY_FALSE); - Py_DECREF(kwnames); - return res; -} - - -#if PY_VERSION_HEX >= 0x03080000 /* * Implement vectorcallfunc which should be defined with Python 3.8+. * In principle this could be backported, but the speed gain seems moderate @@ -5001,7 +4942,6 @@ ufunc_generic_vectorcall(PyObject *ufunc, return ufunc_generic_fastcall((PyUFuncObject *)ufunc, args, PyVectorcall_NARGS(len_args), kwnames, NPY_FALSE); } -#endif /* PY_VERSION_HEX >= 0x03080000 */ NPY_NO_EXPORT PyObject * @@ -5178,11 +5118,7 @@ PyUFunc_FromFuncAndDataAndSignatureAndIdentity(PyUFuncGenericFunction *func, voi ufunc->core_dim_flags = NULL; ufunc->userloops = NULL; ufunc->ptr = NULL; -#if PY_VERSION_HEX >= 0x03080000 ufunc->vectorcall = &ufunc_generic_vectorcall; -#else - ufunc->reserved2 = NULL; -#endif ufunc->reserved1 = 0; ufunc->iter_flags = 0; @@ -6437,19 +6373,15 @@ NPY_NO_EXPORT PyTypeObject PyUFunc_Type = { .tp_basicsize = sizeof(PyUFuncObject), .tp_dealloc = (destructor)ufunc_dealloc, .tp_repr = (reprfunc)ufunc_repr, - .tp_call = (ternaryfunc)ufunc_generic_call, + .tp_call = &PyVectorcall_Call, .tp_str = (reprfunc)ufunc_repr, .tp_flags = Py_TPFLAGS_DEFAULT | -#if PY_VERSION_HEX >= 0x03080000 _Py_TPFLAGS_HAVE_VECTORCALL | -#endif Py_TPFLAGS_HAVE_GC, .tp_traverse = (traverseproc)ufunc_traverse, .tp_methods = ufunc_methods, .tp_getset = ufunc_getset, -#if PY_VERSION_HEX >= 0x03080000 .tp_vectorcall_offset = offsetof(PyUFuncObject, vectorcall), -#endif }; /* End of code for ufunc objects */ -- cgit v1.2.1 From 035d853e32d6e60a40a6a845699723238a01431b Mon Sep 17 00:00:00 2001 From: abel Date: Mon, 8 Nov 2021 17:35:19 +0100 Subject: DOC: Remove non-existent alias --- numpy/lib/function_base.py | 14 ++++++-------- numpy/lib/function_base.pyi | 1 - numpy/lib/nanfunctions.py | 8 ++++---- 3 files changed, 10 insertions(+), 13 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 4d57acdb2..86125168a 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3901,7 +3901,7 @@ def percentile(a, * (NPY 2): 'higher', * (NPY 3): 'midpoint' * (NPY 4): 'nearest' - * (NPY 5): 'linear', aliased with 'inclusive' (default) + * (NPY 5): 'linear' New options: @@ -3911,7 +3911,7 @@ def percentile(a, * (H&F 4): 'interpolated_inverted_cdf' * (H&F 5): 'hazen' * (H&F 6): 'weibull' - * (H&F 7): 'inclusive', aliased with 'linear' (default) + * (H&F 7): 'linear' (default) * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' @@ -4007,8 +4007,7 @@ def percentile(a, * alpha = 0 * beta = 0 - inclusive: - Default method, aliased with "linear". + linear: method 7 of H&F [1]_. This method give continuous results using: * alpha = 1 @@ -4164,7 +4163,7 @@ def quantile(a, * (NPY 2): 'higher', * (NPY 3): 'midpoint' * (NPY 4): 'nearest' - * (NPY 5): 'linear', aliased with 'inclusive' (default) + * (NPY 5): 'linear' New options: @@ -4174,7 +4173,7 @@ def quantile(a, * (H&F 4): 'interpolated_inverted_cdf' * (H&F 5): 'hazen' * (H&F 6): 'weibull' - * (H&F 7): 'inclusive', aliased with 'linear' (default) + * (H&F 7): 'linear' (default) * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' @@ -4261,8 +4260,7 @@ def quantile(a, * alpha = 0 * beta = 0 - inclusive: - Default method, aliased with "linear". + linear: method 7 of H&F [1]_. This method give continuous results using: * alpha = 1 diff --git a/numpy/lib/function_base.pyi b/numpy/lib/function_base.pyi index 4bbd873a3..82c625fed 100644 --- a/numpy/lib/function_base.pyi +++ b/numpy/lib/function_base.pyi @@ -514,7 +514,6 @@ _InterpolationKind = L[ "higher", "midpoint", "nearest", - "inclusive", ] @overload diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 9fab77f45..7e953be03 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -1277,7 +1277,7 @@ def nanpercentile( * (NPY 2): 'higher', * (NPY 3): 'midpoint' * (NPY 4): 'nearest' - * (NPY 5): 'linear', aliased with 'inclusive' (default) + * (NPY 5): 'linear' (default) New options: @@ -1287,7 +1287,7 @@ def nanpercentile( * (H&F 4): 'interpolated_inverted_cdf' * (H&F 5): 'hazen' * (H&F 6): 'weibull' - * (H&F 7): 'inclusive', aliased with 'linear' (default) + * (H&F 7): 'linear' (default) * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' @@ -1418,7 +1418,7 @@ def nanquantile( * (NPY 2): 'higher', * (NPY 3): 'midpoint' * (NPY 4): 'nearest' - * (NPY 5): 'linear', aliased with 'inclusive' (default) + * (NPY 5): 'linear' (default) New options: @@ -1428,7 +1428,7 @@ def nanquantile( * (H&F 4): 'interpolated_inverted_cdf' * (H&F 5): 'hazen' * (H&F 6): 'weibull' - * (H&F 7): 'inclusive', aliased with 'linear' (default) + * (H&F 7): 'linear' (default) * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' -- cgit v1.2.1 From f6076ee90985e7839d7ec6ed83ea1f85c0ece44d Mon Sep 17 00:00:00 2001 From: mattip Date: Tue, 9 Nov 2021 11:16:33 +0200 Subject: add release note, error out if offset is used --- numpy/core/src/multiarray/dlpack.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c index f061a6bf9..b0eaa7786 100644 --- a/numpy/core/src/multiarray/dlpack.c +++ b/numpy/core/src/multiarray/dlpack.c @@ -206,6 +206,12 @@ array_dlpack(PyArrayObject *self, if (data == NULL) { return NULL; } + if ((char *)PyArray_DATA(self) - data != 0) { + PyErr_SetString(PyExc_TypeError, + "Offsets not clearly supported by this " + "version of DLPack."); + return NULL; + } DLManagedTensor *managed = PyMem_Malloc(sizeof(DLManagedTensor)); if (managed == NULL) { @@ -238,7 +244,7 @@ array_dlpack(PyArrayObject *self, if (PyArray_SIZE(self) != 1 && !PyArray_IS_C_CONTIGUOUS(self)) { managed->dl_tensor.strides = managed_strides; } - managed->dl_tensor.byte_offset = (char *)PyArray_DATA(self) - data; + managed->dl_tensor.byte_offset = 0; managed->manager_ctx = self; managed->deleter = array_dlpack_deleter; -- cgit v1.2.1 From 6cd68755c6fd0686dd57a9daec43d8aa09d15c3e Mon Sep 17 00:00:00 2001 From: abel Date: Tue, 9 Nov 2021 10:35:12 +0100 Subject: MTH: Update quantile default lerp method For method 7 of H&F, using `(n - 1) * quantiles` instead of the usual method gives a more accurate result. --- numpy/lib/function_base.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 86125168a..3c9983edf 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -67,7 +67,7 @@ __all__ = [ # fix_gamma : Callable # A function used for discret methods to force the index to a specific value. _QuantileInterpolation = dict( - # --- HYNDMAN and FAN methods + # --- HYNDMAN AND FAN METHODS # Discrete methods inverted_cdf=dict( get_virtual_index=lambda n, quantiles: _inverted_cdf(n, quantiles), @@ -102,10 +102,12 @@ _QuantileInterpolation = dict( _compute_virtual_index(n, quantiles, 0, 0), fix_gamma=lambda gamma, _: gamma, ), - # Default value + # Default method. + # To avoid some rounding issues, `(n-1) * quantiles` is preferred to + # `_compute_virtual_index(n, quantiles, 1, 1)`. + # They are mathematically equivalent. linear=dict( - get_virtual_index=lambda n, quantiles: - _compute_virtual_index(n, quantiles, 1, 1), + get_virtual_index=lambda n, quantiles: (n - 1) * quantiles, fix_gamma=lambda gamma, _: gamma, ), median_unbiased=dict( @@ -118,7 +120,7 @@ _QuantileInterpolation = dict( _compute_virtual_index(n, quantiles, 3 / 8.0, 3 / 8.0), fix_gamma=lambda gamma, _: gamma, ), - # --- OTHER METHODS fixme add deprecated ? + # --- OTHER METHODS lower=dict( get_virtual_index=lambda n, quantiles: np.floor( (n - 1) * quantiles).astype(np.intp), -- cgit v1.2.1 From 5cb2d64eb00b8fd8531c0c722dbb6105cbe8e277 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Tue, 9 Nov 2021 17:22:54 +0530 Subject: Addressed reviews and increased code coverage --- numpy/core/include/numpy/ndarraytypes.h | 2 +- numpy/core/src/multiarray/_multiarray_tests.c.src | 4 ++-- numpy/core/src/multiarray/ctors.c | 8 ++++---- numpy/core/src/multiarray/multiarraymodule.c | 2 +- numpy/core/tests/test_multiarray.py | 6 ++++++ 5 files changed, 14 insertions(+), 8 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 566eae357..cc3b7c006 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -938,7 +938,7 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *); #define NPY_ARRAY_UPDATEIFCOPY 0x1000 /* Deprecated in 1.14 */ #define NPY_ARRAY_WRITEBACKIFCOPY 0x2000 -#define _NPY_ARRAY_ENSURENOCOPY 0x4000 +#define NPY_ARRAY_ENSURENOCOPY 0x4000 /* * NOTE: there are also internal flags defined in multiarray/arrayobject.h, diff --git a/numpy/core/src/multiarray/_multiarray_tests.c.src b/numpy/core/src/multiarray/_multiarray_tests.c.src index f21a901c1..be3eab6d6 100644 --- a/numpy/core/src/multiarray/_multiarray_tests.c.src +++ b/numpy/core/src/multiarray/_multiarray_tests.c.src @@ -2363,11 +2363,11 @@ run_intp_converter(PyObject* NPY_UNUSED(self), PyObject *args) return tup; } -/* used to test _NPY_ARRAY_ENSURENOCOPY raises ValueError */ +/* used to test NPY_ARRAY_ENSURENOCOPY raises ValueError */ static PyObject* npy_ensurenocopy(PyObject* NPY_UNUSED(self), PyObject* args) { - int flags = _NPY_ARRAY_ENSURENOCOPY; + int flags = NPY_ARRAY_ENSURENOCOPY; if (!PyArray_CheckFromAny(args, NULL, 0, 0, flags, NULL)) { return NULL; } diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index a8f41f582..470b3b2f8 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1738,7 +1738,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, /* Create a new array and copy the data */ Py_INCREF(dtype); /* hold on in case of a subarray that is replaced */ - if( flags & _NPY_ARRAY_ENSURENOCOPY ) { + if( flags & NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating " "an array from descriptor."); @@ -1808,7 +1808,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, * NPY_ARRAY_FORCECAST, * NPY_ARRAY_ENSUREARRAY, * NPY_ARRAY_ELEMENTSTRIDES, - * _NPY_ARRAY_ENSURENOCOPY + * NPY_ARRAY_ENSURENOCOPY * * or'd (|) together * @@ -1873,7 +1873,7 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, if ((requires & NPY_ARRAY_ELEMENTSTRIDES) && !PyArray_ElementStrides(obj)) { PyObject *ret; - if( requires & _NPY_ARRAY_ENSURENOCOPY ) { + if( requires & NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating a new array."); return NULL; @@ -1953,7 +1953,7 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) if (copy) { - if( flags & _NPY_ARRAY_ENSURENOCOPY ) { + if( flags & NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating " "an array from given array."); diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index bf60314ad..15c2e3c10 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1623,7 +1623,7 @@ _array_fromobject_generic( if (copy == NPY_COPY_ALWAYS) { flags = NPY_ARRAY_ENSURECOPY; } else if( copy == NPY_COPY_NEVER ) { - flags = _NPY_ARRAY_ENSURENOCOPY; + flags = NPY_ARRAY_ENSURENOCOPY; } if (order == NPY_CORDER) { flags |= NPY_ARRAY_C_CONTIGUOUS; diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 50133798c..4ef58d904 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -8009,9 +8009,15 @@ class TestArrayCreationCopyArgument(object): assert_raises(ValueError, np.array, arr.T, copy=np._CopyMode.NEVER, order='C') + assert_raises(ValueError, np.array, + arr.T, copy=np._CopyMode.NEVER, + order='C', dtype=np.int64) assert_raises(ValueError, np.array, arr, copy=np._CopyMode.NEVER, order='F') + assert_raises(ValueError, np.array, + arr, copy=np._CopyMode.NEVER, + order='F', dtype=np.int64) class TestArrayAttributeDeletion: -- cgit v1.2.1 From 8b939c9e9a4264a4d3b10fcecbfd5ad21ef9901d Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Tue, 9 Nov 2021 17:27:12 +0530 Subject: Intentional RuntimeError --- numpy/core/src/multiarray/ctors.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 470b3b2f8..1ff26a311 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1872,15 +1872,17 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, if ((requires & NPY_ARRAY_ELEMENTSTRIDES) && !PyArray_ElementStrides(obj)) { - PyObject *ret; - if( requires & NPY_ARRAY_ENSURENOCOPY ) { - PyErr_SetString(PyExc_ValueError, - "Unable to avoid copy while creating a new array."); - return NULL; - } - ret = PyArray_NewCopy((PyArrayObject *)obj, NPY_ANYORDER); - Py_DECREF(obj); - obj = ret; + // Testing if this code can be ever hit by existing tests + PyErr_SetString(PyExc_RuntimeError, "Not Implemented"); + // PyObject *ret; + // if( requires & NPY_ARRAY_ENSURENOCOPY ) { + // PyErr_SetString(PyExc_ValueError, + // "Unable to avoid copy while creating a new array."); + // return NULL; + // } + // ret = PyArray_NewCopy((PyArrayObject *)obj, NPY_ANYORDER); + // Py_DECREF(obj); + // obj = ret; } return obj; } -- cgit v1.2.1 From 5b94a03b93d171232e0a64dc2160e4f2139e9b1a Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 9 Nov 2021 12:14:13 -0600 Subject: MAINT: Simplify `byte_offset` handling in dlpack.h and add comment If `byte_offset = 0` is forced anyway, there is no point in trying to preserve a previous `data` information from the capsule. (And probably it should have used a base array also, and not just a base DLPack capsule, anyway.) --- numpy/core/src/multiarray/dlpack.c | 40 +++++++++++++++----------------------- 1 file changed, 16 insertions(+), 24 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c index b0eaa7786..291e60a22 100644 --- a/numpy/core/src/multiarray/dlpack.c +++ b/numpy/core/src/multiarray/dlpack.c @@ -3,6 +3,7 @@ #define PY_SSIZE_T_CLEAN #include +#include #include "numpy/arrayobject.h" #include "common/npy_argparse.h" @@ -100,19 +101,6 @@ array_get_dl_device(PyArrayObject *self) { return ret; } -static char * -array_get_dl_data(PyArrayObject *self) { - PyObject *base = PyArray_BASE(self); - if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) { - DLManagedTensor *managed = PyCapsule_GetPointer( - base, NPY_DLPACK_INTERNAL_CAPSULE_NAME); - if (managed == NULL) { - return NULL; - } - return managed->dl_tensor.data; - } - return PyArray_DATA(self); -} PyObject * array_dlpack(PyArrayObject *self, @@ -202,16 +190,6 @@ array_dlpack(PyArrayObject *self, if (PyErr_Occurred()) { return NULL; } - char *data = array_get_dl_data(self); - if (data == NULL) { - return NULL; - } - if ((char *)PyArray_DATA(self) - data != 0) { - PyErr_SetString(PyExc_TypeError, - "Offsets not clearly supported by this " - "version of DLPack."); - return NULL; - } DLManagedTensor *managed = PyMem_Malloc(sizeof(DLManagedTensor)); if (managed == NULL) { @@ -219,7 +197,21 @@ array_dlpack(PyArrayObject *self, return NULL; } - managed->dl_tensor.data = data; + /* + * Note: the `dlpack.h` header suggests/standardizes that `data` must be + * 256-byte aligned. We ignore this intentionally, because `__dlpack__` + * standardizes that `byte_offset` must be 0 (for now) to not break pytorch: + * https://github.com/data-apis/array-api/issues/293#issuecomment-964111413 + * + * We further assume that exporting fully unaligned data is OK even without + * `byte_offset` since the standard does not reject it. + * Presumably, pytorch will support importing `byte_offset != 0` and NumPy + * can choose to use it starting about 2023. At that point, it may be + * that NumPy MUST use `byte_offset` to adhere to the standard (as + * specified in the header)! + */ + managed->dl_tensor.data = PyArray_DATA(self); + managed->dl_tensor.byte_offset = 0; managed->dl_tensor.device = device; managed->dl_tensor.dtype = managed_dtype; -- cgit v1.2.1 From 7e03a56194a9def4296ef502bb380d8cc4a2d061 Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Tue, 9 Nov 2021 19:18:52 +0100 Subject: DEP: remove code for supporting GCC <4 in Mingw32CCompiler The last GCC 3.x release before 4.0 was 3.4 which was released 2004, so 17 years ago. Removes all code only used with those old toolchains. --- numpy/distutils/mingw32ccompiler.py | 81 ++++++------------------------------- 1 file changed, 12 insertions(+), 69 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/mingw32ccompiler.py b/numpy/distutils/mingw32ccompiler.py index 82d296434..fbe3655c9 100644 --- a/numpy/distutils/mingw32ccompiler.py +++ b/numpy/distutils/mingw32ccompiler.py @@ -24,7 +24,6 @@ from numpy.distutils import log # 3. Force windows to use g77 import distutils.cygwinccompiler -from distutils.version import StrictVersion from distutils.unixccompiler import UnixCCompiler from distutils.msvccompiler import get_build_version as get_build_msvc_version from distutils.errors import UnknownFileError @@ -62,35 +61,6 @@ class Mingw32CCompiler(distutils.cygwinccompiler.CygwinCCompiler): distutils.cygwinccompiler.CygwinCCompiler.__init__ (self, verbose, dry_run, force) - # we need to support 3.2 which doesn't match the standard - # get_versions methods regex - if self.gcc_version is None: - try: - out_string = subprocess.check_output(['gcc', '-dumpversion']) - except (OSError, CalledProcessError): - out_string = "" # ignore failures to match old behavior - result = re.search(r'(\d+\.\d+)', out_string) - if result: - self.gcc_version = StrictVersion(result.group(1)) - - # A real mingw32 doesn't need to specify a different entry point, - # but cygwin 2.91.57 in no-cygwin-mode needs it. - if self.gcc_version <= "2.91.57": - entry_point = '--entry _DllMain@12' - else: - entry_point = '' - - if self.linker_dll == 'dllwrap': - # Commented out '--driver-name g++' part that fixes weird - # g++.exe: g++: No such file or directory - # error (mingw 1.0 in Enthon24 tree, gcc-3.4.5). - # If the --driver-name part is required for some environment - # then make the inclusion of this part specific to that - # environment. - self.linker = 'dllwrap' # --driver-name g++' - elif self.linker_dll == 'gcc': - self.linker = 'g++' - # **changes: eric jones 4/11/01 # 1. Check for import library on Windows. Build if it doesn't exist. @@ -113,42 +83,18 @@ class Mingw32CCompiler(distutils.cygwinccompiler.CygwinCCompiler): # kind of bad consequences, like using Py_ModuleInit4 instead of # Py_ModuleInit4_64, etc... So we add it here if get_build_architecture() == 'AMD64': - if self.gcc_version < "4.0": - self.set_executables( - compiler='gcc -g -DDEBUG -DMS_WIN64 -mno-cygwin -O0 -Wall', - compiler_so='gcc -g -DDEBUG -DMS_WIN64 -mno-cygwin -O0' - ' -Wall -Wstrict-prototypes', - linker_exe='gcc -g -mno-cygwin', - linker_so='gcc -g -mno-cygwin -shared') - else: - # gcc-4 series releases do not support -mno-cygwin option - self.set_executables( - compiler='gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall', - compiler_so='gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall -Wstrict-prototypes', - linker_exe='gcc -g', - linker_so='gcc -g -shared') + self.set_executables( + compiler='gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall', + compiler_so='gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall ' + '-Wstrict-prototypes', + linker_exe='gcc -g', + linker_so='gcc -g -shared') else: - if self.gcc_version <= "3.0.0": - self.set_executables( - compiler='gcc -mno-cygwin -O2 -w', - compiler_so='gcc -mno-cygwin -mdll -O2 -w' - ' -Wstrict-prototypes', - linker_exe='g++ -mno-cygwin', - linker_so='%s -mno-cygwin -mdll -static %s' % - (self.linker, entry_point)) - elif self.gcc_version < "4.0": - self.set_executables( - compiler='gcc -mno-cygwin -O2 -Wall', - compiler_so='gcc -mno-cygwin -O2 -Wall' - ' -Wstrict-prototypes', - linker_exe='g++ -mno-cygwin', - linker_so='g++ -mno-cygwin -shared') - else: - # gcc-4 series releases do not support -mno-cygwin option - self.set_executables(compiler='gcc -O2 -Wall', - compiler_so='gcc -O2 -Wall -Wstrict-prototypes', - linker_exe='g++ ', - linker_so='g++ -shared') + self.set_executables( + compiler='gcc -O2 -Wall', + compiler_so='gcc -O2 -Wall -Wstrict-prototypes', + linker_exe='g++ ', + linker_so='g++ -shared') # added for python2.3 support # we can't pass it through set_executables because pre 2.2 would fail self.compiler_cxx = ['g++'] @@ -198,10 +144,7 @@ class Mingw32CCompiler(distutils.cygwinccompiler.CygwinCCompiler): extra_postargs, build_temp, target_lang) - if self.gcc_version < "3.0.0": - func = distutils.cygwinccompiler.CygwinCCompiler.link - else: - func = UnixCCompiler.link + func = UnixCCompiler.link func(*args[:func.__code__.co_argcount]) return -- cgit v1.2.1 From 53e3df3c99a26791cc07e2ea1570e87643fdf7e0 Mon Sep 17 00:00:00 2001 From: abel Date: Tue, 9 Nov 2021 17:35:03 +0100 Subject: TST: Add test for max ulp in default quantile calculation --- numpy/lib/tests/test_function_base.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'numpy') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index d5fa012f1..1c274afae 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -3356,6 +3356,14 @@ class TestPercentile: class TestQuantile: # most of this is already tested by TestPercentile + def test_max_ulp(self): + x = [0.0, 0.2, 0.4] + a = np.quantile(x, 0.45) + # The default linear method would result in 0 + 0.2 * (0.45/2) = 0.18. + # 0.18 is not exactly representable and the formula leads to a 1 ULP + # different result. Ensure it is this exact within 1 ULP, see gh-20331. + np.testing.assert_array_max_ulp(a, 0.18, maxulp=1) + def test_basic(self): x = np.arange(8) * 0.5 assert_equal(np.quantile(x, 0), 0.) -- cgit v1.2.1 From 6312c18e99dad89231b65693a05c92c1f06d3671 Mon Sep 17 00:00:00 2001 From: mattip Date: Wed, 10 Nov 2021 00:16:56 +0200 Subject: ENH: add a 'version' field to PyDataMem_Handler --- numpy/core/_add_newdocs.py | 10 +++++++ numpy/core/include/numpy/ndarraytypes.h | 7 ++++- numpy/core/multiarray.py | 16 ++++++------ numpy/core/src/multiarray/alloc.c | 39 +++++++++++++++++++++++++++- numpy/core/src/multiarray/alloc.h | 2 ++ numpy/core/src/multiarray/multiarraymodule.c | 3 +++ numpy/core/tests/test_mem_policy.py | 6 +++++ 7 files changed, 73 insertions(+), 10 deletions(-) (limited to 'numpy') diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index cae5bc281..078c58976 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -4759,6 +4759,16 @@ add_newdoc('numpy.core.multiarray', 'get_handler_name', memory, in which case you can traverse ``a.base`` for a memory handler. """) +add_newdoc('numpy.core.multiarray', 'get_handler_version', + """ + get_handler_version(a: ndarray) -> int,None + + Return the version of the memory handler used by `a`. If not provided, + return the version of the memory handler that will be used to allocate data + for the next `ndarray` in this context. May return None if `a` does not own + its memory, in which case you can traverse ``a.base`` for a memory handler. + """) + add_newdoc('numpy.core.multiarray', '_set_madvise_hugepage', """ _set_madvise_hugepage(enabled: bool) -> bool diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 80177e2bb..2607fb732 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -674,10 +674,15 @@ typedef struct { void* (*calloc) (void *ctx, size_t nelem, size_t elsize); void* (*realloc) (void *ctx, void *ptr, size_t new_size); void (*free) (void *ctx, void *ptr, size_t size); + /* + * This is the end of the version=1 struct. Only add new fields after + * this line + */ } PyDataMemAllocator; typedef struct { - char name[128]; /* multiple of 64 to keep the struct aligned */ + char name[127]; /* multiple of 64 to keep the struct aligned */ + uint8_t version; /* currently 1 */ PyDataMemAllocator allocator; } PyDataMem_Handler; diff --git a/numpy/core/multiarray.py b/numpy/core/multiarray.py index f96274263..f88d75978 100644 --- a/numpy/core/multiarray.py +++ b/numpy/core/multiarray.py @@ -33,14 +33,14 @@ __all__ = [ 'dot', 'dragon4_positional', 'dragon4_scientific', 'dtype', 'empty', 'empty_like', 'error', 'flagsobj', 'flatiter', 'format_longfloat', 'frombuffer', 'fromfile', 'fromiter', 'fromstring', - 'get_handler_name', 'inner', 'interp', 'interp_complex', 'is_busday', - 'lexsort', 'matmul', 'may_share_memory', 'min_scalar_type', 'ndarray', - 'nditer', 'nested_iters', 'normalize_axis_index', 'packbits', - 'promote_types', 'putmask', 'ravel_multi_index', 'result_type', 'scalar', - 'set_datetimeparse_function', 'set_legacy_print_mode', 'set_numeric_ops', - 'set_string_function', 'set_typeDict', 'shares_memory', - 'tracemalloc_domain', 'typeinfo', 'unpackbits', 'unravel_index', 'vdot', - 'where', 'zeros'] + 'get_handler_name', 'get_handler_version', 'inner', 'interp', + 'interp_complex', 'is_busday', 'lexsort', 'matmul', 'may_share_memory', + 'min_scalar_type', 'ndarray', 'nditer', 'nested_iters', + 'normalize_axis_index', 'packbits', 'promote_types', 'putmask', + 'ravel_multi_index', 'result_type', 'scalar', 'set_datetimeparse_function', + 'set_legacy_print_mode', 'set_numeric_ops', 'set_string_function', + 'set_typeDict', 'shares_memory', 'tracemalloc_domain', 'typeinfo', + 'unpackbits', 'unravel_index', 'vdot', 'where', 'zeros'] # For backward compatibility, make sure pickle imports these functions from here _reconstruct.__module__ = 'numpy.core.multiarray' diff --git a/numpy/core/src/multiarray/alloc.c b/numpy/core/src/multiarray/alloc.c index e4756264d..d1173410d 100644 --- a/numpy/core/src/multiarray/alloc.c +++ b/numpy/core/src/multiarray/alloc.c @@ -370,6 +370,7 @@ default_free(void *NPY_UNUSED(ctx), void *ptr, size_t size) /* Memory handler global default */ PyDataMem_Handler default_handler = { "default_allocator", + 1, { NULL, /* ctx */ default_malloc, /* malloc */ @@ -395,7 +396,6 @@ PyDataMem_UserNEW(size_t size, PyObject *mem_handler) if (handler == NULL) { return NULL; } - assert(size != 0); result = handler->allocator.malloc(handler->allocator.ctx, size); if (_PyDataMem_eventhook != NULL) { @@ -639,3 +639,40 @@ get_handler_name(PyObject *NPY_UNUSED(self), PyObject *args) Py_DECREF(mem_handler); return name; } + +NPY_NO_EXPORT PyObject * +get_handler_version(PyObject *NPY_UNUSED(self), PyObject *args) +{ + PyObject *arr=NULL; + if (!PyArg_ParseTuple(args, "|O:get_handler_version", &arr)) { + return NULL; + } + if (arr != NULL && !PyArray_Check(arr)) { + PyErr_SetString(PyExc_ValueError, "if supplied, argument must be an ndarray"); + return NULL; + } + PyObject *mem_handler; + PyDataMem_Handler *handler; + PyObject *version; + if (arr != NULL) { + mem_handler = PyArray_HANDLER((PyArrayObject *) arr); + if (mem_handler == NULL) { + Py_RETURN_NONE; + } + Py_INCREF(mem_handler); + } + else { + mem_handler = PyDataMem_GetHandler(); + if (mem_handler == NULL) { + return NULL; + } + } + handler = (PyDataMem_Handler *) PyCapsule_GetPointer(mem_handler, "mem_handler"); + if (handler == NULL) { + Py_DECREF(mem_handler); + return NULL; + } + version = PyLong_FromLong(handler->version); + Py_DECREF(mem_handler); + return version; +} diff --git a/numpy/core/src/multiarray/alloc.h b/numpy/core/src/multiarray/alloc.h index 4f7df1f84..f1ccf0bcd 100644 --- a/numpy/core/src/multiarray/alloc.h +++ b/numpy/core/src/multiarray/alloc.h @@ -47,5 +47,7 @@ extern PyDataMem_Handler default_handler; NPY_NO_EXPORT PyObject * get_handler_name(PyObject *NPY_UNUSED(self), PyObject *obj); +NPY_NO_EXPORT PyObject * +get_handler_version(PyObject *NPY_UNUSED(self), PyObject *obj); #endif /* NUMPY_CORE_SRC_MULTIARRAY_ALLOC_H_ */ diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 84179d5f0..a854bcb3b 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4437,6 +4437,9 @@ static struct PyMethodDef array_module_methods[] = { {"get_handler_name", (PyCFunction) get_handler_name, METH_VARARGS, NULL}, + {"get_handler_version", + (PyCFunction) get_handler_version, + METH_VARARGS, NULL}, {"_add_newdoc_ufunc", (PyCFunction)add_newdoc_ufunc, METH_VARARGS, NULL}, {"_get_sfloat_dtype", diff --git a/numpy/core/tests/test_mem_policy.py b/numpy/core/tests/test_mem_policy.py index 7fec8897f..abf340062 100644 --- a/numpy/core/tests/test_mem_policy.py +++ b/numpy/core/tests/test_mem_policy.py @@ -179,6 +179,7 @@ def get_module(tmp_path): }; static PyDataMem_Handler secret_data_handler = { "secret_data_allocator", + 1, { &secret_data_handler_ctx, /* ctx */ shift_alloc, /* malloc */ @@ -212,17 +213,22 @@ def get_module(tmp_path): def test_set_policy(get_module): get_handler_name = np.core.multiarray.get_handler_name + get_handler_version = np.core.multiarray.get_handler_version orig_policy_name = get_handler_name() a = np.arange(10).reshape((2, 5)) # a doesn't own its own data assert get_handler_name(a) is None + assert get_handler_version(a) is None assert get_handler_name(a.base) == orig_policy_name + assert get_handler_version(a.base) == 1 orig_policy = get_module.set_secret_data_policy() b = np.arange(10).reshape((2, 5)) # b doesn't own its own data assert get_handler_name(b) is None + assert get_handler_version(b) is None assert get_handler_name(b.base) == 'secret_data_allocator' + assert get_handler_version(b.base) == 1 if orig_policy_name == 'default_allocator': get_module.set_old_policy(None) # tests PyDataMem_SetHandler(NULL) -- cgit v1.2.1 From a4daaf5a3924248f71caddbcbd3cf28afef802a6 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 5 Nov 2021 10:23:42 -0500 Subject: BUG: Fix float16 einsum fastpaths using wrong tempvar --- numpy/core/src/multiarray/einsum_sumprod.c.src | 30 +++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/einsum_sumprod.c.src b/numpy/core/src/multiarray/einsum_sumprod.c.src index 29ceabd71..3114a5896 100644 --- a/numpy/core/src/multiarray/einsum_sumprod.c.src +++ b/numpy/core/src/multiarray/einsum_sumprod.c.src @@ -337,13 +337,13 @@ static NPY_GCC_OPT_3 void /**begin repeat2 * #i = 0, 1, 2, 3# */ - const @type@ b@i@ = @from@(data[@i@]); - const @type@ c@i@ = @from@(data_out[@i@]); + const @temptype@ b@i@ = @from@(data[@i@]); + const @temptype@ c@i@ = @from@(data_out[@i@]); /**end repeat2**/ /**begin repeat2 * #i = 0, 1, 2, 3# */ - const @type@ abc@i@ = scalar * b@i@ + c@i@; + const @temptype@ abc@i@ = scalar * b@i@ + c@i@; /**end repeat2**/ /**begin repeat2 * #i = 0, 1, 2, 3# @@ -353,8 +353,8 @@ static NPY_GCC_OPT_3 void } #endif // !NPY_DISABLE_OPTIMIZATION for (; count > 0; --count, ++data, ++data_out) { - const @type@ b = @from@(*data); - const @type@ c = @from@(*data_out); + const @temptype@ b = @from@(*data); + const @temptype@ c = @from@(*data_out); *data_out = @to@(scalar * b + c); } #endif // NPYV check for @type@ @@ -417,14 +417,14 @@ static void /**begin repeat2 * #i = 0, 1, 2, 3# */ - const @type@ a@i@ = @from@(data0[@i@]); - const @type@ b@i@ = @from@(data1[@i@]); - const @type@ c@i@ = @from@(data_out[@i@]); + const @temptype@ a@i@ = @from@(data0[@i@]); + const @temptype@ b@i@ = @from@(data1[@i@]); + const @temptype@ c@i@ = @from@(data_out[@i@]); /**end repeat2**/ /**begin repeat2 * #i = 0, 1, 2, 3# */ - const @type@ abc@i@ = a@i@ * b@i@ + c@i@; + const @temptype@ abc@i@ = a@i@ * b@i@ + c@i@; /**end repeat2**/ /**begin repeat2 * #i = 0, 1, 2, 3# @@ -434,9 +434,9 @@ static void } #endif // !NPY_DISABLE_OPTIMIZATION for (; count > 0; --count, ++data0, ++data1, ++data_out) { - const @type@ a = @from@(*data0); - const @type@ b = @from@(*data1); - const @type@ c = @from@(*data_out); + const @temptype@ a = @from@(*data0); + const @temptype@ b = @from@(*data1); + const @temptype@ c = @from@(*data_out); *data_out = @to@(a * b + c); } #endif // NPYV check for @type@ @@ -521,14 +521,14 @@ static NPY_GCC_OPT_3 void /**begin repeat2 * #i = 0, 1, 2, 3# */ - const @type@ ab@i@ = @from@(data0[@i@]) * @from@(data1[@i@]); + const @temptype@ ab@i@ = @from@(data0[@i@]) * @from@(data1[@i@]); /**end repeat2**/ accum += ab0 + ab1 + ab2 + ab3; } #endif // !NPY_DISABLE_OPTIMIZATION for (; count > 0; --count, ++data0, ++data1) { - const @type@ a = @from@(*data0); - const @type@ b = @from@(*data1); + const @temptype@ a = @from@(*data0); + const @temptype@ b = @from@(*data1); accum += a * b; } #endif // NPYV check for @type@ -- cgit v1.2.1 From 7658ad93ba845fd1e7cd6ef81f266b5d18fd565d Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 11:48:06 +0530 Subject: Removed dead code --- numpy/core/src/multiarray/ctors.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 1ff26a311..2170a9a99 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1872,17 +1872,7 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, if ((requires & NPY_ARRAY_ELEMENTSTRIDES) && !PyArray_ElementStrides(obj)) { - // Testing if this code can be ever hit by existing tests - PyErr_SetString(PyExc_RuntimeError, "Not Implemented"); - // PyObject *ret; - // if( requires & NPY_ARRAY_ENSURENOCOPY ) { - // PyErr_SetString(PyExc_ValueError, - // "Unable to avoid copy while creating a new array."); - // return NULL; - // } - // ret = PyArray_NewCopy((PyArrayObject *)obj, NPY_ANYORDER); - // Py_DECREF(obj); - // obj = ret; + PyErr_SetString(PyExc_RuntimeError, "Not Implemented."); } return obj; } -- cgit v1.2.1 From 2cf561ba0284dd4dd70d0396262974495cb4edfa Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 12:32:03 +0530 Subject: Prohibited calling ``__array__`` method in never copy mode --- numpy/core/src/multiarray/array_coercion.c | 13 +++++++------ numpy/core/src/multiarray/array_coercion.h | 2 +- numpy/core/src/multiarray/arrayobject.c | 2 +- numpy/core/src/multiarray/common.c | 2 +- numpy/core/src/multiarray/ctors.c | 14 +++++++++++--- numpy/core/src/multiarray/ctors.h | 3 ++- numpy/core/tests/test_multiarray.py | 3 ++- 7 files changed, 25 insertions(+), 14 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index 847bdafc3..60525026c 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -865,7 +865,8 @@ PyArray_DiscoverDTypeAndShape_Recursive( PyObject *obj, int curr_dims, int max_dims, PyArray_Descr**out_descr, npy_intp out_shape[NPY_MAXDIMS], coercion_cache_obj ***coercion_cache_tail_ptr, - PyArray_DTypeMeta *fixed_DType, enum _dtype_discovery_flags *flags) + PyArray_DTypeMeta *fixed_DType, enum _dtype_discovery_flags *flags, + int do_copy) { PyArrayObject *arr = NULL; PyObject *seq; @@ -923,7 +924,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( requested_descr = *out_descr; } arr = (PyArrayObject *)_array_from_array_like(obj, - requested_descr, 0, NULL); + requested_descr, 0, NULL, do_copy); if (arr == NULL) { return -1; } @@ -1117,7 +1118,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( max_dims = PyArray_DiscoverDTypeAndShape_Recursive( objects[i], curr_dims + 1, max_dims, out_descr, out_shape, coercion_cache_tail_ptr, fixed_DType, - flags); + flags, do_copy); if (max_dims < 0) { return -1; @@ -1169,7 +1170,7 @@ PyArray_DiscoverDTypeAndShape( npy_intp out_shape[NPY_MAXDIMS], coercion_cache_obj **coercion_cache, PyArray_DTypeMeta *fixed_DType, PyArray_Descr *requested_descr, - PyArray_Descr **out_descr) + PyArray_Descr **out_descr, int do_copy) { coercion_cache_obj **coercion_cache_head = coercion_cache; *coercion_cache = NULL; @@ -1214,7 +1215,7 @@ PyArray_DiscoverDTypeAndShape( int ndim = PyArray_DiscoverDTypeAndShape_Recursive( obj, 0, max_dims, out_descr, out_shape, &coercion_cache, - fixed_DType, &flags); + fixed_DType, &flags, do_copy); if (ndim < 0) { goto fail; } @@ -1499,7 +1500,7 @@ _discover_array_parameters(PyObject *NPY_UNUSED(self), int ndim = PyArray_DiscoverDTypeAndShape( obj, NPY_MAXDIMS, shape, &coercion_cache, - fixed_DType, fixed_descriptor, (PyArray_Descr **)&out_dtype); + fixed_DType, fixed_descriptor, (PyArray_Descr **)&out_dtype, 0); Py_XDECREF(fixed_DType); Py_XDECREF(fixed_descriptor); if (ndim < 0) { diff --git a/numpy/core/src/multiarray/array_coercion.h b/numpy/core/src/multiarray/array_coercion.h index db0e479fe..fe59b731c 100644 --- a/numpy/core/src/multiarray/array_coercion.h +++ b/numpy/core/src/multiarray/array_coercion.h @@ -31,7 +31,7 @@ PyArray_DiscoverDTypeAndShape( npy_intp out_shape[NPY_MAXDIMS], coercion_cache_obj **coercion_cache, PyArray_DTypeMeta *fixed_DType, PyArray_Descr *requested_descr, - PyArray_Descr **out_descr); + PyArray_Descr **out_descr, int do_copy); NPY_NO_EXPORT int PyArray_ExtractDTypeAndDescriptor(PyObject *dtype, diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index c8aaced4e..1b197d0f2 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -263,7 +263,7 @@ PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object) */ ndim = PyArray_DiscoverDTypeAndShape(src_object, PyArray_NDIM(dest), dims, &cache, - NPY_DTYPE(PyArray_DESCR(dest)), PyArray_DESCR(dest), &dtype); + NPY_DTYPE(PyArray_DESCR(dest)), PyArray_DESCR(dest), &dtype, 0); if (ndim < 0) { return -1; } diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c index 82d34193d..aa95d285a 100644 --- a/numpy/core/src/multiarray/common.c +++ b/numpy/core/src/multiarray/common.c @@ -119,7 +119,7 @@ PyArray_DTypeFromObject(PyObject *obj, int maxdims, PyArray_Descr **out_dtype) int ndim; ndim = PyArray_DiscoverDTypeAndShape( - obj, maxdims, shape, &cache, NULL, NULL, out_dtype); + obj, maxdims, shape, &cache, NULL, NULL, out_dtype, 0); if (ndim < 0) { return -1; } diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 2170a9a99..23d868c2c 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1291,7 +1291,8 @@ fail: */ NPY_NO_EXPORT PyObject * _array_from_array_like(PyObject *op, - PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context) { + PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context, + int do_copy) { PyObject* tmp; /* @@ -1347,6 +1348,12 @@ _array_from_array_like(PyObject *op, * this should be changed! */ if (!writeable && tmp == Py_NotImplemented) { + PyObject* array_meth = PyArray_LookupSpecial_OnInstance(op, "__array__"); + PyObject* has_get = array_meth && PyType_Check(op) && PyObject_HasAttrString(array_meth, "__get__"); + if (array_meth != NULL && !has_get && do_copy) { + PyErr_SetString(PyExc_ValueError, "Calling __array__ in never copy mode is not allowed."); + return NULL; + } tmp = PyArray_FromArrayAttr(op, requested_dtype, context); if (tmp == NULL) { return NULL; @@ -1447,7 +1454,7 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s, } /* Try __array__ before using s as a sequence */ - PyObject *tmp = _array_from_array_like(s, NULL, 0, NULL); + PyObject *tmp = _array_from_array_like(s, NULL, 0, NULL, 0); if (tmp == NULL) { goto fail; } @@ -1575,7 +1582,8 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, Py_XDECREF(newtype); ndim = PyArray_DiscoverDTypeAndShape(op, - NPY_MAXDIMS, dims, &cache, fixed_DType, fixed_descriptor, &dtype); + NPY_MAXDIMS, dims, &cache, fixed_DType, fixed_descriptor, &dtype, + flags & NPY_ARRAY_ENSURENOCOPY); Py_XDECREF(fixed_descriptor); Py_XDECREF(fixed_DType); diff --git a/numpy/core/src/multiarray/ctors.h b/numpy/core/src/multiarray/ctors.h index e59e86e8b..4f8bd5a82 100644 --- a/numpy/core/src/multiarray/ctors.h +++ b/numpy/core/src/multiarray/ctors.h @@ -32,7 +32,8 @@ PyArray_New( NPY_NO_EXPORT PyObject * _array_from_array_like(PyObject *op, - PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context); + PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context, + int do_copy); NPY_NO_EXPORT PyObject * PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 4ef58d904..18302543a 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7943,7 +7943,8 @@ class TestArrayCreationCopyArgument(object): assert_array_equal(res, base_arr) assert res is base_arr # numpy trusts the ArrayLike - assert np.array(arr, copy=np._CopyMode.NEVER) is base_arr + with pytest.raises(ValueError): + np.array(arr, copy=np._CopyMode.NEVER) @pytest.mark.parametrize( "arr", [np.ones(()), np.arange(81).reshape((9, 9))]) -- cgit v1.2.1 From 37cd05eb4fa732230345785de53722a80c27fc62 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 13:15:59 +0530 Subject: Fixed warning and updated docs --- numpy/core/src/multiarray/array_coercion.c | 1 + numpy/core/src/multiarray/ctors.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index 60525026c..93957242b 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -857,6 +857,7 @@ PyArray_AdaptDescriptorToArray(PyArrayObject *arr, PyObject *dtype) * (Initially it is a pointer to the user-provided head pointer). * @param fixed_DType User provided fixed DType class * @param flags Discovery flags (reporting and behaviour flags, see def.) + * @param do_copy Specifies if a copy is to be made during array creation. * @return The updated number of maximum dimensions (i.e. scalars will set * this to the current dimensions). */ diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 23d868c2c..0fbf0f91f 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1349,7 +1349,7 @@ _array_from_array_like(PyObject *op, */ if (!writeable && tmp == Py_NotImplemented) { PyObject* array_meth = PyArray_LookupSpecial_OnInstance(op, "__array__"); - PyObject* has_get = array_meth && PyType_Check(op) && PyObject_HasAttrString(array_meth, "__get__"); + int has_get = array_meth && PyType_Check(op) && PyObject_HasAttrString(array_meth, "__get__"); if (array_meth != NULL && !has_get && do_copy) { PyErr_SetString(PyExc_ValueError, "Calling __array__ in never copy mode is not allowed."); return NULL; -- cgit v1.2.1 From 37cbe22f16b6460024854f96461b11c79e08d7c1 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Wed, 10 Nov 2021 09:03:03 +0100 Subject: Do not position 'cxx=-std=c++11' as a default compiler flag Otherwise it gets used for C source too, which is either useless or invalid (for clang(family compiler) Fix #20335 --- numpy/distutils/ccompiler_opt.py | 5 ----- 1 file changed, 5 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/ccompiler_opt.py b/numpy/distutils/ccompiler_opt.py index d7df386fe..85bf5d0d1 100644 --- a/numpy/distutils/ccompiler_opt.py +++ b/numpy/distutils/ccompiler_opt.py @@ -196,7 +196,6 @@ class _Config: native = '-march=native', opt = '-O3', werror = '-Werror', - cxx = '-std=c++11', ), clang = dict( native = '-march=native', @@ -207,25 +206,21 @@ class _Config: # "unused arguments" warnings. # see https://github.com/numpy/numpy/issues/19624 werror = '-Werror=switch -Werror', - cxx = '-std=c++11', ), icc = dict( native = '-xHost', opt = '-O3', werror = '-Werror', - cxx = '-std=c++11', ), iccw = dict( native = '/QxHost', opt = '/O3', werror = '/Werror', - cxx = '-std=c++11', ), msvc = dict( native = None, opt = '/O2', werror = '/WX', - cxx = '-std=c++11', ) ) conf_min_features = dict( -- cgit v1.2.1 From 9f9a34843016faea897cd591a5c4c104fa68c883 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 14:27:35 +0530 Subject: Apply suggestions from code review Co-authored-by: Matti Picus --- numpy/_globals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/_globals.py b/numpy/_globals.py index b17ca1979..41adaae25 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -97,7 +97,7 @@ _NoValue = _NoValueType() class _CopyMode(enum.Enum): """ An enumeration for the copy modes supported - by numpy. The following three modes are supported, + by numpy.copy() and numpy.array(). The following three modes are supported, - ALWAYS: This means that a deep copy of the input array will always be taken. -- cgit v1.2.1 From 946ab2461870047b1c210b93152234d185684927 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 14:28:06 +0530 Subject: Updated docs --- numpy/core/src/multiarray/array_coercion.c | 1 + numpy/core/src/multiarray/ctors.c | 1 + 2 files changed, 2 insertions(+) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index 93957242b..aa5f1f2fd 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -1159,6 +1159,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( * The result may be unchanged (remain NULL) when converting a * sequence with no elements. In this case it is callers responsibility * to choose a default. + * @param do_copy Specifies if a copy is to be made during array creation. * @return dimensions of the discovered object or -1 on error. * WARNING: If (and only if) the output is a single array, the ndim * returned _can_ exceed the maximum allowed number of dimensions. diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 0fbf0f91f..ca3a29d53 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1284,6 +1284,7 @@ fail: * DType may be used, but is not enforced. * @param writeable whether the result must be writeable. * @param context Unused parameter, must be NULL (should be removed later). + * @param do_copy Specifies if a copy is to be made during array creation. * * @returns The array object, Py_NotImplemented if op is not array-like, * or NULL with an error set. (A new reference to Py_NotImplemented -- cgit v1.2.1 From a5e9556a55942ea6f0acb91618eb9648b801371a Mon Sep 17 00:00:00 2001 From: Elias Koromilas Date: Wed, 10 Nov 2021 11:50:11 +0200 Subject: Use a default handler capsule singleton Signed-off-by: Elias Koromilas --- numpy/core/src/multiarray/alloc.c | 30 ++++++---------------------- numpy/core/src/multiarray/alloc.h | 3 ++- numpy/core/src/multiarray/multiarraymodule.c | 15 ++++++++------ numpy/core/tests/test_mem_policy.py | 27 ++++++++++++++++++++----- 4 files changed, 39 insertions(+), 36 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/alloc.c b/numpy/core/src/multiarray/alloc.c index e4756264d..d81b25e4e 100644 --- a/numpy/core/src/multiarray/alloc.c +++ b/numpy/core/src/multiarray/alloc.c @@ -378,6 +378,8 @@ PyDataMem_Handler default_handler = { default_free /* free */ } }; +/* singleton capsule of the default handler */ +PyObject *default_handler_capsule; #if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) PyObject *current_handler; @@ -519,16 +521,9 @@ PyDataMem_SetHandler(PyObject *handler) return NULL; } if (handler == NULL) { - handler = PyCapsule_New(&default_handler, "mem_handler", NULL); - if (handler == NULL) { - return NULL; - } - } - else { - Py_INCREF(handler); + handler = default_handler_capsule; } token = PyContextVar_Set(current_handler, handler); - Py_DECREF(handler); if (token == NULL) { Py_DECREF(old_handler); return NULL; @@ -543,26 +538,13 @@ PyDataMem_SetHandler(PyObject *handler) } old_handler = PyDict_GetItemString(p, "current_allocator"); if (old_handler == NULL) { - old_handler = PyCapsule_New(&default_handler, "mem_handler", NULL); - if (old_handler == NULL) { - return NULL; - } - } - else { - Py_INCREF(old_handler); + old_handler = default_handler_capsule } + Py_INCREF(old_handler); if (handler == NULL) { - handler = PyCapsule_New(&default_handler, "mem_handler", NULL); - if (handler == NULL) { - Py_DECREF(old_handler); - return NULL; - } - } - else { - Py_INCREF(handler); + handler = default_handler_capsule; } const int error = PyDict_SetItemString(p, "current_allocator", handler); - Py_DECREF(handler); if (error) { Py_DECREF(old_handler); return NULL; diff --git a/numpy/core/src/multiarray/alloc.h b/numpy/core/src/multiarray/alloc.h index 4f7df1f84..3bd6a4d74 100644 --- a/numpy/core/src/multiarray/alloc.h +++ b/numpy/core/src/multiarray/alloc.h @@ -40,9 +40,10 @@ npy_free_cache_dim_array(PyArrayObject * arr) npy_free_cache_dim(PyArray_DIMS(arr), PyArray_NDIM(arr)); } +extern PyDataMem_Handler default_handler; +extern PyObject *default_handler_capsule; #if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) extern PyObject *current_handler; /* PyContextVar/PyCapsule */ -extern PyDataMem_Handler default_handler; #endif NPY_NO_EXPORT PyObject * diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 84179d5f0..503c9ef0d 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4916,16 +4916,19 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { if (initumath(m) != 0) { goto err; } -#if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) /* - * Initialize the context-local PyDataMem_Handler capsule. + * Initialize the default PyDataMem_Handler capsule singleton. */ - c_api = PyCapsule_New(&default_handler, "mem_handler", NULL); - if (c_api == NULL) { + default_handler_capsule = PyCapsule_New(&default_handler, "mem_handler", NULL); + if (default_handler_capsule == NULL) { goto err; } - current_handler = PyContextVar_New("current_allocator", c_api); - Py_DECREF(c_api); +#if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) + /* + * Initialize the context-local current handler + * with the default PyDataMem_Handler capsule. + */ + current_handler = PyContextVar_New("current_allocator", default_handler_capsule); if (current_handler == NULL) { goto err; } diff --git a/numpy/core/tests/test_mem_policy.py b/numpy/core/tests/test_mem_policy.py index 7fec8897f..a05abbf98 100644 --- a/numpy/core/tests/test_mem_policy.py +++ b/numpy/core/tests/test_mem_policy.py @@ -37,11 +37,7 @@ def get_module(tmp_path): else { old = PyDataMem_SetHandler(NULL); } - if (old == NULL) { - return NULL; - } - Py_DECREF(old); - Py_RETURN_NONE; + return old; """), ("get_array", "METH_NOARGS", """ char *buf = (char *)malloc(20); @@ -232,6 +228,27 @@ def test_set_policy(get_module): assert get_handler_name() == orig_policy_name +def test_default_policy_singleton(get_module): + get_handler_name = np.core.multiarray.get_handler_name + + # set the policy to default + orig_policy = get_module.set_old_policy(None) + + assert get_handler_name() == 'default_allocator' + + # re-set the policy to default + default_policy_1 = get_module.set_old_policy(None) + + assert get_handler_name() == 'default_allocator' + + # set the policy to orig + default_policy_2 = get_module.set_old_policy(orig_policy) + + # since default policy is a singleton, + # these should be the same object + assert default_policy_1 is default_policy_2 + + def test_policy_propagation(get_module): # The memory policy goes hand-in-hand with flags.owndata -- cgit v1.2.1 From d1cb6624f5b52b1de5aa89dd18062dbca79ab00d Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 15:40:55 +0530 Subject: L[0]->L[False], L[1]->L[True] --- numpy/__init__.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 4e6969e32..d510acaa5 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -3243,8 +3243,8 @@ trunc: _UFunc_Nin1_Nout1[L['trunc'], L[7], None] abs = absolute class _CopyMode(enum.Enum): - ALWAYS: L[1] - IF_NEEDED: L[0] + ALWAYS: L[True] + IF_NEEDED: L[False] NEVER: L[2] # Warnings -- cgit v1.2.1 From 855b5ed82381c088a0dbf8e629927a7f337e2dbb Mon Sep 17 00:00:00 2001 From: Elias Koromilas Date: Wed, 10 Nov 2021 13:27:13 +0200 Subject: Export globally the default handler capsule singleton Signed-off-by: Elias Koromilas --- numpy/core/code_generators/cversions.txt | 2 +- numpy/core/code_generators/numpy_api.py | 1 + numpy/core/src/multiarray/alloc.c | 8 ++++---- numpy/core/src/multiarray/alloc.h | 1 - numpy/core/src/multiarray/multiarraymodule.c | 6 +++--- numpy/core/tests/test_mem_policy.py | 12 ++++++++---- 6 files changed, 17 insertions(+), 13 deletions(-) (limited to 'numpy') diff --git a/numpy/core/code_generators/cversions.txt b/numpy/core/code_generators/cversions.txt index 38ee4dac2..f0a128d3d 100644 --- a/numpy/core/code_generators/cversions.txt +++ b/numpy/core/code_generators/cversions.txt @@ -59,4 +59,4 @@ 0x0000000e = 17a0f366e55ec05e5c5c149123478452 # Version 15 (NumPy 1.22) Configurable memory allocations -0x0000000f = 0c420aed67010594eb81f23ddfb02a88 +0x0000000f = b8783365b873681cd204be50cdfb448d diff --git a/numpy/core/code_generators/numpy_api.py b/numpy/core/code_generators/numpy_api.py index 3813c6ad7..d12d62d8f 100644 --- a/numpy/core/code_generators/numpy_api.py +++ b/numpy/core/code_generators/numpy_api.py @@ -19,6 +19,7 @@ from code_generators.genapi import StealRef, NonNull multiarray_global_vars = { 'NPY_NUMUSERTYPES': (7, 'int'), 'NPY_DEFAULT_ASSIGN_CASTING': (292, 'NPY_CASTING'), + 'PyDataMem_DefaultHandler': (306, 'PyObject*'), } multiarray_scalar_bool_values = { diff --git a/numpy/core/src/multiarray/alloc.c b/numpy/core/src/multiarray/alloc.c index d81b25e4e..d25f8df2a 100644 --- a/numpy/core/src/multiarray/alloc.c +++ b/numpy/core/src/multiarray/alloc.c @@ -379,7 +379,7 @@ PyDataMem_Handler default_handler = { } }; /* singleton capsule of the default handler */ -PyObject *default_handler_capsule; +PyObject *PyDataMem_DefaultHandler; #if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) PyObject *current_handler; @@ -521,7 +521,7 @@ PyDataMem_SetHandler(PyObject *handler) return NULL; } if (handler == NULL) { - handler = default_handler_capsule; + handler = PyDataMem_DefaultHandler; } token = PyContextVar_Set(current_handler, handler); if (token == NULL) { @@ -538,11 +538,11 @@ PyDataMem_SetHandler(PyObject *handler) } old_handler = PyDict_GetItemString(p, "current_allocator"); if (old_handler == NULL) { - old_handler = default_handler_capsule + old_handler = PyDataMem_DefaultHandler } Py_INCREF(old_handler); if (handler == NULL) { - handler = default_handler_capsule; + handler = PyDataMem_DefaultHandler; } const int error = PyDict_SetItemString(p, "current_allocator", handler); if (error) { diff --git a/numpy/core/src/multiarray/alloc.h b/numpy/core/src/multiarray/alloc.h index 3bd6a4d74..68b46735a 100644 --- a/numpy/core/src/multiarray/alloc.h +++ b/numpy/core/src/multiarray/alloc.h @@ -41,7 +41,6 @@ npy_free_cache_dim_array(PyArrayObject * arr) } extern PyDataMem_Handler default_handler; -extern PyObject *default_handler_capsule; #if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) extern PyObject *current_handler; /* PyContextVar/PyCapsule */ #endif diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 503c9ef0d..e5fa0011f 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4919,8 +4919,8 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { /* * Initialize the default PyDataMem_Handler capsule singleton. */ - default_handler_capsule = PyCapsule_New(&default_handler, "mem_handler", NULL); - if (default_handler_capsule == NULL) { + PyDataMem_DefaultHandler = PyCapsule_New(&default_handler, "mem_handler", NULL); + if (PyDataMem_DefaultHandler == NULL) { goto err; } #if (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x07030600) @@ -4928,7 +4928,7 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) { * Initialize the context-local current handler * with the default PyDataMem_Handler capsule. */ - current_handler = PyContextVar_New("current_allocator", default_handler_capsule); + current_handler = PyContextVar_New("current_allocator", PyDataMem_DefaultHandler); if (current_handler == NULL) { goto err; } diff --git a/numpy/core/tests/test_mem_policy.py b/numpy/core/tests/test_mem_policy.py index a05abbf98..65cdda2ab 100644 --- a/numpy/core/tests/test_mem_policy.py +++ b/numpy/core/tests/test_mem_policy.py @@ -19,6 +19,10 @@ def get_module(tmp_path): if sys.platform.startswith('cygwin'): pytest.skip('link fails on cygwin') functions = [ + ("get_default_policy", "METH_NOARGS", """ + Py_INCREF(PyDataMem_DefaultHandler); + return PyDataMem_DefaultHandler; + """), ("set_secret_data_policy", "METH_NOARGS", """ PyObject *secret_data = PyCapsule_New(&secret_data_handler, "mem_handler", NULL); @@ -237,16 +241,16 @@ def test_default_policy_singleton(get_module): assert get_handler_name() == 'default_allocator' # re-set the policy to default - default_policy_1 = get_module.set_old_policy(None) + def_policy_1 = get_module.set_old_policy(None) assert get_handler_name() == 'default_allocator' - # set the policy to orig - default_policy_2 = get_module.set_old_policy(orig_policy) + # set the policy to original + def_policy_2 = get_module.set_old_policy(orig_policy) # since default policy is a singleton, # these should be the same object - assert default_policy_1 is default_policy_2 + assert def_policy_1 is def_policy_2 is get_module.get_default_policy() def test_policy_propagation(get_module): -- cgit v1.2.1 From 8871c7285fc097fd1bf713aa184cba7e2804f625 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Wed, 10 Nov 2021 15:36:00 +0100 Subject: MAINT: Do not forward `__(deep)copy__` calls of `_GenericAlias` to the wrapped type Adapt to the python 3.9.8 changes made in bpo-45167. --- numpy/typing/_generic_alias.py | 2 ++ numpy/typing/tests/test_generic_alias.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) (limited to 'numpy') diff --git a/numpy/typing/_generic_alias.py b/numpy/typing/_generic_alias.py index 932f12dd0..1eb2c8c05 100644 --- a/numpy/typing/_generic_alias.py +++ b/numpy/typing/_generic_alias.py @@ -185,6 +185,8 @@ class _GenericAlias: "__mro_entries__", "__reduce__", "__reduce_ex__", + "__copy__", + "__deepcopy__", }) def __getattribute__(self, name: str) -> Any: diff --git a/numpy/typing/tests/test_generic_alias.py b/numpy/typing/tests/test_generic_alias.py index 3021d9859..39343420b 100644 --- a/numpy/typing/tests/test_generic_alias.py +++ b/numpy/typing/tests/test_generic_alias.py @@ -1,6 +1,7 @@ from __future__ import annotations import sys +import copy import types import pickle import weakref @@ -80,6 +81,21 @@ class TestGenericAlias: value_ref = func(NDArray_ref) assert value == value_ref + @pytest.mark.parametrize("name,func", [ + ("__copy__", lambda n: n == copy.copy(n)), + ("__deepcopy__", lambda n: n == copy.deepcopy(n)), + ]) + def test_copy(self, name: str, func: FuncType) -> None: + value = func(NDArray) + + # xref bpo-45167 + GE_398 = ( + sys.version_info[:2] == (3, 9) and sys.version_info >= (3, 9, 8) + ) + if GE_398 or sys.version_info >= (3, 10, 1): + value_ref = func(NDArray_ref) + assert value == value_ref + def test_weakref(self) -> None: """Test ``__weakref__``.""" value = weakref.ref(NDArray)() -- cgit v1.2.1 From 5f1965fd36d9d28c0f8743384485e346564a919b Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 21:49:45 +0530 Subject: do_copy -> allow_copy --- numpy/core/src/multiarray/array_coercion.c | 14 +++++++------- numpy/core/src/multiarray/array_coercion.h | 2 +- numpy/core/src/multiarray/ctors.c | 6 +++--- numpy/core/src/multiarray/ctors.h | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index aa5f1f2fd..8778ec20c 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -857,7 +857,7 @@ PyArray_AdaptDescriptorToArray(PyArrayObject *arr, PyObject *dtype) * (Initially it is a pointer to the user-provided head pointer). * @param fixed_DType User provided fixed DType class * @param flags Discovery flags (reporting and behaviour flags, see def.) - * @param do_copy Specifies if a copy is to be made during array creation. + * @param allow_copy Specifies if a copy is allowed during array creation. * @return The updated number of maximum dimensions (i.e. scalars will set * this to the current dimensions). */ @@ -867,7 +867,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( npy_intp out_shape[NPY_MAXDIMS], coercion_cache_obj ***coercion_cache_tail_ptr, PyArray_DTypeMeta *fixed_DType, enum _dtype_discovery_flags *flags, - int do_copy) + int allow_copy) { PyArrayObject *arr = NULL; PyObject *seq; @@ -925,7 +925,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( requested_descr = *out_descr; } arr = (PyArrayObject *)_array_from_array_like(obj, - requested_descr, 0, NULL, do_copy); + requested_descr, 0, NULL, allow_copy); if (arr == NULL) { return -1; } @@ -1119,7 +1119,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( max_dims = PyArray_DiscoverDTypeAndShape_Recursive( objects[i], curr_dims + 1, max_dims, out_descr, out_shape, coercion_cache_tail_ptr, fixed_DType, - flags, do_copy); + flags, allow_copy); if (max_dims < 0) { return -1; @@ -1159,7 +1159,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( * The result may be unchanged (remain NULL) when converting a * sequence with no elements. In this case it is callers responsibility * to choose a default. - * @param do_copy Specifies if a copy is to be made during array creation. + * @param allow_copy Specifies if a copy is allowed during array creation. * @return dimensions of the discovered object or -1 on error. * WARNING: If (and only if) the output is a single array, the ndim * returned _can_ exceed the maximum allowed number of dimensions. @@ -1172,7 +1172,7 @@ PyArray_DiscoverDTypeAndShape( npy_intp out_shape[NPY_MAXDIMS], coercion_cache_obj **coercion_cache, PyArray_DTypeMeta *fixed_DType, PyArray_Descr *requested_descr, - PyArray_Descr **out_descr, int do_copy) + PyArray_Descr **out_descr, int allow_copy) { coercion_cache_obj **coercion_cache_head = coercion_cache; *coercion_cache = NULL; @@ -1217,7 +1217,7 @@ PyArray_DiscoverDTypeAndShape( int ndim = PyArray_DiscoverDTypeAndShape_Recursive( obj, 0, max_dims, out_descr, out_shape, &coercion_cache, - fixed_DType, &flags, do_copy); + fixed_DType, &flags, allow_copy); if (ndim < 0) { goto fail; } diff --git a/numpy/core/src/multiarray/array_coercion.h b/numpy/core/src/multiarray/array_coercion.h index fe59b731c..4790d8030 100644 --- a/numpy/core/src/multiarray/array_coercion.h +++ b/numpy/core/src/multiarray/array_coercion.h @@ -31,7 +31,7 @@ PyArray_DiscoverDTypeAndShape( npy_intp out_shape[NPY_MAXDIMS], coercion_cache_obj **coercion_cache, PyArray_DTypeMeta *fixed_DType, PyArray_Descr *requested_descr, - PyArray_Descr **out_descr, int do_copy); + PyArray_Descr **out_descr, int allow_copy); NPY_NO_EXPORT int PyArray_ExtractDTypeAndDescriptor(PyObject *dtype, diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index ca3a29d53..d9289dc96 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1284,7 +1284,7 @@ fail: * DType may be used, but is not enforced. * @param writeable whether the result must be writeable. * @param context Unused parameter, must be NULL (should be removed later). - * @param do_copy Specifies if a copy is to be made during array creation. + * @param allow_copy Specifies if a copy is allowed during array creation. * * @returns The array object, Py_NotImplemented if op is not array-like, * or NULL with an error set. (A new reference to Py_NotImplemented @@ -1293,7 +1293,7 @@ fail: NPY_NO_EXPORT PyObject * _array_from_array_like(PyObject *op, PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context, - int do_copy) { + int allow_copy) { PyObject* tmp; /* @@ -1351,7 +1351,7 @@ _array_from_array_like(PyObject *op, if (!writeable && tmp == Py_NotImplemented) { PyObject* array_meth = PyArray_LookupSpecial_OnInstance(op, "__array__"); int has_get = array_meth && PyType_Check(op) && PyObject_HasAttrString(array_meth, "__get__"); - if (array_meth != NULL && !has_get && do_copy) { + if (array_meth != NULL && !has_get && allow_copy) { PyErr_SetString(PyExc_ValueError, "Calling __array__ in never copy mode is not allowed."); return NULL; } diff --git a/numpy/core/src/multiarray/ctors.h b/numpy/core/src/multiarray/ctors.h index 4f8bd5a82..cf01a6256 100644 --- a/numpy/core/src/multiarray/ctors.h +++ b/numpy/core/src/multiarray/ctors.h @@ -33,7 +33,7 @@ PyArray_New( NPY_NO_EXPORT PyObject * _array_from_array_like(PyObject *op, PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context, - int do_copy); + int allow_copy); NPY_NO_EXPORT PyObject * PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, -- cgit v1.2.1 From 05ff102c00558de31f2f5ef8145241490156267b Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 21:52:32 +0530 Subject: Apply suggestions from code review Co-authored-by: Sebastian Berg --- numpy/core/src/multiarray/conversion_utils.c | 2 +- numpy/core/src/multiarray/ctors.c | 8 +++----- numpy/core/src/multiarray/multiarraymodule.c | 3 ++- 3 files changed, 6 insertions(+), 7 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 4df46cffa..ee3e7b50c 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -182,7 +182,7 @@ PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) { } int_copymode = PyLong_AsLong(mode_value); - if (int_copymode < 0 || PyErr_Occurred()) { + if (error_converting(int_copymode)) { return NPY_FAIL; } } diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index ca3a29d53..fcb19096b 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1747,7 +1747,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, /* Create a new array and copy the data */ Py_INCREF(dtype); /* hold on in case of a subarray that is replaced */ - if( flags & NPY_ARRAY_ENSURENOCOPY ) { + if (flags & NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, "Unable to avoid copy while creating " "an array from descriptor."); @@ -1953,11 +1953,9 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) !PyArray_EquivTypes(oldtype, newtype); if (copy) { - - if( flags & NPY_ARRAY_ENSURENOCOPY ) { + if (flags & NPY_ARRAY_ENSURENOCOPY ) { PyErr_SetString(PyExc_ValueError, - "Unable to avoid copy while creating " - "an array from given array."); + "Unable to avoid copy while creating an array from given array."); return NULL; } diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 15c2e3c10..5f72674fe 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1622,7 +1622,8 @@ _array_fromobject_generic( if (copy == NPY_COPY_ALWAYS) { flags = NPY_ARRAY_ENSURECOPY; - } else if( copy == NPY_COPY_NEVER ) { + } + else if (copy == NPY_COPY_NEVER ) { flags = NPY_ARRAY_ENSURENOCOPY; } if (order == NPY_CORDER) { -- cgit v1.2.1 From 2db65c9db122d780462c6fdcd8d54b85c6631365 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 22:02:46 +0530 Subject: Addressed reviews --- numpy/core/src/multiarray/methods.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 7530a2e36..dddfb35f6 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -859,16 +859,16 @@ array_astype(PyArrayObject *self, * can skip the copy. */ if (forcecopy != NPY_COPY_ALWAYS && - (order == NPY_KEEPORDER || - (order == NPY_ANYORDER && - (PyArray_IS_C_CONTIGUOUS(self) || - PyArray_IS_F_CONTIGUOUS(self))) || - (order == NPY_CORDER && - PyArray_IS_C_CONTIGUOUS(self)) || - (order == NPY_FORTRANORDER && - PyArray_IS_F_CONTIGUOUS(self))) && - (subok || PyArray_CheckExact(self)) && - PyArray_EquivTypes(dtype, PyArray_DESCR(self))) { + (order == NPY_KEEPORDER || + (order == NPY_ANYORDER && + (PyArray_IS_C_CONTIGUOUS(self) || + PyArray_IS_F_CONTIGUOUS(self))) || + (order == NPY_CORDER && + PyArray_IS_C_CONTIGUOUS(self)) || + (order == NPY_FORTRANORDER && + PyArray_IS_F_CONTIGUOUS(self))) && + (subok || PyArray_CheckExact(self)) && + PyArray_EquivTypes(dtype, PyArray_DESCR(self))) { Py_DECREF(dtype); Py_INCREF(self); return (PyObject *)self; -- cgit v1.2.1 From d0d75f39f28ac26d4cc1aa3a4cbea63a6a027929 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 10 Nov 2021 22:27:45 +0530 Subject: Reverted dead code removal --- numpy/core/src/multiarray/ctors.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index dd92f715c..286e45e39 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1881,7 +1881,16 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, if ((requires & NPY_ARRAY_ELEMENTSTRIDES) && !PyArray_ElementStrides(obj)) { - PyErr_SetString(PyExc_RuntimeError, "Not Implemented."); + PyObject *ret; + if (requires & NPY_ARRAY_ENSURENOCOPY) { + PyErr_SetString(PyExc_ValueError, + "Unable to avoid copy " + "while creating a new array."); + return NULL; + } + ret = PyArray_NewCopy((PyArrayObject *)obj, NPY_ANYORDER); + Py_DECREF(obj); + obj = ret; } return obj; } -- cgit v1.2.1 From 89c6446b44641c720031f93c6e1c823900c65a50 Mon Sep 17 00:00:00 2001 From: Mukulika Date: Mon, 8 Nov 2021 17:42:39 +0530 Subject: DOC: Added and modified examples Also fixed minor typos and styling errors --- numpy/lib/index_tricks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py index 2a4402c89..b69226d48 100644 --- a/numpy/lib/index_tricks.py +++ b/numpy/lib/index_tricks.py @@ -227,13 +227,13 @@ class MGridClass(nd_grid): See Also -------- - numpy.lib.index_tricks.nd_grid : class of `ogrid` and `mgrid` objects + lib.index_tricks.nd_grid : class of `ogrid` and `mgrid` objects ogrid : like mgrid but returns open (not fleshed out) mesh grids r_ : array concatenator Examples -------- - >>> np.mgrid[0:5,0:5] + >>> np.mgrid[0:5, 0:5] array([[[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2], -- cgit v1.2.1 From cf71558b6c3648256bcc307a488866ecbfa1467e Mon Sep 17 00:00:00 2001 From: Warren Weckesser Date: Thu, 11 Nov 2021 04:55:10 -0500 Subject: DOC: random: Fix a mistake in the zipf example. There was a mistake in the code that generated the plot in the zipf docstring. The Riemann zeta function is `scipy.special.zeta`, not `scipy.special.zetac`. I also tweaked the sample parameters and the plot code so the plot is a bit more informative. --- numpy/random/_generator.pyx | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'numpy') diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index 5347ea125..c103f42aa 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -3153,22 +3153,29 @@ cdef class Generator: -------- Draw samples from the distribution: - >>> a = 2. # parameter - >>> s = np.random.default_rng().zipf(a, 1000) + >>> a = 4.0 + >>> n = 20000 + >>> s = np.random.default_rng().zipf(a, size=n) Display the histogram of the samples, along with - the probability density function: + the expected histogram based on the probability + density function: >>> import matplotlib.pyplot as plt - >>> from scipy import special # doctest: +SKIP + >>> from scipy.special import zeta # doctest: +SKIP + + `bincount` provides a fast histogram for small integers. - Truncate s values at 50 so plot is interesting: + >>> count = np.bincount(s) + >>> x = np.arange(1, s.max() + 1) - >>> count, bins, ignored = plt.hist(s[s<50], - ... 50, density=True) - >>> x = np.arange(1., 50.) - >>> y = x**(-a) / special.zetac(a) # doctest: +SKIP - >>> plt.plot(x, y/max(y), linewidth=2, color='r') # doctest: +SKIP + >>> plt.bar(x, count[1:], alpha=0.5, label='sample count') + >>> plt.plot(x, n*(x**-a)/zeta(a), 'k.-', alpha=0.5, + ... label='expected count') # doctest: +SKIP + >>> plt.semilogy() + >>> plt.grid(alpha=0.4) + >>> plt.legend() + >>> plt.title(f'Zipf sample, a={a}, size={n}') >>> plt.show() """ -- cgit v1.2.1 From f0cce63fb7adcbd6664074fec4536d1ab761051e Mon Sep 17 00:00:00 2001 From: warren Date: Thu, 11 Nov 2021 13:28:42 -0500 Subject: DOC: random: Update the zipf example for the legacy RandomState method. --- numpy/random/mtrand.pyx | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'numpy') diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index 81a526ab4..fa7e95412 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -3660,21 +3660,29 @@ cdef class RandomState: -------- Draw samples from the distribution: - >>> a = 2. # parameter - >>> s = np.random.zipf(a, 1000) + >>> a = 4.0 + >>> n = 20000 + >>> s = np.random.zipf(a, n) Display the histogram of the samples, along with - the probability density function: + the expected histogram based on the probability + density function: >>> import matplotlib.pyplot as plt - >>> from scipy import special # doctest: +SKIP + >>> from scipy.special import zeta # doctest: +SKIP + + `bincount` provides a fast histogram for small integers. - Truncate s values at 50 so plot is interesting: + >>> count = np.bincount(s) + >>> x = np.arange(1, s.max() + 1) - >>> count, bins, ignored = plt.hist(s[s<50], 50, density=True) - >>> x = np.arange(1., 50.) - >>> y = x**(-a) / special.zetac(a) # doctest: +SKIP - >>> plt.plot(x, y/max(y), linewidth=2, color='r') # doctest: +SKIP + >>> plt.bar(x, count[1:], alpha=0.5, label='sample count') + >>> plt.plot(x, n*(x**-a)/zeta(a), 'k.-', alpha=0.5, + ... label='expected count') # doctest: +SKIP + >>> plt.semilogy() + >>> plt.grid(alpha=0.4) + >>> plt.legend() + >>> plt.title(f'Zipf sample, a={a}, size={n}') >>> plt.show() """ -- cgit v1.2.1 From fd78f1fd44f46fc3d23f01e601268af2467dfa8b Mon Sep 17 00:00:00 2001 From: warren Date: Thu, 11 Nov 2021 13:41:20 -0500 Subject: DOC: random: Copy-edit zipf docstring: zipf is a discrete distribution. --- numpy/random/_generator.pyx | 13 +++++++------ numpy/random/mtrand.pyx | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index c103f42aa..1e65be3f1 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -3107,7 +3107,7 @@ cdef class Generator: `a` > 1. The Zipf distribution (also known as the zeta distribution) is a - continuous probability distribution that satisfies Zipf's law: the + discrete probability distribution that satisfies Zipf's law: the frequency of an item is inversely proportional to its rank in a frequency table. @@ -3135,9 +3135,10 @@ cdef class Generator: ----- The probability density for the Zipf distribution is - .. math:: p(x) = \\frac{x^{-a}}{\\zeta(a)}, + .. math:: p(k) = \\frac{k^{-a}}{\\zeta(a)}, - where :math:`\\zeta` is the Riemann Zeta function. + for integers ``k`` >= 1, where :math:`\\zeta` is the Riemann Zeta + function. It is named for the American linguist George Kingsley Zipf, who noted that the frequency of any word in a sample of a language is inversely @@ -3167,10 +3168,10 @@ cdef class Generator: `bincount` provides a fast histogram for small integers. >>> count = np.bincount(s) - >>> x = np.arange(1, s.max() + 1) + >>> k = np.arange(1, s.max() + 1) - >>> plt.bar(x, count[1:], alpha=0.5, label='sample count') - >>> plt.plot(x, n*(x**-a)/zeta(a), 'k.-', alpha=0.5, + >>> plt.bar(k, count[1:], alpha=0.5, label='sample count') + >>> plt.plot(k, n*(k**-a)/zeta(a), 'k.-', alpha=0.5, ... label='expected count') # doctest: +SKIP >>> plt.semilogy() >>> plt.grid(alpha=0.4) diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index fa7e95412..280b0faac 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -3609,7 +3609,7 @@ cdef class RandomState: `a` > 1. The Zipf distribution (also known as the zeta distribution) is a - continuous probability distribution that satisfies Zipf's law: the + discrete probability distribution that satisfies Zipf's law: the frequency of an item is inversely proportional to its rank in a frequency table. @@ -3642,9 +3642,10 @@ cdef class RandomState: ----- The probability density for the Zipf distribution is - .. math:: p(x) = \\frac{x^{-a}}{\\zeta(a)}, + .. math:: p(k) = \\frac{k^{-a}}{\\zeta(a)}, - where :math:`\\zeta` is the Riemann Zeta function. + for integers ``k`` >= 1, where :math:`\\zeta` is the Riemann Zeta + function. It is named for the American linguist George Kingsley Zipf, who noted that the frequency of any word in a sample of a language is inversely @@ -3674,10 +3675,10 @@ cdef class RandomState: `bincount` provides a fast histogram for small integers. >>> count = np.bincount(s) - >>> x = np.arange(1, s.max() + 1) + >>> k = np.arange(1, s.max() + 1) - >>> plt.bar(x, count[1:], alpha=0.5, label='sample count') - >>> plt.plot(x, n*(x**-a)/zeta(a), 'k.-', alpha=0.5, + >>> plt.bar(k, count[1:], alpha=0.5, label='sample count') + >>> plt.plot(k, n*(k**-a)/zeta(a), 'k.-', alpha=0.5, ... label='expected count') # doctest: +SKIP >>> plt.semilogy() >>> plt.grid(alpha=0.4) -- cgit v1.2.1 From abb136cad711a0c23657926f8e6f3f50d9c37572 Mon Sep 17 00:00:00 2001 From: warren Date: Thu, 11 Nov 2021 17:44:57 -0300 Subject: DOC: Fix math formatting for zipf docstring --- numpy/random/_generator.pyx | 2 +- numpy/random/mtrand.pyx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index 1e65be3f1..7087b6e1d 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -3137,7 +3137,7 @@ cdef class Generator: .. math:: p(k) = \\frac{k^{-a}}{\\zeta(a)}, - for integers ``k`` >= 1, where :math:`\\zeta` is the Riemann Zeta + for integers :math:`k \geq 1`, where :math:`\\zeta` is the Riemann Zeta function. It is named for the American linguist George Kingsley Zipf, who noted diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index 280b0faac..3e13503d0 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -3644,7 +3644,7 @@ cdef class RandomState: .. math:: p(k) = \\frac{k^{-a}}{\\zeta(a)}, - for integers ``k`` >= 1, where :math:`\\zeta` is the Riemann Zeta + for integers :math:`k \geq 1`, where :math:`\\zeta` is the Riemann Zeta function. It is named for the American linguist George Kingsley Zipf, who noted -- cgit v1.2.1 From d06f2288e078876f5e0bebde4e844b64a0b0ffee Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Fri, 12 Nov 2021 07:54:07 +0200 Subject: BLD: Verify the ability to compile C++ sources before initiating the build --- numpy/core/setup.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'numpy') diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 2c99060ec..3fc0f85f1 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -672,16 +672,29 @@ def configuration(parent_package='',top_path=None): # but we cannot use add_installed_pkg_config here either, so we only # update the substitution dictionary during npymath build config_cmd = config.get_config_cmd() - # Check that the toolchain works, to fail early if it doesn't # (avoid late errors with MATHLIB which are confusing if the # compiler does not work). - st = config_cmd.try_link('int main(void) { return 0;}') - if not st: - # rerun the failing command in verbose mode - config_cmd.compiler.verbose = True - config_cmd.try_link('int main(void) { return 0;}') - raise RuntimeError("Broken toolchain: cannot link a simple C program") + for lang, test_code, note in ( + ('c', 'int main(void) { return 0;}', ''), + ('c++', ( + 'int main(void)' + '{ auto x = 0.0; return static_cast(x); }' + ), ( + 'note: A compiler with support for C++11 language ' + 'features is required.' + ) + ), + ): + st = config_cmd.try_link(test_code, lang=lang) + if not st: + # rerun the failing command in verbose mode + config_cmd.compiler.verbose = True + config_cmd.try_link(test_code, lang=lang) + raise RuntimeError( + f"Broken toolchain: cannot link a simple {lang.upper()} " + f"program. {note}" + ) mlibs = check_mathlib(config_cmd) posix_mlib = ' '.join(['-l%s' % l for l in mlibs]) -- cgit v1.2.1 From ff2e2a1e7eea29d925063b13922e096d14331222 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Fri, 12 Nov 2021 08:07:23 -0700 Subject: MAINT: A few updates to the array_api (#20066) * Allow casting in the array API asarray() * Restrict multidimensional indexing in the array API namespace The spec has recently been updated to only require multiaxis (i.e., tuple) indices in the case where every axis is indexed, meaning there are either as many indices as axes or the index has an ellipsis. * Fix type promotion for numpy.array_api.where where does value-based promotion for 0-dimensional arrays, so we use the same trick as in the Array operators to avoid this. * Print empty array_api arrays using empty() Printing behavior isn't required by the spec. This is just to make things easier to understand, especially with the array API test suite. * Fix an incorrect slice bounds guard in the array API * Disallow multiple different dtypes in the input to np.array_api.meshgrid * Remove DLPack support from numpy.array_api.asarray() from_dlpack() should be used to create arrays using DLPack. * Remove __len__ from the array API array object * Add astype() to numpy.array_api * Update the unique_* functions in numpy.array_api unique() in the array API was replaced with three separate functions, unique_all(), unique_inverse(), and unique_values(), in order to avoid polymorphic return types. Additionally, it should be noted that these functions to not currently conform to the spec with respect to NaN behavior. The spec requires multiple NaNs to be returned, but np.unique() returns a single NaN. Since this is currently an open issue in NumPy to possibly revert, I have not yet worked around this. See https://github.com/numpy/numpy/issues/20326. * Add the stream argument to the array API to_device method This does nothing in NumPy, and is just present so that the signature is valid according to the spec. * Use the NamedTuple classes for the type signatures * Add unique_counts to the array API namespace * Remove some unused imports * Update the array_api indexing restrictions The "multiaxis indexing must index every axis explicitly or use an ellipsis" was supposed to include any type of index, not just tuple indices. * Use a simpler type annotation for the array API to_device method * Fix a test failure in the array_api submodule The array_api cannot use the NumPy testing functions because array_api arrays do not mix with NumPy arrays, and also NumPy testing functions may use APIs that aren't supported in the array API. * Add dlpack support to the array_api submodule --- numpy/array_api/__init__.py | 10 +-- numpy/array_api/_array_object.py | 48 ++++++++----- numpy/array_api/_creation_functions.py | 19 +++-- numpy/array_api/_data_type_functions.py | 7 ++ numpy/array_api/_searching_functions.py | 1 + numpy/array_api/_set_functions.py | 89 ++++++++++++++++++++---- numpy/array_api/tests/test_array_object.py | 21 +++--- numpy/array_api/tests/test_creation_functions.py | 10 +++ 8 files changed, 156 insertions(+), 49 deletions(-) (limited to 'numpy') diff --git a/numpy/array_api/__init__.py b/numpy/array_api/__init__.py index 89f5e9cba..36e3f3ed5 100644 --- a/numpy/array_api/__init__.py +++ b/numpy/array_api/__init__.py @@ -136,7 +136,7 @@ from ._creation_functions import ( empty, empty_like, eye, - _from_dlpack, + from_dlpack, full, full_like, linspace, @@ -155,7 +155,7 @@ __all__ += [ "empty", "empty_like", "eye", - "_from_dlpack", + "from_dlpack", "full", "full_like", "linspace", @@ -169,6 +169,7 @@ __all__ += [ ] from ._data_type_functions import ( + astype, broadcast_arrays, broadcast_to, can_cast, @@ -178,6 +179,7 @@ from ._data_type_functions import ( ) __all__ += [ + "astype", "broadcast_arrays", "broadcast_to", "can_cast", @@ -358,9 +360,9 @@ from ._searching_functions import argmax, argmin, nonzero, where __all__ += ["argmax", "argmin", "nonzero", "where"] -from ._set_functions import unique +from ._set_functions import unique_all, unique_counts, unique_inverse, unique_values -__all__ += ["unique"] +__all__ += ["unique_all", "unique_counts", "unique_inverse", "unique_values"] from ._sorting_functions import argsort, sort diff --git a/numpy/array_api/_array_object.py b/numpy/array_api/_array_object.py index ef66c5efd..dc74bb8c5 100644 --- a/numpy/array_api/_array_object.py +++ b/numpy/array_api/_array_object.py @@ -32,7 +32,7 @@ from ._dtypes import ( from typing import TYPE_CHECKING, Optional, Tuple, Union, Any if TYPE_CHECKING: - from ._typing import PyCapsule, Device, Dtype + from ._typing import Any, PyCapsule, Device, Dtype import numpy as np @@ -99,9 +99,13 @@ class Array: """ Performs the operation __repr__. """ - prefix = "Array(" suffix = f", dtype={self.dtype.name})" - mid = np.array2string(self._array, separator=', ', prefix=prefix, suffix=suffix) + if 0 in self.shape: + prefix = "empty(" + mid = str(self.shape) + else: + prefix = "Array(" + mid = np.array2string(self._array, separator=', ', prefix=prefix, suffix=suffix) return prefix + mid + suffix # These are various helper functions to make the array behavior match the @@ -244,6 +248,10 @@ class Array: The following cases are allowed by NumPy, but not specified by the array API specification: + - Indices to not include an implicit ellipsis at the end. That is, + every axis of an array must be explicitly indexed or an ellipsis + included. + - The start and stop of a slice may not be out of bounds. In particular, for a slice ``i:j:k`` on an axis of size ``n``, only the following are allowed: @@ -270,6 +278,10 @@ class Array: return key if shape == (): return key + if len(shape) > 1: + raise IndexError( + "Multidimensional arrays must include an index for every axis or use an ellipsis" + ) size = shape[0] # Ensure invalid slice entries are passed through. if key.start is not None: @@ -277,7 +289,7 @@ class Array: operator.index(key.start) except TypeError: return key - if not (-size <= key.start <= max(0, size - 1)): + if not (-size <= key.start <= size): raise IndexError( "Slices with out-of-bounds start are not allowed in the array API namespace" ) @@ -322,6 +334,10 @@ class Array: zip(key[:ellipsis_i:-1], shape[:ellipsis_i:-1]) ): Array._validate_index(idx, (size,)) + if n_ellipsis == 0 and len(key) < len(shape): + raise IndexError( + "Multidimensional arrays must include an index for every axis or use an ellipsis" + ) return key elif isinstance(key, bool): return key @@ -339,7 +355,12 @@ class Array: "newaxis indices are not allowed in the array API namespace" ) try: - return operator.index(key) + key = operator.index(key) + if shape is not None and len(shape) > 1: + raise IndexError( + "Multidimensional arrays must include an index for every axis or use an ellipsis" + ) + return key except TypeError: # Note: This also omits boolean arrays that are not already in # Array() form, like a list of booleans. @@ -403,16 +424,14 @@ class Array: """ Performs the operation __dlpack__. """ - res = self._array.__dlpack__(stream=stream) - return self.__class__._new(res) + return self._array.__dlpack__(stream=stream) def __dlpack_device__(self: Array, /) -> Tuple[IntEnum, int]: """ Performs the operation __dlpack_device__. """ # Note: device support is required for this - res = self._array.__dlpack_device__() - return self.__class__._new(res) + return self._array.__dlpack_device__() def __eq__(self: Array, other: Union[int, float, bool, Array], /) -> Array: """ @@ -527,13 +546,6 @@ class Array: res = self._array.__le__(other._array) return self.__class__._new(res) - # Note: __len__ may end up being removed from the array API spec. - def __len__(self, /) -> int: - """ - Performs the operation __len__. - """ - return self._array.__len__() - def __lshift__(self: Array, other: Union[int, Array], /) -> Array: """ Performs the operation __lshift__. @@ -995,7 +1007,9 @@ class Array: res = self._array.__rxor__(other._array) return self.__class__._new(res) - def to_device(self: Array, device: Device, /) -> Array: + def to_device(self: Array, device: Device, /, stream: None = None) -> Array: + if stream is not None: + raise ValueError("The stream argument to to_device() is not supported") if device == 'cpu': return self raise ValueError(f"Unsupported device {device!r}") diff --git a/numpy/array_api/_creation_functions.py b/numpy/array_api/_creation_functions.py index c3644ac2c..23beec444 100644 --- a/numpy/array_api/_creation_functions.py +++ b/numpy/array_api/_creation_functions.py @@ -9,7 +9,6 @@ if TYPE_CHECKING: Device, Dtype, NestedSequence, - SupportsDLPack, SupportsBufferProtocol, ) from collections.abc import Sequence @@ -36,7 +35,6 @@ def asarray( int, float, NestedSequence[bool | int | float], - SupportsDLPack, SupportsBufferProtocol, ], /, @@ -60,7 +58,9 @@ def asarray( if copy is False: # Note: copy=False is not yet implemented in np.asarray raise NotImplementedError("copy=False is not yet implemented") - if isinstance(obj, Array) and (dtype is None or obj.dtype == dtype): + if isinstance(obj, Array): + if dtype is not None and obj.dtype != dtype: + copy = True if copy is True: return Array._new(np.array(obj._array, copy=True, dtype=dtype)) return obj @@ -151,9 +151,10 @@ def eye( return Array._new(np.eye(n_rows, M=n_cols, k=k, dtype=dtype)) -def _from_dlpack(x: object, /) -> Array: - # Note: dlpack support is not yet implemented on Array - raise NotImplementedError("DLPack support is not yet implemented") +def from_dlpack(x: object, /) -> Array: + from ._array_object import Array + + return Array._new(np._from_dlpack(x)) def full( @@ -240,6 +241,12 @@ def meshgrid(*arrays: Array, indexing: str = "xy") -> List[Array]: """ from ._array_object import Array + # Note: unlike np.meshgrid, only inputs with all the same dtype are + # allowed + + if len({a.dtype for a in arrays}) > 1: + raise ValueError("meshgrid inputs must all have the same dtype") + return [ Array._new(array) for array in np.meshgrid(*[a._array for a in arrays], indexing=indexing) diff --git a/numpy/array_api/_data_type_functions.py b/numpy/array_api/_data_type_functions.py index 7ccbe9469..e4d6db61b 100644 --- a/numpy/array_api/_data_type_functions.py +++ b/numpy/array_api/_data_type_functions.py @@ -13,6 +13,13 @@ if TYPE_CHECKING: import numpy as np +# Note: astype is a function, not an array method as in NumPy. +def astype(x: Array, dtype: Dtype, /, *, copy: bool = True) -> Array: + if not copy and dtype == x.dtype: + return x + return Array._new(x._array.astype(dtype=dtype, copy=copy)) + + def broadcast_arrays(*arrays: Array) -> List[Array]: """ Array API compatible wrapper for :py:func:`np.broadcast_arrays `. diff --git a/numpy/array_api/_searching_functions.py b/numpy/array_api/_searching_functions.py index 3dcef61c3..40f5a4d2e 100644 --- a/numpy/array_api/_searching_functions.py +++ b/numpy/array_api/_searching_functions.py @@ -43,4 +43,5 @@ def where(condition: Array, x1: Array, x2: Array, /) -> Array: """ # Call result type here just to raise on disallowed type combinations _result_type(x1.dtype, x2.dtype) + x1, x2 = Array._normalize_two_args(x1, x2) return Array._new(np.where(condition._array, x1._array, x2._array)) diff --git a/numpy/array_api/_set_functions.py b/numpy/array_api/_set_functions.py index 357f238f5..05ee7e555 100644 --- a/numpy/array_api/_set_functions.py +++ b/numpy/array_api/_set_functions.py @@ -2,19 +2,82 @@ from __future__ import annotations from ._array_object import Array -from typing import Tuple, Union +from typing import NamedTuple import numpy as np +# Note: np.unique() is split into four functions in the array API: +# unique_all, unique_counts, unique_inverse, and unique_values (this is done +# to remove polymorphic return types). -def unique( - x: Array, - /, - *, - return_counts: bool = False, - return_index: bool = False, - return_inverse: bool = False, -) -> Union[Array, Tuple[Array, ...]]: +# Note: The various unique() functions are supposed to return multiple NaNs. +# This does not match the NumPy behavior, however, this is currently left as a +# TODO in this implementation as this behavior may be reverted in np.unique(). +# See https://github.com/numpy/numpy/issues/20326. + +# Note: The functions here return a namedtuple (np.unique() returns a normal +# tuple). + +class UniqueAllResult(NamedTuple): + values: Array + indices: Array + inverse_indices: Array + counts: Array + + +class UniqueCountsResult(NamedTuple): + values: Array + counts: Array + + +class UniqueInverseResult(NamedTuple): + values: Array + inverse_indices: Array + + +def unique_all(x: Array, /) -> UniqueAllResult: + """ + Array API compatible wrapper for :py:func:`np.unique `. + + See its docstring for more information. + """ + res = np.unique( + x._array, + return_counts=True, + return_index=True, + return_inverse=True, + ) + + return UniqueAllResult(*[Array._new(i) for i in res]) + + +def unique_counts(x: Array, /) -> UniqueCountsResult: + res = np.unique( + x._array, + return_counts=True, + return_index=False, + return_inverse=False, + ) + + return UniqueCountsResult(*[Array._new(i) for i in res]) + + +def unique_inverse(x: Array, /) -> UniqueInverseResult: + """ + Array API compatible wrapper for :py:func:`np.unique `. + + See its docstring for more information. + """ + res = np.unique( + x._array, + return_counts=False, + return_index=False, + return_inverse=True, + ) + return UniqueInverseResult(*[Array._new(i) for i in res]) + + +def unique_values(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.unique `. @@ -22,10 +85,8 @@ def unique( """ res = np.unique( x._array, - return_counts=return_counts, - return_index=return_index, - return_inverse=return_inverse, + return_counts=False, + return_index=False, + return_inverse=False, ) - if isinstance(res, tuple): - return tuple(Array._new(i) for i in res) return Array._new(res) diff --git a/numpy/array_api/tests/test_array_object.py b/numpy/array_api/tests/test_array_object.py index fb42cf621..12479d765 100644 --- a/numpy/array_api/tests/test_array_object.py +++ b/numpy/array_api/tests/test_array_object.py @@ -3,7 +3,7 @@ import operator from numpy.testing import assert_raises import numpy as np -from .. import ones, asarray, result_type +from .. import ones, asarray, result_type, all, equal from .._dtypes import ( _all_dtypes, _boolean_dtypes, @@ -39,18 +39,18 @@ def test_validate_index(): assert_raises(IndexError, lambda: a[:-4]) assert_raises(IndexError, lambda: a[:3:-1]) assert_raises(IndexError, lambda: a[:-5:-1]) - assert_raises(IndexError, lambda: a[3:]) + assert_raises(IndexError, lambda: a[4:]) assert_raises(IndexError, lambda: a[-4:]) - assert_raises(IndexError, lambda: a[3::-1]) + assert_raises(IndexError, lambda: a[4::-1]) assert_raises(IndexError, lambda: a[-4::-1]) assert_raises(IndexError, lambda: a[...,:5]) assert_raises(IndexError, lambda: a[...,:-5]) - assert_raises(IndexError, lambda: a[...,:4:-1]) + assert_raises(IndexError, lambda: a[...,:5:-1]) assert_raises(IndexError, lambda: a[...,:-6:-1]) - assert_raises(IndexError, lambda: a[...,4:]) + assert_raises(IndexError, lambda: a[...,5:]) assert_raises(IndexError, lambda: a[...,-5:]) - assert_raises(IndexError, lambda: a[...,4::-1]) + assert_raises(IndexError, lambda: a[...,5::-1]) assert_raises(IndexError, lambda: a[...,-5::-1]) # Boolean indices cannot be part of a larger tuple index @@ -74,6 +74,11 @@ def test_validate_index(): assert_raises(IndexError, lambda: a[None, ...]) assert_raises(IndexError, lambda: a[..., None]) + # Multiaxis indices must contain exactly as many indices as dimensions + assert_raises(IndexError, lambda: a[()]) + assert_raises(IndexError, lambda: a[0,]) + assert_raises(IndexError, lambda: a[0]) + assert_raises(IndexError, lambda: a[:]) def test_operators(): # For every operator, we test that it works for the required type @@ -291,8 +296,8 @@ def test_device_property(): a = ones((3, 4)) assert a.device == 'cpu' - assert np.array_equal(a.to_device('cpu'), a) + assert all(equal(a.to_device('cpu'), a)) assert_raises(ValueError, lambda: a.to_device('gpu')) - assert np.array_equal(asarray(a, device='cpu'), a) + assert all(equal(asarray(a, device='cpu'), a)) assert_raises(ValueError, lambda: asarray(a, device='gpu')) diff --git a/numpy/array_api/tests/test_creation_functions.py b/numpy/array_api/tests/test_creation_functions.py index 7b633eaf1..ebbb6aab3 100644 --- a/numpy/array_api/tests/test_creation_functions.py +++ b/numpy/array_api/tests/test_creation_functions.py @@ -11,11 +11,13 @@ from .._creation_functions import ( full, full_like, linspace, + meshgrid, ones, ones_like, zeros, zeros_like, ) +from .._dtypes import float32, float64 from .._array_object import Array @@ -124,3 +126,11 @@ def test_zeros_like_errors(): assert_raises(ValueError, lambda: zeros_like(asarray(1), device="gpu")) assert_raises(ValueError, lambda: zeros_like(asarray(1), dtype=int)) assert_raises(ValueError, lambda: zeros_like(asarray(1), dtype="i")) + +def test_meshgrid_dtype_errors(): + # Doesn't raise + meshgrid() + meshgrid(asarray([1.], dtype=float32)) + meshgrid(asarray([1.], dtype=float32), asarray([1.], dtype=float32)) + + assert_raises(ValueError, lambda: meshgrid(asarray([1.], dtype=float32), asarray([1.], dtype=float64))) -- cgit v1.2.1 From d04c9ef75a711c9117f5c5662ca604ed5b88607d Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 11 Nov 2021 15:38:16 +0000 Subject: ENH: add log level as prefixes to respective messages --- numpy/distutils/log.py | 22 ++++++++++++++++++++++ numpy/distutils/tests/test_log.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 numpy/distutils/tests/test_log.py (limited to 'numpy') diff --git a/numpy/distutils/log.py b/numpy/distutils/log.py index a8113b9c6..3347f56d6 100644 --- a/numpy/distutils/log.py +++ b/numpy/distutils/log.py @@ -87,3 +87,25 @@ _global_color_map = { # don't use INFO,.. flags in set_verbosity, these flags are for set_threshold. set_verbosity(0, force=True) + + +_error = error +_warn = warn +_info = info +_debug = debug + + +def error(msg, *a, **kw): + _error(f"ERROR: {msg}", *a, **kw) + + +def warn(msg, *a, **kw): + _warn(f"WARN: {msg}", *a, **kw) + + +def info(msg, *a, **kw): + _info(f"INFO: {msg}", *a, **kw) + + +def debug(msg, *a, **kw): + _debug(f"DEBUG: {msg}", *a, **kw) diff --git a/numpy/distutils/tests/test_log.py b/numpy/distutils/tests/test_log.py new file mode 100644 index 000000000..36f49f592 --- /dev/null +++ b/numpy/distutils/tests/test_log.py @@ -0,0 +1,32 @@ +import io +import re +from contextlib import redirect_stdout + +import pytest + +from numpy.distutils import log + + +def setup_module(): + log.set_verbosity(2, force=True) # i.e. DEBUG + + +def teardown_module(): + log.set_verbosity(0, force=True) # the default + + +r_ansi = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") + + +@pytest.mark.parametrize("func_name", ["error", "warn", "info", "debug"]) +def test_log_prefix(func_name): + func = getattr(log, func_name) + msg = f"{func_name} message" + f = io.StringIO() + with redirect_stdout(f): + func(msg) + out = f.getvalue() + assert out # sanity check + clean_out = r_ansi.sub("", out) + line = next(line for line in clean_out.splitlines()) + assert line == f"{func_name.upper()}: {msg}" -- cgit v1.2.1 From d9dae76fce6fe3e87ee01670d3905f6fbdd04569 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 9 Nov 2021 18:30:45 -0600 Subject: TST: Add exhaustive test for einsum specialized loops This hopefully tests things well enough, at least some/most of the paths get triggered and led to errors without the previous float16 typing fixes. I manually confirmed that all paths that were *modified* in the previous commit actually get hit with float16 specialized loops. NOTE: This test may be a bit fragile with floating point roundoff errors, and can in parts be relaxed if this happens. --- numpy/core/tests/test_einsum.py | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'numpy') diff --git a/numpy/core/tests/test_einsum.py b/numpy/core/tests/test_einsum.py index 78c5e527b..172311624 100644 --- a/numpy/core/tests/test_einsum.py +++ b/numpy/core/tests/test_einsum.py @@ -1,5 +1,7 @@ import itertools +import pytest + import numpy as np from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_almost_equal, @@ -744,6 +746,52 @@ class TestEinsum: np.einsum('ij,jk->ik', x, x, out=out) assert_array_equal(out.base, correct_base) + @pytest.mark.parametrize("dtype", + np.typecodes["AllFloat"] + np.typecodes["AllInteger"]) + def test_different_paths(self, dtype): + # Test originally added to cover broken float16 path: gh-20305 + # Likely most are covered elsewhere, at least partially. + dtype = np.dtype(dtype) + # Simple test, designed to excersize most specialized code paths, + # note the +0.5 for floats. This makes sure we use a float value + # where the results must be exact. + arr = (np.arange(7) + 0.5).astype(dtype) + scalar = np.array(2, dtype=dtype) + + # contig -> scalar: + res = np.einsum('i->', arr) + assert res == arr.sum() + # contig, contig -> contig: + res = np.einsum('i,i->i', arr, arr) + assert_array_equal(res, arr * arr) + # noncontig, noncontig -> contig: + res = np.einsum('i,i->i', arr.repeat(2)[::2], arr.repeat(2)[::2]) + assert_array_equal(res, arr * arr) + # contig + contig -> scalar + assert np.einsum('i,i->', arr, arr) == (arr * arr).sum() + # contig + scalar -> contig (with out) + out = np.ones(7, dtype=dtype) + res = np.einsum('i,->i', arr, dtype.type(2), out=out) + assert_array_equal(res, arr * dtype.type(2)) + # scalar + contig -> contig (with out) + res = np.einsum(',i->i', scalar, arr) + assert_array_equal(res, arr * dtype.type(2)) + # scalar + contig -> scalar + res = np.einsum(',i->', scalar, arr) + # Use einsum to compare to not have difference due to sum round-offs: + assert res == np.einsum('i->', scalar * arr) + # contig + scalar -> scalar + res = np.einsum('i,->', arr, scalar) + # Use einsum to compare to not have difference due to sum round-offs: + assert res == np.einsum('i->', scalar * arr) + # contig + contig + contig -> scalar + arr = np.array([0.5, 0.5, 0.25, 4.5, 3.], dtype=dtype) + res = np.einsum('i,i,i->', arr, arr, arr) + assert_array_equal(res, (arr * arr * arr).sum()) + # four arrays: + res = np.einsum('i,i,i,i->', arr, arr, arr, arr) + assert_array_equal(res, (arr * arr * arr * arr).sum()) + def test_small_boolean_arrays(self): # See gh-5946. # Use array of True embedded in False. -- cgit v1.2.1 From 3993408877ab414cb5e3639ac0e20fdec972933f Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 8 Nov 2021 15:38:30 -0600 Subject: API,DEP: Rename percentile/quantile `interpolation=` to `method=` --- numpy/lib/function_base.py | 183 ++++++++++++++++++++-------------- numpy/lib/nanfunctions.py | 109 ++++++++++++-------- numpy/lib/tests/test_function_base.py | 92 ++++++++--------- numpy/lib/tests/test_nanfunctions.py | 4 +- 4 files changed, 226 insertions(+), 162 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 3c9983edf..cef281fab 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -50,8 +50,8 @@ __all__ = [ 'quantile' ] -# _QuantileInterpolation is a dictionary listing all the supported -# interpolation methods to compute quantile/percentile. +# _QuantileMethods is a dictionary listing all the supported methods to +# compute quantile/percentile. # # Below virtual_index refer to the index of the element where the percentile # would be found in the sorted sample. @@ -61,13 +61,13 @@ __all__ = [ # is made of a integer part (a.k.a 'i' or 'left') and a fractional part # (a.k.a 'g' or 'gamma') # -# Each _QuantileInterpolation have two properties +# Each _QuantileMethods has two properties # get_virtual_index : Callable # The function used to compute the virtual_index. # fix_gamma : Callable # A function used for discret methods to force the index to a specific value. -_QuantileInterpolation = dict( - # --- HYNDMAN AND FAN METHODS +_QuantileMethods = dict( + # --- HYNDMAN and FAN METHODS # Discrete methods inverted_cdf=dict( get_virtual_index=lambda n, quantiles: _inverted_cdf(n, quantiles), @@ -3854,7 +3854,7 @@ def _median(a, axis=None, out=None, overwrite_input=False): def _percentile_dispatcher(a, q, axis=None, out=None, overwrite_input=None, - interpolation=None, keepdims=None): + method=None, keepdims=None, *, interpolation=None): return (a, q, out) @@ -3864,8 +3864,10 @@ def percentile(a, axis=None, out=None, overwrite_input=False, - interpolation="linear", - keepdims=False): + method="linear", + keepdims=False, + *, + interpolation=None): """ Compute the q-th percentile of the data along the specified axis. @@ -3893,19 +3895,11 @@ def percentile(a, If True, then allow the input array `a` to be modified by intermediate calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - interpolation : str, optional - This parameter specifies the interpolation method to - use when the desired percentile lies between two data points - There are many different methods, some unique to NumPy. See the - notes for explanation. Options - - * (NPY 1): 'lower' - * (NPY 2): 'higher', - * (NPY 3): 'midpoint' - * (NPY 4): 'nearest' - * (NPY 5): 'linear' - - New options: + method : str, optional + This parameter specifies the method to use for estimating the + percentile. There are many different methods, some unique to NumPy. + See the notes for explanation. The options aligning with the R types + and the H&F paper [1]_ are: * (H&F 1): 'inverted_cdf' * (H&F 2): 'averaged_inverted_cdf' @@ -3917,7 +3911,17 @@ def percentile(a, * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' + Mainly for compatibility reasons, NumPy also supports the following + options which appear to be unique to NumPy: + + * 'lower' + * 'higher', + * 'midpoint' + * 'nearest' + .. versionchanged:: 1.22.0 + This argument was previously called "interpolation" and only + offered the "linear" default and last four options. keepdims : bool, optional If this is set to True, the axes which are reduced are left in @@ -3926,6 +3930,11 @@ def percentile(a, .. versionadded:: 1.9.0 + interpolation : str, optional + Deprecated name for the method keyword argument. + + .. deprecated:: 1.22.0 + Returns ------- percentile : scalar or ndarray @@ -3950,16 +3959,16 @@ def percentile(a, Given a vector ``V`` of length ``N``, the q-th percentile of ``V`` is the value ``q/100`` of the way from the minimum to the maximum in a sorted copy of ``V``. The values and distances of the two nearest - neighbors as well as the `interpolation` parameter will determine the + neighbors as well as the `method` parameter will determine the percentile if the normalized ranking does not match the location of ``q`` exactly. This function is the same as the median if ``q=50``, the same as the minimum if ``q=0`` and the same as the maximum if ``q=100``. - This optional `interpolation` parameter specifies the interpolation - method to use when the desired quantile lies between two data points - ``i < j``. If ``g`` is the fractional part of the index surrounded by - ``i`` and alpha and beta are correction constants modifying i and j. + This optional `method` parameter specifies the method to use when the + desired quantile lies between two data points ``i < j``. + If ``g`` is the fractional part of the index surrounded by ``i`` and + alpha and beta are correction constants modifying i and j. Below, 'q' is the quantile value, 'n' is the sample size and alpha and beta are constants. @@ -3970,7 +3979,7 @@ def percentile(a, .. math:: i + g = (q - alpha) / ( n - alpha - beta + 1 ) - The different interpolation methods then work as follows + The different methods then work as follows inverted_cdf: method 1 of H&F [1]_. @@ -4096,7 +4105,7 @@ def percentile(a, p, np.percentile(a, p, interpolation=interpolation), label=interpolation, linestyle=style) ax.set( - title='Interpolation methods for list: ' + str(a), + title='Result for the data: ' + str(a), xlabel='Percentile', ylabel='List item returned', yticks=a) @@ -4110,16 +4119,19 @@ def percentile(a, The American Statistician, 50(4), pp. 361-365, 1996 """ + if interpolation is not None: + method = _check_interpolation_as_method( + method, interpolation, "percentile") q = np.true_divide(q, 100) q = asanyarray(q) # undo any decay that the ufunc performed (see gh-13105) if not _quantile_is_valid(q): raise ValueError("Percentiles must be in the range [0, 100]") return _quantile_unchecked( - a, q, axis, out, overwrite_input, interpolation, keepdims) + a, q, axis, out, overwrite_input, method, keepdims) def _quantile_dispatcher(a, q, axis=None, out=None, overwrite_input=None, - interpolation=None, keepdims=None): + method=None, keepdims=None, *, interpolation=None): return (a, q, out) @@ -4129,8 +4141,10 @@ def quantile(a, axis=None, out=None, overwrite_input=False, - interpolation="linear", - keepdims=False): + method="linear", + keepdims=False, + *, + interpolation=None): """ Compute the q-th quantile of the data along the specified axis. @@ -4155,19 +4169,11 @@ def quantile(a, intermediate calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - interpolation : str, optional - This parameter specifies the interpolation method to use when the - desired quantile lies between two data points There are many - different methods, some unique to NumPy. See the notes for - explanation. Options: - - * (NPY 1): 'lower' - * (NPY 2): 'higher', - * (NPY 3): 'midpoint' - * (NPY 4): 'nearest' - * (NPY 5): 'linear' - - New options: + method : str, optional + This parameter specifies the method to use for estimating the + quantile. There are many different methods, some unique to NumPy. + See the notes for explanation. The options aligning with the R types + and the H&F paper [1]_ are: * (H&F 1): 'inverted_cdf' * (H&F 2): 'averaged_inverted_cdf' @@ -4179,13 +4185,28 @@ def quantile(a, * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' - .. versionadded:: 1.22.0 + Mainly for compatibility reasons, NumPy also supports the following + options which appear to be unique to NumPy: + + * 'lower' + * 'higher', + * 'midpoint' + * 'nearest' + + .. versionchanged:: 1.22.0 + This argument was previously called "interpolation" and only + offered the "linear" default and last four options. keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array `a`. + interpolation : str, optional + Deprecated name for the method keyword argument. + + .. deprecated:: 1.22.0 + Returns ------- quantile : scalar or ndarray @@ -4210,20 +4231,20 @@ def quantile(a, Given a vector ``V`` of length ``N``, the q-th quantile of ``V`` is the value ``q`` of the way from the minimum to the maximum in a sorted copy of ``V``. The values and distances of the two nearest neighbors as well as the - `interpolation` parameter will determine the quantile if the normalized + `method` parameter will determine the quantile if the normalized ranking does not match the location of ``q`` exactly. This function is the same as the median if ``q=0.5``, the same as the minimum if ``q=0.0`` and the same as the maximum if ``q=1.0``. - This optional `interpolation` parameter specifies the interpolation method - to use when the desired quantile lies between two data points ``i < j``. If - ``g`` is the fractional part of the index surrounded by ``i`` and alpha - and beta are correction constants modifying i and j. + This optional `method` parameter specifies the method to use when the + desired quantile lies between two data points ``i < j``. + If ``g`` is the fractional part of the index surrounded by ``i`` and + alpha and beta are correction constants modifying i and j. .. math:: i + g = (q - alpha) / ( n - alpha - beta + 1 ) - The different interpolation methods then work as follows + The different methods then work as follows inverted_cdf: method 1 of H&F [1]_. @@ -4333,11 +4354,15 @@ def quantile(a, The American Statistician, 50(4), pp. 361-365, 1996 """ + if interpolation is not None: + method = _check_interpolation_as_method( + method, interpolation, "quantile") + q = np.asanyarray(q) if not _quantile_is_valid(q): raise ValueError("Quantiles must be in the range [0, 1]") return _quantile_unchecked( - a, q, axis, out, overwrite_input, interpolation, keepdims) + a, q, axis, out, overwrite_input, method, keepdims) def _quantile_unchecked(a, @@ -4345,7 +4370,7 @@ def _quantile_unchecked(a, axis=None, out=None, overwrite_input=False, - interpolation="linear", + method="linear", keepdims=False): """Assumes that q is in [0, 1], and is an ndarray""" r, k = _ureduce(a, @@ -4354,7 +4379,7 @@ def _quantile_unchecked(a, axis=axis, out=out, overwrite_input=overwrite_input, - interpolation=interpolation) + method=method) if keepdims: return r.reshape(q.shape + k) else: @@ -4373,6 +4398,23 @@ def _quantile_is_valid(q): return True +def _check_interpolation_as_method(method, interpolation, fname): + # Deprecated NumPy 1.22, 2021-11-08 + warnings.warn( + f"the `interpolation=` argument to {fname} was renamed to " + "`method=`, which has additional options.\n" + "Users of the modes 'nearest', 'lower', 'higher', or " + "'midpoint' are encouraged to review the method they. " + "(Deprecated NumPy 1.22)", + DeprecationWarning, stacklevel=4) + if method != "linear": + # sanity check, we assume this basically never happens + raise TypeError( + "You shall not pass both `method` and `interpolation`!\n" + "(`interpolation` is Deprecated in favor of method)") + return interpolation + + def _compute_virtual_index(n, quantiles, alpha: float, beta: float): """ Compute the floating point indexes of an array for the linear @@ -4398,9 +4440,7 @@ def _compute_virtual_index(n, quantiles, alpha: float, beta: float): ) - 1 -def _get_gamma(virtual_indexes, - previous_indexes, - interpolation: _QuantileInterpolation): +def _get_gamma(virtual_indexes, previous_indexes, method): """ Compute gamma (a.k.a 'm' or 'weight') for the linear interpolation of quantiles. @@ -4410,7 +4450,7 @@ def _get_gamma(virtual_indexes, sample. previous_indexes : array_like The floor values of virtual_indexes. - interpolation : _QuantileInterpolation + interpolation : dict The interpolation method chosen, which may have a specific rule modifying gamma. @@ -4418,7 +4458,7 @@ def _get_gamma(virtual_indexes, by the interpolation method. """ gamma = np.asanyarray(virtual_indexes - previous_indexes) - gamma = interpolation["fix_gamma"](gamma, virtual_indexes) + gamma = method["fix_gamma"](gamma, virtual_indexes) return np.asanyarray(gamma) @@ -4480,7 +4520,7 @@ def _quantile_ureduce_func( axis: int = None, out=None, overwrite_input: bool = False, - interpolation="linear", + method="linear", ) -> np.array: if q.ndim > 2: # The code below works fine for nd, but it might not have useful @@ -4502,7 +4542,7 @@ def _quantile_ureduce_func( result = _quantile(arr, quantiles=q, axis=axis, - interpolation=interpolation, + method=method, out=out) return result @@ -4546,7 +4586,7 @@ def _quantile( arr: np.array, quantiles: np.array, axis: int = -1, - interpolation="linear", + method="linear", out=None, ): """ @@ -4556,8 +4596,8 @@ def _quantile( It computes the quantiles of the array for the given axis. A linear interpolation is performed based on the `interpolation`. - By default, the interpolation is "linear" where - alpha == beta == 1 which performs the 7th method of Hyndman&Fan. + By default, the method is "linear" where alpha == beta == 1 which + performs the 7th method of Hyndman&Fan. With "median_unbiased" we get alpha == beta == 1/3 thus the 8th method of Hyndman&Fan. """ @@ -4574,13 +4614,12 @@ def _quantile( # Virtual because it is a floating point value, not an valid index. # The nearest neighbours are used for interpolation try: - interpolation = _QuantileInterpolation[interpolation] + method = _QuantileMethods[method] except KeyError: raise ValueError( - f"{interpolation!r} is not a valid interpolation. Use one of: " - f"{_QuantileInterpolation.keys()}") from None - virtual_indexes = interpolation["get_virtual_index"](values_count, - quantiles) + f"{method!r} is not a valid method. Use one of: " + f"{_QuantileMethods.keys()}") from None + virtual_indexes = method["get_virtual_index"](values_count, quantiles) virtual_indexes = np.asanyarray(virtual_indexes) if np.issubdtype(virtual_indexes.dtype, np.integer): # No interpolation needed, take the points along axis @@ -4614,9 +4653,7 @@ def _quantile( previous = np.take(arr, previous_indexes, axis=DATA_AXIS) next = np.take(arr, next_indexes, axis=DATA_AXIS) # --- Linear interpolation - gamma = _get_gamma(virtual_indexes, - previous_indexes, - interpolation) + gamma = _get_gamma(virtual_indexes, previous_indexes, method) result_shape = virtual_indexes.shape + (1,) * (arr.ndim - 1) gamma = gamma.reshape(result_shape) result = _lerp(previous, diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 7e953be03..4613c1d26 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -1223,8 +1223,9 @@ def nanmedian(a, axis=None, out=None, overwrite_input=False, keepdims=np._NoValu return r -def _nanpercentile_dispatcher(a, q, axis=None, out=None, overwrite_input=None, - interpolation=None, keepdims=None): +def _nanpercentile_dispatcher( + a, q, axis=None, out=None, overwrite_input=None, + method=None, keepdims=None, *, interpolation=None): return (a, q, out) @@ -1235,8 +1236,10 @@ def nanpercentile( axis=None, out=None, overwrite_input=False, - interpolation="linear", + method="linear", keepdims=np._NoValue, + *, + interpolation=None, ): """ Compute the qth percentile of the data along the specified axis, @@ -1267,19 +1270,11 @@ def nanpercentile( intermediate calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - interpolation : str, optional - This parameter specifies the interpolation method to use when the - desired percentile lies between two data points There are many - different methods, some unique to NumPy. See the notes for - explanation. Options: - - * (NPY 1): 'lower' - * (NPY 2): 'higher', - * (NPY 3): 'midpoint' - * (NPY 4): 'nearest' - * (NPY 5): 'linear' (default) - - New options: + method : str, optional + This parameter specifies the method to use for estimating the + percentile. There are many different methods, some unique to NumPy. + See the notes for explanation. The options aligning with the R types + and the H&F paper [1]_ are: * (H&F 1): 'inverted_cdf' * (H&F 2): 'averaged_inverted_cdf' @@ -1291,7 +1286,17 @@ def nanpercentile( * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' - .. versionadded:: 1.22.0 + Mainly for compatibility reasons, NumPy also supports the following + options which appear to be unique to NumPy: + + * 'lower' + * 'higher', + * 'midpoint' + * 'nearest' + + .. versionchanged:: 1.22.0 + This argument was previously called "interpolation" and only + offered the "linear" default and last four options. keepdims : bool, optional If this is set to True, the axes which are reduced are left in @@ -1304,6 +1309,11 @@ def nanpercentile( a sub-class and `mean` does not have the kwarg `keepdims` this will raise a RuntimeError. + interpolation : str, optional + Deprecated name for the method keyword argument. + + .. deprecated:: 1.22.0 + Returns ------- percentile : scalar or ndarray @@ -1356,6 +1366,10 @@ def nanpercentile( >>> assert not np.all(a==b) """ + if interpolation is not None: + method = function_base._check_interpolation_as_method( + method, interpolation, "nanpercentile") + a = np.asanyarray(a) q = np.true_divide(q, 100.0) # undo any decay that the ufunc performed (see gh-13105) @@ -1363,11 +1377,11 @@ def nanpercentile( if not function_base._quantile_is_valid(q): raise ValueError("Percentiles must be in the range [0, 100]") return _nanquantile_unchecked( - a, q, axis, out, overwrite_input, interpolation, keepdims) + a, q, axis, out, overwrite_input, method, keepdims) def _nanquantile_dispatcher(a, q, axis=None, out=None, overwrite_input=None, - interpolation=None, keepdims=None): + method=None, keepdims=None, *, interpolation=None): return (a, q, out) @@ -1378,8 +1392,10 @@ def nanquantile( axis=None, out=None, overwrite_input=False, - interpolation="linear", + method="linear", keepdims=np._NoValue, + *, + interpolation=None, ): """ Compute the qth quantile of the data along the specified axis, @@ -1408,19 +1424,11 @@ def nanquantile( If True, then allow the input array `a` to be modified by intermediate calculations, to save memory. In this case, the contents of the input `a` after this function completes is undefined. - interpolation : str, optional - This parameter specifies the interpolation method to - use when the desired quantile lies between two data points - There are many different methods, some unique to NumPy. See the - notes for explanation. Options: - - * (NPY 1): 'lower' - * (NPY 2): 'higher', - * (NPY 3): 'midpoint' - * (NPY 4): 'nearest' - * (NPY 5): 'linear' (default) - - New options: + method : str, optional + This parameter specifies the method to use for estimating the + quantile. There are many different methods, some unique to NumPy. + See the notes for explanation. The options aligning with the R types + and the H&F paper [1]_ are: * (H&F 1): 'inverted_cdf' * (H&F 2): 'averaged_inverted_cdf' @@ -1432,7 +1440,17 @@ def nanquantile( * (H&F 8): 'median_unbiased' * (H&F 9): 'normal_unbiased' + Mainly for compatibility reasons, NumPy also supports the following + options which appear to be unique to NumPy: + + * 'lower' + * 'higher', + * 'midpoint' + * 'nearest' + .. versionchanged:: 1.22.0 + This argument was previously called "interpolation" and only + offered the "linear" default and last four options. keepdims : bool, optional If this is set to True, the axes which are reduced are left in @@ -1445,6 +1463,11 @@ def nanquantile( a sub-class and `mean` does not have the kwarg `keepdims` this will raise a RuntimeError. + interpolation : str, optional + Deprecated name for the method keyword argument. + + .. deprecated:: 1.22.0 + Returns ------- quantile : scalar or ndarray @@ -1496,12 +1519,16 @@ def nanquantile( >>> assert not np.all(a==b) """ + if interpolation is not None: + method = function_base._check_interpolation_as_method( + method, interpolation, "nanquantile") + a = np.asanyarray(a) q = np.asanyarray(q) if not function_base._quantile_is_valid(q): raise ValueError("Quantiles must be in the range [0, 1]") return _nanquantile_unchecked( - a, q, axis, out, overwrite_input, interpolation, keepdims) + a, q, axis, out, overwrite_input, method, keepdims) def _nanquantile_unchecked( @@ -1510,7 +1537,7 @@ def _nanquantile_unchecked( axis=None, out=None, overwrite_input=False, - interpolation="linear", + method="linear", keepdims=np._NoValue, ): """Assumes that q is in [0, 1], and is an ndarray""" @@ -1524,7 +1551,7 @@ def _nanquantile_unchecked( axis=axis, out=out, overwrite_input=overwrite_input, - interpolation=interpolation) + method=method) if keepdims and keepdims is not np._NoValue: return r.reshape(q.shape + k) else: @@ -1532,7 +1559,7 @@ def _nanquantile_unchecked( def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, - interpolation="linear"): + method="linear"): """ Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce @@ -1540,10 +1567,10 @@ def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, """ if axis is None or a.ndim == 1: part = a.ravel() - result = _nanquantile_1d(part, q, overwrite_input, interpolation) + result = _nanquantile_1d(part, q, overwrite_input, method) else: result = np.apply_along_axis(_nanquantile_1d, axis, a, q, - overwrite_input, interpolation) + overwrite_input, method) # apply_along_axis fills in collapsed axis with results. # Move that axis to the beginning to match percentile's # convention. @@ -1555,7 +1582,7 @@ def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, return result -def _nanquantile_1d(arr1d, q, overwrite_input=False, interpolation="linear"): +def _nanquantile_1d(arr1d, q, overwrite_input=False, method="linear"): """ Private function for rank 1 arrays. Compute quantile ignoring NaNs. See nanpercentile for parameter usage @@ -1567,7 +1594,7 @@ def _nanquantile_1d(arr1d, q, overwrite_input=False, interpolation="linear"): return np.full(q.shape, np.nan, dtype=arr1d.dtype)[()] return function_base._quantile_unchecked( - arr1d, q, overwrite_input=overwrite_input, interpolation=interpolation) + arr1d, q, overwrite_input=overwrite_input, method=method) def _nanvar_dispatcher(a, axis=None, dtype=None, out=None, ddof=None, diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 1c274afae..0b94b9a85 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2867,7 +2867,7 @@ class TestPercentile: assert_equal(np.percentile(x, 50), 1.75) x[1] = np.nan assert_equal(np.percentile(x, 0), np.nan) - assert_equal(np.percentile(x, 0, interpolation='nearest'), np.nan) + assert_equal(np.percentile(x, 0, method='nearest'), np.nan) def test_fraction(self): x = [Fraction(i, 2) for i in range(8)] @@ -2910,7 +2910,7 @@ class TestPercentile: res = np.percentile( arr, 40.0, - interpolation="linear") + method="linear") np.testing.assert_equal(res, np.NAN) np.testing.assert_equal(res.dtype, arr.dtype) @@ -2926,7 +2926,7 @@ class TestPercentile: (np.dtype("O"), np.float64)] @pytest.mark.parametrize(["input_dtype", "expected_dtype"], H_F_TYPE_CODES) - @pytest.mark.parametrize(["interpolation", "expected"], + @pytest.mark.parametrize(["method", "expected"], [("inverted_cdf", 20), ("averaged_inverted_cdf", 27.5), ("closest_observation", 20), @@ -2938,16 +2938,16 @@ class TestPercentile: ("normal_unbiased", 27.125), ]) def test_linear_interpolation(self, - interpolation, + method, expected, input_dtype, expected_dtype): arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=input_dtype) - actual = np.percentile(arr, 40.0, interpolation=interpolation) + actual = np.percentile(arr, 40.0, method=method) np.testing.assert_almost_equal(actual, expected, 14) - if interpolation in ["inverted_cdf", "closest_observation"]: + if method in ["inverted_cdf", "closest_observation"]: if input_dtype == "O": np.testing.assert_equal(np.asarray(actual).dtype, np.float64) else: @@ -2962,27 +2962,27 @@ class TestPercentile: @pytest.mark.parametrize("dtype", TYPE_CODES) def test_lower_higher(self, dtype): assert_equal(np.percentile(np.arange(10, dtype=dtype), 50, - interpolation='lower'), 4) + method='lower'), 4) assert_equal(np.percentile(np.arange(10, dtype=dtype), 50, - interpolation='higher'), 5) + method='higher'), 5) @pytest.mark.parametrize("dtype", TYPE_CODES) def test_midpoint(self, dtype): assert_equal(np.percentile(np.arange(10, dtype=dtype), 51, - interpolation='midpoint'), 4.5) + method='midpoint'), 4.5) assert_equal(np.percentile(np.arange(9, dtype=dtype) + 1, 50, - interpolation='midpoint'), 5) + method='midpoint'), 5) assert_equal(np.percentile(np.arange(11, dtype=dtype), 51, - interpolation='midpoint'), 5.5) + method='midpoint'), 5.5) assert_equal(np.percentile(np.arange(11, dtype=dtype), 50, - interpolation='midpoint'), 5) + method='midpoint'), 5) @pytest.mark.parametrize("dtype", TYPE_CODES) def test_nearest(self, dtype): assert_equal(np.percentile(np.arange(10, dtype=dtype), 51, - interpolation='nearest'), 5) + method='nearest'), 5) assert_equal(np.percentile(np.arange(10, dtype=dtype), 49, - interpolation='nearest'), 4) + method='nearest'), 4) def test_linear_interpolation_extrapolation(self): arr = np.random.rand(5) @@ -3019,19 +3019,19 @@ class TestPercentile: assert_equal( np.percentile(x, (25, 50, 75), axis=1).shape, (3, 3, 5, 6)) assert_equal(np.percentile(x, (25, 50), - interpolation="higher").shape, (2,)) + method="higher").shape, (2,)) assert_equal(np.percentile(x, (25, 50, 75), - interpolation="higher").shape, (3,)) + method="higher").shape, (3,)) assert_equal(np.percentile(x, (25, 50), axis=0, - interpolation="higher").shape, (2, 4, 5, 6)) + method="higher").shape, (2, 4, 5, 6)) assert_equal(np.percentile(x, (25, 50), axis=1, - interpolation="higher").shape, (2, 3, 5, 6)) + method="higher").shape, (2, 3, 5, 6)) assert_equal(np.percentile(x, (25, 50), axis=2, - interpolation="higher").shape, (2, 3, 4, 6)) + method="higher").shape, (2, 3, 4, 6)) assert_equal(np.percentile(x, (25, 50), axis=3, - interpolation="higher").shape, (2, 3, 4, 5)) + method="higher").shape, (2, 3, 4, 5)) assert_equal(np.percentile(x, (25, 50, 75), axis=1, - interpolation="higher").shape, (3, 3, 5, 6)) + method="higher").shape, (3, 3, 5, 6)) def test_scalar_q(self): # test for no empty dimensions for compatibility with old percentile @@ -3057,33 +3057,33 @@ class TestPercentile: # test for no empty dimensions for compatibility with old percentile x = np.arange(12).reshape(3, 4) - assert_equal(np.percentile(x, 50, interpolation='lower'), 5.) + assert_equal(np.percentile(x, 50, method='lower'), 5.) assert_(np.isscalar(np.percentile(x, 50))) r0 = np.array([4., 5., 6., 7.]) - c0 = np.percentile(x, 50, interpolation='lower', axis=0) + c0 = np.percentile(x, 50, method='lower', axis=0) assert_equal(c0, r0) assert_equal(c0.shape, r0.shape) r1 = np.array([1., 5., 9.]) - c1 = np.percentile(x, 50, interpolation='lower', axis=1) + c1 = np.percentile(x, 50, method='lower', axis=1) assert_almost_equal(c1, r1) assert_equal(c1.shape, r1.shape) out = np.empty((), dtype=x.dtype) - c = np.percentile(x, 50, interpolation='lower', out=out) + c = np.percentile(x, 50, method='lower', out=out) assert_equal(c, 5) assert_equal(out, 5) out = np.empty(4, dtype=x.dtype) - c = np.percentile(x, 50, interpolation='lower', axis=0, out=out) + c = np.percentile(x, 50, method='lower', axis=0, out=out) assert_equal(c, r0) assert_equal(out, r0) out = np.empty(3, dtype=x.dtype) - c = np.percentile(x, 50, interpolation='lower', axis=1, out=out) + c = np.percentile(x, 50, method='lower', axis=1, out=out) assert_equal(c, r1) assert_equal(out, r1) def test_exception(self): assert_raises(ValueError, np.percentile, [1, 2], 56, - interpolation='foobar') + method='foobar') assert_raises(ValueError, np.percentile, [1], 101) assert_raises(ValueError, np.percentile, [1], -1) assert_raises(ValueError, np.percentile, [1], list(range(50)) + [101]) @@ -3124,12 +3124,12 @@ class TestPercentile: # q.dim > 1, int r0 = np.array([[0, 1, 2, 3], [4, 5, 6, 7]]) out = np.empty((2, 4), dtype=x.dtype) - c = np.percentile(x, (25, 50), interpolation='lower', axis=0, out=out) + c = np.percentile(x, (25, 50), method='lower', axis=0, out=out) assert_equal(c, r0) assert_equal(out, r0) r1 = np.array([[0, 4, 8], [1, 5, 9]]) out = np.empty((2, 3), dtype=x.dtype) - c = np.percentile(x, (25, 50), interpolation='lower', axis=1, out=out) + c = np.percentile(x, (25, 50), method='lower', axis=1, out=out) assert_equal(c, r1) assert_equal(out, r1) @@ -3146,10 +3146,10 @@ class TestPercentile: assert_array_equal(np.percentile(d, 50, axis=-4).shape, (1, 2, 1)) assert_array_equal(np.percentile(d, 50, axis=2, - interpolation='midpoint').shape, + method='midpoint').shape, (11, 1, 1)) assert_array_equal(np.percentile(d, 50, axis=-2, - interpolation='midpoint').shape, + method='midpoint').shape, (11, 1, 1)) assert_array_equal(np.array(np.percentile(d, [10, 50], axis=0)).shape, @@ -3172,10 +3172,10 @@ class TestPercentile: def test_no_p_overwrite(self): p = np.linspace(0., 100., num=5) - np.percentile(np.arange(100.), p, interpolation="midpoint") + np.percentile(np.arange(100.), p, method="midpoint") assert_array_equal(p, np.linspace(0., 100., num=5)) p = np.linspace(0., 100., num=5).tolist() - np.percentile(np.arange(100.), p, interpolation="midpoint") + np.percentile(np.arange(100.), p, method="midpoint") assert_array_equal(p, np.linspace(0., 100., num=5).tolist()) def test_percentile_overwrite(self): @@ -3253,14 +3253,14 @@ class TestPercentile: o = np.zeros((4,)) d = np.ones((3, 4)) assert_equal(np.percentile(d, 0, 0, out=o), o) - assert_equal(np.percentile(d, 0, 0, interpolation='nearest', out=o), o) + assert_equal(np.percentile(d, 0, 0, method='nearest', out=o), o) o = np.zeros((3,)) assert_equal(np.percentile(d, 1, 1, out=o), o) - assert_equal(np.percentile(d, 1, 1, interpolation='nearest', out=o), o) + assert_equal(np.percentile(d, 1, 1, method='nearest', out=o), o) o = np.zeros(()) assert_equal(np.percentile(d, 2, out=o), o) - assert_equal(np.percentile(d, 2, interpolation='nearest', out=o), o) + assert_equal(np.percentile(d, 2, method='nearest', out=o), o) def test_out_nan(self): with warnings.catch_warnings(record=True): @@ -3270,15 +3270,15 @@ class TestPercentile: d[2, 1] = np.nan assert_equal(np.percentile(d, 0, 0, out=o), o) assert_equal( - np.percentile(d, 0, 0, interpolation='nearest', out=o), o) + np.percentile(d, 0, 0, method='nearest', out=o), o) o = np.zeros((3,)) assert_equal(np.percentile(d, 1, 1, out=o), o) assert_equal( - np.percentile(d, 1, 1, interpolation='nearest', out=o), o) + np.percentile(d, 1, 1, method='nearest', out=o), o) o = np.zeros(()) assert_equal(np.percentile(d, 1, out=o), o) assert_equal( - np.percentile(d, 1, interpolation='nearest', out=o), o) + np.percentile(d, 1, method='nearest', out=o), o) def test_nan_behavior(self): a = np.arange(24, dtype=float) @@ -3333,13 +3333,13 @@ class TestPercentile: b[:, 1] = np.nan b[:, 2] = np.nan assert_equal(np.percentile(a, [0.3, 0.6], (0, 2)), b) - # axis02 not zerod with nearest interpolation + # axis02 not zerod with method='nearest' b = np.percentile(np.arange(24, dtype=float).reshape(2, 3, 4), - [0.3, 0.6], (0, 2), interpolation='nearest') + [0.3, 0.6], (0, 2), method='nearest') b[:, 1] = np.nan b[:, 2] = np.nan assert_equal(np.percentile( - a, [0.3, 0.6], (0, 2), interpolation='nearest'), b) + a, [0.3, 0.6], (0, 2), method='nearest'), b) def test_nan_q(self): # GH18830 @@ -3412,18 +3412,18 @@ class TestQuantile: # this is worth retesting, because quantile does not make a copy p0 = np.array([0, 0.75, 0.25, 0.5, 1.0]) p = p0.copy() - np.quantile(np.arange(100.), p, interpolation="midpoint") + np.quantile(np.arange(100.), p, method="midpoint") assert_array_equal(p, p0) p0 = p0.tolist() p = p.tolist() - np.quantile(np.arange(100.), p, interpolation="midpoint") + np.quantile(np.arange(100.), p, method="midpoint") assert_array_equal(p, p0) @pytest.mark.parametrize("dtype", np.typecodes["AllInteger"]) def test_quantile_preserve_int_type(self, dtype): res = np.quantile(np.array([1, 2], dtype=dtype), [0.5], - interpolation="nearest") + method="nearest") assert res.dtype == dtype def test_quantile_monotonic(self): diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index 126dba495..733a077ea 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -1108,12 +1108,12 @@ class TestNanFunctions_Quantile: # this is worth retesting, because quantile does not make a copy p0 = np.array([0, 0.75, 0.25, 0.5, 1.0]) p = p0.copy() - np.nanquantile(np.arange(100.), p, interpolation="midpoint") + np.nanquantile(np.arange(100.), p, method="midpoint") assert_array_equal(p, p0) p0 = p0.tolist() p = p.tolist() - np.nanquantile(np.arange(100.), p, interpolation="midpoint") + np.nanquantile(np.arange(100.), p, method="midpoint") assert_array_equal(p, p0) @pytest.mark.parametrize("axis", [None, 0, 1]) -- cgit v1.2.1 From 85f3ddab60e522968dd6222a92a19a81eda036a8 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 8 Nov 2021 16:37:41 -0600 Subject: BUG: quantile discrete methods ended up using -1 as index sometimes Also, the closest-observation did not correctly support multiple quantiles calculated at the same time (broadcasting error). --- numpy/lib/function_base.py | 15 +++++++++------ numpy/lib/tests/test_function_base.py | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 9 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index cef281fab..dee64c671 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4487,7 +4487,7 @@ def _lerp(a, b, t, out=None): def _get_gamma_mask(shape, default_value, conditioned_value, where): out = np.full(shape, default_value) - out[where] = conditioned_value + np.copyto(out, conditioned_value, where=where, casting="unsafe") return out @@ -4495,11 +4495,14 @@ def _discret_interpolation_to_boundaries(index, gamma_condition_fun): previous = np.floor(index) next = previous + 1 gamma = index - previous - return _get_gamma_mask(shape=index.shape, - default_value=next, - conditioned_value=previous, - where=gamma_condition_fun(gamma, index) - ).astype(np.intp) + res = _get_gamma_mask(shape=index.shape, + default_value=next, + conditioned_value=previous, + where=gamma_condition_fun(gamma, index) + ).astype(np.intp) + # Some methods can lead to out-of-bound integers, clip them: + res[res < 0] = 0 + return res def _closest_observation(n, quantiles): diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 0b94b9a85..b67a31b18 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -3426,12 +3426,22 @@ class TestQuantile: method="nearest") assert res.dtype == dtype - def test_quantile_monotonic(self): + @pytest.mark.parametrize("method", + ['inverted_cdf', 'averaged_inverted_cdf', 'closest_observation', + 'interpolated_inverted_cdf', 'hazen', 'weibull', 'linear', + 'median_unbiased', 'normal_unbiased', + 'nearest', 'lower', 'higher', 'midpoint']) + def test_quantile_monotonic(self, method): # GH 14685 # test that the return value of quantile is monotonic if p0 is ordered - p0 = np.arange(0, 1, 0.01) + # Also tests that the boundary values are not mishandled. + p0 = np.linspace(0, 1, 101) quantile = np.quantile(np.array([0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 1, 1, 9, 9, 9, - 8, 8, 7]) * 0.1, p0) + 8, 8, 7]) * 0.1, p0, method=method) + assert_equal(np.sort(quantile), quantile) + + # Also test one where the number of data points is clearly divisible: + quantile = np.quantile([0., 1., 2., 3.], p0, method=method) assert_equal(np.sort(quantile), quantile) @hypothesis.given( -- cgit v1.2.1 From a5ac5a5421c4b65fed757a82e201587f7254eff6 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 8 Nov 2021 16:41:24 -0600 Subject: DOC: Fixup the percentile methods plot --- numpy/lib/function_base.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index dee64c671..03019f1b5 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4084,7 +4084,7 @@ def percentile(a, array([7., 2.]) >>> assert not np.all(a == b) - The different types of interpolation can be visualized graphically: + The different methods can be visualized graphically: .. plot:: @@ -4094,20 +4094,25 @@ def percentile(a, p = np.linspace(0, 100, 6001) ax = plt.gca() lines = [ - ('linear', None), - ('higher', '--'), - ('lower', '--'), - ('nearest', '-.'), - ('midpoint', '-.'), - ] - for interpolation, style in lines: + ('linear', '-', 'C0'), + ('inverted_cdf', ':', 'C1'), + # Almost the same as `inverted_cdf`: + ('averaged_inverted_cdf', '-.', 'C1'), + ('closest_observation', ':', 'C2'), + ('interpolated_inverted_cdf', '--', 'C1'), + ('hazen', '--', 'C3'), + ('weibull', '-.', 'C4'), + ('median_unbiased', '--', 'C5'), + ('normal_unbiased', '-.', 'C6'), + ] + for method, style, color in lines: ax.plot( - p, np.percentile(a, p, interpolation=interpolation), - label=interpolation, linestyle=style) + p, np.percentile(a, p, method=method), + label=method, linestyle=style, color=color) ax.set( - title='Result for the data: ' + str(a), + title='Percentiles for different methods and data: ' + str(a), xlabel='Percentile', - ylabel='List item returned', + ylabel='Estimated percentile value', yticks=a) ax.legend() plt.show() @@ -4347,6 +4352,8 @@ def quantile(a, array([7., 2.]) >>> assert not np.all(a == b) + See also `numpy.percentile` for a visualization of most methods. + References ---------- .. [1] R. J. Hyndman and Y. Fan, -- cgit v1.2.1 From 8cfb6b5b29a23c627d0f806441726c4470de777f Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 8 Nov 2021 16:47:27 -0600 Subject: TST: Add deprecation testcase for quantile interpolation rename --- numpy/core/tests/test_deprecations.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'numpy') diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index a1b379d92..93e07216b 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -1230,3 +1230,23 @@ class TestMachAr(_DeprecationTestCase): def test_deprecated_attr(self): finfo = np.finfo(float) self.assert_deprecated(lambda: getattr(finfo, "machar")) + + +class TestQuantileInterpolationDeprecation(_DeprecationTestCase): + # Deprecated 2021-11-08, NumPy 1.22 + @pytest.mark.parametrize("func", + [np.percentile, np.quantile, np.nanpercentile, np.nanquantile]) + def test_deprecated(self, func): + self.assert_deprecated( + lambda: func([0., 1.], 0., interpolation="linear")) + self.assert_deprecated( + lambda: func([0., 1.], 0., interpolation="nearest")) + + @pytest.mark.parametrize("func", + [np.percentile, np.quantile, np.nanpercentile, np.nanquantile]) + def test_both_passed(self, func): + with warnings.catch_warnings(): + # catch the warning, but make sure it does not raise: + warnings.simplefilter("always", DeprecationWarning) + with pytest.raises(TypeError): + func([0., 1.], 0., interpolation="nearest", method="nearest") -- cgit v1.2.1 From 8437663e851d358cac74d346d3f3dbb3ad0d52ed Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 8 Nov 2021 18:25:37 -0600 Subject: MAINT: Rename interpolation to method in percentile stubs --- numpy/lib/function_base.pyi | 26 +++++++++++----------- .../typing/tests/data/reveal/lib_function_base.pyi | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.pyi b/numpy/lib/function_base.pyi index 82c625fed..7e227f9da 100644 --- a/numpy/lib/function_base.pyi +++ b/numpy/lib/function_base.pyi @@ -500,7 +500,7 @@ def median( keepdims: bool = ..., ) -> _ArrayType: ... -_InterpolationKind = L[ +_MethodKind = L[ "inverted_cdf", "averaged_inverted_cdf", "closest_observation", @@ -523,7 +523,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> floating[Any]: ... @overload @@ -533,7 +533,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> complexfloating[Any, Any]: ... @overload @@ -543,7 +543,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> timedelta64: ... @overload @@ -553,7 +553,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> datetime64: ... @overload @@ -563,7 +563,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> Any: ... @overload @@ -573,7 +573,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> NDArray[floating[Any]]: ... @overload @@ -583,7 +583,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> NDArray[complexfloating[Any, Any]]: ... @overload @@ -593,7 +593,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> NDArray[timedelta64]: ... @overload @@ -603,7 +603,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> NDArray[datetime64]: ... @overload @@ -613,7 +613,7 @@ def percentile( axis: None = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: L[False] = ..., ) -> NDArray[object_]: ... @overload @@ -623,7 +623,7 @@ def percentile( axis: None | _ShapeLike = ..., out: None = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: bool = ..., ) -> Any: ... @overload @@ -633,7 +633,7 @@ def percentile( axis: None | _ShapeLike = ..., out: _ArrayType = ..., overwrite_input: bool = ..., - interpolation: _InterpolationKind = ..., + method: _MethodKind = ..., keepdims: bool = ..., ) -> _ArrayType: ... diff --git a/numpy/typing/tests/data/reveal/lib_function_base.pyi b/numpy/typing/tests/data/reveal/lib_function_base.pyi index 854b955b4..c559eb295 100644 --- a/numpy/typing/tests/data/reveal/lib_function_base.pyi +++ b/numpy/typing/tests/data/reveal/lib_function_base.pyi @@ -144,7 +144,7 @@ reveal_type(np.percentile(AR_O, 50)) # E: Any reveal_type(np.percentile(AR_f8, [50])) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(np.percentile(AR_c16, [50])) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] reveal_type(np.percentile(AR_m, [50])) # E: ndarray[Any, dtype[timedelta64]] -reveal_type(np.percentile(AR_M, [50], interpolation="nearest")) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.percentile(AR_M, [50], method="nearest")) # E: ndarray[Any, dtype[datetime64]] reveal_type(np.percentile(AR_O, [50])) # E: ndarray[Any, dtype[object_]] reveal_type(np.percentile(AR_f8, [50], keepdims=True)) # E: Any reveal_type(np.percentile(AR_f8, [50], axis=[1])) # E: Any @@ -158,7 +158,7 @@ reveal_type(np.quantile(AR_O, 0.5)) # E: Any reveal_type(np.quantile(AR_f8, [0.5])) # E: ndarray[Any, dtype[floating[Any]]] reveal_type(np.quantile(AR_c16, [0.5])) # E: ndarray[Any, dtype[complexfloating[Any, Any]]] reveal_type(np.quantile(AR_m, [0.5])) # E: ndarray[Any, dtype[timedelta64]] -reveal_type(np.quantile(AR_M, [0.5], interpolation="nearest")) # E: ndarray[Any, dtype[datetime64]] +reveal_type(np.quantile(AR_M, [0.5], method="nearest")) # E: ndarray[Any, dtype[datetime64]] reveal_type(np.quantile(AR_O, [0.5])) # E: ndarray[Any, dtype[object_]] reveal_type(np.quantile(AR_f8, [0.5], keepdims=True)) # E: Any reveal_type(np.quantile(AR_f8, [0.5], axis=[1])) # E: Any -- cgit v1.2.1 From 0d5fb819bd6ff8f025db1dfdd0e86e109a64d694 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 8 Nov 2021 20:39:50 -0600 Subject: DOC: Remove reference to paper from quantile `method` kwarg Apparently, sphinx does not resolve references to footnotes from parameter descriptions. --- numpy/lib/function_base.py | 4 ++-- numpy/lib/nanfunctions.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 03019f1b5..7d0f7bb6a 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3899,7 +3899,7 @@ def percentile(a, This parameter specifies the method to use for estimating the percentile. There are many different methods, some unique to NumPy. See the notes for explanation. The options aligning with the R types - and the H&F paper [1]_ are: + and the H&F paper are: * (H&F 1): 'inverted_cdf' * (H&F 2): 'averaged_inverted_cdf' @@ -4178,7 +4178,7 @@ def quantile(a, This parameter specifies the method to use for estimating the quantile. There are many different methods, some unique to NumPy. See the notes for explanation. The options aligning with the R types - and the H&F paper [1]_ are: + and the H&F paper are: * (H&F 1): 'inverted_cdf' * (H&F 2): 'averaged_inverted_cdf' diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 4613c1d26..39e168944 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -1274,7 +1274,7 @@ def nanpercentile( This parameter specifies the method to use for estimating the percentile. There are many different methods, some unique to NumPy. See the notes for explanation. The options aligning with the R types - and the H&F paper [1]_ are: + and the H&F paper are: * (H&F 1): 'inverted_cdf' * (H&F 2): 'averaged_inverted_cdf' @@ -1428,7 +1428,7 @@ def nanquantile( This parameter specifies the method to use for estimating the quantile. There are many different methods, some unique to NumPy. See the notes for explanation. The options aligning with the R types - and the H&F paper [1]_ are: + and the H&F paper are: * (H&F 1): 'inverted_cdf' * (H&F 2): 'averaged_inverted_cdf' -- cgit v1.2.1 From 5bd71fb76c68f41debe3a15fbf316ce6ef7fd795 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Tue, 9 Nov 2021 09:48:59 -0600 Subject: DOC: Add ticks to quantile interpolation/method error Co-authored-by: abel --- numpy/lib/function_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 7d0f7bb6a..558dfbe34 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4418,7 +4418,7 @@ def _check_interpolation_as_method(method, interpolation, fname): # sanity check, we assume this basically never happens raise TypeError( "You shall not pass both `method` and `interpolation`!\n" - "(`interpolation` is Deprecated in favor of method)") + "(`interpolation` is Deprecated in favor of `method`)") return interpolation -- cgit v1.2.1 From 7d8a8e746fc841a99f71242f60559b1be2e7340c Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 11:57:22 -0600 Subject: DOC: Update percentile/quantile docs Mainly fixes the method list slightly, tones down the warning a bit and fixes the link to the paper (I did not realize that the link failed to work due only because the reference was missing from nanquantile/nanpercentile). --- numpy/lib/function_base.py | 60 +++++++++++++++++++------------------- numpy/lib/nanfunctions.py | 72 +++++++++++++++++++++++++++------------------- 2 files changed, 72 insertions(+), 60 deletions(-) (limited to 'numpy') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 558dfbe34..6d84627cd 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3898,21 +3898,21 @@ def percentile(a, method : str, optional This parameter specifies the method to use for estimating the percentile. There are many different methods, some unique to NumPy. - See the notes for explanation. The options aligning with the R types - and the H&F paper are: - - * (H&F 1): 'inverted_cdf' - * (H&F 2): 'averaged_inverted_cdf' - * (H&F 3): 'closest_observation' - * (H&F 4): 'interpolated_inverted_cdf' - * (H&F 5): 'hazen' - * (H&F 6): 'weibull' - * (H&F 7): 'linear' (default) - * (H&F 8): 'median_unbiased' - * (H&F 9): 'normal_unbiased' - - Mainly for compatibility reasons, NumPy also supports the following - options which appear to be unique to NumPy: + See the notes for explanation. The options sorted by their R type + as summarized in the H&F paper [1]_ are: + + 1. 'inverted_cdf' + 2. 'averaged_inverted_cdf' + 3. 'closest_observation' + 4. 'interpolated_inverted_cdf' + 5. 'hazen' + 6. 'weibull' + 7. 'linear' (default) + 8. 'median_unbiased' + 9. 'normal_unbiased' + + The first three methods are discontiuous. NumPy further defines the + following discontinuous variations of the default 'linear' (7.) option: * 'lower' * 'higher', @@ -4177,21 +4177,21 @@ def quantile(a, method : str, optional This parameter specifies the method to use for estimating the quantile. There are many different methods, some unique to NumPy. - See the notes for explanation. The options aligning with the R types - and the H&F paper are: - - * (H&F 1): 'inverted_cdf' - * (H&F 2): 'averaged_inverted_cdf' - * (H&F 3): 'closest_observation' - * (H&F 4): 'interpolated_inverted_cdf' - * (H&F 5): 'hazen' - * (H&F 6): 'weibull' - * (H&F 7): 'linear' (default) - * (H&F 8): 'median_unbiased' - * (H&F 9): 'normal_unbiased' - - Mainly for compatibility reasons, NumPy also supports the following - options which appear to be unique to NumPy: + See the notes for explanation. The options sorted by their R type + as summarized in the H&F paper [1]_ are: + + 1. 'inverted_cdf' + 2. 'averaged_inverted_cdf' + 3. 'closest_observation' + 4. 'interpolated_inverted_cdf' + 5. 'hazen' + 6. 'weibull' + 7. 'linear' (default) + 8. 'median_unbiased' + 9. 'normal_unbiased' + + The first three methods are discontiuous. NumPy further defines the + following discontinuous variations of the default 'linear' (7.) option: * 'lower' * 'higher', diff --git a/numpy/lib/nanfunctions.py b/numpy/lib/nanfunctions.py index 39e168944..d7ea1ca65 100644 --- a/numpy/lib/nanfunctions.py +++ b/numpy/lib/nanfunctions.py @@ -1273,21 +1273,21 @@ def nanpercentile( method : str, optional This parameter specifies the method to use for estimating the percentile. There are many different methods, some unique to NumPy. - See the notes for explanation. The options aligning with the R types - and the H&F paper are: - - * (H&F 1): 'inverted_cdf' - * (H&F 2): 'averaged_inverted_cdf' - * (H&F 3): 'closest_observation' - * (H&F 4): 'interpolated_inverted_cdf' - * (H&F 5): 'hazen' - * (H&F 6): 'weibull' - * (H&F 7): 'linear' (default) - * (H&F 8): 'median_unbiased' - * (H&F 9): 'normal_unbiased' - - Mainly for compatibility reasons, NumPy also supports the following - options which appear to be unique to NumPy: + See the notes for explanation. The options sorted by their R type + as summarized in the H&F paper [1]_ are: + + 1. 'inverted_cdf' + 2. 'averaged_inverted_cdf' + 3. 'closest_observation' + 4. 'interpolated_inverted_cdf' + 5. 'hazen' + 6. 'weibull' + 7. 'linear' (default) + 8. 'median_unbiased' + 9. 'normal_unbiased' + + The first three methods are discontiuous. NumPy further defines the + following discontinuous variations of the default 'linear' (7.) option: * 'lower' * 'higher', @@ -1365,6 +1365,12 @@ def nanpercentile( array([7., 2.]) >>> assert not np.all(a==b) + References + ---------- + .. [1] R. J. Hyndman and Y. Fan, + "Sample quantiles in statistical packages," + The American Statistician, 50(4), pp. 361-365, 1996 + """ if interpolation is not None: method = function_base._check_interpolation_as_method( @@ -1427,21 +1433,21 @@ def nanquantile( method : str, optional This parameter specifies the method to use for estimating the quantile. There are many different methods, some unique to NumPy. - See the notes for explanation. The options aligning with the R types - and the H&F paper are: - - * (H&F 1): 'inverted_cdf' - * (H&F 2): 'averaged_inverted_cdf' - * (H&F 3): 'closest_observation' - * (H&F 4): 'interpolated_inverted_cdf' - * (H&F 5): 'hazen' - * (H&F 6): 'weibull' - * (H&F 7): 'linear' (default) - * (H&F 8): 'median_unbiased' - * (H&F 9): 'normal_unbiased' - - Mainly for compatibility reasons, NumPy also supports the following - options which appear to be unique to NumPy: + See the notes for explanation. The options sorted by their R type + as summarized in the H&F paper [1]_ are: + + 1. 'inverted_cdf' + 2. 'averaged_inverted_cdf' + 3. 'closest_observation' + 4. 'interpolated_inverted_cdf' + 5. 'hazen' + 6. 'weibull' + 7. 'linear' (default) + 8. 'median_unbiased' + 9. 'normal_unbiased' + + The first three methods are discontiuous. NumPy further defines the + following discontinuous variations of the default 'linear' (7.) option: * 'lower' * 'higher', @@ -1518,6 +1524,12 @@ def nanquantile( array([7., 2.]) >>> assert not np.all(a==b) + References + ---------- + .. [1] R. J. Hyndman and Y. Fan, + "Sample quantiles in statistical packages," + The American Statistician, 50(4), pp. 361-365, 1996 + """ if interpolation is not None: method = function_base._check_interpolation_as_method( -- cgit v1.2.1 From a688ed683b4e68f3917b87eb942e55f8592210d7 Mon Sep 17 00:00:00 2001 From: Raghuveer Devulapalli Date: Fri, 12 Nov 2021 10:53:49 -0800 Subject: MAINT: Update SVML sources to prevent an executable stack --- numpy/core/src/umath/svml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/svml b/numpy/core/src/umath/svml index 9f8af767e..1c5260a61 160000 --- a/numpy/core/src/umath/svml +++ b/numpy/core/src/umath/svml @@ -1 +1 @@ -Subproject commit 9f8af767ed6c75455d9a382af829048f8dd18067 +Subproject commit 1c5260a61e7dce6be48073dfa96291edb0a11d79 -- cgit v1.2.1 From 4b2cd27a5eceb288685020c8efa925ee3c72aed1 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 15:20:49 -0600 Subject: STY: Small style fixups for never-copy changes Yes, these may be slightly biased towards my own opinions. --- numpy/core/include/numpy/ndarraytypes.h | 6 +++--- numpy/core/src/multiarray/conversion_utils.c | 12 ++++++------ numpy/core/src/multiarray/ctors.c | 7 +++---- numpy/core/src/multiarray/methods.c | 5 ++--- numpy/core/src/multiarray/multiarraymodule.c | 14 ++++++-------- 5 files changed, 20 insertions(+), 24 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 6f6a00b8f..9a610908f 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -450,9 +450,9 @@ typedef struct { } PyArray_Dims; typedef enum { - NPY_COPY_IF_NEEDED, - NPY_COPY_ALWAYS, - NPY_COPY_NEVER + NPY_COPY_IF_NEEDED = 0, + NPY_COPY_ALWAYS = 1, + NPY_COPY_NEVER = 2, } _PyArray_CopyMode; typedef struct { diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index ee3e7b50c..ef101a78b 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -171,30 +171,30 @@ PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copymode) { return NPY_FAIL; } - int int_copymode = -1; + int int_copymode; PyObject* numpy_CopyMode = NULL; npy_cache_import("numpy", "_CopyMode", &numpy_CopyMode); - if (numpy_CopyMode != NULL && PyObject_Type(obj) == numpy_CopyMode) { + if (numpy_CopyMode != NULL && (PyObject *)Py_TYPE(obj) == numpy_CopyMode) { PyObject* mode_value = PyObject_GetAttrString(obj, "value"); if (mode_value == NULL) { return NPY_FAIL; } - int_copymode = PyLong_AsLong(mode_value); + int_copymode = (int)PyLong_AsLong(mode_value); if (error_converting(int_copymode)) { return NPY_FAIL; } } else { npy_bool bool_copymode; - if( !PyArray_BoolConverter(obj, &bool_copymode) ) { + if (!PyArray_BoolConverter(obj, &bool_copymode)) { return NPY_FAIL; } - int_copymode = (int) bool_copymode; + int_copymode = (int)bool_copymode; } - *copymode = (_PyArray_CopyMode) int_copymode; + *copymode = (_PyArray_CopyMode)int_copymode; return NPY_SUCCEED; } diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 286e45e39..7c3ac61c0 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1879,13 +1879,12 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, return NULL; } - if ((requires & NPY_ARRAY_ELEMENTSTRIDES) && - !PyArray_ElementStrides(obj)) { + if ((requires & NPY_ARRAY_ELEMENTSTRIDES) + && !PyArray_ElementStrides(obj)) { PyObject *ret; if (requires & NPY_ARRAY_ENSURENOCOPY) { PyErr_SetString(PyExc_ValueError, - "Unable to avoid copy " - "while creating a new array."); + "Unable to avoid copy while creating a new array."); return NULL; } ret = PyArray_NewCopy((PyArrayObject *)obj, NPY_ANYORDER); diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 0a471da92..627096b3c 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -875,10 +875,9 @@ array_astype(PyArrayObject *self, return (PyObject *)self; } - if( forcecopy == NPY_COPY_NEVER ) { + if (forcecopy == NPY_COPY_NEVER) { PyErr_SetString(PyExc_ValueError, - "Unable to avoid copy while casting in " - "never copy mode."); + "Unable to avoid copy while casting in never copy mode."); Py_DECREF(dtype); return NULL; } diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index d9dce2517..d28c033f8 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1579,16 +1579,15 @@ _array_fromobject_generic( if (PyArray_CheckExact(op) || (subok && PyArray_Check(op))) { oparr = (PyArrayObject *)op; if (type == NULL) { - if ((copy == NPY_COPY_IF_NEEDED || copy == NPY_COPY_NEVER) && - STRIDING_OK(oparr, order)) { + if (copy != NPY_COPY_ALWAYS && STRIDING_OK(oparr, order)) { ret = oparr; Py_INCREF(ret); goto finish; } else { - if( copy == NPY_COPY_NEVER ) { + if (copy == NPY_COPY_NEVER) { PyErr_SetString(PyExc_ValueError, - "Unable to avoid copy while creating a new array."); + "Unable to avoid copy while creating a new array."); return NULL; } ret = (PyArrayObject *)PyArray_NewCopy(oparr, order); @@ -1598,16 +1597,15 @@ _array_fromobject_generic( /* One more chance */ oldtype = PyArray_DESCR(oparr); if (PyArray_EquivTypes(oldtype, type)) { - if ((copy == NPY_COPY_IF_NEEDED || copy == NPY_COPY_NEVER) && - STRIDING_OK(oparr, order)) { + if (copy != NPY_COPY_ALWAYS && STRIDING_OK(oparr, order)) { Py_INCREF(op); ret = oparr; goto finish; } else { - if( copy == NPY_COPY_NEVER ) { + if (copy == NPY_COPY_NEVER) { PyErr_SetString(PyExc_ValueError, - "Unable to avoid copy while creating a new array."); + "Unable to avoid copy while creating a new array."); return NULL; } ret = (PyArrayObject *)PyArray_NewCopy(oparr, order); -- cgit v1.2.1 From 84951a63df4dce887f31969e2a109936b368198a Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 15:24:57 -0600 Subject: MAINT: Remove private CopyMode enum to private header --- numpy/core/include/numpy/ndarraytypes.h | 6 ------ numpy/core/src/multiarray/conversion_utils.h | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 9a610908f..616738d56 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -449,12 +449,6 @@ typedef struct { int len; } PyArray_Dims; -typedef enum { - NPY_COPY_IF_NEEDED = 0, - NPY_COPY_ALWAYS = 1, - NPY_COPY_NEVER = 2, -} _PyArray_CopyMode; - typedef struct { /* * Functions to cast to most other standard types diff --git a/numpy/core/src/multiarray/conversion_utils.h b/numpy/core/src/multiarray/conversion_utils.h index 643b67d59..4072841ee 100644 --- a/numpy/core/src/multiarray/conversion_utils.h +++ b/numpy/core/src/multiarray/conversion_utils.h @@ -9,6 +9,12 @@ PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq); NPY_NO_EXPORT int PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq); +typedef enum { + NPY_COPY_IF_NEEDED = 0, + NPY_COPY_ALWAYS = 1, + NPY_COPY_NEVER = 2, +} _PyArray_CopyMode; + NPY_NO_EXPORT int PyArray_CopyConverter(PyObject *obj, _PyArray_CopyMode *copyflag); -- cgit v1.2.1 From f31c4a66ad27b428b6a20f9fa28c1b33854ea949 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 15:45:26 -0600 Subject: MAINT,BUG: Refactor __array__ and never-copy to move check later Doing the check before calling `PyArray_FromArrayAttr` means we look up the attribute twice. It further fixes two bugs: 1. The reference counting was wrong. 2. In debug mode there was a failure for some deprecation warnings (probably due to error propagation) --- numpy/core/src/multiarray/ctors.c | 61 +++++++++++++++++++++++++++++---------- numpy/core/src/multiarray/ctors.h | 4 +++ 2 files changed, 50 insertions(+), 15 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 7c3ac61c0..3f1d7835e 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1349,13 +1349,7 @@ _array_from_array_like(PyObject *op, * this should be changed! */ if (!writeable && tmp == Py_NotImplemented) { - PyObject* array_meth = PyArray_LookupSpecial_OnInstance(op, "__array__"); - int has_get = array_meth && PyType_Check(op) && PyObject_HasAttrString(array_meth, "__get__"); - if (array_meth != NULL && !has_get && allow_copy) { - PyErr_SetString(PyExc_ValueError, "Calling __array__ in never copy mode is not allowed."); - return NULL; - } - tmp = PyArray_FromArrayAttr(op, requested_dtype, context); + tmp = PyArray_FromArrayAttr_int(op, requested_dtype, allow_copy); if (tmp == NULL) { return NULL; } @@ -2463,18 +2457,30 @@ PyArray_FromInterface(PyObject *origin) return NULL; } -/*NUMPY_API + +/** + * Check for an __array__ attribute and call it when it exists. + * + * .. warning: + * If returned, `NotImplemented` is borrowed and must not be Decref'd + * + * @param op The Python object to convert to an array. + * @param descr The desired `arr.dtype`, passed into the `__array__` call, + * as information but is not checked/enforced! + * @param never_copy Indicator that a copy is not allowed. + * NOTE: Currently, this means an error is raised instead of calling + * `op.__array__()`. In the future we could call for example call + * `op.__array__(never_copy=True)` instead. + * @returns NotImplemented if `__array__` is not defined or a NumPy array + * (or subclass). On error, return NULL. */ NPY_NO_EXPORT PyObject * -PyArray_FromArrayAttr(PyObject *op, PyArray_Descr *typecode, PyObject *context) +PyArray_FromArrayAttr_int( + PyObject *op, PyArray_Descr *descr, int never_copy) { PyObject *new; PyObject *array_meth; - if (context != NULL) { - PyErr_SetString(PyExc_RuntimeError, "'context' must be NULL"); - return NULL; - } array_meth = PyArray_LookupSpecial_OnInstance(op, "__array__"); if (array_meth == NULL) { if (PyErr_Occurred()) { @@ -2490,6 +2496,16 @@ PyArray_FromArrayAttr(PyObject *op, PyArray_Descr *typecode, PyObject *context) } return Py_NotImplemented; } + if (never_copy) { + /* Currently, we must always assume that `__array__` returns a copy */ + PyErr_SetString(PyExc_ValueError, + "Unable to avoid copy while converting from an object " + "implementing the `__array__` protocol. NumPy cannot ensure " + "that no copy will be made."); + Py_DECREF(array_meth); + return NULL; + } + if (PyType_Check(op) && PyObject_HasAttrString(array_meth, "__get__")) { /* * If the input is a class `array_meth` may be a property-like object. @@ -2500,11 +2516,11 @@ PyArray_FromArrayAttr(PyObject *op, PyArray_Descr *typecode, PyObject *context) Py_DECREF(array_meth); return Py_NotImplemented; } - if (typecode == NULL) { + if (descr == NULL) { new = PyObject_CallFunction(array_meth, NULL); } else { - new = PyObject_CallFunction(array_meth, "O", typecode); + new = PyObject_CallFunction(array_meth, "O", descr); } Py_DECREF(array_meth); if (new == NULL) { @@ -2520,6 +2536,21 @@ PyArray_FromArrayAttr(PyObject *op, PyArray_Descr *typecode, PyObject *context) return new; } + +/*NUMPY_API + */ +NPY_NO_EXPORT PyObject * +PyArray_FromArrayAttr(PyObject *op, PyArray_Descr *typecode, PyObject *context) +{ + if (context != NULL) { + PyErr_SetString(PyExc_RuntimeError, "'context' must be NULL"); + return NULL; + } + + return PyArray_FromArrayAttr_int(op, typecode, 0); +} + + /*NUMPY_API * new reference -- accepts NULL for mintype */ diff --git a/numpy/core/src/multiarray/ctors.h b/numpy/core/src/multiarray/ctors.h index cf01a6256..2f9e8547d 100644 --- a/numpy/core/src/multiarray/ctors.h +++ b/numpy/core/src/multiarray/ctors.h @@ -52,6 +52,10 @@ PyArray_FromStructInterface(PyObject *input); NPY_NO_EXPORT PyObject * PyArray_FromInterface(PyObject *input); +NPY_NO_EXPORT PyObject * +PyArray_FromArrayAttr_int( + PyObject *op, PyArray_Descr *descr, int never_copy); + NPY_NO_EXPORT PyObject * PyArray_FromArrayAttr(PyObject *op, PyArray_Descr *typecode, PyObject *context); -- cgit v1.2.1 From dea40555bd90afb368917dab31e613e00f1f6665 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 15:51:12 -0600 Subject: MAINT: Rename `allow_copy` to `never_copy` in never-copy machinery The first name was `do_copy`, which was confusing because it sounds like a copy is forced (opposite is the case!) `allow_copy` would have been better, but corrects it into the wrong direction. `never_copy` is correct, and aligns with the Python side names --- numpy/core/src/multiarray/array_coercion.c | 14 +++++++------- numpy/core/src/multiarray/array_coercion.h | 2 +- numpy/core/src/multiarray/ctors.c | 8 ++++---- numpy/core/src/multiarray/ctors.h | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/array_coercion.c b/numpy/core/src/multiarray/array_coercion.c index d58dd5d21..2598e4bde 100644 --- a/numpy/core/src/multiarray/array_coercion.c +++ b/numpy/core/src/multiarray/array_coercion.c @@ -858,7 +858,7 @@ PyArray_AdaptDescriptorToArray(PyArrayObject *arr, PyObject *dtype) * (Initially it is a pointer to the user-provided head pointer). * @param fixed_DType User provided fixed DType class * @param flags Discovery flags (reporting and behaviour flags, see def.) - * @param allow_copy Specifies if a copy is allowed during array creation. + * @param never_copy Specifies if a copy is allowed during array creation. * @return The updated number of maximum dimensions (i.e. scalars will set * this to the current dimensions). */ @@ -868,7 +868,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( npy_intp out_shape[NPY_MAXDIMS], coercion_cache_obj ***coercion_cache_tail_ptr, PyArray_DTypeMeta *fixed_DType, enum _dtype_discovery_flags *flags, - int allow_copy) + int never_copy) { PyArrayObject *arr = NULL; PyObject *seq; @@ -926,7 +926,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( requested_descr = *out_descr; } arr = (PyArrayObject *)_array_from_array_like(obj, - requested_descr, 0, NULL, allow_copy); + requested_descr, 0, NULL, never_copy); if (arr == NULL) { return -1; } @@ -1120,7 +1120,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( max_dims = PyArray_DiscoverDTypeAndShape_Recursive( objects[i], curr_dims + 1, max_dims, out_descr, out_shape, coercion_cache_tail_ptr, fixed_DType, - flags, allow_copy); + flags, never_copy); if (max_dims < 0) { return -1; @@ -1160,7 +1160,7 @@ PyArray_DiscoverDTypeAndShape_Recursive( * The result may be unchanged (remain NULL) when converting a * sequence with no elements. In this case it is callers responsibility * to choose a default. - * @param allow_copy Specifies if a copy is allowed during array creation. + * @param never_copy Specifies that a copy is not allowed. * @return dimensions of the discovered object or -1 on error. * WARNING: If (and only if) the output is a single array, the ndim * returned _can_ exceed the maximum allowed number of dimensions. @@ -1173,7 +1173,7 @@ PyArray_DiscoverDTypeAndShape( npy_intp out_shape[NPY_MAXDIMS], coercion_cache_obj **coercion_cache, PyArray_DTypeMeta *fixed_DType, PyArray_Descr *requested_descr, - PyArray_Descr **out_descr, int allow_copy) + PyArray_Descr **out_descr, int never_copy) { coercion_cache_obj **coercion_cache_head = coercion_cache; *coercion_cache = NULL; @@ -1218,7 +1218,7 @@ PyArray_DiscoverDTypeAndShape( int ndim = PyArray_DiscoverDTypeAndShape_Recursive( obj, 0, max_dims, out_descr, out_shape, &coercion_cache, - fixed_DType, &flags, allow_copy); + fixed_DType, &flags, never_copy); if (ndim < 0) { goto fail; } diff --git a/numpy/core/src/multiarray/array_coercion.h b/numpy/core/src/multiarray/array_coercion.h index 4790d8030..f2482cecc 100644 --- a/numpy/core/src/multiarray/array_coercion.h +++ b/numpy/core/src/multiarray/array_coercion.h @@ -31,7 +31,7 @@ PyArray_DiscoverDTypeAndShape( npy_intp out_shape[NPY_MAXDIMS], coercion_cache_obj **coercion_cache, PyArray_DTypeMeta *fixed_DType, PyArray_Descr *requested_descr, - PyArray_Descr **out_descr, int allow_copy); + PyArray_Descr **out_descr, int never_copy); NPY_NO_EXPORT int PyArray_ExtractDTypeAndDescriptor(PyObject *dtype, diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 3f1d7835e..819bb22be 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1284,7 +1284,7 @@ fail: * DType may be used, but is not enforced. * @param writeable whether the result must be writeable. * @param context Unused parameter, must be NULL (should be removed later). - * @param allow_copy Specifies if a copy is allowed during array creation. + * @param never_copy Specifies that a copy is not allowed. * * @returns The array object, Py_NotImplemented if op is not array-like, * or NULL with an error set. (A new reference to Py_NotImplemented @@ -1293,7 +1293,7 @@ fail: NPY_NO_EXPORT PyObject * _array_from_array_like(PyObject *op, PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context, - int allow_copy) { + int never_copy) { PyObject* tmp; /* @@ -1349,7 +1349,7 @@ _array_from_array_like(PyObject *op, * this should be changed! */ if (!writeable && tmp == Py_NotImplemented) { - tmp = PyArray_FromArrayAttr_int(op, requested_dtype, allow_copy); + tmp = PyArray_FromArrayAttr_int(op, requested_dtype, never_copy); if (tmp == NULL) { return NULL; } @@ -2467,7 +2467,7 @@ PyArray_FromInterface(PyObject *origin) * @param op The Python object to convert to an array. * @param descr The desired `arr.dtype`, passed into the `__array__` call, * as information but is not checked/enforced! - * @param never_copy Indicator that a copy is not allowed. + * @param never_copy Specifies that a copy is not allowed. * NOTE: Currently, this means an error is raised instead of calling * `op.__array__()`. In the future we could call for example call * `op.__array__(never_copy=True)` instead. diff --git a/numpy/core/src/multiarray/ctors.h b/numpy/core/src/multiarray/ctors.h index 2f9e8547d..98160b1cc 100644 --- a/numpy/core/src/multiarray/ctors.h +++ b/numpy/core/src/multiarray/ctors.h @@ -33,7 +33,7 @@ PyArray_New( NPY_NO_EXPORT PyObject * _array_from_array_like(PyObject *op, PyArray_Descr *requested_dtype, npy_bool writeable, PyObject *context, - int allow_copy); + int never_copy); NPY_NO_EXPORT PyObject * PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, -- cgit v1.2.1 From 9fee0f8db5def3d119a38845111724f436fcfe2e Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 16:02:06 -0600 Subject: DOC: Slightly extend to docs to note that we assume no-copy buffer protocol --- numpy/_globals.py | 3 +++ numpy/core/include/numpy/ndarraytypes.h | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/_globals.py b/numpy/_globals.py index 41adaae25..c88874725 100644 --- a/numpy/_globals.py +++ b/numpy/_globals.py @@ -106,6 +106,9 @@ class _CopyMode(enum.Enum): - NEVER: This means that the deep copy will never be taken. If a copy cannot be avoided then a `ValueError` will be raised. + + Note that the buffer-protocol could in theory do copies. NumPy currently + assumes an object exporting the buffer protocol will never do this. """ ALWAYS = True diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 616738d56..6240adc0c 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -868,7 +868,7 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *); /* * Always copy the array. Returned arrays are always CONTIGUOUS, - * ALIGNED, and WRITEABLE. + * ALIGNED, and WRITEABLE. See also: NPY_ARRAY_ENSURENOCOPY = 0x4000. * * This flag may be requested in constructor functions. */ @@ -937,6 +937,11 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *); #define NPY_ARRAY_UPDATEIFCOPY 0x1000 /* Deprecated in 1.14 */ #define NPY_ARRAY_WRITEBACKIFCOPY 0x2000 +/* + * No copy may be made while converting from an object/array (result is a view) + * + * This flag may be requested in constructor functions. + */ #define NPY_ARRAY_ENSURENOCOPY 0x4000 /* -- cgit v1.2.1 From f058aea694327b517eb5c3d786c7f6a3e9c9898d Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 16:40:50 -0600 Subject: BUG: Fix refcounting issue and missed scalar special case for never-copy logic --- numpy/core/src/multiarray/ctors.c | 18 +++++++++++------- numpy/core/tests/test_multiarray.py | 3 +++ 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 819bb22be..b62426854 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1703,7 +1703,17 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, ((PyVoidScalarObject *)op)->flags, NULL, op); } - else if (cache == 0 && newtype != NULL && + /* + * If we got this far, we definitely have to create a copy, since we are + * converting either from a scalar (cache == NULL) or a (nested) sequence. + */ + if (flags & NPY_ARRAY_ENSURENOCOPY ) { + PyErr_SetString(PyExc_ValueError, + "Unable to avoid copy while creating an array."); + return NULL; + } + + if (cache == 0 && newtype != NULL && PyDataType_ISSIGNED(newtype) && PyArray_IsScalar(op, Generic)) { assert(ndim == 0); /* @@ -1741,12 +1751,6 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, /* Create a new array and copy the data */ Py_INCREF(dtype); /* hold on in case of a subarray that is replaced */ - if (flags & NPY_ARRAY_ENSURENOCOPY ) { - PyErr_SetString(PyExc_ValueError, - "Unable to avoid copy while creating " - "an array from descriptor."); - return NULL; - } ret = (PyArrayObject *)PyArray_NewFromDescr( &PyArray_Type, dtype, ndim, dims, NULL, NULL, flags&NPY_ARRAY_F_CONTIGUOUS, NULL); diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 18302543a..4413cd0d0 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7840,6 +7840,9 @@ class TestArrayCreationCopyArgument(object): copy=self.RaiseOnBool()) assert_raises(ValueError, _multiarray_tests.npy_ensurenocopy, [1]) + # Casting with a dtype (to unsigned integers) can be special: + with pytest.raises(ValueError): + np.array(pyscalar, dtype=np.int64, copy=np._CopyMode.NEVER) def test_compatible_cast(self): -- cgit v1.2.1 From b5f1b3e6531ba5748795c000a23d1be94a2f76c2 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 17:35:36 -0600 Subject: BUG: Fix failure to create aligned, empty structured dtype This fixes a SIGFPE exception due to division/modulo by zero when an empty structured dtype is requested aligned. --- numpy/core/src/multiarray/descriptor.c | 2 +- numpy/core/tests/test_dtype.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index fd2577bc6..0c539053c 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -1326,7 +1326,7 @@ _convert_from_dict(PyObject *obj, int align) goto fail; } /* If align is set, make sure the alignment divides into the size */ - if (align && itemsize % new->alignment != 0) { + if (align && new->alignment > 0 && itemsize % new->alignment != 0) { PyErr_Format(PyExc_ValueError, "NumPy dtype descriptor requires alignment of %d bytes, " "which is not divisible into the specified itemsize %d", diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 8fe859919..c20010dc3 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -627,6 +627,12 @@ class TestSubarray: t2 = np.dtype('2i4', align=True) assert_equal(t1.alignment, t2.alignment) + def test_aligned_empty(self): + # Mainly regression test for gh-19696: construction failed completely + dt = np.dtype([], align=True) + assert dt == np.dtype([]) + dt = np.dtype({"names": [], "formats": [], "itemsize": 0}, align=True) + assert dt == np.dtype([]) def iter_struct_object_dtypes(): """ -- cgit v1.2.1 From b73f84f3c3782eaacde09ea4f8fb4f6abac338d2 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 17:57:48 -0600 Subject: MAINT,TST: Avoid small positive integers in refcount test This hopefully makes the test less susceptible to errors, but we have to use small "singleton" integers for the test to work. It is not clear that this will actually fix the issue always. If the issue crops up again, we should simply remove all manual asserts and rely on `pytest-leaks` as a leak checker. Closes gh-17381 --- numpy/core/tests/test_dtype.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 8fe859919..585b24976 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -723,26 +723,30 @@ class TestStructuredObjectRefcounting: def test_structured_object_indexing(self, shape, index, items_changed, dt, pat, count, singleton): """Structured object reference counting for advanced indexing.""" - zero = 0 - one = 1 + # Use two small negative values (should be singletons, but less likely + # to run into race-conditions). This failed in some threaded envs + # When using 0 and 1. If it fails again, should remove all explicit + # checks, and rely on `pytest-leaks` reference count checker only. + val0 = -4 + val1 = -5 - arr = np.zeros(shape, dt) + arr = np.full(shape, val0, dt) gc.collect() - before_zero = sys.getrefcount(zero) - before_one = sys.getrefcount(one) + before_val0 = sys.getrefcount(val0) + before_val1 = sys.getrefcount(val1) # Test item getting: part = arr[index] - after_zero = sys.getrefcount(zero) - assert after_zero - before_zero == count * items_changed + after_val0 = sys.getrefcount(val0) + assert after_val0 - before_val0 == count * items_changed del part # Test item setting: - arr[index] = one + arr[index] = val1 gc.collect() - after_zero = sys.getrefcount(zero) - after_one = sys.getrefcount(one) - assert before_zero - after_zero == count * items_changed - assert after_one - before_one == count * items_changed + after_val0 = sys.getrefcount(val0) + after_val1 = sys.getrefcount(val1) + assert before_val0 - after_val0 == count * items_changed + assert after_val1 - before_val1 == count * items_changed @pytest.mark.parametrize(['dt', 'pat', 'count', 'singleton'], iter_struct_object_dtypes()) -- cgit v1.2.1 From aa52dee8dcaba963faa0ec4e36a85d97bbc8e2de Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Sat, 13 Nov 2021 04:17:50 +0000 Subject: ENH: random: Add broadcast support to Generator.multinomial (#16740) xref github issue #15201 --- numpy/random/_generator.pyi | 4 +- numpy/random/_generator.pyx | 155 ++++++++++++++++++++------- numpy/random/tests/test_generator_mt19937.py | 64 +++++++++-- 3 files changed, 178 insertions(+), 45 deletions(-) (limited to 'numpy') diff --git a/numpy/random/_generator.pyi b/numpy/random/_generator.pyi index 64b683d7c..c574bef9a 100644 --- a/numpy/random/_generator.pyi +++ b/numpy/random/_generator.pyi @@ -623,7 +623,9 @@ class Generator: method: Literal["svd", "eigh", "cholesky"] = ..., ) -> ndarray[Any, dtype[float64]]: ... def multinomial( - self, n: _ArrayLikeInt_co, pvals: _ArrayLikeFloat_co, size: Optional[_ShapeLike] = ... + self, n: _ArrayLikeInt_co, + pvals: _ArrayLikeFloat_co, + size: Optional[_ShapeLike] = ... ) -> ndarray[Any, dtype[int64]]: ... def multivariate_hypergeometric( self, diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index 7087b6e1d..2134f0e24 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -3683,24 +3683,35 @@ cdef class Generator: ---------- n : int or array-like of ints Number of experiments. - pvals : sequence of floats, length p - Probabilities of each of the ``p`` different outcomes. These - must sum to 1 (however, the last element is always assumed to - account for the remaining probability, as long as - ``sum(pvals[:-1]) <= 1)``. + pvals : array-like of floats + Probabilities of each of the ``p`` different outcomes with shape + ``(k0, k1, ..., kn, p)``. Each element ``pvals[i,j,...,:]`` must + sum to 1 (however, the last element is always assumed to account + for the remaining probability, as long as + ``sum(pvals[..., :-1], axis=-1) <= 1.0``. Must have at least 1 + dimension where pvals.shape[-1] > 0. size : int or tuple of ints, optional Output shape. If the given shape is, e.g., ``(m, n, k)``, then - ``m * n * k`` samples are drawn. Default is None, in which case a - single value is returned. + ``m * n * k`` samples are drawn each with ``p`` elements. Default + is None where the output size is determined by the broadcast shape + of ``n`` and all by the final dimension of ``pvals``, which is + denoted as ``b=(b0, b1, ..., bq)``. If size is not None, then it + must be compatible with the broadcast shape ``b``. Specifically, + size must have ``q`` or more elements and size[-(q-j):] must equal + ``bj``. Returns ------- out : ndarray - The drawn samples, of shape *size*, if that was provided. If not, - the shape is ``(N,)``. + The drawn samples, of shape size, if provided. When size is + provided, the output shape is size + (p,) If not specified, + the shape is determined by the broadcast shape of ``n`` and + ``pvals``, ``(b0, b1, ..., bq)`` augmented with the dimension of + the multinomial, ``p``, so that that output shape is + ``(b0, b1, ..., bq, p)``. - In other words, each entry ``out[i,j,...,:]`` is an N-dimensional - value drawn from the distribution. + Each entry ``out[i,j,...,:]`` is a ``p``-dimensional value drawn + from the distribution. Examples -------- @@ -3738,6 +3749,38 @@ cdef class Generator: >>> rng.multinomial(100, [1/7.]*5 + [2/7.]) array([11, 16, 14, 17, 16, 26]) # random + Simulate 10 throws of a 4-sided die and 20 throws of a 6-sided die + + >>> rng.multinomial([10, 20],[[1/4]*4 + [0]*2, [1/6]*6]) + array([[2, 1, 4, 3, 0, 0], + [3, 3, 3, 6, 1, 4]], dtype=int64) # random + + Generate categorical random variates from two categories where the + first has 3 outcomes and the second has 2. + + >>> rng.multinomial(1, [[.1, .5, .4 ], [.3, .7, .0]]) + array([[0, 0, 1], + [0, 1, 0]], dtype=int64) # random + + ``argmax(axis=-1)`` is then used to return the categories. + + >>> pvals = [[.1, .5, .4 ], [.3, .7, .0]] + >>> rvs = rng.multinomial(1, pvals, size=(4,2)) + >>> rvs.argmax(axis=-1) + array([[0, 1], + [2, 0], + [2, 1], + [2, 0]], dtype=int64) # random + + The same output dimension can be produced using broadcasting. + + >>> rvs = rng.multinomial([[1]] * 4, pvals) + >>> rvs.argmax(axis=-1) + array([[0, 1], + [2, 0], + [2, 1], + [2, 0]], dtype=int64) # random + The probability inputs should be normalized. As an implementation detail, the value of the last entry is ignored and assumed to take up any leftover probability mass, but this should not be relied on. @@ -3752,47 +3795,82 @@ cdef class Generator: >>> rng.multinomial(100, [1.0, 2.0]) # WRONG Traceback (most recent call last): ValueError: pvals < 0, pvals > 1 or pvals contains NaNs - """ - cdef np.npy_intp d, i, sz, offset + cdef np.npy_intp d, i, sz, offset, pi cdef np.ndarray parr, mnarr, on, temp_arr cdef double *pix + cdef int ndim cdef int64_t *mnix cdef int64_t ni cdef np.broadcast it + on = np.PyArray_FROM_OTF(n, + np.NPY_INT64, + np.NPY_ARRAY_ALIGNED | + np.NPY_ARRAY_C_CONTIGUOUS) + parr = np.PyArray_FROM_OTF(pvals, + np.NPY_DOUBLE, + np.NPY_ARRAY_ALIGNED | + np.NPY_ARRAY_C_CONTIGUOUS) + ndim = parr.ndim + d = parr.shape[ndim - 1] if ndim >= 1 else 0 + if d == 0: + raise ValueError( + "pvals must have at least 1 dimension and the last dimension " + "of pvals must be greater than 0." + ) - d = len(pvals) - on = np.PyArray_FROM_OTF(n, np.NPY_INT64, np.NPY_ALIGNED) - parr = np.PyArray_FROMANY( - pvals, np.NPY_DOUBLE, 1, 1, np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS) - pix = np.PyArray_DATA(parr) check_array_constraint(parr, 'pvals', CONS_BOUNDED_0_1) - if kahan_sum(pix, d-1) > (1.0 + 1e-12): - # When floating, but not float dtype, and close, improve the error - # 1.0001 works for float16 and float32 - if (isinstance(pvals, np.ndarray) - and np.issubdtype(pvals.dtype, np.floating) - and pvals.dtype != float - and pvals.sum() < 1.0001): - msg = ("sum(pvals[:-1].astype(np.float64)) > 1.0. The pvals " - "array is cast to 64-bit floating point prior to " - "checking the sum. Precision changes when casting may " - "cause problems even if the sum of the original pvals " - "is valid.") - else: - msg = "sum(pvals[:-1]) > 1.0" - raise ValueError(msg) + pix = np.PyArray_DATA(parr) + sz = np.PyArray_SIZE(parr) + # Cython 0.29.20 would not correctly translate the range-based for + # loop to a C for loop + # for offset in range(0, sz, d): + offset = 0 + while offset < sz: + if kahan_sum(pix + offset, d-1) > (1.0 + 1e-12): + # When floating, but not float dtype, and close, improve the error + # 1.0001 works for float16 and float32 + slice_repr = "[:-1]" if ndim == 1 else "[...,:-1]" + if (isinstance(pvals, np.ndarray) + and np.issubdtype(pvals.dtype, np.floating) + and pvals.dtype != float + and pvals.sum() < 1.0001): + msg = (f"sum(pvals{slice_repr}.astype(np.float64)) > 1.0." + " The pvals array is cast to 64-bit floating" + " point prior to checking the sum. Precision " + "changes when casting may cause problems even " + "if the sum of the original pvals is valid.") + else: + msg = f"sum(pvals{slice_repr}) > 1.0" + raise ValueError(msg) + offset += d - if np.PyArray_NDIM(on) != 0: # vector + if np.PyArray_NDIM(on) != 0 or ndim > 1: # vector check_array_constraint(on, 'n', CONS_NON_NEGATIVE) + # This provides the offsets to use in the C-contig parr when + # broadcasting + offsets = np.arange( + 0, np.PyArray_SIZE(parr), d, dtype=np.intp + ).reshape((parr).shape[:ndim - 1]) if size is None: - it = np.PyArray_MultiIterNew1(on) + it = np.PyArray_MultiIterNew2(on, offsets) else: temp = np.empty(size, dtype=np.int8) temp_arr = temp - it = np.PyArray_MultiIterNew2(on, temp_arr) - validate_output_shape(it.shape, temp_arr) + it = np.PyArray_MultiIterNew3(on, offsets, temp_arr) + # Validate size and the broadcast shape + try: + size = (operator.index(size),) + except: + size = tuple(size) + # This test verifies that an axis with dim 1 in size has not + # been increased by broadcasting with the input + if it.shape != size: + raise ValueError( + f"Output size {size} is not compatible with " + f"broadcast dimensions of inputs {it.shape}." + ) shape = it.shape + (d,) multin = np.zeros(shape, dtype=np.int64) mnarr = multin @@ -3802,7 +3880,8 @@ cdef class Generator: with self.lock, nogil: for i in range(sz): ni = (np.PyArray_MultiIter_DATA(it, 0))[0] - random_multinomial(&self._bitgen, ni, &mnix[offset], pix, d, &self._binomial) + pi = (np.PyArray_MultiIter_DATA(it, 1))[0] + random_multinomial(&self._bitgen, ni, &mnix[offset], &pix[pi], d, &self._binomial) offset += d np.PyArray_MultiIter_NEXT(it) return multin diff --git a/numpy/random/tests/test_generator_mt19937.py b/numpy/random/tests/test_generator_mt19937.py index d057122f1..e5411b8ef 100644 --- a/numpy/random/tests/test_generator_mt19937.py +++ b/numpy/random/tests/test_generator_mt19937.py @@ -136,12 +136,6 @@ class TestMultinomial: contig = random.multinomial(100, pvals=np.ascontiguousarray(pvals)) assert_array_equal(non_contig, contig) - def test_multidimensional_pvals(self): - assert_raises(ValueError, random.multinomial, 10, [[0, 1]]) - assert_raises(ValueError, random.multinomial, 10, [[0], [1]]) - assert_raises(ValueError, random.multinomial, 10, [[[0], [1]], [[1], [0]]]) - assert_raises(ValueError, random.multinomial, 10, np.array([[0, 1], [1, 0]])) - def test_multinomial_pvals_float32(self): x = np.array([9.9e-01, 9.9e-01, 1.0e-09, 1.0e-09, 1.0e-09, 1.0e-09, 1.0e-09, 1.0e-09, 1.0e-09, 1.0e-09], dtype=np.float32) @@ -2361,6 +2355,64 @@ class TestBroadcast: [2, 3, 6, 4, 2, 3]], dtype=np.int64) assert_array_equal(actual, desired) + random = Generator(MT19937(self.seed)) + actual = random.multinomial([5, 20], [[1 / 6.] * 6] * 2) + desired = np.array([[0, 0, 2, 1, 2, 0], + [2, 3, 6, 4, 2, 3]], dtype=np.int64) + assert_array_equal(actual, desired) + + random = Generator(MT19937(self.seed)) + actual = random.multinomial([[5], [20]], [[1 / 6.] * 6] * 2) + desired = np.array([[[0, 0, 2, 1, 2, 0], + [0, 0, 2, 1, 1, 1]], + [[4, 2, 3, 3, 5, 3], + [7, 2, 2, 1, 4, 4]]], dtype=np.int64) + assert_array_equal(actual, desired) + + @pytest.mark.parametrize("n", [10, + np.array([10, 10]), + np.array([[[10]], [[10]]]) + ] + ) + def test_multinomial_pval_broadcast(self, n): + random = Generator(MT19937(self.seed)) + pvals = np.array([1 / 4] * 4) + actual = random.multinomial(n, pvals) + n_shape = tuple() if isinstance(n, int) else n.shape + expected_shape = n_shape + (4,) + assert actual.shape == expected_shape + pvals = np.vstack([pvals, pvals]) + actual = random.multinomial(n, pvals) + expected_shape = np.broadcast_shapes(n_shape, pvals.shape[:-1]) + (4,) + assert actual.shape == expected_shape + + pvals = np.vstack([[pvals], [pvals]]) + actual = random.multinomial(n, pvals) + expected_shape = np.broadcast_shapes(n_shape, pvals.shape[:-1]) + assert actual.shape == expected_shape + (4,) + actual = random.multinomial(n, pvals, size=(3, 2) + expected_shape) + assert actual.shape == (3, 2) + expected_shape + (4,) + + with pytest.raises(ValueError): + # Ensure that size is not broadcast + actual = random.multinomial(n, pvals, size=(1,) * 6) + + def test_invalid_pvals_broadcast(self): + random = Generator(MT19937(self.seed)) + pvals = [[1 / 6] * 6, [1 / 4] * 6] + assert_raises(ValueError, random.multinomial, 1, pvals) + assert_raises(ValueError, random.multinomial, 6, 0.5) + + def test_empty_outputs(self): + random = Generator(MT19937(self.seed)) + actual = random.multinomial(np.empty((10, 0, 6), "i8"), [1 / 6] * 6) + assert actual.shape == (10, 0, 6, 6) + actual = random.multinomial(12, np.empty((10, 0, 10))) + assert actual.shape == (10, 0, 10) + actual = random.multinomial(np.empty((3, 0, 7), "i8"), + np.empty((3, 0, 7, 4))) + assert actual.shape == (3, 0, 7, 4) + class TestThread: # make sure each state produces the same sequence even in threads -- cgit v1.2.1 From 9ba1440501d53d72ecce24ed207db62e3037d807 Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Sat, 13 Nov 2021 20:35:23 +0200 Subject: BUG: Revert from `long double` changes, and force `npymath` to respect `npy_longdouble` (#20360) In some certain cases mostly workarounds, there's a necessity to define `npy_longdouble` as `double` even if that was against the compiler implementation(sizeof(long double) != sizeof(double)). Therefore, avoid `long double`, use `npy_longdouble` instead, and when it comes to standard math functions make sure of using the double version when `NPY_SIZEOF_LONGDOUBLE` == `NPY_SIZEOF_DOUBLE`. --- numpy/core/include/numpy/npy_common.h | 28 +++++++++++++--- numpy/core/src/multiarray/_multiarray_tests.c.src | 2 +- numpy/core/src/npymath/npy_math_internal.h.src | 40 +++++++++++++++-------- 3 files changed, 50 insertions(+), 20 deletions(-) (limited to 'numpy') diff --git a/numpy/core/include/numpy/npy_common.h b/numpy/core/include/numpy/npy_common.h index 57cc592b9..88794ca07 100644 --- a/numpy/core/include/numpy/npy_common.h +++ b/numpy/core/include/numpy/npy_common.h @@ -356,14 +356,32 @@ typedef unsigned long npy_ulonglong; typedef unsigned char npy_bool; #define NPY_FALSE 0 #define NPY_TRUE 1 - - +/* + * `NPY_SIZEOF_LONGDOUBLE` isn't usually equal to sizeof(long double). + * In some certain cases, it may forced to be equal to sizeof(double) + * even against the compiler implementation and the same goes for + * `complex long double`. + * + * Therefore, avoid `long double`, use `npy_longdouble` instead, + * and when it comes to standard math functions make sure of using + * the double version when `NPY_SIZEOF_LONGDOUBLE` == `NPY_SIZEOF_DOUBLE`. + * For example: + * npy_longdouble *ptr, x; + * #if NPY_SIZEOF_LONGDOUBLE == NPY_SIZEOF_DOUBLE + * npy_longdouble r = modf(x, ptr); + * #else + * npy_longdouble r = modfl(x, ptr); + * #endif + * + * See https://github.com/numpy/numpy/issues/20348 + */ #if NPY_SIZEOF_LONGDOUBLE == NPY_SIZEOF_DOUBLE - #define NPY_LONGDOUBLE_FMT "g" + #define NPY_LONGDOUBLE_FMT "g" + typedef double npy_longdouble; #else - #define NPY_LONGDOUBLE_FMT "Lg" + #define NPY_LONGDOUBLE_FMT "Lg" + typedef long double npy_longdouble; #endif -typedef long double npy_longdouble; #ifndef Py_USING_UNICODE #error Must use Python with unicode enabled. diff --git a/numpy/core/src/multiarray/_multiarray_tests.c.src b/numpy/core/src/multiarray/_multiarray_tests.c.src index e945d0771..d7dfa4829 100644 --- a/numpy/core/src/multiarray/_multiarray_tests.c.src +++ b/numpy/core/src/multiarray/_multiarray_tests.c.src @@ -2193,7 +2193,7 @@ PrintFloat_Printf_g(PyObject *obj, int precision) } else if (PyArray_IsScalar(obj, LongDouble)) { npy_longdouble x = PyArrayScalar_VAL(obj, LongDouble); - PyOS_snprintf(str, sizeof(str), "%.*Lg", precision, x); + PyOS_snprintf(str, sizeof(str), "%.*" NPY_LONGDOUBLE_FMT, precision, x); } else{ double val = PyFloat_AsDouble(obj); diff --git a/numpy/core/src/npymath/npy_math_internal.h.src b/numpy/core/src/npymath/npy_math_internal.h.src index dd2424db8..5b418342f 100644 --- a/numpy/core/src/npymath/npy_math_internal.h.src +++ b/numpy/core/src/npymath/npy_math_internal.h.src @@ -477,10 +477,16 @@ NPY_INPLACE @type@ npy_frexp@c@(@type@ x, int* exp) /**begin repeat * #type = npy_longdouble, npy_double, npy_float# + * #TYPE = LONGDOUBLE, DOUBLE, FLOAT# * #c = l,,f# * #C = L,,F# */ - +#undef NPY__FP_SFX +#if NPY_SIZEOF_@TYPE@ == NPY_SIZEOF_DOUBLE + #define NPY__FP_SFX(X) X +#else + #define NPY__FP_SFX(X) NPY_CAT(X, @c@) +#endif /* * On arm64 macOS, there's a bug with sin, cos, and tan where they don't * raise "invalid" when given INFINITY as input. @@ -506,7 +512,7 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x) return (x - x); } #endif - return @kind@@c@(x); + return NPY__FP_SFX(@kind@)(x); } #endif @@ -521,7 +527,7 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x) #ifdef HAVE_@KIND@@C@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y) { - return @kind@@c@(x, y); + return NPY__FP_SFX(@kind@)(x, y); } #endif /**end repeat1**/ @@ -529,21 +535,21 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y) #ifdef HAVE_MODF@C@ NPY_INPLACE @type@ npy_modf@c@(@type@ x, @type@ *iptr) { - return modf@c@(x, iptr); + return NPY__FP_SFX(modf)(x, iptr); } #endif #ifdef HAVE_LDEXP@C@ NPY_INPLACE @type@ npy_ldexp@c@(@type@ x, int exp) { - return ldexp@c@(x, exp); + return NPY__FP_SFX(ldexp)(x, exp); } #endif #ifdef HAVE_FREXP@C@ NPY_INPLACE @type@ npy_frexp@c@(@type@ x, int* exp) { - return frexp@c@(x, exp); + return NPY__FP_SFX(frexp)(x, exp); } #endif @@ -566,10 +572,10 @@ NPY_INPLACE @type@ npy_cbrt@c@(@type@ x) #else NPY_INPLACE @type@ npy_cbrt@c@(@type@ x) { - return cbrt@c@(x); + return NPY__FP_SFX(cbrt)(x); } #endif - +#undef NPY__FP_SFX /**end repeat**/ @@ -579,10 +585,16 @@ NPY_INPLACE @type@ npy_cbrt@c@(@type@ x) /**begin repeat * #type = npy_float, npy_double, npy_longdouble# + * #TYPE = FLOAT, DOUBLE, LONGDOUBLE# * #c = f, ,l# * #C = F, ,L# */ - +#undef NPY__FP_SFX +#if NPY_SIZEOF_@TYPE@ == NPY_SIZEOF_DOUBLE + #define NPY__FP_SFX(X) X +#else + #define NPY__FP_SFX(X) NPY_CAT(X, @c@) +#endif @type@ npy_heaviside@c@(@type@ x, @type@ h0) { if (npy_isnan(x)) { @@ -599,10 +611,10 @@ NPY_INPLACE @type@ npy_cbrt@c@(@type@ x) } } -#define LOGE2 NPY_LOGE2@c@ -#define LOG2E NPY_LOG2E@c@ -#define RAD2DEG (180.0@c@/NPY_PI@c@) -#define DEG2RAD (NPY_PI@c@/180.0@c@) +#define LOGE2 NPY__FP_SFX(NPY_LOGE2) +#define LOG2E NPY__FP_SFX(NPY_LOG2E) +#define RAD2DEG (NPY__FP_SFX(180.0)/NPY__FP_SFX(NPY_PI)) +#define DEG2RAD (NPY__FP_SFX(NPY_PI)/NPY__FP_SFX(180.0)) NPY_INPLACE @type@ npy_rad2deg@c@(@type@ x) { @@ -756,7 +768,7 @@ npy_divmod@c@(@type@ a, @type@ b, @type@ *modulus) #undef LOG2E #undef RAD2DEG #undef DEG2RAD - +#undef NPY__FP_SFX /**end repeat**/ /**begin repeat -- cgit v1.2.1 From 6d698863f6d38e84ae78b6f1aeae1b3d3993ed6b Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Fri, 12 Nov 2021 05:14:13 +0200 Subject: BUG, DIST: Print os error message when the executable not exist this patch is really important since most of the users aren't able to determine the build error when the toolchain or the built environment missing executable files of compiler, linker, assembler, etc. --- numpy/distutils/ccompiler.py | 10 ++++++++-- numpy/distutils/ccompiler_opt.py | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/ccompiler.py b/numpy/distutils/ccompiler.py index 9c85d28b9..713b8c72f 100644 --- a/numpy/distutils/ccompiler.py +++ b/numpy/distutils/ccompiler.py @@ -144,12 +144,18 @@ def CCompiler_spawn(self, cmd, display=None): except subprocess.CalledProcessError as exc: o = exc.output s = exc.returncode - except OSError: + except OSError as e: # OSError doesn't have the same hooks for the exception # output, but exec_command() historically would use an # empty string for EnvironmentError (base class for # OSError) - o = b'' + # o = b'' + # still that would make the end-user lost in translation! + o = f"\n\n{e}\n\n\n" + try: + o = o.encode(sys.stdout.encoding) + except AttributeError: + o = o.encode('utf8') # status previously used by exec_command() for parent # of OSError s = 127 diff --git a/numpy/distutils/ccompiler_opt.py b/numpy/distutils/ccompiler_opt.py index d7df386fe..c11be8727 100644 --- a/numpy/distutils/ccompiler_opt.py +++ b/numpy/distutils/ccompiler_opt.py @@ -721,8 +721,8 @@ class _Distutils: except subprocess.CalledProcessError as exc: o = exc.output s = exc.returncode - except OSError: - o = b'' + except OSError as e: + o = e s = 127 else: return None -- cgit v1.2.1 From 10ac74940b1e6b1a9e9525f1aaa68fb1e096b326 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 12 Nov 2021 17:20:58 -0600 Subject: BUG: Relax unary ufunc (sqrt, etc.) stride assert The stride is never used if the length is 0 or 1, so it is OK if if the strides have strange values here. Not adding a test: This is a simple assert and the original issue was found due to intentionally garbled up strides in `ufunc.at`, but `ufunc.at` stopped doing that. Closes gh-19806 --- numpy/core/src/umath/loops_trigonometric.dispatch.c.src | 2 +- numpy/core/src/umath/loops_umath_fp.dispatch.c.src | 4 ++-- numpy/core/src/umath/loops_unary_fp.dispatch.c.src | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/umath/loops_trigonometric.dispatch.c.src b/numpy/core/src/umath/loops_trigonometric.dispatch.c.src index 8c2c83e7c..cd9b2ed54 100644 --- a/numpy/core/src/umath/loops_trigonometric.dispatch.c.src +++ b/numpy/core/src/umath/loops_trigonometric.dispatch.c.src @@ -209,7 +209,7 @@ NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(FLOAT_@func@) const npy_intp ssrc = steps[0] / lsize; const npy_intp sdst = steps[1] / lsize; npy_intp len = dimensions[0]; - assert(steps[0] % lsize == 0 && steps[1] % lsize == 0); + assert(len <= 1 || (steps[0] % lsize == 0 && steps[1] % lsize == 0)); #if NPY_SIMD_FMA3 if (is_mem_overlap(src, steps[0], dst, steps[1], len) || !npyv_loadable_stride_f32(ssrc) || !npyv_storable_stride_f32(sdst) diff --git a/numpy/core/src/umath/loops_umath_fp.dispatch.c.src b/numpy/core/src/umath/loops_umath_fp.dispatch.c.src index 852604655..a8289fc51 100644 --- a/numpy/core/src/umath/loops_umath_fp.dispatch.c.src +++ b/numpy/core/src/umath/loops_umath_fp.dispatch.c.src @@ -96,7 +96,7 @@ NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(@TYPE@_@func@) const npy_intp ssrc = steps[0] / lsize; const npy_intp sdst = steps[1] / lsize; const npy_intp len = dimensions[0]; - assert(steps[0] % lsize == 0 && steps[1] % lsize == 0); + assert(len <= 1 || (steps[0] % lsize == 0 && steps[1] % lsize == 0)); if (!is_mem_overlap(src, steps[0], dst, steps[1], len) && npyv_loadable_stride_@sfx@(ssrc) && npyv_storable_stride_@sfx@(sdst)) { @@ -125,7 +125,7 @@ NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(DOUBLE_@func@) const npy_intp ssrc = steps[0] / lsize; const npy_intp sdst = steps[1] / lsize; const npy_intp len = dimensions[0]; - assert(steps[0] % lsize == 0 && steps[1] % lsize == 0); + assert(len <= 1 || (steps[0] % lsize == 0 && steps[1] % lsize == 0)); if (!is_mem_overlap(src, steps[0], dst, steps[1], len) && npyv_loadable_stride_f64(ssrc) && npyv_storable_stride_f64(sdst)) { diff --git a/numpy/core/src/umath/loops_unary_fp.dispatch.c.src b/numpy/core/src/umath/loops_unary_fp.dispatch.c.src index 789733fb6..93761b98c 100644 --- a/numpy/core/src/umath/loops_unary_fp.dispatch.c.src +++ b/numpy/core/src/umath/loops_unary_fp.dispatch.c.src @@ -263,7 +263,7 @@ NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(@TYPE@_@kind@) npy_intp len = dimensions[0]; #if @VCHK@ const int lsize = sizeof(npyv_lanetype_@sfx@); - assert(src_step % lsize == 0 && dst_step % lsize == 0); + assert(len <= 1 || (src_step % lsize == 0 && dst_step % lsize == 0)); if (is_mem_overlap(src, src_step, dst, dst_step, len)) { goto no_unroll; } -- cgit v1.2.1 From b9173e5c8b0410a9c96f3711d304e756a3e55efc Mon Sep 17 00:00:00 2001 From: Sayed Adel Date: Sun, 14 Nov 2021 05:14:00 +0200 Subject: DIST: Workaround ignore invalid C++ flags for config/try_link --- numpy/core/setup.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'numpy') diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 3fc0f85f1..a5f423d8f 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -686,6 +686,13 @@ def configuration(parent_package='',top_path=None): ) ), ): + is_cpp = lang == 'c++' + if is_cpp: + # this a workround to get rid of invalid c++ flags + # without doing big changes to config. + # c tested first, compiler should be here + bk_c = config_cmd.compiler + config_cmd.compiler = bk_c.cxx_compiler() st = config_cmd.try_link(test_code, lang=lang) if not st: # rerun the failing command in verbose mode @@ -695,6 +702,8 @@ def configuration(parent_package='',top_path=None): f"Broken toolchain: cannot link a simple {lang.upper()} " f"program. {note}" ) + if is_cpp: + config_cmd.compiler = bk_c mlibs = check_mathlib(config_cmd) posix_mlib = ' '.join(['-l%s' % l for l in mlibs]) -- cgit v1.2.1 From cbc25d2cc9ed50e8f0d026d1e2e4766378d1640f Mon Sep 17 00:00:00 2001 From: Christoph Buchner Date: Sun, 14 Nov 2021 08:36:03 +0100 Subject: MAINT: Fix newlines in diagnostics output of numpy.f2py. Linebreaks were not consistently added to errmess/outmess arguments, which led to very long lines and wrong concatenation with compiler messages in f2py console output. --- numpy/f2py/capi_maps.py | 2 +- numpy/f2py/crackfortran.py | 12 ++++++------ numpy/f2py/f2py2e.py | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'numpy') diff --git a/numpy/f2py/capi_maps.py b/numpy/f2py/capi_maps.py index 655cfd768..581f946e5 100644 --- a/numpy/f2py/capi_maps.py +++ b/numpy/f2py/capi_maps.py @@ -442,7 +442,7 @@ def getpydocsign(a, var): sigout = sig else: errmess( - 'getpydocsign: Could not resolve docsignature for "%s".\\n' % a) + 'getpydocsign: Could not resolve docsignature for "%s".\n' % a) return sig, sigout diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py index 67675af45..b02eb68b7 100755 --- a/numpy/f2py/crackfortran.py +++ b/numpy/f2py/crackfortran.py @@ -1170,10 +1170,10 @@ def analyzeline(m, case, line): groupcache[groupcounter]['args'].append(k) else: errmess( - 'analyzeline: intent(callback) %s is ignored' % (k)) + 'analyzeline: intent(callback) %s is ignored\n' % (k)) else: errmess('analyzeline: intent(callback) %s is already' - ' in argument list' % (k)) + ' in argument list\n' % (k)) if case in ['optional', 'required', 'public', 'external', 'private', 'intrinsic']: ap = case if 'attrspec' in edecl[k]: @@ -1868,11 +1868,11 @@ def get_useparameters(block, param_map=None): continue # XXX: apply mapping if mapping: - errmess('get_useparameters: mapping for %s not impl.' % (mapping)) + errmess('get_useparameters: mapping for %s not impl.\n' % (mapping)) for k, v in list(params.items()): if k in param_map: outmess('get_useparameters: overriding parameter %s with' - ' value from module %s' % (repr(k), repr(usename))) + ' value from module %s\n' % (repr(k), repr(usename))) param_map[k] = v return param_map @@ -2385,7 +2385,7 @@ def get_parameters(vars, global_params={}): elif iscomplex(vars[n]): outmess(f'get_parameters[TODO]: ' - f'implement evaluation of complex expression {v}') + f'implement evaluation of complex expression {v}\n') try: params[n] = eval(v, g_params, params) @@ -2633,7 +2633,7 @@ def analyzevars(block): vars[n]['intent'].append('c') else: errmess( - "analyzevars: charselector=%r unhandled." % (d)) + "analyzevars: charselector=%r unhandled.\n" % (d)) if 'check' not in vars[n] and 'args' in block and n in block['args']: # n is an argument that has no checks defined. Here we diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py index 605495574..0c0ec25e3 100755 --- a/numpy/f2py/f2py2e.py +++ b/numpy/f2py/f2py2e.py @@ -286,7 +286,7 @@ def scaninputline(inputline): sys.exit() if not os.path.isdir(buildpath): if not verbose: - outmess('Creating build directory %s' % (buildpath)) + outmess('Creating build directory %s\n' % (buildpath)) os.mkdir(buildpath) if signsfile: signsfile = os.path.join(buildpath, signsfile) -- cgit v1.2.1 From a1813504ad44b70fb139181a9df8465bcb22e24d Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Sun, 14 Nov 2021 14:35:06 -0700 Subject: ENH: Add the linalg extension to the array_api submodule (#19980) --- numpy/array_api/__init__.py | 12 +- numpy/array_api/_array_object.py | 2 +- numpy/array_api/_linear_algebra_functions.py | 67 ----- numpy/array_api/_statistical_functions.py | 9 +- numpy/array_api/linalg.py | 408 +++++++++++++++++++++++++++ numpy/tests/test_public_api.py | 1 + 6 files changed, 420 insertions(+), 79 deletions(-) delete mode 100644 numpy/array_api/_linear_algebra_functions.py create mode 100644 numpy/array_api/linalg.py (limited to 'numpy') diff --git a/numpy/array_api/__init__.py b/numpy/array_api/__init__.py index 36e3f3ed5..bbe2fdce2 100644 --- a/numpy/array_api/__init__.py +++ b/numpy/array_api/__init__.py @@ -109,9 +109,6 @@ Still TODO in this module are: - The spec is still in an RFC phase and may still have minor updates, which will need to be reflected here. -- The linear algebra extension in the spec will be added in a future pull - request. - - Complex number support in array API spec is planned but not yet finalized, as are the fft extension and certain linear algebra functions such as eig that require complex dtypes. @@ -334,12 +331,13 @@ __all__ += [ "trunc", ] -# einsum is not yet implemented in the array API spec. +# linalg is an extension in the array API spec, which is a sub-namespace. Only +# a subset of functions in it are imported into the top-level namespace. +from . import linalg -# from ._linear_algebra_functions import einsum -# __all__ += ['einsum'] +__all__ += ["linalg"] -from ._linear_algebra_functions import matmul, tensordot, matrix_transpose, vecdot +from .linalg import matmul, tensordot, matrix_transpose, vecdot __all__ += ["matmul", "tensordot", "matrix_transpose", "vecdot"] diff --git a/numpy/array_api/_array_object.py b/numpy/array_api/_array_object.py index dc74bb8c5..8794c5ea5 100644 --- a/numpy/array_api/_array_object.py +++ b/numpy/array_api/_array_object.py @@ -1030,7 +1030,7 @@ class Array: # Note: mT is new in array API spec (see matrix_transpose) @property def mT(self) -> Array: - from ._linear_algebra_functions import matrix_transpose + from .linalg import matrix_transpose return matrix_transpose(self) @property diff --git a/numpy/array_api/_linear_algebra_functions.py b/numpy/array_api/_linear_algebra_functions.py deleted file mode 100644 index 7a6c9846c..000000000 --- a/numpy/array_api/_linear_algebra_functions.py +++ /dev/null @@ -1,67 +0,0 @@ -from __future__ import annotations - -from ._array_object import Array -from ._dtypes import _numeric_dtypes, _result_type - -from typing import Optional, Sequence, Tuple, Union - -import numpy as np - -# einsum is not yet implemented in the array API spec. - -# def einsum(): -# """ -# Array API compatible wrapper for :py:func:`np.einsum `. -# -# See its docstring for more information. -# """ -# return np.einsum() - - -def matmul(x1: Array, x2: Array, /) -> Array: - """ - Array API compatible wrapper for :py:func:`np.matmul `. - - See its docstring for more information. - """ - # Note: the restriction to numeric dtypes only is different from - # np.matmul. - if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError("Only numeric dtypes are allowed in matmul") - # Call result type here just to raise on disallowed type combinations - _result_type(x1.dtype, x2.dtype) - - return Array._new(np.matmul(x1._array, x2._array)) - - -# Note: axes must be a tuple, unlike np.tensordot where it can be an array or array-like. -def tensordot( - x1: Array, - x2: Array, - /, - *, - axes: Union[int, Tuple[Sequence[int], Sequence[int]]] = 2, -) -> Array: - # Note: the restriction to numeric dtypes only is different from - # np.tensordot. - if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: - raise TypeError("Only numeric dtypes are allowed in tensordot") - # Call result type here just to raise on disallowed type combinations - _result_type(x1.dtype, x2.dtype) - - return Array._new(np.tensordot(x1._array, x2._array, axes=axes)) - - -# Note: this function is new in the array API spec. Unlike transpose, it only -# transposes the last two axes. -def matrix_transpose(x: Array, /) -> Array: - if x.ndim < 2: - raise ValueError("x must be at least 2-dimensional for matrix_transpose") - return Array._new(np.swapaxes(x._array, -1, -2)) - - -# Note: vecdot is not in NumPy -def vecdot(x1: Array, x2: Array, /, *, axis: Optional[int] = None) -> Array: - if axis is None: - axis = -1 - return tensordot(x1, x2, axes=((axis,), (axis,))) diff --git a/numpy/array_api/_statistical_functions.py b/numpy/array_api/_statistical_functions.py index c5abf9468..7bee3f4db 100644 --- a/numpy/array_api/_statistical_functions.py +++ b/numpy/array_api/_statistical_functions.py @@ -93,11 +93,12 @@ def sum( ) -> Array: if x.dtype not in _numeric_dtypes: raise TypeError("Only numeric dtypes are allowed in sum") - # Note: sum() and prod() always upcast float32 to float64 for dtype=None - # We need to do so here before summing to avoid overflow + # Note: sum() and prod() always upcast integers to (u)int64 and float32 to + # float64 for dtype=None. `np.sum` does that too for integers, but not for + # float32, so we need to special-case it here if dtype is None and x.dtype == float32: - x = asarray(x, dtype=float64) - return Array._new(np.sum(x._array, axis=axis, keepdims=keepdims)) + dtype = float64 + return Array._new(np.sum(x._array, axis=axis, dtype=dtype, keepdims=keepdims)) def var( diff --git a/numpy/array_api/linalg.py b/numpy/array_api/linalg.py new file mode 100644 index 000000000..8d7ba659e --- /dev/null +++ b/numpy/array_api/linalg.py @@ -0,0 +1,408 @@ +from __future__ import annotations + +from ._dtypes import _floating_dtypes, _numeric_dtypes +from ._array_object import Array + +from typing import TYPE_CHECKING +if TYPE_CHECKING: + from ._typing import Literal, Optional, Sequence, Tuple, Union + +from typing import NamedTuple + +import numpy.linalg +import numpy as np + +class EighResult(NamedTuple): + eigenvalues: Array + eigenvectors: Array + +class QRResult(NamedTuple): + Q: Array + R: Array + +class SlogdetResult(NamedTuple): + sign: Array + logabsdet: Array + +class SVDResult(NamedTuple): + U: Array + S: Array + Vh: Array + +# Note: the inclusion of the upper keyword is different from +# np.linalg.cholesky, which does not have it. +def cholesky(x: Array, /, *, upper: bool = False) -> Array: + """ + Array API compatible wrapper for :py:func:`np.linalg.cholesky `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.cholesky. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in cholesky') + L = np.linalg.cholesky(x._array) + if upper: + return Array._new(L).mT + return Array._new(L) + +# Note: cross is the numpy top-level namespace, not np.linalg +def cross(x1: Array, x2: Array, /, *, axis: int = -1) -> Array: + """ + Array API compatible wrapper for :py:func:`np.cross `. + + See its docstring for more information. + """ + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in cross') + # Note: this is different from np.cross(), which broadcasts + if x1.shape != x2.shape: + raise ValueError('x1 and x2 must have the same shape') + if x1.ndim == 0: + raise ValueError('cross() requires arrays of dimension at least 1') + # Note: this is different from np.cross(), which allows dimension 2 + if x1.shape[axis] != 3: + raise ValueError('cross() dimension must equal 3') + return Array._new(np.cross(x1._array, x2._array, axis=axis)) + +def det(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.linalg.det `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.det. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in det') + return Array._new(np.linalg.det(x._array)) + +# Note: diagonal is the numpy top-level namespace, not np.linalg +def diagonal(x: Array, /, *, offset: int = 0) -> Array: + """ + Array API compatible wrapper for :py:func:`np.diagonal `. + + See its docstring for more information. + """ + # Note: diagonal always operates on the last two axes, whereas np.diagonal + # operates on the first two axes by default + return Array._new(np.diagonal(x._array, offset=offset, axis1=-2, axis2=-1)) + + +# Note: the keyword argument name upper is different from np.linalg.eigh +def eigh(x: Array, /) -> EighResult: + """ + Array API compatible wrapper for :py:func:`np.linalg.eigh `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.eigh. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in eigh') + + # Note: the return type here is a namedtuple, which is different from + # np.eigh, which only returns a tuple. + return EighResult(*map(Array._new, np.linalg.eigh(x._array))) + + +# Note: the keyword argument name upper is different from np.linalg.eigvalsh +def eigvalsh(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.linalg.eigvalsh `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.eigvalsh. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in eigvalsh') + + return Array._new(np.linalg.eigvalsh(x._array)) + +def inv(x: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.linalg.inv `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.inv. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in inv') + + return Array._new(np.linalg.inv(x._array)) + + +# Note: matmul is the numpy top-level namespace but not in np.linalg +def matmul(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.matmul `. + + See its docstring for more information. + """ + # Note: the restriction to numeric dtypes only is different from + # np.matmul. + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in matmul') + + return Array._new(np.matmul(x1._array, x2._array)) + + +# Note: the name here is different from norm(). The array API norm is split +# into matrix_norm and vector_norm(). + +# The type for ord should be Optional[Union[int, float, Literal[np.inf, +# -np.inf, 'fro', 'nuc']]], but Literal does not support floating-point +# literals. +def matrix_norm(x: Array, /, *, keepdims: bool = False, ord: Optional[Union[int, float, Literal['fro', 'nuc']]] = 'fro') -> Array: + """ + Array API compatible wrapper for :py:func:`np.linalg.norm `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.norm. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in matrix_norm') + + return Array._new(np.linalg.norm(x._array, axis=(-2, -1), keepdims=keepdims, ord=ord)) + + +def matrix_power(x: Array, n: int, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.matrix_power `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.matrix_power. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed for the first argument of matrix_power') + + # np.matrix_power already checks if n is an integer + return Array._new(np.linalg.matrix_power(x._array, n)) + +# Note: the keyword argument name rtol is different from np.linalg.matrix_rank +def matrix_rank(x: Array, /, *, rtol: Optional[Union[float, Array]] = None) -> Array: + """ + Array API compatible wrapper for :py:func:`np.matrix_rank `. + + See its docstring for more information. + """ + # Note: this is different from np.linalg.matrix_rank, which supports 1 + # dimensional arrays. + if x.ndim < 2: + raise np.linalg.LinAlgError("1-dimensional array given. Array must be at least two-dimensional") + S = np.linalg.svd(x._array, compute_uv=False) + if rtol is None: + tol = S.max(axis=-1, keepdims=True) * max(x.shape[-2:]) * np.finfo(S.dtype).eps + else: + if isinstance(rtol, Array): + rtol = rtol._array + # Note: this is different from np.linalg.matrix_rank, which does not multiply + # the tolerance by the largest singular value. + tol = S.max(axis=-1, keepdims=True)*np.asarray(rtol)[..., np.newaxis] + return Array._new(np.count_nonzero(S > tol, axis=-1)) + + +# Note: this function is new in the array API spec. Unlike transpose, it only +# transposes the last two axes. +def matrix_transpose(x: Array, /) -> Array: + if x.ndim < 2: + raise ValueError("x must be at least 2-dimensional for matrix_transpose") + return Array._new(np.swapaxes(x._array, -1, -2)) + +# Note: outer is the numpy top-level namespace, not np.linalg +def outer(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.outer `. + + See its docstring for more information. + """ + # Note: the restriction to numeric dtypes only is different from + # np.outer. + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in outer') + + # Note: the restriction to only 1-dim arrays is different from np.outer + if x1.ndim != 1 or x2.ndim != 1: + raise ValueError('The input arrays to outer must be 1-dimensional') + + return Array._new(np.outer(x1._array, x2._array)) + +# Note: the keyword argument name rtol is different from np.linalg.pinv +def pinv(x: Array, /, *, rtol: Optional[Union[float, Array]] = None) -> Array: + """ + Array API compatible wrapper for :py:func:`np.linalg.pinv `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.pinv. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in pinv') + + # Note: this is different from np.linalg.pinv, which does not multiply the + # default tolerance by max(M, N). + if rtol is None: + rtol = max(x.shape[-2:]) * np.finfo(x.dtype).eps + return Array._new(np.linalg.pinv(x._array, rcond=rtol)) + +def qr(x: Array, /, *, mode: Literal['reduced', 'complete'] = 'reduced') -> QRResult: + """ + Array API compatible wrapper for :py:func:`np.linalg.qr `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.qr. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in qr') + + # Note: the return type here is a namedtuple, which is different from + # np.linalg.qr, which only returns a tuple. + return QRResult(*map(Array._new, np.linalg.qr(x._array, mode=mode))) + +def slogdet(x: Array, /) -> SlogdetResult: + """ + Array API compatible wrapper for :py:func:`np.linalg.slogdet `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.slogdet. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in slogdet') + + # Note: the return type here is a namedtuple, which is different from + # np.linalg.slogdet, which only returns a tuple. + return SlogdetResult(*map(Array._new, np.linalg.slogdet(x._array))) + +# Note: unlike np.linalg.solve, the array API solve() only accepts x2 as a +# vector when it is exactly 1-dimensional. All other cases treat x2 as a stack +# of matrices. The np.linalg.solve behavior of allowing stacks of both +# matrices and vectors is ambiguous c.f. +# https://github.com/numpy/numpy/issues/15349 and +# https://github.com/data-apis/array-api/issues/285. + +# To workaround this, the below is the code from np.linalg.solve except +# only calling solve1 in the exactly 1D case. +def _solve(a, b): + from ..linalg.linalg import (_makearray, _assert_stacked_2d, + _assert_stacked_square, _commonType, + isComplexType, get_linalg_error_extobj, + _raise_linalgerror_singular) + from ..linalg import _umath_linalg + + a, _ = _makearray(a) + _assert_stacked_2d(a) + _assert_stacked_square(a) + b, wrap = _makearray(b) + t, result_t = _commonType(a, b) + + # This part is different from np.linalg.solve + if b.ndim == 1: + gufunc = _umath_linalg.solve1 + else: + gufunc = _umath_linalg.solve + + # This does nothing currently but is left in because it will be relevant + # when complex dtype support is added to the spec in 2022. + signature = 'DD->D' if isComplexType(t) else 'dd->d' + extobj = get_linalg_error_extobj(_raise_linalgerror_singular) + r = gufunc(a, b, signature=signature, extobj=extobj) + + return wrap(r.astype(result_t, copy=False)) + +def solve(x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.linalg.solve `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.solve. + if x1.dtype not in _floating_dtypes or x2.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in solve') + + return Array._new(_solve(x1._array, x2._array)) + +def svd(x: Array, /, *, full_matrices: bool = True) -> SVDResult: + """ + Array API compatible wrapper for :py:func:`np.linalg.svd `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.svd. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in svd') + + # Note: the return type here is a namedtuple, which is different from + # np.svd, which only returns a tuple. + return SVDResult(*map(Array._new, np.linalg.svd(x._array, full_matrices=full_matrices))) + +# Note: svdvals is not in NumPy (but it is in SciPy). It is equivalent to +# np.linalg.svd(compute_uv=False). +def svdvals(x: Array, /) -> Union[Array, Tuple[Array, ...]]: + return Array._new(np.linalg.svd(x._array, compute_uv=False)) + +# Note: tensordot is the numpy top-level namespace but not in np.linalg + +# Note: axes must be a tuple, unlike np.tensordot where it can be an array or array-like. +def tensordot(x1: Array, x2: Array, /, *, axes: Union[int, Tuple[Sequence[int], Sequence[int]]] = 2) -> Array: + # Note: the restriction to numeric dtypes only is different from + # np.tensordot. + if x1.dtype not in _numeric_dtypes or x2.dtype not in _numeric_dtypes: + raise TypeError('Only numeric dtypes are allowed in tensordot') + + return Array._new(np.tensordot(x1._array, x2._array, axes=axes)) + +# Note: trace is the numpy top-level namespace, not np.linalg +def trace(x: Array, /, *, offset: int = 0) -> Array: + """ + Array API compatible wrapper for :py:func:`np.trace `. + + See its docstring for more information. + """ + # Note: trace always operates on the last two axes, whereas np.trace + # operates on the first two axes by default + return Array._new(np.asarray(np.trace(x._array, offset=offset, axis1=-2, axis2=-1))) + +# Note: vecdot is not in NumPy +def vecdot(x1: Array, x2: Array, /, *, axis: int = -1) -> Array: + return tensordot(x1, x2, axes=((axis,), (axis,))) + + +# Note: the name here is different from norm(). The array API norm is split +# into matrix_norm and vector_norm(). + +# The type for ord should be Optional[Union[int, float, Literal[np.inf, +# -np.inf]]] but Literal does not support floating-point literals. +def vector_norm(x: Array, /, *, axis: Optional[Union[int, Tuple[int, int]]] = None, keepdims: bool = False, ord: Optional[Union[int, float]] = 2) -> Array: + """ + Array API compatible wrapper for :py:func:`np.linalg.norm `. + + See its docstring for more information. + """ + # Note: the restriction to floating-point dtypes only is different from + # np.linalg.norm. + if x.dtype not in _floating_dtypes: + raise TypeError('Only floating-point dtypes are allowed in norm') + + a = x._array + if axis is None: + a = a.flatten() + axis = 0 + elif isinstance(axis, tuple): + # Note: The axis argument supports any number of axes, whereas norm() + # only supports a single axis for vector norm. + rest = tuple(i for i in range(a.ndim) if i not in axis) + newshape = axis + rest + a = np.transpose(a, newshape).reshape((np.prod([a.shape[i] for i in axis]), *[a.shape[i] for i in rest])) + axis = 0 + return Array._new(np.linalg.norm(a, axis=axis, keepdims=keepdims, ord=ord)) + + +__all__ = ['cholesky', 'cross', 'det', 'diagonal', 'eigh', 'eigvalsh', 'inv', 'matmul', 'matrix_norm', 'matrix_power', 'matrix_rank', 'matrix_transpose', 'outer', 'pinv', 'qr', 'slogdet', 'solve', 'svd', 'svdvals', 'tensordot', 'trace', 'vecdot', 'vector_norm'] diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py index fa29c75b5..0754df402 100644 --- a/numpy/tests/test_public_api.py +++ b/numpy/tests/test_public_api.py @@ -123,6 +123,7 @@ def test_NPY_NO_EXPORT(): # private, to clean up our public API and avoid confusion. PUBLIC_MODULES = ['numpy.' + s for s in [ "array_api", + "array_api.linalg", "ctypeslib", "distutils", "distutils.cpuinfo", -- cgit v1.2.1 From a565be5792bac34938f477e27bc085e65bd3191d Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sun, 14 Nov 2021 21:25:27 +0000 Subject: MAINT: Fix build issues --- numpy/f2py/__init__.py | 2 +- numpy/f2py/f2py2e.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/f2py/__init__.py b/numpy/f2py/__init__.py index a0fb73619..f147f1b97 100644 --- a/numpy/f2py/__init__.py +++ b/numpy/f2py/__init__.py @@ -71,7 +71,7 @@ def compile(source, Examples -------- - .. literalinclude:: code/results/compile_session.dat + .. literalinclude:: ../../source/f2py/code/results/compile_session.dat :language: python """ diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py index 605495574..4fbb27c7e 100755 --- a/numpy/f2py/f2py2e.py +++ b/numpy/f2py/f2py2e.py @@ -416,7 +416,7 @@ def run_main(comline_list): Examples -------- - .. literalinclude:: code/results/run_main_session.dat + .. literalinclude:: ../../source/f2py/code/results/run_main_session.dat :language: python """ -- cgit v1.2.1 From 9c8c88eb68565b80329503f6225b1ee6ebef2356 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sun, 7 Nov 2021 14:38:03 -0700 Subject: MAINT: Prepare for branching maintenace/1.22.x - Update .mailmap - Update NPY_1_22_API_VERSION in numpyconfig.h --- numpy/core/code_generators/cversions.txt | 4 +++- numpy/core/include/numpy/numpyconfig.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/code_generators/cversions.txt b/numpy/core/code_generators/cversions.txt index f0a128d3d..e7b3ef697 100644 --- a/numpy/core/code_generators/cversions.txt +++ b/numpy/core/code_generators/cversions.txt @@ -1,6 +1,8 @@ # Hash below were defined from numpy_api_order.txt and ufunc_api_order.txt # When adding a new version here for a new minor release, also add the same -# version as NPY_x_y_API_VERSION in numpyconfig.h +# version as NPY_x_y_API_VERSION in numpyconfig.h and C_API_VERSION in +# setup_common.py. + 0x00000001 = 603580d224763e58c5e7147f804dc0f5 0x00000002 = 8ecb29306758515ae69749c803a75da1 0x00000003 = bf22c0d05b31625d2a7015988d61ce5a diff --git a/numpy/core/include/numpy/numpyconfig.h b/numpy/core/include/numpy/numpyconfig.h index 1c3686769..f761555b9 100644 --- a/numpy/core/include/numpy/numpyconfig.h +++ b/numpy/core/include/numpy/numpyconfig.h @@ -56,6 +56,6 @@ #define NPY_1_19_API_VERSION 0x00000008 #define NPY_1_20_API_VERSION 0x0000000e #define NPY_1_21_API_VERSION 0x0000000e -#define NPY_1_22_API_VERSION 0x0000000e +#define NPY_1_22_API_VERSION 0x0000000f #endif /* NUMPY_CORE_INCLUDE_NUMPY_NPY_NUMPYCONFIG_H_ */ -- cgit v1.2.1 From 31fbd0266df082240dc904c681e25f779d7fd025 Mon Sep 17 00:00:00 2001 From: Eero Vaher Date: Mon, 15 Nov 2021 18:36:58 +0100 Subject: DOC: Fix formatting of a code example One of the code examples in the docstring of `numpy.random.Generator.multivariate_normal()` was not being displayed properly in the built documentation. --- numpy/random/_generator.pyx | 1 + 1 file changed, 1 insertion(+) (limited to 'numpy') diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index 2134f0e24..391987a1e 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -3567,6 +3567,7 @@ cdef class Generator: (3, 3, 2) We can use a different method other than the default to factorize cov: + >>> y = rng.multivariate_normal(mean, cov, (3, 3), method='cholesky') >>> y.shape (3, 3, 2) -- cgit v1.2.1 From 546c47adae1066411ff7a3e3da5d758236ee90cf Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Mon, 15 Nov 2021 16:13:50 -0600 Subject: DOC: Fixups for interpolation rename comments from review Co-authored-by: Charles Harris --- numpy/core/tests/test_deprecations.py | 2 +- numpy/lib/function_base.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 93e07216b..94583a5ee 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -1246,7 +1246,7 @@ class TestQuantileInterpolationDeprecation(_DeprecationTestCase): [np.percentile, np.quantile, np.nanpercentile, np.nanquantile]) def test_both_passed(self, func): with warnings.catch_warnings(): - # catch the warning, but make sure it does not raise: + # catch the DeprecationWarning so that it does not raise: warnings.simplefilter("always", DeprecationWarning) with pytest.raises(TypeError): func([0., 1.], 0., interpolation="nearest", method="nearest") diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 6d84627cd..a215f63d3 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -61,7 +61,7 @@ __all__ = [ # is made of a integer part (a.k.a 'i' or 'left') and a fractional part # (a.k.a 'g' or 'gamma') # -# Each _QuantileMethods has two properties +# Each method in _QuantileMethods has two properties # get_virtual_index : Callable # The function used to compute the virtual_index. # fix_gamma : Callable -- cgit v1.2.1 From 9b1bd0d60b976e3e130cbb6d1eac84c9c6835adb Mon Sep 17 00:00:00 2001 From: HowJMay Date: Sun, 14 Nov 2021 02:32:29 +0800 Subject: ENH, SIMD: add new universal intrinsics for trunc --- numpy/core/src/_simd/_simd.dispatch.c.src | 4 ++-- numpy/core/src/common/simd/avx2/math.h | 4 ++++ numpy/core/src/common/simd/avx512/math.h | 4 ++++ numpy/core/src/common/simd/neon/math.h | 33 +++++++++++++++++++++++++++++++ numpy/core/src/common/simd/sse/math.h | 28 ++++++++++++++++++++++++++ numpy/core/src/common/simd/vsx/math.h | 4 ++++ numpy/core/tests/test_simd.py | 16 +++++++++------ 7 files changed, 85 insertions(+), 8 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/_simd/_simd.dispatch.c.src b/numpy/core/src/_simd/_simd.dispatch.c.src index 5c494ae7a..84de9a059 100644 --- a/numpy/core/src/_simd/_simd.dispatch.c.src +++ b/numpy/core/src/_simd/_simd.dispatch.c.src @@ -381,7 +381,7 @@ SIMD_IMPL_INTRIN_1(sumup_@sfx@, @esfx@, v@sfx@) ***************************/ #if @fp_only@ /**begin repeat1 - * #intrin = sqrt, recip, abs, square, ceil# + * #intrin = sqrt, recip, abs, square, ceil, trunc# */ SIMD_IMPL_INTRIN_1(@intrin@_@sfx@, v@sfx@, v@sfx@) /**end repeat1**/ @@ -615,7 +615,7 @@ SIMD_INTRIN_DEF(sumup_@sfx@) ***************************/ #if @fp_only@ /**begin repeat1 - * #intrin = sqrt, recip, abs, square, ceil# + * #intrin = sqrt, recip, abs, square, ceil, trunc# */ SIMD_INTRIN_DEF(@intrin@_@sfx@) /**end repeat1**/ diff --git a/numpy/core/src/common/simd/avx2/math.h b/numpy/core/src/common/simd/avx2/math.h index b1f3915a6..ec15e50e1 100644 --- a/numpy/core/src/common/simd/avx2/math.h +++ b/numpy/core/src/common/simd/avx2/math.h @@ -109,4 +109,8 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b) #define npyv_ceil_f32 _mm256_ceil_ps #define npyv_ceil_f64 _mm256_ceil_pd +// trunc +#define npyv_trunc_f32(A) _mm256_round_ps(A, _MM_FROUND_TO_ZERO) +#define npyv_trunc_f64(A) _mm256_round_pd(A, _MM_FROUND_TO_ZERO) + #endif // _NPY_SIMD_AVX2_MATH_H diff --git a/numpy/core/src/common/simd/avx512/math.h b/numpy/core/src/common/simd/avx512/math.h index c4f8d3410..f30e50ad0 100644 --- a/numpy/core/src/common/simd/avx512/math.h +++ b/numpy/core/src/common/simd/avx512/math.h @@ -116,4 +116,8 @@ NPY_FINLINE npyv_f64 npyv_minp_f64(npyv_f64 a, npyv_f64 b) #define npyv_ceil_f32(A) _mm512_roundscale_ps(A, _MM_FROUND_TO_POS_INF) #define npyv_ceil_f64(A) _mm512_roundscale_pd(A, _MM_FROUND_TO_POS_INF) +// trunc +#define npyv_trunc_f32(A) _mm512_roundscale_ps(A, _MM_FROUND_TO_ZERO) +#define npyv_trunc_f64(A) _mm512_roundscale_pd(A, _MM_FROUND_TO_ZERO) + #endif // _NPY_SIMD_AVX512_MATH_H diff --git a/numpy/core/src/common/simd/neon/math.h b/numpy/core/src/common/simd/neon/math.h index 38c3899e4..19e5cd846 100644 --- a/numpy/core/src/common/simd/neon/math.h +++ b/numpy/core/src/common/simd/neon/math.h @@ -190,4 +190,37 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b) #define npyv_ceil_f64 vrndpq_f64 #endif // NPY_SIMD_F64 +// trunc +#ifdef NPY_HAVE_ASIMD + #define npyv_trunc_f32 vrndq_f32 +#else + NPY_FINLINE npyv_f32 npyv_trunc_f32(npyv_f32 a) + { + const npyv_s32 szero = vreinterpretq_s32_f32(vdupq_n_f32(-0.0f)); + const npyv_s32 max_int = vdupq_n_s32(0x7fffffff); + /** + * On armv7, vcvtq.f32 handles special cases as follows: + * NaN return 0 + * +inf or +outrange return 0x80000000(-0.0f) + * -inf or -outrange return 0x7fffffff(nan) + */ + npyv_s32 roundi = vcvtq_s32_f32(a); + npyv_f32 round = vcvtq_f32_s32(roundi); + // respect signed zero, e.g. -0.5 -> -0.0 + npyv_f32 rzero = vreinterpretq_f32_s32(vorrq_s32( + vreinterpretq_s32_f32(round), + vandq_s32(vreinterpretq_s32_f32(a), szero) + )); + // if nan or overflow return a + npyv_u32 nnan = npyv_notnan_f32(a); + npyv_u32 overflow = vorrq_u32( + vceqq_s32(roundi, szero), vceqq_s32(roundi, max_int) + ); + return vbslq_f32(vbicq_u32(nnan, overflow), rzero, a); + } +#endif +#if NPY_SIMD_F64 + #define npyv_trunc_f64 vrndq_f64 +#endif // NPY_SIMD_F64 + #endif // _NPY_SIMD_NEON_MATH_H diff --git a/numpy/core/src/common/simd/sse/math.h b/numpy/core/src/common/simd/sse/math.h index 02eb06a29..5daf7711e 100644 --- a/numpy/core/src/common/simd/sse/math.h +++ b/numpy/core/src/common/simd/sse/math.h @@ -174,4 +174,32 @@ NPY_FINLINE npyv_s64 npyv_min_s64(npyv_s64 a, npyv_s64 b) } #endif +// trunc +#ifdef NPY_HAVE_SSE41 + #define npyv_trunc_f32(A) _mm_round_ps(A, _MM_FROUND_TO_ZERO) + #define npyv_trunc_f64(A) _mm_round_pd(A, _MM_FROUND_TO_ZERO) +#else + NPY_FINLINE npyv_f32 npyv_trunc_f32(npyv_f32 a) + { + const npyv_f32 szero = _mm_set1_ps(-0.0f); + npyv_s32 roundi = _mm_cvttps_epi32(a); + npyv_f32 trunc = _mm_cvtepi32_ps(roundi); + // respect signed zero, e.g. -0.5 -> -0.0 + npyv_f32 rzero = _mm_or_ps(trunc, _mm_and_ps(a, szero)); + // if overflow return a + return npyv_select_f32(_mm_cmpeq_epi32(roundi, _mm_castps_si128(szero)), a, rzero); + } + NPY_FINLINE npyv_f64 npyv_trunc_f64(npyv_f64 a) + { + const npyv_f64 szero = _mm_set1_pd(-0.0); + const npyv_f64 one = _mm_set1_pd(1.0); + const npyv_f64 two_power_52 = _mm_set1_pd(0x10000000000000); + npyv_f64 abs_a = npyv_abs_f64(a); + // round by add magic number 2^52 + npyv_f64 abs_round = _mm_sub_pd(_mm_add_pd(abs_a, two_power_52), two_power_52); + npyv_f64 subtrahend = _mm_and_pd(_mm_cmpgt_pd(abs_round, abs_a), one); + return _mm_or_pd(_mm_sub_pd(abs_round, subtrahend), _mm_and_pd(a, szero)); + } +#endif + #endif // _NPY_SIMD_SSE_MATH_H diff --git a/numpy/core/src/common/simd/vsx/math.h b/numpy/core/src/common/simd/vsx/math.h index f387dac4d..d138cae8a 100644 --- a/numpy/core/src/common/simd/vsx/math.h +++ b/numpy/core/src/common/simd/vsx/math.h @@ -73,4 +73,8 @@ NPY_FINLINE npyv_f64 npyv_square_f64(npyv_f64 a) #define npyv_ceil_f32 vec_ceil #define npyv_ceil_f64 vec_ceil +// trunc +#define npyv_trunc_f32 vec_trunc +#define npyv_trunc_f64 vec_trunc + #endif // _NPY_SIMD_VSX_MATH_H diff --git a/numpy/core/tests/test_simd.py b/numpy/core/tests/test_simd.py index 379fef8af..12a67c44d 100644 --- a/numpy/core/tests/test_simd.py +++ b/numpy/core/tests/test_simd.py @@ -330,12 +330,15 @@ class _SIMD_FP(_Test_Utility): square = self.square(vdata) assert square == data_square - @pytest.mark.parametrize("intrin, func", [("self.ceil", math.ceil)]) + @pytest.mark.parametrize("intrin, func", [("self.ceil", math.ceil), + ("self.trunc", math.trunc)]) def test_rounding(self, intrin, func): """ Test intrinsics: npyv_ceil_##SFX + npyv_trunc_##SFX """ + intrin_name = intrin intrin = eval(intrin) pinf, ninf, nan = self._pinfinity(), self._ninfinity(), self._nan() # special cases @@ -352,11 +355,12 @@ class _SIMD_FP(_Test_Utility): _round = intrin(vdata) assert _round == data_round # signed zero - for w in (-0.25, -0.30, -0.45): - _round = self._to_unsigned(intrin(self.setall(w))) - data_round = self._to_unsigned(self.setall(-0.0)) - assert _round == data_round - + if "ceil" in intrin_name or "trunc" in intrin_name: + for w in (-0.25, -0.30, -0.45): + _round = self._to_unsigned(intrin(self.setall(w))) + data_round = self._to_unsigned(self.setall(-0.0)) + assert _round == data_round + def test_max(self): """ Test intrinsics: -- cgit v1.2.1 From b31a3a3fb05910abc2ee55d63255efdb2a2c383e Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Tue, 16 Nov 2021 12:37:19 -0700 Subject: REV: Add MaskedArray creation from non nd-array back in (#20386) * REV: Add MaskedArray creation from non nd-array back in This code path was removed in beacb39. This adds back in the MaskedArray portion of that commit. A test with a Quantity-like (non inherited, but still acts like a MaskedArray) class for this case. * Update numpy/ma/core.py Co-authored-by: Sebastian Berg --- numpy/ma/core.py | 6 ++++++ numpy/ma/tests/test_subclassing.py | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'numpy') diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 036d6312c..491c2c605 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -2837,6 +2837,12 @@ class MaskedArray(ndarray): _data = ndarray.view(_data, type(data)) else: _data = ndarray.view(_data, cls) + + # Handle the case where data is not a subclass of ndarray, but + # still has the _mask attribute like MaskedArrays + if hasattr(data, '_mask') and not isinstance(data, ndarray): + _data._mask = data._mask + # FIXME: should we set `_data._sharedmask = True`? # Process mask. # Type of the mask mdtype = make_mask_descr(_data.dtype) diff --git a/numpy/ma/tests/test_subclassing.py b/numpy/ma/tests/test_subclassing.py index 1af539625..83a9b2f51 100644 --- a/numpy/ma/tests/test_subclassing.py +++ b/numpy/ma/tests/test_subclassing.py @@ -343,3 +343,45 @@ class TestSubclassing: diff2 = arr1 - arr2 assert_('info' in diff2._optinfo) assert_(diff2._optinfo['info'] == 'test') + + +class ArrayNoInheritance: + """Quantity-like class that does not inherit from ndarray""" + def __init__(self, data, units): + self.magnitude = data + self.units = units + + def __getattr__(self, attr): + return getattr(self.magnitude, attr) + + +def test_array_no_inheritance(): + data_masked = np.ma.array([1, 2, 3], mask=[True, False, True]) + data_masked_units = ArrayNoInheritance(data_masked, 'meters') + + # Get the masked representation of the Quantity-like class + new_array = np.ma.array(data_masked_units) + assert_equal(data_masked.data, new_array.data) + assert_equal(data_masked.mask, new_array.mask) + # Test sharing the mask + data_masked.mask = [True, False, False] + assert_equal(data_masked.mask, new_array.mask) + assert_(new_array.sharedmask) + + # Get the masked representation of the Quantity-like class + new_array = np.ma.array(data_masked_units, copy=True) + assert_equal(data_masked.data, new_array.data) + assert_equal(data_masked.mask, new_array.mask) + # Test that the mask is not shared when copy=True + data_masked.mask = [True, False, True] + assert_equal([True, False, False], new_array.mask) + assert_(not new_array.sharedmask) + + # Get the masked representation of the Quantity-like class + new_array = np.ma.array(data_masked_units, keep_mask=False) + assert_equal(data_masked.data, new_array.data) + # The change did not affect the original mask + assert_equal(data_masked.mask, [True, False, True]) + # Test that the mask is False and not shared when keep_mask=False + assert_(not new_array.mask) + assert_(not new_array.sharedmask) -- cgit v1.2.1 From fdf884efb9418a7088aef291ffa47ce8c709a8cb Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 16 Nov 2021 15:20:42 -0700 Subject: REL: Prepare main for NumPy 1.23.0 development --- numpy/core/code_generators/cversions.txt | 1 + numpy/core/include/numpy/numpyconfig.h | 1 + numpy/core/setup_common.py | 1 + 3 files changed, 3 insertions(+) (limited to 'numpy') diff --git a/numpy/core/code_generators/cversions.txt b/numpy/core/code_generators/cversions.txt index e7b3ef697..e1ee8a860 100644 --- a/numpy/core/code_generators/cversions.txt +++ b/numpy/core/code_generators/cversions.txt @@ -61,4 +61,5 @@ 0x0000000e = 17a0f366e55ec05e5c5c149123478452 # Version 15 (NumPy 1.22) Configurable memory allocations +# Version 14 (NumPy 1.23) No change. 0x0000000f = b8783365b873681cd204be50cdfb448d diff --git a/numpy/core/include/numpy/numpyconfig.h b/numpy/core/include/numpy/numpyconfig.h index f761555b9..4eac083e7 100644 --- a/numpy/core/include/numpy/numpyconfig.h +++ b/numpy/core/include/numpy/numpyconfig.h @@ -57,5 +57,6 @@ #define NPY_1_20_API_VERSION 0x0000000e #define NPY_1_21_API_VERSION 0x0000000e #define NPY_1_22_API_VERSION 0x0000000f +#define NPY_1_23_API_VERSION 0x0000000f #endif /* NUMPY_CORE_INCLUDE_NUMPY_NPY_NUMPYCONFIG_H_ */ diff --git a/numpy/core/setup_common.py b/numpy/core/setup_common.py index 70e8fc897..772c87c96 100644 --- a/numpy/core/setup_common.py +++ b/numpy/core/setup_common.py @@ -44,6 +44,7 @@ C_ABI_VERSION = 0x01000009 # 0x0000000e - 1.20.x # 0x0000000e - 1.21.x # 0x0000000f - 1.22.x +# 0x0000000f - 1.23.x C_API_VERSION = 0x0000000f class MismatchCAPIWarning(Warning): -- cgit v1.2.1 From 2113cad8cbb4c4bc9469e0c94025bd7cbbe6f2d6 Mon Sep 17 00:00:00 2001 From: Thomas Green Date: Tue, 16 Nov 2021 22:36:18 +0000 Subject: Update cpu_asimdfhm.c Updated `vfmlal_low_u32` and `vfmlslq_high_u32` to their `f16` new names. Described here: https://www.mail-archive.com/gcc-bugs@gcc.gnu.org/msg664008.html Many of the intrinsics had names updated. Supposedly previous specifications were not published so old names not required. --- numpy/distutils/checks/cpu_asimdfhm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/distutils/checks/cpu_asimdfhm.c b/numpy/distutils/checks/cpu_asimdfhm.c index bb437aa40..cb49751c4 100644 --- a/numpy/distutils/checks/cpu_asimdfhm.c +++ b/numpy/distutils/checks/cpu_asimdfhm.c @@ -10,8 +10,8 @@ int main(void) float32x4_t vf = vdupq_n_f32(1.0f); float32x2_t vlf = vdup_n_f32(1.0f); - int ret = (int)vget_lane_f32(vfmlal_low_u32(vlf, vlhp, vlhp), 0); - ret += (int)vgetq_lane_f32(vfmlslq_high_u32(vf, vhp, vhp), 0); + int ret = (int)vget_lane_f32(vfmlal_low_f16(vlf, vlhp, vlhp), 0); + ret += (int)vgetq_lane_f32(vfmlslq_high_f16(vf, vhp, vhp), 0); return ret; } -- cgit v1.2.1 From 0cae372f06576059e77a6306a1cda5ab9e931c3d Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 17 Nov 2021 14:59:44 -0800 Subject: BLD: Fix Macos Builds [wheel build] --- numpy/distutils/misc_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy') diff --git a/numpy/distutils/misc_util.py b/numpy/distutils/misc_util.py index f0f9b4bd7..b68b3af47 100644 --- a/numpy/distutils/misc_util.py +++ b/numpy/distutils/misc_util.py @@ -2481,7 +2481,7 @@ def get_build_architecture(): return get_build_architecture() -_cxx_ignore_flags = {'-Werror=implicit-function-declaration'} +_cxx_ignore_flags = {'-Werror=implicit-function-declaration', '-std=c99'} def sanitize_cxx_flags(cxxflags): -- cgit v1.2.1 From d4243a381778109ba2522ed7d155e1338e4ca6de Mon Sep 17 00:00:00 2001 From: Soumya <14910227+soumyas567@users.noreply.github.com> Date: Thu, 18 Nov 2021 10:52:53 +0000 Subject: DOC: np.fromfunction documentation not clear (#20396) * DOC: fromfunction documentation edited * DOC: Example added in fromfunction documentation Added one more example to numpy.fromfunction documentation that will help to clarify how parameters are passed to the function. --- numpy/core/numeric.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'numpy') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 344d40d93..014fa0a39 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1829,6 +1829,14 @@ def fromfunction(function, shape, *, dtype=float, like=None, **kwargs): Examples -------- + >>> np.fromfunction(lambda i, j: i, (2, 2), dtype=float) + array([[0., 0.], + [1., 1.]]) + + >>> np.fromfunction(lambda i, j: j, (2, 2), dtype=float) + array([[0., 1.], + [0., 1.]]) + >>> np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) array([[ True, False, False], [False, True, False], -- cgit v1.2.1 From 258ce2523ffad99be69afbd421d540086cb6bf61 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 18 Nov 2021 07:58:28 -0800 Subject: MAINT: Fix METH_NOARGS function signatures (#20368) The METH_NOARGS calling convention is required to take a second PyObject* which will always be NULL. This is a continuation of #19058 --- numpy/core/src/multiarray/methods.c | 2 +- numpy/core/src/multiarray/multiarraymodule.c | 2 +- numpy/core/src/multiarray/nditer_pywrap.c | 20 +++++++++++--------- numpy/core/src/multiarray/scalartypes.c.src | 10 +++++----- 4 files changed, 18 insertions(+), 16 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 627096b3c..b0b6f42f1 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -2246,7 +2246,7 @@ array_dumps(PyArrayObject *self, PyObject *args, PyObject *kwds) static PyObject * -array_sizeof(PyArrayObject *self) +array_sizeof(PyArrayObject *self, PyObject *NPY_UNUSED(args)) { /* object + dimension and strides */ Py_ssize_t nbytes = Py_TYPE(self)->tp_basicsize + diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index dbf5ab161..cf0160a2b 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4212,7 +4212,7 @@ normalize_axis_index(PyObject *NPY_UNUSED(self), static PyObject * -_reload_guard(PyObject *NPY_UNUSED(self)) { +_reload_guard(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args)) { static int initialized = 0; #if !defined(PYPY_VERSION) diff --git a/numpy/core/src/multiarray/nditer_pywrap.c b/numpy/core/src/multiarray/nditer_pywrap.c index 8e072d5f4..2675496ab 100644 --- a/numpy/core/src/multiarray/nditer_pywrap.c +++ b/numpy/core/src/multiarray/nditer_pywrap.c @@ -1190,7 +1190,7 @@ npyiter_resetbasepointers(NewNpyArrayIterObject *self) } static PyObject * -npyiter_reset(NewNpyArrayIterObject *self) +npyiter_reset(NewNpyArrayIterObject *self, PyObject *NPY_UNUSED(args)) { if (self->iter == NULL) { PyErr_SetString(PyExc_ValueError, @@ -1227,7 +1227,7 @@ npyiter_reset(NewNpyArrayIterObject *self) * copied. */ static PyObject * -npyiter_copy(NewNpyArrayIterObject *self) +npyiter_copy(NewNpyArrayIterObject *self, PyObject *NPY_UNUSED(args)) { NewNpyArrayIterObject *iter; @@ -1263,7 +1263,7 @@ npyiter_copy(NewNpyArrayIterObject *self) } static PyObject * -npyiter_iternext(NewNpyArrayIterObject *self) +npyiter_iternext(NewNpyArrayIterObject *self, PyObject *NPY_UNUSED(args)) { if (self->iter != NULL && self->iternext != NULL && !self->finished && self->iternext(self->iter)) { @@ -1320,7 +1320,8 @@ npyiter_remove_axis(NewNpyArrayIterObject *self, PyObject *args) } static PyObject * -npyiter_remove_multi_index(NewNpyArrayIterObject *self) +npyiter_remove_multi_index( + NewNpyArrayIterObject *self, PyObject *NPY_UNUSED(args)) { if (self->iter == NULL) { PyErr_SetString(PyExc_ValueError, @@ -1345,7 +1346,8 @@ npyiter_remove_multi_index(NewNpyArrayIterObject *self) } static PyObject * -npyiter_enable_external_loop(NewNpyArrayIterObject *self) +npyiter_enable_external_loop( + NewNpyArrayIterObject *self, PyObject *NPY_UNUSED(args)) { if (self->iter == NULL) { PyErr_SetString(PyExc_ValueError, @@ -1370,7 +1372,7 @@ npyiter_enable_external_loop(NewNpyArrayIterObject *self) } static PyObject * -npyiter_debug_print(NewNpyArrayIterObject *self) +npyiter_debug_print(NewNpyArrayIterObject *self, PyObject *NPY_UNUSED(args)) { if (self->iter != NULL) { NpyIter_DebugPrint(self->iter); @@ -2315,7 +2317,7 @@ npyiter_ass_subscript(NewNpyArrayIterObject *self, PyObject *op, } static PyObject * -npyiter_enter(NewNpyArrayIterObject *self) +npyiter_enter(NewNpyArrayIterObject *self, PyObject *NPY_UNUSED(args)) { if (self->iter == NULL) { PyErr_SetString(PyExc_RuntimeError, "operation on non-initialized iterator"); @@ -2326,7 +2328,7 @@ npyiter_enter(NewNpyArrayIterObject *self) } static PyObject * -npyiter_close(NewNpyArrayIterObject *self) +npyiter_close(NewNpyArrayIterObject *self, PyObject *NPY_UNUSED(args)) { NpyIter *iter = self->iter; int ret; @@ -2347,7 +2349,7 @@ static PyObject * npyiter_exit(NewNpyArrayIterObject *self, PyObject *NPY_UNUSED(args)) { /* even if called via exception handling, writeback any data */ - return npyiter_close(self); + return npyiter_close(self, NULL); } static PyMethodDef npyiter_methods[] = { diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src index bbbc5bfa2..db1e49db8 100644 --- a/numpy/core/src/multiarray/scalartypes.c.src +++ b/numpy/core/src/multiarray/scalartypes.c.src @@ -229,7 +229,7 @@ gentype_multiply(PyObject *m1, PyObject *m2) * #convert = Long*8, LongLong*2# */ static PyObject * -@type@_bit_count(PyObject *self) +@type@_bit_count(PyObject *self, PyObject *NPY_UNUSED(args)) { @type@ scalar = PyArrayScalar_VAL(self, @Name@); uint8_t count = npy_popcount@c@(scalar); @@ -1160,7 +1160,7 @@ gentype_size_get(PyObject *NPY_UNUSED(self), void *NPY_UNUSED(ignored)) } static PyObject * -gentype_sizeof(PyObject *self) +gentype_sizeof(PyObject *self, PyObject *NPY_UNUSED(args)) { Py_ssize_t nbytes; PyObject * isz = gentype_itemsize_get(self, NULL); @@ -1918,7 +1918,7 @@ static PyObject * */ /* Heavily copied from the builtin float.as_integer_ratio */ static PyObject * -@name@_as_integer_ratio(PyObject *self) +@name@_as_integer_ratio(PyObject *self, PyObject *NPY_UNUSED(args)) { #if @is_half@ npy_double val = npy_half_to_double(PyArrayScalar_VAL(self, @Name@)); @@ -1999,7 +1999,7 @@ error: * #c = f, f, , l# */ static PyObject * -@name@_is_integer(PyObject *self) +@name@_is_integer(PyObject *self, PyObject *NPY_UNUSED(args)) { #if @is_half@ npy_double val = npy_half_to_double(PyArrayScalar_VAL(self, @Name@)); @@ -2022,7 +2022,7 @@ static PyObject * /**end repeat**/ static PyObject * -integer_is_integer(PyObject *self) { +integer_is_integer(PyObject *self, PyObject *NPY_UNUSED(args)) { Py_RETURN_TRUE; } -- cgit v1.2.1 From ee6a23b1186343394d4e927b786c0c53e9446034 Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Thu, 18 Nov 2021 20:25:46 +0200 Subject: DEP: remove allocation_tracking, deprecate PyDataMem_SetEventHook (#20394) * DEP: remove allocation_tracking * DOC: add release note * DEP: deprecate PyDataMem_SetEventHook * DOC: fix name of release note * fixes from review * DOC: document deprecation of PyDataMem_EventHookFunc --- numpy/core/src/multiarray/alloc.c | 45 +++++++++++++++++++---------------- numpy/core/tests/test_deprecations.py | 22 ++++++++++++++++- numpy/core/tests/test_multiarray.py | 12 ---------- 3 files changed, 46 insertions(+), 33 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/alloc.c b/numpy/core/src/multiarray/alloc.c index 0a694cf62..94a7daa83 100644 --- a/numpy/core/src/multiarray/alloc.c +++ b/numpy/core/src/multiarray/alloc.c @@ -186,6 +186,24 @@ npy_free_cache_dim(void * p, npy_uintp sz) &PyArray_free); } +/* Similar to array_dealloc in arrayobject.c */ +static NPY_INLINE void +WARN_NO_RETURN(PyObject* warning, const char * msg) { + if (PyErr_WarnEx(warning, msg, 1) < 0) { + PyObject * s; + + s = PyUnicode_FromString("PyDataMem_UserFREE"); + if (s) { + PyErr_WriteUnraisable(s); + Py_DECREF(s); + } + else { + PyErr_WriteUnraisable(Py_None); + } + } +} + + /* malloc/free/realloc hook */ NPY_NO_EXPORT PyDataMem_EventHookFunc *_PyDataMem_eventhook = NULL; @@ -210,6 +228,8 @@ NPY_NO_EXPORT void *_PyDataMem_eventhook_user_data = NULL; * operations that might cause new allocation events (such as the * creation/destruction numpy objects, or creating/destroying Python * objects which might cause a gc) + * + * Deprecated in 1.23 */ NPY_NO_EXPORT PyDataMem_EventHookFunc * PyDataMem_SetEventHook(PyDataMem_EventHookFunc *newhook, @@ -218,6 +238,10 @@ PyDataMem_SetEventHook(PyDataMem_EventHookFunc *newhook, PyDataMem_EventHookFunc *temp; NPY_ALLOW_C_API_DEF NPY_ALLOW_C_API + /* 2021-11-18, 1.23 */ + WARN_NO_RETURN(PyExc_DeprecationWarning, + "PyDataMem_SetEventHook is deprecated, use tracemalloc " + "and the 'np.lib.tracemalloc_domain' domain"); temp = _PyDataMem_eventhook; _PyDataMem_eventhook = newhook; if (old_data != NULL) { @@ -435,33 +459,14 @@ PyDataMem_UserNEW_ZEROED(size_t nmemb, size_t size, PyObject *mem_handler) return result; } -/* Similar to array_dealloc in arrayobject.c */ -static NPY_INLINE void -WARN_IN_FREE(PyObject* warning, const char * msg) { - if (PyErr_WarnEx(warning, msg, 1) < 0) { - PyObject * s; - - s = PyUnicode_FromString("PyDataMem_UserFREE"); - if (s) { - PyErr_WriteUnraisable(s); - Py_DECREF(s); - } - else { - PyErr_WriteUnraisable(Py_None); - } - } -} - - NPY_NO_EXPORT void PyDataMem_UserFREE(void *ptr, size_t size, PyObject *mem_handler) { PyDataMem_Handler *handler = (PyDataMem_Handler *) PyCapsule_GetPointer(mem_handler, "mem_handler"); if (handler == NULL) { - WARN_IN_FREE(PyExc_RuntimeWarning, + WARN_NO_RETURN(PyExc_RuntimeWarning, "Could not get pointer to 'mem_handler' from PyCapsule"); - PyErr_Clear(); return; } PyTraceMalloc_Untrack(NPY_TRACE_DOMAIN, (npy_uintp)ptr); diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 94583a5ee..e0b66defc 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -13,7 +13,8 @@ import sys import numpy as np from numpy.testing import ( - assert_raises, assert_warns, assert_, assert_array_equal, SkipTest, KnownFailureException + assert_raises, assert_warns, assert_, assert_array_equal, SkipTest, + KnownFailureException, break_cycles, ) from numpy.core._multiarray_tests import fromstring_null_term_c_api @@ -1250,3 +1251,22 @@ class TestQuantileInterpolationDeprecation(_DeprecationTestCase): warnings.simplefilter("always", DeprecationWarning) with pytest.raises(TypeError): func([0., 1.], 0., interpolation="nearest", method="nearest") + + +class TestMemEventHook(_DeprecationTestCase): + # Deprecated 2021-11-18, NumPy 1.23 + def test_mem_seteventhook(self): + # The actual tests are within the C code in + # multiarray/_multiarray_tests.c.src + import numpy.core._multiarray_tests as ma_tests + with pytest.warns(DeprecationWarning, + match='PyDataMem_SetEventHook is deprecated'): + ma_tests.test_pydatamem_seteventhook_start() + # force an allocation and free of a numpy array + # needs to be larger then limit of small memory cacher in ctors.c + a = np.zeros(1000) + del a + break_cycles() + with pytest.warns(DeprecationWarning, + match='PyDataMem_SetEventHook is deprecated'): + ma_tests.test_pydatamem_seteventhook_end() diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 4413cd0d0..23182470b 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -8187,18 +8187,6 @@ def test_scalar_element_deletion(): assert_raises(ValueError, a[0].__delitem__, 'x') -class TestMemEventHook: - def test_mem_seteventhook(self): - # The actual tests are within the C code in - # multiarray/_multiarray_tests.c.src - _multiarray_tests.test_pydatamem_seteventhook_start() - # force an allocation and free of a numpy array - # needs to be larger then limit of small memory cacher in ctors.c - a = np.zeros(1000) - del a - break_cycles() - _multiarray_tests.test_pydatamem_seteventhook_end() - class TestMapIter: def test_mapiter(self): # The actual tests are within the C code in -- cgit v1.2.1