I have a class with an attribute sources
that can or cannot be defined. Before using numba, I set the variable sources
to None
when it was undefined, otherwise it was a numpy array.
Now, it seems that this behavior is not allowed by numba. Is this correct? I though to use a boolean variable as a workaround, but this messes up with the signature of the function (property) source
:
import numba
from numba.experimental import jitclass
import numpy as np
spec = [
("_is_source_none", numba.bool_),
("_sources", numba.int32[:, :])
]
@jitclass(spec)
class MyClass:
def __init__(self, sources: np.array):
# Numba does not allow to set a var to None so we need an external variable to track it
self._is_source_none = True if sources is None else False
self._sources = sources.astype(np.int32) if sources is not None else np.zeros(shape=(50, 50), dtype=np.int32)
@property
def sources(self):
if self._is_source_none:
return None
else:
return self.sources
if __name__ == "__main__":
MyClass(
sources=None
)
Error:
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython mode backend)
Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Internal error at resolving type of attribute "sources" of "self".
Failed in nopython mode pipeline (step: nopython frontend)
compiler re-entrant to the same function signature
During: typing of get attribute at <input> (25)
Enable logging at debug level for details.
File "<input>", line 25:
<source missing, REPL/exec in use?>
During: typing of get attribute at <string> (3)
During: typing of get attribute at <string> (3)
File "<string>", line 3:
<source missing, REPL/exec in use?>
Is this even possible in Numba?
The error you get has nothing to do with None and typing. It is due to self.sources
in the member function sources
not being declared nor initialized while self._sources
is and should be used instead.
Note Numba actually supports None
values using optional
types. You can find more information in the Numba documentation.