Repository: cupy/cupy
Pull request: Fix default dtype of full
This PR fixes #1203. The default `dtype` of `cupy.full` should be determined by `fill_value`.

Problem statement:
default type of `full`is int in numpy but float in cupy
```python
In [53]: np.full((2,2), -1)
Out[53]:
array([[-1, -1],
       [-1, -1]])

In [54]: cp.full((2,2), -1)
Out[54]:
array([[-1., -1.],
       [-1., -1.]])

In [55]: cp.full((2,2), -1, dtype=int)
Out[55]:
array([[-1, -1],
       [-1, -1]])
```

Hints from issue discussion:

Commit message: Fix default dtype of full

Patch to review:
diff --git a/cupy/creation/basic.py b/cupy/creation/basic.py
index 0e28f2e0a33..57ae51d910a 100644
--- a/cupy/creation/basic.py
+++ b/cupy/creation/basic.py
@@ -188,6 +188,8 @@ def full(shape, fill_value, dtype=None):
 
     """
     # TODO(beam2d): Support ordering option
+    if dtype is None:
+        dtype = cupy.array(fill_value).dtype
     a = cupy.ndarray(shape, dtype=dtype)
     a.fill(fill_value)
     return a
diff --git a/tests/cupy_tests/creation_tests/test_basic.py b/tests/cupy_tests/creation_tests/test_basic.py
index edae77db6aa..326a8094944 100644
--- a/tests/cupy_tests/creation_tests/test_basic.py
+++ b/tests/cupy_tests/creation_tests/test_basic.py
@@ -141,6 +141,11 @@ def test_ones_like(self, xp, dtype):
     def test_full(self, xp, dtype):
         return xp.full((2, 3, 4), 1, dtype=dtype)
 
+    @testing.for_all_dtypes()
+    @testing.numpy_cupy_array_equal()
+    def test_full_default_dtype(self, xp, dtype):
+        return xp.full((2, 3, 4), xp.array(1, dtype=dtype))
+
     @testing.for_all_dtypes()
     @testing.numpy_cupy_array_equal()
     def test_full_like(self, xp, dtype):


Code review comments:
diff_hunk: @@ -188,6 +188,8 @@ def full(shape, fill_value, dtype=None):
 
     """
     # TODO(beam2d): Support ordering option
+    if dtype is None:
+        dtype = cupy.array(fill_value).dtype
path: cupy/creation/basic.py
text: @user1:
How about using `numpy.array` instead of `cupy.array` to avoid unnecessary GPU use?