2Tests type checking of the __post_init__ method in a dataclass.
5# Specification: https://peps.python.org/pep-0557/#post-init-processing
7from dataclasses import InitVar, dataclass, field, replace
8from typing import assert_type
19 def __post_init__(self, x: int, y: int) -> None: # E: wrong type for y
[invalid-dataclass] Invalid `__post_init__` signature for dataclass `DC1`
23dc1 = DC1(1, 2, 3, 4, "")
25assert_type(dc1.a, int)
26assert_type(dc1.b, int)
27assert_type(dc1.c, int)
28print(dc1.x) # E: cannot access InitVar
[unresolved-attribute] Object of type `DC1` has no attribute `x`
29print(dc1.y) # E: cannot access InitVar
[unresolved-attribute] Object of type `DC1` has no attribute `y`
36 def __post_init__(self, x: int) -> None: # E: missing y
[invalid-dataclass] Invalid `__post_init__` signature for dataclass `DC2`
42 _name: InitVar[str] = field()
43 name: str = field(init=False)
45 def __post_init__(self, _name: str):
51 _age: InitVar[int] = field()
52 age: int = field(init=False)
54 def __post_init__(self, _name: str, _age: int):