dataclasses_postinit
error
InitVar field validation in dataclasses
Detects two categories of InitVar violations:
1. **__post_init__ signature mismatch**: A dataclass with InitVar fields must declare a __post_init__ method whose parameters (after self) match the InitVar fields in count and type.
2. **Access to InitVar fields as instance attributes**: InitVarT fields are constructor-only parameters passed to __post_init__; they are not stored as instance attributes and cannot be accessed as instance.field.
from dataclasses import InitVar, dataclass
@dataclass
class DC1:
x: InitVar[int]
y: InitVar[str]
def __post_init__(self, x: int, y: int) -> None: # E: y should be str
pass
dc1 = DC1(1, "")
dc1.x # E: cannot access InitVar field as attribute
How to handle it
Every rule is on by default — strict is the default, not a cage. You can dial
dataclasses_postinit down per-file or per-path from your editor or
pyproject.toml, or fix the code
so it type-checks. See the Type System rules and
the complete diagnostic reference.
Canonical URL: https://www.basilisk-python.dev/errors/dataclasses_postinit