Numba types inside nopython functions

I’m having an issue similar to this question/solution:

Basically I want to be able to do empty_list(numba.float64[:]) inside a nopython function, but get an error. The only workaround so far has been to put the type as default argument to the function like in the solution.

Hi Justin,

in the past I encountered the same problem, and I agree that it is a little unfortunate that the type definition does not work inside jitted code, but you don’t have to create the abomination of passing the types in the function call.

Simply define the element type outside the function as a constant and then use it inside the function body, e.g.

import numba
_T_F8_ARRAY = numba.float64[:] 

def f():
    return numba.typed.List.empty_list(_T_F8_ARRAY)

I just realised that I should probably give a little more context.

I think it is key to understand that numba.float64[...] is not itself a type but a cleverly disguised call to the __getitem__ function of numba.float64. This call returns the actual type and does not work inside jitted code as far as I can tell.

The reason it works in the function signature is that it the function call to numba.float64__getitem__ is evaluated at definition time and not at call time (this is a common misunderstanding surrounding python).

That means by the time numba sees that type in the signature, it has already been evaluated and the actual type has been returned. That is also why it is possible to define the type outside the function and then use it as a constant.

In [2]: _T_F8_ARRAY
Out[2]: array(float64, 1d, A)

In [3]: numba.float64.__getitem__
Out[3]: <bound method Type.__getitem__ of float64>

In [4]: ?numba.float64.__getitem__
Signature: numba.float64.__getitem__(args)
Docstring: Return an array of this type.
File:      ~/anaconda3/envs/py38/lib/python3.8/site-packages/numba/core/types/
Type:      method