Implementing n-tree with list

I want to implement n-tree by using list of nodes and jitclass but using deferred type for list but it is not possible(error is down below). If it needs additional functionality ,i need to report. Can you help?
TypingError: List.item_type cannot be of type OptionalType(DeferredType#1744333178288) i.e. the type ‘DeferredType#1744333178288 or None’

node_type = deferred_type()
    
@jitclass([('data', int32), ('children', types.ListType(optional(node_type)))])
class Node():    
    def __init__(self,key):
        self.data=key
        self.children=[]
    
node_type.define(Node.class_type.instance_type)

(if i don’t use optinal() function it gives this error:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Cannot infer the type of variable ‘$8build_list.2’ (temporary variable), have imprecise type: list(undefined)<iv=None>.

For Numba to be able to compile a list, the list must have a known and
precise type that can be inferred from the other variables. Whilst sometimes
the type of empty lists can be inferred, this is not always the case, see this
documentation for help:Troubleshooting and tips — Numba 0.50.1 documentation
) )

When I use a typed list with:

from numba.experimental import jitclass
from numba.types import deferred_type, int32, optional, ListType
from numba.typed import List


node_type = deferred_type()


@jitclass([('data', int32), ('children', ListType(node_type))])
class Node():
    def __init__(self,key):
        self.data = key
        self.children = List()

node_type.define(Node.class_type.instance_type)

I get no exception - does this help for your use case or do you run into another error?

Hey there, I do get an exception

I run

     from numba.experimental import jitclass
     from numba.types import deferred_type, int32, optional, ListType
     from numba.typed import List


     node_type = deferred_type()


    @jitclass([('data', int32), ('children', ListType(node_type))])
    class Node():
    def __init__(self,key):
        self.data = key
        self.children = List()

    node_type.define(Node.class_type.instance_type)

    x=Node(30)

ang get the following error


TypingError Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 x = Node(30)

File ~/.local/lib/python3.10/site-packages/numba/experimental/jitclass/base.py:124, in JitClassType.call(cls, *args, **kwargs)
122 bind = cls._ctor_sig.bind(None, *args, **kwargs)
123 bind.apply_defaults()
→ 124 return cls._ctor(*bind.args[1:], **bind.kwargs)

File ~/.local/lib/python3.10/site-packages/numba/core/dispatcher.py:468, in _DispatcherBase._compile_for_args(self, *args, **kws)
464 msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
465 f"by the following argument(s):\n{args_str}\n")
466 e.patch_message(msg)
→ 468 error_rewrite(e, ‘typing’)
469 except errors.UnsupportedError as e:
470 # Something unsupported is present in the user code, add help info
471 error_rewrite(e, ‘unsupported_error’)

File ~/.local/lib/python3.10/site-packages/numba/core/dispatcher.py:409, in _DispatcherBase._compile_for_args..error_rewrite(e, issue_type)
407 raise e
408 else:
→ 409 raise e.with_traceback(None)

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Cannot infer the type of variable ‘$10call_function.3’ (temporary variable), have imprecise type: ListType[undefined].

File “…/…/…/…/…/tmp/ipykernel_34085/1423327351.py”, line 13:

During: resolving callee type: jitclass.Node#7f4adf48d4e0<data:int32,children:ListType[DeferredType#139958412588656]>
During: typing of call at (3)

During: resolving callee type: jitclass.Node#7f4adf48d4e0<data:int32,children:ListType[DeferredType#139958412588656]>
During: typing of call at (3)

File “”, line 3:

Edit: Added second link

Does this conversation help at all?

Or this one?