2

I have a numba.experimental.jitclass and I want to pass it a lot of parameters. using a dataclass is not supported in numba, so I defined my own parameters class.

I try to pass the parameters class to the jitclass, but I get an error that the parameters class isn't defined.

When I remove the import of annotations it works fine.

Code:

from __future__ import annotations

import numpy as np
from numba import float64
from numba.experimental import jitclass


@jitclass
class Parameters:
    p1: int
    p2: int
    p3: int
    p4: int
    p5: int
    p6: int
    p7: int
    p8: int

    def __init__(self, p1, p2, p3, p4, p5, p6, p7, p8):
        self.p1: int = p1
        self.p2: int = p2
        self.p3: int = p3
        self.p4: int = p4
        self.p5: int = p5
        self.p6: int = p6
        self.p7: int = p7
        self.p8: int = p8


@jitclass([('table', float64[:, :])])
class MyClass:
    params: Parameters
    table: np.ndarray

    def __init__(self, params):
        self.params = params
        self.table = np.zeros((params.p7, params.p8))

    def __repr__(self):
        return f"{self.table}"


if __name__ == "__main__":
    my = MyClass(Parameters(1, 2, 3, 4, 5, 6, 7, 8))
    print(my.table)

The error:

NameError: name 'Parameters' is not defined

Works when removing the line:

from __future__ import annotations
  1. Can anyone explain why removing the import of annotations solves it?
  2. Is there any risk removing the import of annotations in this case?
  3. Is this way to use jitclass in a jitclass correct?

I'm aware of the solution of using deffered_type, but this doesn't work with the import of annotations also.

user107511
  • 772
  • 3
  • 23

0 Answers0