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
Closed

Feature request: ForwardRef support for dataclasses #397

ceelian opened this issue Feb 16, 2019 · 5 comments

Comments

@ceelian
Copy link

@ceelian 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
Copy link
Owner

@samuelcolvin 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.

Loading

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

@samuelcolvin samuelcolvin commented Feb 17, 2019

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

Loading

@ceelian
Copy link
Author

@ceelian 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.

Loading

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Feb 17, 2019

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

Loading

@ceelian
Copy link
Author

@ceelian ceelian commented Feb 18, 2019

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

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants