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
Basemodel.dict not converting std python dataclasses children #3764
Comments
I reproduced the bug with that case:
from pydantic.dataclasses import dataclass
from pydantic import BaseModel
@dataclass
class Child:
n: int
class Root(BaseModel):
s:str
c: Child
r = Root(
s="test",
c=Child(n=2)
)
print(r.dict()) Output: {'s': 'test', 'c': Child(n=2)}
|
@Zekii in your example your child class is using pydantic dataclass (It is the second row of my "Extra info" table). If you change this line Edit: If so, I guess It is another issue, that pydantic doesn't have a ".json_dict" method... |
Closing the issue, it was solved in pydantic version 1.10.0, i don't know in which commit / PR, my guess is this one:
For testing sake, I made a repo running the cases in the table with some pydantic versions here: https://github.com/giuliano-oliveira/pydantic-3764 FYI: @ypsah |
Thanks for the heads-up @giuliano-oliveira. For clarity's sake, when you opened this issue, you reported that the output you expected of your reproducer was:
But with the latest version of
I agree this is an improvement over the old The reason I'm pushing for this is that I can still reproduce tiangolo/fastapi#4402 (exact same stack trace). My understanding is that Happy to work on a pydantic-specific reproducer if you want. |
That being said, I acknowledge that nested So maybe converting Would like to see a real-world example of code relying on this behavior though to convince myself there is value in it. |
I agree with you, the problem still stands, just the mangled pydatnic dataclasses that is solved... TBH, I forgot what was my train of thought in this issue, so I assumed it was the mangled dataclass issue, I'm open to the idea of reopening the issue, or opening another one if needed.
The use case that brought me to this was that I was trying to |
I totally agree that this should be serialized to I doubt however we can change this in v1.10 as it would be a breaking chance for many. |
Great @samuelcolvin ! I will update the example repo and the issue :) |
@Zekii I included your results in the issue and updated pydantic to 1.10.4 :) |
I confirm this is the behavior on V2. 🙏 |
Bug
The issue occurs with some combinations of a root and child types varying between pydantic's
BaseModel
, pydanticdataclass
and builtin dataclass and then the method.dict()
orasdict
is called, the returning value does not convert the child dataclass as dict.Output of executing the following script:
Output:
Expected output:
Extra info
Repo: https://github.com/giuliano-oliveira/pydantic-3764
I tested all combinations of using
pydantic.dataclasses.dataclass
,pydantic.BaseModel
anddataclasses.dataclass
as root and child types, and have noted that some combination doesn't return the expected outputVersion info
Python: 3.9.13
pydantic: 1.10.4
The text was updated successfully, but these errors were encountered: