codon icon indicating copy to clipboard operation
codon copied to clipboard

Type annotation `def f(a: List[int])` not working in codon.jit decorated function

Open ypfmde opened this issue 2 years ago • 1 comments

The code

import codon
@codon.jit
def f(a: List[int]):
    return len(a)

raises a NameError: name 'List' is not defined, while the same function definition works fine in a pure codon program. Also, other type annotations like int work in codon.jit decorated functions. Do I misunderstand something, or is this a bug or missing feature in v0.16.1?

ypfmde avatar May 24 '23 20:05 ypfmde

@ypfmde List is not a built-in name in Python; you need to import it from the "typing" module. Alternatively, you can use the built-in list instead of List.

import codon

@codon.jit
def f(a: list[int]):
    return len(a)

print(f([1]))

or

from typing import List
import codon

@codon.jit
def f(a: List[int]):
    return len(a)

print(f([1]))

See #355 if you attempt to call the "f" function with an empty list.

elisbyberi avatar May 24 '23 21:05 elisbyberi