pythonooptypesjsonschemapydantic

Defining recursive models in Pydantic?


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!


Solution

  • Either put from __future__ import annotations at the top of the file, or change your annotation to List['Task'].

    The relevant pydantic documentation is here: https://pydantic-docs.helpmanual.io/usage/postponed_annotations/

    But the error in your question is not pydantic specific, that's just how type annotations work.