Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: ForwardRef support for dataclasses #397

Closed
ceelian opened this Issue Feb 16, 2019 · 5 comments

Comments

Projects
None yet
2 participants
@ceelian
Copy link

ceelian commented Feb 16, 2019

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()
@samuelcolvin

This comment has been minimized.

Copy link
Owner

samuelcolvin commented Feb 17, 2019

This isn't a bug, it's a feature request.

the example is broken and raises an exception

Wrong, this isn't the example. This is some other code that looks like the example, adding lazy type evaluation via from __future__ import annotations completely changes the context.

@samuelcolvin samuelcolvin changed the title Example nested dataclasses broken with __future__ annotations (FwdRefs) Feature request: ForwardRef support for dataclasses Feb 17, 2019

samuelcolvin added a commit that referenced this issue Feb 17, 2019

@samuelcolvin samuelcolvin referenced this issue Feb 17, 2019

Merged

support ForwardRef on dataclasses #399

4 of 4 tasks complete
@samuelcolvin

This comment has been minimized.

Copy link
Owner

samuelcolvin commented Feb 17, 2019

should be fixed by #399, let me know if this fixes the issue for you.

@ceelian

This comment has been minimized.

Copy link
Author

ceelian commented Feb 17, 2019

Well, the example code works in a standalone script with the new version, but not in my pytest in the project I use it. Can't figure it out why at the moment, but I am pretty sure that might not be related to pydantic. Although the error I get is from the new version:

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

and the python env with pytest is exactly the same as with the standalone script.

@samuelcolvin

This comment has been minimized.

Copy link
Owner

samuelcolvin commented Feb 17, 2019

Because, for pydantic to be able to resolve type annotations, they need to be global in the module.

samuelcolvin added a commit that referenced this issue Feb 18, 2019

@ceelian

This comment has been minimized.

Copy link
Author

ceelian commented Feb 18, 2019

Ah, got it. Thank you for clarification and the fast implementation of the new feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.