Create recursive dataclass with self-referential type hints

Viewed 1475

I want to write a dataclass definition in Python, but can't refer to that same class inside the declaration.

Mainly what I want to achieve is the typing of this nested structure, as illustrated below:

 @dataclass
 class Category:
     title: str
     children: [Category] # I can't refer to a "Category"
  
 tree = Category(title='title 1', children=[
     Category('title 11', children=[]),
     Category('title 12', children=[])
 ])
1 Answers

Option #1

You can wrap class name in a string in order to forward-declare the annotation:

from dataclasses import dataclass
from typing import List


@dataclass
class Category:
    title: str
    children: List['Category']

Option #2

You can include a __future__ import so that all annotations by default are forward-declared as below. In this case, you can also eliminate the typing import and use new-style annotations in Python 3.7 and above.

from __future__ import annotations

from dataclasses import dataclass


@dataclass
class Category:
    title: str
    children: list[Category]
Related