Skip to content

attr-defined error is raised when reading default value of an InitVar field of a dataclass #19889

@ktns

Description

@ktns

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.

To Reproduce

import dataclasses


@dataclasses.dataclass
class Foo:
    foo: int = 42
    bar: dataclasses.InitVar[int] = 42


print(Foo.foo)
print(Foo.bar)

Expected Behavior

no-error

Actual Behavior

<string>:12: error: "type[Foo]" has no attribute "bar"  [attr-defined]

Your Environment

  • Mypy version used: 1.18.2, 1.17.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.13.3, 3.12.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions