I’m running numba 0.57.1 with python 3.9.16 on Windows 10 Pro.
When I create a structref with a single first-class function member I can add it to a typed list without issue, but when I add a second member the object cannot be added to a typed list.
I’m sure I’m missing something, but a day of searching and reading the docs didn’t get me to a solution.
Minimal reproducer below… the code runs without error if the second element of the fields
list is commented out.
import numba
from numba import int64
from numba.core import types
from numba.experimental import structref
from numba.experimental.structref import define_boxing
print(numba.__version__)
@numba.njit('int64(int64, int64)')
def sum(a, b):
return a + b
sum_type = sum.nopython_signatures[0].as_type()
@structref.register
class TesterTypeTemplate(types.StructRef):
pass
class Tester(structref.StructRefProxy):
def __new__(cls, *args):
return structref.StructRefProxy.__new__(cls, *args)
fields = [
('sum', sum_type),
('arg1', int64), # error goes away if you comment this line
]
structref.define_constructor(Tester, TesterTypeTemplate, [field[0] for field in fields])
define_boxing(TesterTypeTemplate, Tester)
TesterType = TesterTypeTemplate(fields)
tester_lst = numba.typed.List.empty_list(TesterType)
arg1 = 1
tester = Tester(sum) if 1 == len(fields) else Tester(sum, arg1)
tester_lst.append(tester)
The (gently edited) output is
python.exe discourse.py
0.57.1
venv\lib\site-packages\numba\core\utils.py:554: NumbaExperimentalFeatureWarning: First-class function type feature is experimental
warnings.warn("First-class function type feature is experimental",
Traceback (most recent call last):
File "discourse.py", line 33, in <module>
tester_lst.append(tester)
File "venv\lib\site-packages\numba\typed\typedlist.py", line 344, in append
_append(self, item)
File "venv\lib\site-packages\numba\core\dispatcher.py", line 468, in _compile_for_args
error_rewrite(e, 'typing')
File "venv\lib\site-packages\numba\core\dispatcher.py", line 409, in error_rewrite
raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
- Resolution failure for literal arguments:
No implementation of function Function(<function impl_append at 0x00000297E7058B80>) found for signature:
>>> impl_append(ListType[numba.TesterTypeTemplate(('sum', FunctionType[int64(int64, int64)]), ('arg1', int64))], numba.TesterTypeTemplate(('sum', type(CPUDispatcher(<function sum_it at 0x00000297C75FEA60>))), ('arg1', int64)))
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[numba.TesterTypeTemplate(('sum', FunctionType[int64(int64, int64)]), ('arg1', int64))], numba.TesterTypeTemplate(('sum', type(CPUDispatcher(<function sum_it at 0x00000297C75FEA60>))), ('arg1', int64)))':
Rejected as the implementation raised a specific error:
NumbaNotImplementedError: Failed in nopython mode pipeline (step: native lowering)
Cannot cast numba.TesterTypeTemplate(('sum', type(CPUDispatcher(<function sum_it at 0x00000297C75FEA60>))), ('arg1', int64)) to numba.TesterTypeTemplate(('sum', FunctionType[int64(int64, int64)]), ('arg1', int64)): %"inserted.meminfo.1" = insertvalue {i8*} undef, i8* %"arg.item.0", 0
During: lowering "casteditem = call $2load_global.0(item, $6load_deref.2, func=$2load_global.0, args=[Var(item, listobject.py:599), Var($6load_deref.2, listobject.py:600)], kws=(), vararg=None, varkwarg=None, target=None)" at D:\work\git1\mims\venv\lib\site-packages\numba\typed\listobject.py (600)
raised from venv\lib\site-packages\numba\core\base.py:701
- Resolution failure for non-literal arguments:
None
During: resolving callee type: BoundFunction((<class 'numba.core.types.containers.ListType'>, 'append') for ListType[numba.TesterTypeTemplate(('sum', FunctionType[int64(int64, int64)]), ('arg1', int64))])
During: typing of call at venv\lib\site-packages\numba\typed\typedlist.py (82)