Are jitclass methods jitted functions? : - argument 0: cannot determine Numba type of <class 'method'>

I ran into a problem where I wanted to use a rootfinding jitted function that itself takes jitted functions, in particular this https://quanteconpy.readthedocs.io/en/latest/optimize/root_finding.html

However, my “jitted” function for which I have to find a root is a method of a jitclass. I thought methods of jitclasses were jitted, but when I tried, I got the following error:

This error may have been caused by the following argument(s):
- argument 0: cannot determine Numba type of <class 'method'>

Here’s an example to reproduce the error. The jitclass Foo has method bar, which I understand to be jitted, after all that’s the point of a jitclass, no?

from numba import njit, float64
from numba.experimental import jitclass

specs = {"param": float64}


@jitclass(specs)
class Foo:

    def __init__(self, param):
        self.param = param

    def bar(self, x):
        return (x - 2) * (x + 1) ** 2 + self.param


@njit
def func(bar, x):
    return bar(x)


@njit
def testfunc(x):
    return x**2

foo_inst = Foo(10.0)

print(func(testfunc, 2)) # prints 4

print(func(foo_inst.bar, 2)) # results in error

Is there something I should be doing differently?

Below is the full error:

 4
Traceback (most recent call last):
  File "C:\Users\\anaconda3\envs\mini-hark\lib\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-331fa44ed081>", line 1, in <module>
    runfile('C:/Users///teststuff.py', wdir='C:/Users//')
  File "C:\Users\\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\203.5981.165\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Users\\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\203.5981.165\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users///teststuff.py", line 29, in <module>
    print(func(foo_inst.bar, 2))
  File "C:\Users\\anaconda3\envs\mini-hark\lib\site-packages\numba\core\dispatcher.py", line 415, in _compile_for_args
    error_rewrite(e, 'typing')
  File "C:\Users\\anaconda3\envs\mini-hark\lib\site-packages\numba\core\dispatcher.py", line 358, in error_rewrite
    reraise(type(e), e, None)
  File "C:\Users\\anaconda3\envs\mini-hark\lib\site-packages\numba\core\utils.py", line 80, in reraise
    raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
non-precise type pyobject
During: typing of argument at C:/Users///teststuff.py (19)
File "teststuff.py", line 19:
def func(bar, x):
    return bar(x)
    ^
This error may have been caused by the following argument(s):
- argument 0: cannot determine Numba type of <class 'method'>
1 Like

Hi @alanlujan91

The methods on a jitclass, when used from the interpreter, are true methods, i.e. I’d expect this to succeed:

import types; assert isinstance(foo_inst.bar, types.MethodType)

I can’t immediately think of a way around this (use of the actual jit methods will result in problems with binding to self), but perhaps someone else might know. CC @sklam thoughts?

@stuartarchibald is right. The methods are true python methods. And, Numba does not handle passing the bound method as argument currently.