question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
stuartarchibaldcommented, Oct 2, 2020

@abhinavjonnada82 thanks for asking, I think @Luiz6ustav0 has opened a PR for this already in https://github.com/numba/numba/pull/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?

0reactions
generic-github-usercommented, Jun 24, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

NumPy 1.20.0 Release Notes
The table below shows the full list of deprecated aliases, along with their ... Code that may have used type(dtype) is np.dtype will...
Read more >
Type hinting / annotation (PEP 484) for numpy.ndarray
For instance if the NumPy people added a type alias for their array_like object class. Better yet, implement support at the dtype level,...
Read more >
NumPy Tutorial: Your First Steps Into Data Science in Python
This tutorial will provide you with the knowledge you need to use NumPy and the higher-level libraries that rely on it. In this...
Read more >
NumPy Data Types - W3Schools
Below is a list of all data types in NumPy and the characters used to ... The NumPy array object has a property...
Read more >
6. Release Notes - Numba
The new documentation is better structured and has more detailed coverage ... Better error messages when unsupported types are used in Numpy math...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found