Repository: numba/numba

Issue:
`asfarray` incorrectly handles the default kwarg
- [x] I have tried using the 0.52.0rc2 released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

This:
```python
from numba import njit
import numpy as np

@njit
def foo():
    np.asfarray(np.zeros(4,))

foo()
```
produces:
```
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function asfarray at 0x7ff2ebd55ef0>) found for signature:
 
 >>> asfarray(array(float64, 1d, C))
 
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function 'np_asfarray': File: numba/np/arraymath.py: Line 4089.
    With argument(s): '(array(float64, 1d, C))':
   Rejected as the implementation raised a specific error:
     NotImplementedError: <class 'numpy.float64'> cannot be represented as a Numpy dtype
       Traceback (most recent call last):
         File "<path>/numba/core/typing/templates.py", line 677, in _get_impl
           impl, args = self._impl_cache[cache_key]
       KeyError: (<numba.core.typing.context.Context object at 0x7ff2dccf0050>, (array(float64, 1d, C),), ())
       During handling of the above exception, another exception occurred:
       Traceback (most recent call last):
         File "<path>/numba/core/types/functions.py", line 298, in get_call_type
           sig = temp.apply(nolitargs, nolitkws)
         File "<path>/numba/core/typing/templates.py", line 345, in apply
           sig = generic(args, kws)
         File "<path>/numba/core/typing/templates.py", line 597, in generic
           disp, new_args = self._get_impl(args, kws)
         File "<path>/numba/core/typing/templates.py", line 679, in _get_impl
           impl, args = self._build_impl(cache_key, args, kws)
         File "<path>/numba/core/typing/templates.py", line 710, in _build_impl
           ovf_result = self._overload_func(*args, **kws)
         File "<path>/numba/np/arraymath.py", line 4091, in np_asfarray
           dtype = as_dtype(dtype)
         File "<path>/numba/np/numpy_support.py", line 152, in as_dtype
           % (nbtype,))
       NotImplementedError: <class 'numpy.float64'> cannot be represented as a Numpy dtype
  raised from <path>/numba/np/numpy_support.py:152

During: resolving callee type: Function(<function asfarray at 0x7ff2ebd55ef0>)
During: typing of call at test_farray.py (6)


File "test_farray.py", line 6:
def foo():
    np.asfarray(np.zeros(4,))
    ^
```



Discussion:

Resolving patch:
diff --git a/numba/np/arraymath.py b/numba/np/arraymath.py
--- a/numba/np/arraymath.py
+++ b/numba/np/arraymath.py
@@ -4088,7 +4088,9 @@ def impl(a, dtype=None):
 
 @overload(np.asfarray)
 def np_asfarray(a, dtype=np.float64):
-    dtype = as_dtype(dtype)
+    # convert numba dtype types into NumPy dtype
+    if isinstance(dtype, types.Type):
+        dtype = as_dtype(dtype)
     if not np.issubdtype(dtype, np.inexact):
         dx = types.float64
     else:


Test patch:
diff --git a/numba/tests/test_np_functions.py b/numba/tests/test_np_functions.py
--- a/numba/tests/test_np_functions.py
+++ b/numba/tests/test_np_functions.py
@@ -257,6 +257,10 @@ def asfarray(a, dtype=np.float64):
     return np.asfarray(a, dtype=dtype)
 
 
+def asfarray_default_kwarg(a):
+    return np.asfarray(a)
+
+
 def extract(condition, arr):
     return np.extract(condition, arr)
 
@@ -3392,6 +3396,15 @@ def inputs():
             self.assertPreciseEqual(expected, got)
             self.assertTrue(np.issubdtype(got.dtype, np.inexact), got.dtype)
 
+        # test default kwarg variant
+        pyfunc = asfarray_default_kwarg
+        cfunc = jit(nopython=True)(pyfunc)
+        arr = np.array([1, 2, 3])
+        expected = pyfunc(arr)
+        got = cfunc(arr)
+        self.assertPreciseEqual(expected, got)
+        self.assertTrue(np.issubdtype(got.dtype, np.inexact), got.dtype)
+
     def test_repeat(self):
         # np.repeat(a, repeats)
         np_pyfunc = np_repeat