Skip to content

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

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

Open
2 tasks done
stuartarchibald opened this issue Aug 28, 2020 · 6 comments
Open
2 tasks done
Labels
feature_request good first issue A good issue for a first time contributor

Comments

@stuartarchibald
Copy link
Contributor

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

Can I get this?

@stuartarchibald
Copy link
Contributor Author

@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?

@generic-github-user
Copy link

I'm having a similar issue with np.zeros((50., 50.), dtype=np.float) in a constructor for a class decorated with @jitclass:

There are 2 candidate implementations:
      - Of which 2 did not match due to:
      Overload of function 'zeros': File: numba\core\typing\npydecl.py: Line 511.
        With argument(s): '(UniTuple(float64 x 2), dtype=Function(<class 'float'>))':
       No match.

I have tried using the np.float32 and np.float64 types as well as several of the Numba ones to no avail.

@generic-github-user
Copy link

I was able to resolve this by removing the type signatures and replacing a float tuple in another (NumPy) function with an int tuple ... not sure why this caused an error to be thrown here, but it seems to be working alright for now.

@esc
Copy link
Member

esc commented Jun 24, 2021

@generic-github-user thank you for asking about this. When using np.zeros you need to use integers (not floats) to specify the size. The following works, for example:

from numba import njit
import numpy as np


@njit
def create():
    return np.zeros((50, 50), dtype=np.float32)


print(create())

@generic-github-user
Copy link

Thank you, I've since realized this mistake. The error messages were somewhat difficult to parse at first but it's more clear now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature_request good first issue A good issue for a first time contributor
Projects
None yet
Development

No branches or pull requests

4 participants