I would like to use in a njit function a numpy array, which dtype is some jitclass. Here is a minimal working example:
from numba import njit, float32
from numba.experimental import jitclass
import numpy as np
spec = [
("bar", float32),
]
@jitclass(spec)
class Foo:
def __init__(self, bar):
self.bar = bar
@njit
def f(array):
return array
array = np.empty(100, dtype=Foo)
f(array)
Here is the error I get:
---------------------------------------------------------------------------
TypingError Traceback (most recent call last)
Input In [91], in <cell line: 20>()
16 return array
19 array = np.empty(100 + 1, dtype=Foo)
---> 20 f(array)
File /usr/local/lib/python3.9/site-packages/numba/core/dispatcher.py:420, in _DispatcherBase._compile_for_args(self, *args, **kws)
414 msg = str(e).rstrip() + (
415 "\n\nThis error may have been caused by the following argument(s):\n%s\n"
416 % "\n".join("- argument %d: %s" % (i, err)
417 for i, err in failed_args))
418 e.patch_message(msg)
--> 420 error_rewrite(e, 'typing')
421 except errors.UnsupportedError as e:
422 # Something unsupported is present in the user code, add help info
423 error_rewrite(e, 'unsupported_error')
File /usr/local/lib/python3.9/site-packages/numba/core/dispatcher.py:361, in _DispatcherBase._compile_for_args.<locals>.error_rewrite(e, issue_type)
359 raise e
360 else:
--> 361 raise e.with_traceback(None)
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
non-precise type array(pyobject, 1d, C)
During: typing of argument at /var/folders/v_/4dwsfgm50bl87pbgq646ry700000gn/T/ipykernel_70414/2508373139.py (16)
File "../../../../var/folders/v_/4dwsfgm50bl87pbgq646ry700000gn/T/ipykernel_70414/2508373139.py", line 16:
<source missing, REPL/exec in use?>
Is there a way of making this work? Ideally, I would like to use as most as possible a numpy array with a structured dtype.