I created a Numba typed list that is sometimes empty, and I’d like to check that in the course of some code. The natural solutions don’t work. Here is a minimum working example:
from numba import njit
from numba.typed import List
@njit
def is_empty(my_list):
if len(my_list) == 0:
print("empty")
else:
print("not empty")
non_empty_list = List()
non_empty_list.append(1)
is_empty(non_empty_list)
empty_list_typed = List()
empty_list_typed.append(1)
empty_list_typed.remove(1)
is_empty(empty_list_typed)
empty_list = List()
print(len(List())) # This correctly prints 0
is_empty(empty_list)
Instead of running as I’d expect, I get the following – as you can see, it only works once the list has been typed:
not empty
empty
0
Traceback (most recent call last):
File "C:\Users\lnajt\Documents\GitHub\TinyProjects\ConnectedPartitionSampling\untitled0.py", line 15, in <module>
is_empty(empty_list)
File "C:\Users\lnajt\anaconda3\lib\site-packages\numba\dispatcher.py", line 604, in typeof_pyval
tp = typeof(val, Purpose.argument)
File "C:\Users\lnajt\anaconda3\lib\site-packages\numba\typing\typeof.py", line 30, in typeof
ty = typeof_impl(val, c)
File "C:\Users\lnajt\anaconda3\lib\functools.py", line 840, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "C:\Users\lnajt\anaconda3\lib\site-packages\numba\typing\typeof.py", line 56, in typeof_impl
return getattr(val, "_numba_type_", None)
File "C:\Users\lnajt\anaconda3\lib\site-packages\numba\typed\typedlist.py", line 214, in _numba_type_
raise TypeError("invalid operation on untyped list")
TypeError: invalid operation on untyped list
It is not hard to write my code to avoid this, but it seems like a strange bug.