attr-defined error is raised when reading default value of an `InitVar` field of a dataclass
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the reported dataclasses.InitVar example against mypy 1.18.2 and compare its treatment of Foo.foo and Foo.bar. Trace the dataclass and InitVar handling from the resulting attr-defined diagnostic, then add a regression test showing that reading the InitVar default is accepted and that the existing error remains for genuinely missing attributes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100