Is it possible to call a jitclassed static method from within another jitclass?
Below is a short code snippet illustrating what I mean. I’d like to be able to call Foo.from_value_staticmethod
from within Bar.__init__
.
import numba as nb
foo_spec = [("data", nb.types.int64)]
@nb.experimental.jitclass(foo_spec)
class Foo:
def __init__(self, data):
self.data = data
@staticmethod
def from_value_staticmethod(val):
f = Foo(val * 2)
return f
@nb.njit
def from_value_free_method(val):
f = Foo(val * 2)
return f
from_value_externally_defined = Foo.from_value_staticmethod
bar_spec = [
("foo", Foo.class_type.instance_type),
]
@nb.experimental.jitclass(bar_spec)
class Bar:
def __init__(self, foo_val):
self.foo = from_value_externally_defined(1)
print("Assigning from externally defined method worked: " + str(self.foo.data))
self.foo = from_value_free_method(2)
print("Assigning from free method worked: " + str(self.foo.data))
# This won't compile if you uncomment the line below
# self.foo = Foo.from_value_staticmethod(3)
b = Bar(10)
Uncommenting the Foo.from_value_staticmethod(3)
fails in nopython mode pipeline due to Unknown attribute from_value_staticmethod of type jitclass.Foo#abcd1234
:
Traceback (most recent call last):
File "test.py", line 43, in <module>
b = Bar(10)
File "/home/tomas/miniconda3/envs/localizer/lib/python3.7/site-packages/numba/experimental/jitclass/base.py", line 122, in __call__
return cls._ctor(*bind.args[1:], **bind.kwargs)
File "/home/tomas/miniconda3/envs/localizer/lib/python3.7/site-packages/numba/core/dispatcher.py", line 414, in _compile_for_args
error_rewrite(e, 'typing')
File "/home/tomas/miniconda3/envs/localizer/lib/python3.7/site-packages/numba/core/dispatcher.py", line 357, in error_rewrite
raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'from_value_staticmethod' of type jitclass.Foo#7fedac218c50<data:int64>
File "test.py", line 40:
def __init__(self, foo_val):
<source elided>
# The below won't compile
self.foo = Foo.from_value_staticmethod(3)
^
During: typing of get attribute at test.py (40)
File "test.py", line 40:
def __init__(self, foo_val):
<source elided>
# The below won't compile
self.foo = Foo.from_value_staticmethod(3)
^
During: resolving callee type: jitclass.Bar#7fedabd7b450<foo:instance.jitclass.Foo#7fedac218c50<data:int64>>
During: typing of call at <string> (3)
During: resolving callee type: jitclass.Bar#7fedabd7b450<foo:instance.jitclass.Foo#7fedac218c50<data:int64>>
During: typing of call at <string> (3)
File "<string>", line 3:
<source missing, REPL/exec in use?>