Skip to content

__post_init__ and validators are not triggered in class inherited from stdlib dataclass in 1.10.1 #4477

@togrishka

Description

@togrishka

Initial Checks

  • 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

post_init and validator are not triggered in the following code. However, uncommenting any of the lines: py_dataclass(D_C_Base) or b: str = "string" solves the issue.
Versions 1.9.x could trigger both post_init and validators without these lines.

Example Code

from dataclasses import dataclass
from pydantic.dataclasses import dataclass as py_dataclass
from pydantic import validator


@dataclass
class D_C_Base:
    a: float = 1.0

# py_dataclass(D_C_Base)

@py_dataclass
class D_C_Child(D_C_Base):
    # b: str = "string"
    
    def __post_init__(self):
        print('POST_INIT')
        
    @validator('a')
    def validate_a(cls, value):
        print(f'Validating "a" is {value}')

d_c_class = D_C_Child()

Python, Pydantic & OS Version

pydantic version: 1.10.1
pydantic compiled: True
install path: C:\Users\TestUser\.conda\envs\py38\Lib\site-packages\pydantic
               python version: 3.8.13 (default, Mar 28 2022, 06:59:08) [MSC v.1916 64 bit (AMD64)]
platform: Windows-10-10.0.22000-SP0
optional deps. installed: ['typing-extensions']

Also,

pydantic version: 1.10.1
pydantic compiled: True
install path: /local/envs/py39_test/lib/python3.9/site-packages/pydantic
               python version: 3.9.1 (default, Dec 11 2020, 14:32:07)  [GCC 7.3.0]
platform: Linux-3.10.0-1160.66.1.el7.x86_64-x86_64-with-glibc2.17
optional deps. installed: ['typing-extensions']

Affected Components

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.XpendingIs unconfirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions