summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/__init__.py5
-rw-r--r--numpy/__init__.pyi2
-rw-r--r--numpy/_globals.py25
-rw-r--r--numpy/array_api.py24
-rw-r--r--numpy/core/multiarray.pyi5
-rw-r--r--numpy/core/src/multiarray/conversion_utils.c9
-rw-r--r--numpy/core/src/multiarray/methods.c3
-rw-r--r--numpy/core/tests/test_api.py10
-rw-r--r--numpy/core/tests/test_multiarray.py26
-rw-r--r--numpy/tests/test_public_api.py2
10 files changed, 61 insertions, 50 deletions
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 "<no value>"
-_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",