Repository: cupy/cupy

Issue:
cupy.min_scalar_type does not accept cupy.ndarray
```
>>> cupy.min_scalar_type(cupy.array([1, 2]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: object __array__ method not producing an array
>>> cupy.__version__
'4.0.0'
```

Because 0-dim array is not "scalar" in cupy, this might be fixed by returning the input if its type is cupy.ndarray.


Discussion:
This fails for a different reason now when using 6.5.0.

```python
In [1]: import cupy                                                                            

In [2]: cupy.min_scalar_type(cupy.array([1, 2]))                                               
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-31d2cbe4824f> in <module>
----> 1 cupy.min_scalar_type(cupy.array([1, 2]))

<__array_function__ internals> in min_scalar_type(*args, **kwargs)

TypeError: no implementation found for 'numpy.min_scalar_type' on types that implement __array_function__: [<class 'cupy.core.core.ndarray'>]
```
Any updates on when this will be fixed?

Resolving patch:
diff --git a/cupy/__init__.py b/cupy/__init__.py
--- a/cupy/__init__.py
+++ b/cupy/__init__.py
@@ -317,7 +317,7 @@ def binary_repr(num, width=None):
 
 
 # -----------------------------------------------------------------------------
-# Data type routines (borrowed from NumPy)
+# Data type routines (mostly borrowed from NumPy)
 # -----------------------------------------------------------------------------
 def can_cast(from_, to, casting='safe'):
     """Returns True if cast between data types can occur according to the
@@ -362,7 +362,8 @@ def result_type(*arrays_and_dtypes):
     return _numpy.result_type(*dtypes)
 
 
-from numpy import min_scalar_type  # NOQA
+from cupy._core.core import min_scalar_type  # NOQA
+
 from numpy import obj2sctype  # NOQA
 from numpy import promote_types  # NOQA
 


Test patch:
diff --git a/tests/cupy_tests/core_tests/test_core.py b/tests/cupy_tests/core_tests/test_core.py
--- a/tests/cupy_tests/core_tests/test_core.py
+++ b/tests/cupy_tests/core_tests/test_core.py
@@ -64,6 +64,36 @@ def test_ndarray(self, order):
         assert a.flags.f_contiguous == expect_f
 
 
+class TestMinScalarType:
+    def test_scalar(self):
+        for v in (-129, -128, 0, 1.2, numpy.inf):
+            assert cupy.min_scalar_type(v) is numpy.min_scalar_type(v)
+
+    @testing.for_all_dtypes()
+    def test_numpy_scalar(self, dtype):
+        sc = dtype(-1)
+        for v in (sc, [sc, sc]):
+            assert cupy.min_scalar_type(v) is numpy.min_scalar_type(v)
+
+    @testing.for_all_dtypes()
+    def test_cupy_scalar(self, dtype):
+        sc = cupy.array(-1, dtype=dtype)
+        for v in (sc, [sc, sc]):
+            assert cupy.min_scalar_type(v) is sc.dtype
+
+    @testing.for_all_dtypes()
+    def test_numpy_ndarray(self, dtype):
+        arr = numpy.array([[-1, 1]], dtype=dtype)
+        for v in (arr, (arr, arr)):
+            assert cupy.min_scalar_type(v) is numpy.min_scalar_type(v)
+
+    @testing.for_all_dtypes()
+    def test_cupy_ndarray(self, dtype):
+        arr = cupy.array([[-1, 1]], dtype=dtype)
+        for v in (arr, (arr, arr)):
+            assert cupy.min_scalar_type(v) is arr.dtype
+
+
 @testing.parameterize(*testing.product({
     'cxx': (None, '--std=c++11'),
 }))