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=[])
])
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']
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]