New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple issues when trying to wrap an existing dataclass as pydantic #2555
Comments
Thanks @PrettyWood ! So I don't have much to offer in terms of feedback on the API, short of listing the areas that we found that currently break the pydantic conversion code (which are also listed above):
We just implemented a working workaround that does less than what you desire to achieve, but works for our needs of converting our dataclasses hierarchy to pydantic objects, using create_model, and a bunch of python "reflection tricks". I'll be happy to share this (rather ugly) code if you are interested. |
I see that #2541 was closed, but note my new comment there: the proposed solution doesn't work, the issue remains and is real. |
I reopened it |
Thanks! Sorry for notifying about this also here and not only there, it is just that I don't know how your github project is set up, and do know that in our repos, I never notice responses on closed issues... So just wanted to be safe. |
@PrettyWood Yes, if we use pydantic dataclasses this will work, but we are interfacing with an existing code-base that uses "regular" dataclasses, which cannot change. So we need the ability to wrap regular dataclasses. I assume this will become a common use-case as people start using FastAPI to expose their existing domain models as web endpoints. |
btw, it would have made it easier to switch from |
No worries I just said this to explain it will work ! You can keep your raw |
Checks
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:We are trying to use pydantic (as part of FastAPI) with our own dataclass-based datamodel, using pydantic's automatic conversion from dataclasses. While doing so, we encountered several places where the code crashes in a cryptic way. Three of them we managed to resolve (by changing our code), but not the fourth one.
This is (a stripped down version of) our dataclasses (
definitions.py
). (+ See a more minimal one at the end.)And this is the minimal pydantic code:
On the initial run, we get:
The error message is clear, and the issue is easily resolved by switching the order of the
source
andmetadata
fields in Sentence.However, we think it might be a bug to consider
default_factory
as a non-default argument.After switching the order, we now get:
This has to do with nested frozen dataclasses, and I opened a separate issue about it (#2541).
After removing all frozen annotations, we now get the really cryptic message:
This is due to using type-aliases (
TokenIndex = int
) and then using them as field types.After removing these as well, we get:
This one we couldn't solve by changing our code, though I will note that commenting out some fields did help.
However, the issue is not about any specific field, but about their combinations. Here are minimal
Sentence
classes the exhibits this behavior:Having either
chunks
orevents
by themselves, do work. HavingBinaryRelation
in itself fails, presumably because it uses twoLabeledSpan
s internally.Below is a minimal
definitions.py
file with aSentence
class that exhibits the final issue:We will be happy to provide further details, and/or help to resolve these issues.
The text was updated successfully, but these errors were encountered: