Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create recursive dataclass with self-referential type hints

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=[])
 ])
like image 604
RomanGodMode Avatar asked Sep 04 '25 17:09

RomanGodMode


1 Answers

Option #1

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

from dataclasses import dataclass


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

Note: The ability to use list[type] for type hinting in Python was introduced in Python 3.9 as part of PEP 585.

For Python versions earlier than 3.9, one can use typing.List instead:

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]
like image 52
rv.kvetch Avatar answered Sep 06 '25 07:09

rv.kvetch