Improve error message for NumPy alias type used as dtype in ArrayNdCtors
See original GitHub issueReporting a bug
- I have tried using the latest released version of Numba (most recent is visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- 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).
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:
- Created 3 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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?Thank you, I’ve since realized this mistake. The error messages were somewhat difficult to parse at first but it’s more clear now.