How can I define a recursive Pydantic model?
Here's an example of what I mean:
from typing import List
from pydantic import BaseModel
class Task(BaseModel):
name: str
subtasks: List[Task] = []
but when I run that I get the following error:
NameError Traceback (most recent call last)
<ipython-input-1-c6dca1d390fe> in <module>
2 from pydantic import BaseModel
3
----> 4 class Task(BaseModel):
5 name: str
6 subtasks: List[Task] = []
<ipython-input-1-c6dca1d390fe> in Task()
4 class Task(BaseModel):
5 name: str
----> 6 subtasks: List[Task] = []
7
NameError: name 'Task' is not defined
I looked through the documentation but couldn't find anything. For example, at the page on "Recursive Models", but it seems to be about nesting subtypes of BaseModel
not about a recursive type definition.
Thanks for your help!
from __future__ import annotations
at the top of your code. This blog post goes into a little more detail about what's going on there: dev.to/tiangolo/… – Karb