How to decorate function taking a named tuple as an argument?

Viewed 87

Is there a cleaner way specify numba signature for a function that has NamedUniTuple as an argument? I came up with following code, but len(A._fields) looks like a hack. In the real life I have tuples with different number of fields, counting them to specify a literal is error-prone. The constructor knows about _fields, so why the tuple's length cannot be computed internally? I could not find anything in the documentation, maybe NamedUniTuple is not public?

from typing import NamedTuple
import numba as nb

class A(NamedTuple):
    a: float
    b: float

@nb.njit(nb.float64(nb.types.NamedUniTuple(nb.float64, len(A._fields), A)))
def f(x: A) -> float:
    return x.a + x.b

x = A(1.0, 2.0)
f(x)
0 Answers
Related