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
Closed

__post_init__ is not triggered for descendant dataclass #739

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

Comments

@krassowski
Copy link

@krassowski krassowski 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
Copy link
Author

@krassowski krassowski 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.

Loading

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Aug 11, 2019

this was caused by #299 which fixed #293.

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

Loading

@krassowski
Copy link
Author

@krassowski krassowski 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).

Loading

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Aug 11, 2019

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

Loading

samuelcolvin added a commit that referenced this issue Oct 1, 2019
* fix(validate-assignment): do not validate extra fields when `vaidate_assignment` is on

* Update history

* Fix `value` vs. `value_`

* Add tests for `value` vs `value_` case

* uprev

* Fix generic required (#742)

* Fix required fields on GenericModel

* lint

* version up

* __post_init__ with inheritance, fix #739 (#740)

* Fix custom Schema on GenericModel fields (#754)

* Fix custom Schema on GenericModel fields

* Add PR#

* uprev

* Fix error messages for Literal types with multiple allowed values (#770)

* Fix error messages for Literal types with multiple allowed values

* Incorporate feedback

* update history
andreshndz added a commit to cuenca-mx/pydantic that referenced this issue Jan 17, 2020
* fix(validate-assignment): do not validate extra fields when `vaidate_assignment` is on

* Update history

* Fix `value` vs. `value_`

* Add tests for `value` vs `value_` case

* uprev

* Fix generic required (samuelcolvin#742)

* Fix required fields on GenericModel

* lint

* version up

* __post_init__ with inheritance, fix samuelcolvin#739 (samuelcolvin#740)

* Fix custom Schema on GenericModel fields (samuelcolvin#754)

* Fix custom Schema on GenericModel fields

* Add PR#

* uprev

* Fix error messages for Literal types with multiple allowed values (samuelcolvin#770)

* Fix error messages for Literal types with multiple allowed values

* Incorporate feedback

* update history
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants