question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Add support for Pydantic's dataclasses

See original GitHub issue

Describe the question I am using a pydantic.dataclasses.dataclass as a response_model of my fast api route.

from fastapi import FastAPI
from pydantic.dataclasses import dataclass


@dataclass
class Foo:
    bar: int

app = FastAPI()

@app.get('/foo', response_model=Foo)
def get_bar() -> Foo:
    return Foo(bar=1)

To Reproduce Steps to reproduce the behavior:

  1. Create a file app.py with the above code.
  2. Start the app: uvicorn app:app
  3. Open the browser and go to localhost:8000/foo
  4. See error:
pydantic.error_wrappers.ValidationError: 1 validation error
response
  __init__() got an unexpected keyword argument '__initialised__' (type=type_error)

Expected behavior No validation error and serialized json does not contain any keys __initialised__.

Environment:

  • OS: Linux
  • FastAPI Version 0.25.0
  • Python version 3.7.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

9reactions
dmontagucommented, Nov 24, 2019

@justinttl It looks like pydantic dataclasses work as long as you provide a config with from_orm=True:

from fastapi import FastAPI
from pydantic.dataclasses import dataclass
from starlette.testclient import TestClient


class ORMConfig:
    orm_mode = True


@dataclass(config=ORMConfig)
class XYZResponse:
    x: str


app = FastAPI()


@app.get(
    '/xyz',
    response_model=XYZResponse,
)
async def calculate_xyz():
    return XYZResponse("hello")


print(TestClient(app).get("/xyz").content)
# b'{"x":"hello"}'

@tiangolo I feel like there is room for improvement around the handling and/or error messages here. Maybe we should require orm_mode for any model used by FastAPI? Or turn it on automatically?

@samuelcolvin Any ideas about how we might more clearly indicate to users that the problem is coming from orm_mode=False?

6reactions
frejsoyacommented, Apr 28, 2020

This does not work with nested dataclassed. The inner class record will have ...,__intialized__ : true,...

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dataclasses - pydantic
After v1.10, pydantic dataclasses support Config.extra but some default behaviour of stdlib dataclasses may prevail. For example, when print ing a pydantic ......
Read more >
Pydantic or dataclasses? Why not both? Convert Between Them
Pydantic's arena is data parsing and sanitization, while dataclasses a is a fast and memory-efficient (especially using slots, Python 3.10+) ...
Read more >
Do we still need dataclasses? // PYDANTIC tutorial - YouTube
Here's my FREE 7-step guide to help you consistently design great software: https://arjancodes.com/designguide. Pydantic is a very useful ...
Read more >
Attrs, Dataclasses and Pydantic - Stefan Scherfke
Attrs supports both ways. The default is to allow positional and keyword arguments like data classes. You can enable kw-only arguments by ...
Read more >
Hidden powers of pydantic - Kevin Tewouda
For those who don't know pydantic, it is a library for data validation leveraging type annotations feature added in python3.5. Here is an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found