Skip to content
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

__post_init__ is not triggered for descendant dataclass #739

Closed
krassowski opened this issue Aug 11, 2019 · 4 comments

Comments

@krassowski
Copy link

commented Aug 11, 2019

The __post_init__ method of parent is not called in child classes.

  • OS: Ubuntu
  • Python version import sys; print(sys.version): **3.7.2 (default, Mar 25 2019, 19:29:53) **
  • Pydantic version import pydantic; print(pydantic.VERSION): 0.32.1
from dataclasses import dataclass, field
from typing import Any

# comment the pydantinc import below to see the expected result
from pydantic.dataclasses import dataclass

@dataclass
class Base:
    x: float
    y: float

    def __post_init__(self):
        print('Called!')
        self.z = self.x + self.y

@dataclass
class Child(Base):
    a: int

obj = Child(a=0, x=1.5, y=2.5)
print(obj.z)

Everything works fine when using the dataclass from standard library. After looking through the code I expected that an easy workaround would be to add simple __post_init__ in child:

@dataclass
class Child(Base):
    a: int

    def __post_init__(self):
        super().__post_init__()

Now I do get 'Called!' message, but the 'z' attribute is not preserved anyway...

Do I miss something obvious in the usage of pydantic?

@krassowski krassowski added the bug label Aug 11, 2019

@krassowski

This comment has been minimized.

Copy link
Author

commented Aug 11, 2019

Update: after adding missing z: float = field(init=False) in the Base class the z attribute is properly preserved (does not get lost between the __post_init__ and instance completion). But the first issue - __post_init__ not being called in the first place still stands.

samuelcolvin added a commit that referenced this issue Aug 11, 2019
samuelcolvin added a commit that referenced this issue Aug 11, 2019
@samuelcolvin samuelcolvin referenced this issue Aug 11, 2019
2 of 2 tasks complete
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Aug 11, 2019

this was caused by #299 which fixed #293.

It's fixed by #740, could you check that works.

@krassowski

This comment has been minimized.

Copy link
Author

commented Aug 11, 2019

Thanks for the quick fix! Yes, I tried and it fixes the issue (when z: float = field(init=False) is present).

@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Aug 11, 2019

okay, I'll merge #740 and release a patch in a few days.

samuelcolvin added a commit that referenced this issue Aug 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.