Signature for List(64) does not work

I want to specify signature explicit for my kernel as

from numba import njit, int64
from numba.typed import List

@njit(List(int64))
def square(data):
    for i in range(len(data)):
        data[i] = data[i] * data[i]

But I got an error KeyError: ‘Can only index numba types with slices with no start or stop, got 0.’

But without signature it works:

@njit
def square(data):
    for i in range(len(data)):
        data[i] = data[i] * data[i]

print(square.signatures[0])
(ListType[int64],)

So, it works. But how can I now specify the signature explicit?

I think the main issue is that you also need to specify the return type, even it’s nothing.

So:

from numba import njit, types, int64
from numba.typed import List

@njit(types.void(types.ListType(int64)))
def square(data):
    for i in range(len(data)):
        data[i] = data[i] * data[i]

Or using the string format:

@njit("void(ListType(int64))")
def square(data):
    for i in range(len(data)):
        data[i] = data[i] * data[i]

For me that makes it work with something like:

l = List()
l.append(5)

square(l)

assert l[0] == 25

Thank you for the help. The definition

@njit("void(ListType(int64))")

works perfect. Unfortunately when I try to define types explicitly

from numba import njit, types, void, int64
from numba.typed import List

@njit(void(types.List(int64)))
def square(data):
    for i in range(len(data)):
        data[i] = data[i] * data[i]

def main():
    a = [0, 1, 2, 3, 4]

    lst = List()
    [lst.append(x) for x in a]
    square(lst)

if __name__ == '__main__':
    main()

I got an error

TypeError: No matching definition for argument type(s) ListType[int64]

And also I am a bit confusing, what is the difference between “types.List” and “types.List” in numba?

But does it work when you use my first example (non-string version)? That uses types.void and types.ListType, which is different from what you’re now trying to use. Importing void directly from Numba might be identical, I haven’t checked.

It also took me a couple of tries to get working, so I’ll second that it’s not super intuitive. But it’s also something I have never used myself before, I’ve only used typed Lists without an explicit signature in the function.

Finally, I get it work. Thanks for the help. Here is the working example

from numba import njit, types, typed, void, int64

@njit(void(types.ListType(int64)))
def square(data):
    for i in range(len(data)):
        data[i] = data[i] * data[i]

a = [0, 1, 2, 3, 4]

lst = typed.List()
[lst.append(x) for x in a]
square(lst)
1 Like