Assert variable types in numba
dev/python
To assert that a variable is of any specific type, e.g., float32[:]
, one may apply this trick that makes use of numba
signature:
import numba as nb
# Define an auxiliary function that admits only the type you
# want to assert, e.g. float32[:]
assert_f32_1d = nb.njit(nb.none(nb.float32[:]))(lambda x: None)
def function_to_debug_type(x, y, z):
...
some_variable = ...
...
# If `some_variable` is not of type float32[:], numba will
# point it out.
assert_f32_1d(some_variable)