Skip to content

Can't use frozen native dataclass #4763

@dimaqq

Description

@dimaqq

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.

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