You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched GitHub for a duplicate issue and I'm sure this is something new
I have searched Google & StackOverflow for a solution and couldn't find anything
I have read and followed the docs and still think this is a bug
I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)
Description
When using inheritance with pydantic.dataclasses.dataclass based objects with pydantic>=1.10 I seem to have encountered a bug that calls __post_init_post_parse__ methods multiple times if a __post_init__ method is called further down an inheritance chain.
Running the below code snippet outputs the print statement three times, whereas if __post_init__ is placed in AbstractClass the "post init post parse called" statement is called twice.
The exact same code snippet on pydantic<1.10 works as expected with the print statement only printed the once.
Including an overridden __post_init__ method in ConcreteClass only prints the statement once - but of course if there is more useful logic contained in BaseClass.__post_init__ (as would be the case for this pattern) then this is ignored.
Removing the @dataclass decorators from the base classes works fine however, except if we didn't have access to the code for those underlying base classes.
frompydantic.dataclassesimportdataclass@dataclassclassBaseClass:
def__post_init__(self):
pass@dataclassclassAbstractClass(BaseClass):
pass@dataclassclassConcreteClass(AbstractClass):
def__post_init_post_parse__(self):
print("post init post parse called")
ConcreteClass()
# prints:# post init post parse called# post init post parse called# post init post parse called# Example of nested dataclasses from docs: https://pydantic-docs.helpmanual.io/usage/dataclasses/#convert-stdlib-dataclasses-into-pydantic-dataclassesfromdatetimeimportdatetimefromtypingimportOptional@dataclassclassMeta:
modified_date: Optional[datetime]
seen_count: intdef__post_init__(self):
print("POST INIT")
@dataclassclassFile(Meta):
filename: strdef__post_init_post_parse__(self):
print("POST INIT POST PARSE")
File(filename="file.txt", modified_date=datetime.now(), seen_count=1)
# prints:# POST INIT# POST INIT POST PARSE# POST INIT POST PARSE
Initial Checks
Description
When using inheritance with
pydantic.dataclasses.dataclass
based objects withpydantic>=1.10
I seem to have encountered a bug that calls__post_init_post_parse__
methods multiple times if a__post_init__
method is called further down an inheritance chain.Running the below code snippet outputs the print statement three times, whereas if
__post_init__
is placed inAbstractClass
the"post init post parse called"
statement is called twice.The exact same code snippet on
pydantic<1.10
works as expected with the print statement only printed the once.Including an overridden
__post_init__
method inConcreteClass
only prints the statement once - but of course if there is more useful logic contained inBaseClass.__post_init__
(as would be the case for this pattern) then this is ignored.Removing the
@dataclass
decorators from the base classes works fine however, except if we didn't have access to the code for those underlying base classes.EDIT: It appears its due to these lines:
https://github.com/pydantic/pydantic/blob/v1.10.2/pydantic/dataclasses.py#L290-L304
Example Code
Python, Pydantic & OS Version
Affected Components
.dict()
and.json()
construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: