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

AttributeError when using dataclass with schema function #792

Closed
JayH5 opened this issue Sep 6, 2019 · 3 comments · Fixed by #1045
Closed

AttributeError when using dataclass with schema function #792

JayH5 opened this issue Sep 6, 2019 · 3 comments · Fixed by #1045
Labels
feature request help wanted Pull Request welcome

Comments

@JayH5
Copy link

JayH5 commented Sep 6, 2019

Bug

Please complete:

  • OS: macOS
  • Python version import sys; print(sys.version): 3.7.4
  • Pydantic version import pydantic; print(pydantic.VERSION): 0.32.2, as well as master as of 7901711

Where possible please include a self contained code snippet describing your bug:

from pydantic.dataclasses import dataclass
from pydantic.schema import schema

@dataclass
class Model:
    name: str

schema([Model])

This produces an error message like:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jamie/.virtualenvs/tempenv-74cd74727d61/lib/python3.7/site-packages/pydantic/schema.py", line 185, in schema
    flat_models = get_flat_models_from_models(models)
  File "/Users/jamie/.virtualenvs/tempenv-74cd74727d61/lib/python3.7/site-packages/pydantic/schema.py", line 437, in get_flat_models_from_models
    flat_models |= get_flat_models_from_model(model)
  File "/Users/jamie/.virtualenvs/tempenv-74cd74727d61/lib/python3.7/site-packages/pydantic/schema.py", line 378, in get_flat_models_from_model
    fields = cast(Sequence[Field], model.__fields__.values())
AttributeError: type object 'Model' has no attribute '__fields__'

The same code using BaseModel instead of the @dataclass works fine.

With the dataclass, Model.__pydantic_model__.schema() also works.

@JayH5 JayH5 added the bug V1 Bug related to Pydantic V1.X label Sep 6, 2019
@samuelcolvin samuelcolvin added feature request and removed bug V1 Bug related to Pydantic V1.X labels Sep 6, 2019
@samuelcolvin
Copy link
Member

That's not how the schema function is designed to work. Did you find somewhere in the docs that suggested that?

For now the solution is to use Model.__pydantic_model__.schema(), if you'd like to modify schema() to accept a dataclass, please submit a PR.

@JayH5
Copy link
Author

JayH5 commented Sep 6, 2019

That's not how the schema function is designed to work. Did you find somewhere in the docs that suggested that?

Ah, apologies, I must've misunderstood. I mostly just expected dataclasses to work similarly to things that inherit from BaseModel and don't think I read that schema() supports them.

I'd like to make a PR but honestly I'm not sure when I will have the time, so feel free to close if you want.

@samuelcolvin samuelcolvin added the help wanted Pull Request welcome label Sep 6, 2019
@samuelcolvin
Copy link
Member

no problem. Would definitely be better if schema() worked with dataclasses as it does with models, so I'll leave this open and fix it when I get a chance if no one else does before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request help wanted Pull Request welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants