Replies: 4 comments
-
I would agree that dataclasses will be used more and more regardless of pydantic usage, but I think there are a number of benefits to using Typically, I would use
For what it's worth, pydantic has a That said, I would totally be in favor of adding better support for raw dataclasses by basically having FastAPI create a pydantic dataclass as an auxiliary type, use that for parsing, and then dump the parsed results into the original non-pydantic dataclass. (Assuming someone else was willing to write the PR 😁.) Given FastAPI already supports the use of pydantic dataclasses, I think it wouldn't be too hard to implement this. It would also have the benefit of not adding parsing overhead when instantiating the dataclass in your own code (it is possible to avoid the parsing overhead with I'm curious what @tiangolo thinks about this. Related, it might make sense to add raw-dataclass support for |
Beta Was this translation helpful? Give feedback.
-
One example of a cool library that I was hoping to use with fastapi, that does not seem to work with the pydantic dataclass adapter: https://github.com/nabla-c0d3/fireclass (Document and BaseModel seem to interfere)
|
Beta Was this translation helpful? Give feedback.
-
@uriva one simple option is that, if you want your app to only affect just the networking side of things, without documentation, serialization, validation, you can just use the request directly and instantiate your dataclasses with the contents of the request. But again, that would mean giving up validation, documentation, etc. The way all that is supported in FastAPI is via Pydantic. Pydantic is what does the validation and serialization. And JSON Schemas are generated by Pydantic (then incorporated into OpenAPI). I guess the first step to support plain dataclasses would be to have something to convert from a plain dataclass to a Pydantic model automatically. I think that would probably require a considerable effort, and I think that would belong in Pydantic or a third-party package. Not FastAPI itself. @ptone have you tried inheriting from both classes? The |
Beta Was this translation helpful? Give feedback.
-
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
-
I use the native dataclasses api, and I imagine that this will be the standard for python apps in the coming future, so I think fastapi should support them without requiring the use of pydantic.
Otherwise, a heavily nested dc will require changing a lot of code.
The common situation is this:
Where I want to use
A
in my fastapi app, but I end up having to convertA
to pydantic (via mix-in or by giving up dataclasses features).Even if I do that, I might need to convert
B
as well, so this is quite a big change to introduce for an api that's supposed to affect just the networking side of things.Side note: I'm using dataclasses_json to convert to json, so I have two decorators.
Beta Was this translation helpful? Give feedback.
All reactions