Sitelet https://web.archive.org/web/20201007070151/https://github.com/numba/numba/issues/6184
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for NumPy alias type used as dtype in ArrayNdCtors #6184

Open
stuartarchibald opened this issue Aug 28, 2020 · 2 comments
Open

Comments

@stuartarchibald
Copy link
Contributor

@stuartarchibald stuartarchibald commented Aug 28, 2020

Reporting a bug

As per #3993 This

from numba import njit
import numpy as np

@njit
def foo():
    return np.zeros((2, 2), dtype=np.float)

foo()

produces:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function zeros>) found for signature:
 
 >>> zeros(UniTuple(Literal[int](2) x 2), dtype=Function(<class 'float'>))
 
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload of function 'zeros': File: numba/core/typing/npydecl.py: Line 504.
    With argument(s): '(UniTuple(int64 x 2), dtype=Function(<class 'float'>))':
   No match.

During: resolving callee type: Function(<built-in function zeros>)
During: typing of call at issue3993.py (25)


File "issue3993.py", line 25:
def foo():
    return np.zeros((2, 2), dtype=np.float)
    ^


which provides correct information given what Numba can infer generically, but this is a common issue and so it'd probably be a good idea to provide a specific message.

Here's starter patch to provide a better error message:

diff --git a/numba/core/typing/npydecl.py b/numba/core/typing/npydecl.py
index 2dbbed39b..3483fced2 100644
--- a/numba/core/typing/npydecl.py
+++ b/numba/core/typing/npydecl.py
@@ -508,6 +508,25 @@ class NdConstructor(CallableTemplate):
             else:
                 nb_dtype = parse_dtype(dtype)
 
+            if nb_dtype is None:
+                if isinstance(dtype, types.Function):
+                    # user is probably doing `np.float` or `np.int` or similar.
+                    try:
+                        nm = dtype.key[0].__name__
+                        if hasattr(np, nm):
+                            # user probably ought to use a specific type or the
+                            # one with an underscore
+                            if hasattr(np, '%s_' % nm):
+                                msg = ("dtype 'np.%s' is not supported, "
+                                       "perhaps use 'np.%s_' or a more "
+                                       "specific dtype?" % (nm, nm))
+                            else:
+                                msg = "dtype 'np.%s' is not supported" % nm
+                            raise TypingError(msg)
+                    except AttributeError:
+                        pass
+                raise TypingError("dtype '%s' is unsupported." % dtype)
+

with such a patch, this appears:

No implementation of function Function(<built-in function zeros>) found for signature:
 
 >>> zeros(UniTuple(Literal[int](2) x 2), dtype=Function(<class 'float'>))
 
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function 'zeros': File: numba/core/typing/npydecl.py: Line 504.
    With argument(s): '(UniTuple(int64 x 2), dtype=Function(<class 'float'>))':
   Rejected as the implementation raised a specific error:
     TypeError: dtype 'np.float' is not supported, perhaps use 'np.float_' or a more specific dtype?
@abhinavjonnada82
Copy link

@abhinavjonnada82 abhinavjonnada82 commented Oct 2, 2020

Can I get this?

@stuartarchibald
Copy link
Contributor Author

@stuartarchibald stuartarchibald commented Oct 2, 2020

@abhinavjonnada82 thanks for asking, I think @Luiz6ustav0 has opened a PR for this already in #6243. If you'd like to contribute to Numba there's a good first issue label which contains other issues which might be good to try?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.