List & Dict can't accept `array.dtype` as argument.
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/main/CHANGE_LOG).
- I have included a self contained code sample to reproduce the problem. i.e. it’s possible to run as ‘python bug.py’.
As title.
import numpy as np
from numba import njit
from numba.typed.typedlist import List
from numba.typed.typeddict import Dict
@njit
def example_list(a):
ls = List.empty_list(a.dtype)
ls.append(a[0])
return ls
example_list(np.zeros(1))
- Resolution failure for literal arguments:
No implementation of function Function(<function impl_append at 0x7fc16b232af0>) found for signature:
>>> impl_append(ListType[dtype(float64)], float64)
There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload in function 'impl_append': File: numba/typed/listobject.py: Line 592.
With argument(s): '(ListType[dtype(float64)], float64)':
Rejected as the implementation raised a specific error:
NumbaNotImplementedError: Failed in nopython mode pipeline (step: native lowering)
Cannot cast float64 to dtype(float64): double %"arg.item"
During: lowering "casteditem = call $2load_global.0(item, $6load_deref.2, func=$2load_global.0, args=[Var(item, listobject.py:600), Var($6load_deref.2, listobject.py:600)], kws=(), vararg=None, varkwarg=None, target=None)" at /home/auderson/miniconda3/lib/python3.8/site-packages/numba/typed/listobject.py (600)
raised from /home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/base.py:704
- Resolution failure for non-literal arguments:
None
During: resolving callee type: BoundFunction((<class 'numba.core.types.containers.ListType'>, 'append') for ListType[dtype(float64)])
During: typing of call at <ipython-input-12-6baf440651d6> (10)
File "<ipython-input-12-6baf440651d6>", line 10:
def example_list(a):
<source elided>
ls = List.empty_list(a.dtype)
ls.append(a[0])
^
@njit
def example_dict(a):
d = Dict.empty(a.dtype, a.dtype)
d[a[0]] = a[0]
return d
example_dict(np.zeros(1))
No implementation of function Function(<built-in function setitem>) found for signature:
>>> setitem(DictType[dtype(float64),dtype(float64)]<iv=None>, float64, float64)
There are 16 candidate implementations:
- Of which 14 did not match due to:
Overload of function 'setitem': File: <numerous>: Line N/A.
With argument(s): '(DictType[dtype(float64),dtype(float64)]<iv=None>, float64, float64)':
No match.
- Of which 2 did not match due to:
Overload in function 'impl_setitem': File: numba/typed/dictobject.py: Line 695.
With argument(s): '(DictType[dtype(float64),dtype(float64)]<iv=None>, float64, float64)':
Rejected as the implementation raised a specific error:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function hash>) found for signature:
>>> hash(dtype(float64))
There are 4 candidate implementations:
- Of which 2 did not match due to:
Overload of function 'hash': File: numba/experimental/jitclass/overloads.py: Line 0.
With argument(s): '(dtype(float64))':
No match.
- Of which 2 did not match due to:
Overload in function 'hash_overload': File: numba/cpython/hashing.py: Line 48.
With argument(s): '(dtype(float64))':
Rejected as the implementation raised a specific error:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute '__hash__' of type dtype(float64)
File "../miniconda3/lib/python3.8/site-packages/numba/cpython/hashing.py", line 51:
def impl(obj):
return obj.__hash__()
^
During: typing of get attribute at /home/auderson/miniconda3/lib/python3.8/site-packages/numba/cpython/hashing.py (51)
File "../miniconda3/lib/python3.8/site-packages/numba/cpython/hashing.py", line 51:
def impl(obj):
return obj.__hash__()
^
raised from /home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/typeinfer.py:1086
During: resolving callee type: Function(<built-in function hash>)
During: typing of call at /home/auderson/miniconda3/lib/python3.8/site-packages/numba/typed/dictobject.py (706)
File "../miniconda3/lib/python3.8/site-packages/numba/typed/dictobject.py", line 706:
def impl(d, key, value):
<source elided>
castedval = _cast(value, valty)
status = _dict_insert(d, castedkey, hash(castedkey), castedval)
^
raised from /home/auderson/miniconda3/lib/python3.8/site-packages/numba/core/typeinfer.py:1086
During: typing of setitem at <ipython-input-3-3aa1714a8578> (4)
File "<ipython-input-3-3aa1714a8578>", line 4:
def example_dict(a):
<source elided>
d = Dict.empty(a.dtype, a.dtype)
d[a[0]] = a[0]
^
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
python - numba does not accept numpy arrays with dtype=object
If I turn off numba by setting nopython=False the code works fine. Setting dtype=list in the values array does not improve things. Any...
Read more >argument X: Cannot type list element of <class 'dict'> #6461
The goal is to concatenate the dict values into numpy array. The error thrown by @njit is: - argument 2: Cannot type list...
Read more >Working with multiple Dicts in a list - ERROR: MethodError ...
I would like to read a CSV-file and then convert it to a list of dicts, where the keys can contain arrays. But...
Read more >Structured arrays — NumPy v1.23 Manual
Returns a dictionary with fields indexing lists of their parent fields. This function is used to simplify access to fields nested in other...
Read more >Change data type of given numpy array - GeeksforGeeks
In order to change the dtype of the given array object, we will use numpy. astype() function. The function takes an argument which...
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
@esc Thanks! This works.
No problem. Glad you’ve found something that works. Am going to close this as resolved and have opened #8372 to track the issue with
typed.List.append
not validating types correctly.