Skip to content

Feature request: ForwardRef support for dataclasses #397

@ceelian

Description

@ceelian

Bug

With python 3.7+ from __future__ import annotations the example is broken and raises an exception.

  • OS: OS X 10.13.6 (17G4015)
  • Python version: 3.7.1
  • Pydantic version: 0.19
# only the following line was changed compared to the example
# in the docs https://pydantic-docs.helpmanual.io/#nested-dataclasses
from __future__ import annotations 

from pydantic import UrlStr
from pydantic.dataclasses import dataclass

@dataclass
class NavbarButton:
    href: UrlStr

@dataclass
class Navbar:
    button: NavbarButton

navbar = Navbar(button=('https://example.com',))
print(navbar)
 python foo.py
Traceback (most recent call last):
  File "foo.py", line 13, in <module>
    navbar = Navbar(button=('https://example.com',))
  File "<string>", line 3, in __init__
  File "/tmp/env/lib/python3.7/site-packages/pydantic/dataclasses.py", line 25, in _pydantic_post_init
    d = validate_model(self.__pydantic_model__, self.__dict__)
  File "/tmp/env/lib/python3.7/site-packages/pydantic/main.py", line 508, in validate_model
    f"field {field.name} not yet prepared and type is still a ForwardRef, "
pydantic.errors.ConfigError: field button not yet prepared and type is still a ForwardRef, you'll need to call MetaModel.update_forward_refs()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions