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.

Can't use frozen native dataclass

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from dataclasses import dataclass
from fastapi import FastAPI

app = FastAPI()

@dataclass(frozen=True)
class Mod:
    foo: str


@app.get("/", response_model=Mod)
async def get_mod():
    return {"foo": "bar"}

Description

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 366, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/fastapi/applications.py", line 261, in __call__
    await super().__call__(scope, receive, send)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/starlette/routing.py", line 61, in app
    response = await func(request)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/fastapi/applications.py", line 216, in openapi
    return JSONResponse(self.openapi())
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/fastapi/applications.py", line 191, in openapi
    self.openapi_schema = get_openapi(
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/fastapi/openapi/utils.py", line 412, in get_openapi
    flat_models = get_flat_models_from_routes(routes)
  File "/home/dima/.cache/pypoetry/virtualenvs/mre-frozen-dataclass-3kNYbFrL-py3.11/lib/python3.8/site-packages/fastapi/openapi/utils.py", line 377, in get_flat_models_from_routes
    flat_models = callback_flat_models | get_flat_models_from_fields(
  File "pydantic/schema.py", line 448, in pydantic.schema.get_flat_models_from_fields
  File "pydantic/schema.py", line 421, in pydantic.schema.get_flat_models_from_field
  File "pydantic/dataclasses.py", line 255, in pydantic.dataclasses.dataclass
  File "pydantic/dataclasses.py", line 250, in pydantic.dataclasses.dataclass.wrap
  File "pydantic/dataclasses.py", line 159, in pydantic.dataclasses._process_class
  File "/usr/lib/python3.8/dataclasses.py", line 1019, in dataclass
    return wrap(cls)
  File "/usr/lib/python3.8/dataclasses.py", line 1011, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
  File "/usr/lib/python3.8/dataclasses.py", line 891, in _process_class
    raise TypeError('cannot inherit non-frozen dataclass from a '
TypeError: cannot inherit non-frozen dataclass from a frozen one

Operating System

Linux

Operating System Details

Linux

FastAPI Version

0.75.1

Python Version

3.10, 3.11

Additional Context

pydantic 1.9.0

Workarounds:

  1. remove frozen=True, or
  2. from pyndatic.dataclasses impotr dataclass – the pydantic version works.

It could be a bug in pydantic for all I know, or even a limitation, in which case fastapi docs need to be updated.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

0reactions
hafcommented, May 3, 2022

It seems to be specific to something fastapi might do when it converts normal dataclasses to pydantic dataclasses?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I set an attribute in a frozen dataclass custom ...
There is no strict need to freeze a class just to be hashable. You can opt to just not mutate the attributes from...
Read more >
How To Make Your Python Code Prettier With Dataclasses
This Python tutorial explains to you how to make your python code prettier with dataclasses. It will give you an overview through concrete...
Read more >
Data Classes in Python 3.7+ (Guide)
It is created using the new @dataclass decorator, as follows: ... In a frozen data class, you can not assign values to the...
Read more >
Everything you need to know about dataclasses - rmcomplexity
Note: use a frozen data class when using read-only data to avoid unwanted side-effects. Note: You cannot implement __post_init__ hook in a ...
Read more >
Concurrency and coroutines - Kotlin
Get acquainted with the main concepts for using coroutines: Asynchronous vs. parallel processing. Dispatcher for changing threads. Frozen ...
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