summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/_add_newdocs.py3
-rw-r--r--numpy/core/src/multiarray/ctors.c2
-rw-r--r--numpy/core/tests/test_issue14735.py29
-rw-r--r--numpy/core/tests/test_protocols.py44
4 files changed, 46 insertions, 32 deletions
diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py
index 2f1273904..d552348d0 100644
--- a/numpy/core/_add_newdocs.py
+++ b/numpy/core/_add_newdocs.py
@@ -2507,7 +2507,7 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('T',
add_newdoc('numpy.core.multiarray', 'ndarray', ('__array__',
- """ a.__array__(|dtype) -> reference if type unchanged, copy otherwise.
+ """ a.__array__([dtype], /) -> reference if type unchanged, copy otherwise.
Returns either a new reference to self if dtype is not given or a new array
of provided data type if dtype is different from the current dtype of the
@@ -6871,4 +6871,3 @@ for float_name in ('half', 'single', 'double', 'longdouble'):
>>> np.{ftype}(-.25).as_integer_ratio()
(-1, 4)
""".format(ftype=float_name)))
-
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 7276add75..6921427ce 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -1909,7 +1909,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
/* If the requested dtype is flexible, adapt it */
if (newtype != NULL) {
- newtype = PyArray_AdaptFlexibleDType(op,
+ newtype = PyArray_AdaptFlexibleDType((arr == NULL) ? op : (PyObject *)arr,
(dtype == NULL) ? PyArray_DESCR(arr) : dtype,
newtype);
if (newtype == NULL) {
diff --git a/numpy/core/tests/test_issue14735.py b/numpy/core/tests/test_issue14735.py
deleted file mode 100644
index 6105c8e6a..000000000
--- a/numpy/core/tests/test_issue14735.py
+++ /dev/null
@@ -1,29 +0,0 @@
-import pytest
-import warnings
-import numpy as np
-
-
-class Wrapper:
- def __init__(self, array):
- self.array = array
-
- def __len__(self):
- return len(self.array)
-
- def __getitem__(self, item):
- return type(self)(self.array[item])
-
- def __getattr__(self, name):
- if name.startswith("__array_"):
- warnings.warn("object got converted", UserWarning, stacklevel=1)
-
- return getattr(self.array, name)
-
- def __repr__(self):
- return "<Wrapper({self.array})>".format(self=self)
-
-@pytest.mark.filterwarnings("error")
-def test_getattr_warning():
- array = Wrapper(np.arange(10))
- with pytest.raises(UserWarning, match="object got converted"):
- np.asarray(array)
diff --git a/numpy/core/tests/test_protocols.py b/numpy/core/tests/test_protocols.py
new file mode 100644
index 000000000..55a2bcf72
--- /dev/null
+++ b/numpy/core/tests/test_protocols.py
@@ -0,0 +1,44 @@
+import pytest
+import warnings
+import numpy as np
+
+
+@pytest.mark.filterwarnings("error")
+def test_getattr_warning():
+ # issue gh-14735: make sure we clear only getattr errors, and let warnings
+ # through
+ class Wrapper:
+ def __init__(self, array):
+ self.array = array
+
+ def __len__(self):
+ return len(self.array)
+
+ def __getitem__(self, item):
+ return type(self)(self.array[item])
+
+ def __getattr__(self, name):
+ if name.startswith("__array_"):
+ warnings.warn("object got converted", UserWarning, stacklevel=1)
+
+ return getattr(self.array, name)
+
+ def __repr__(self):
+ return "<Wrapper({self.array})>".format(self=self)
+
+ array = Wrapper(np.arange(10))
+ with pytest.raises(UserWarning, match="object got converted"):
+ np.asarray(array)
+
+
+def test_array_called():
+ class Wrapper:
+ val = '0' * 100
+ def __array__(self, result=None):
+ return np.array([self.val], dtype=object)
+
+
+ wrapped = Wrapper()
+ arr = np.array(wrapped, dtype=str)
+ assert arr.dtype == 'U100'
+ assert arr[0] == Wrapper.val