Skip to content

AttributeError when using dataclass with schema function #792

@JayH5

Description

@JayH5

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.

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