You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
attr-defined error is raised when reading default value of an InitVar field of a dataclass #19889
Bug Report
I wrote a code that reads the default value of an InitVar field of a dataclass and got attr-defined error from mypy==1.18.2. But cpython==3.13.3,3.12.10 does run without error and I believe it is reasonable to assume one can read default values from InitVar fields reading the reference.
Hello, there is a test that explicitly enforces the current behavior. Is this issue still a bug or it works as intended? The test is added around 4 years ago so I wonder if anything changes since.
[casetestDataclassInitVarCannotBeSet]
fromdataclassesimportdataclass, InitVar@dataclassclassC:
x: InitVar[int] =0y: InitVar[str] =''deff(self) ->None:
# This works at runtime, but it seems like an abuse of the InitVar# feature and thus we don't support itself.x=1# E: "C" has no attribute "x"self.y: str='x'# E: "C" has no attribute "y"c=C()
c2=C(x=1)
c.x# E: "C" has no attribute "x"
Activity
tqa236 commentedon Nov 8, 2026
Hello, there is a test that explicitly enforces the current behavior. Is this issue still a bug or it works as intended? The test is added around 4 years ago so I wonder if anything changes since.