A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pydantic/pydantic/issues/704 below:

ForwardRef and dataclasses when referencing same class · Issue #704 · pydantic/pydantic · GitHub

Question

Similar to #397, I'm using Python 3.7, importing annotations, and have a dataclass with a field that stores an optional instance of the same class.

This works fine under dataclasses.dataclass but fails under pydantic.dataclasses.dataclass:

For bugs/questions:

from __future__ import annotations
from typing import Optional

from pydantic.dataclasses import dataclass


@dataclass
class Resource:
    parent: Optional[Resource] = None


print(Resource())

...yields:

pydantic.errors.ConfigError: field "parent" not yet prepared so type is still a ForwardRef, you might need to call MetaModel.update_forward_refs().

I can solve it by adding above the class Resource=ForwardRef('Resource') but ForwardRef is private (not in typing.__all__) so I get a warning.

I'll continue by suppressing the warning. Just curious if I'm approaching this wrong.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4