Skip to content

Assigning to an attribute with the same name as a dataclass InitVar raises has no attribute on 0.960 #12877

Closed as not planned
@paw-lu

Description

@paw-lu

Bug Report

On 0.960, if an attribute is typed as a dataclasses.InitVar and later tries to assign an attribute with the same name, mypy will complain on the assignment statement that the dataclass has no such attribute.

This does not occur on 0.950.

To Reproduce

import dataclasses
from dataclasses import InitVar


@dataclasses.dataclass
class Foo:
    a: int
    b: InitVar[int]

    def __post_init__(self, b: int) -> None:
        self.b = self.a + 1
$ pipx run --spec mypy==0.950 mypy --strict script.py
Success: no issues found in 1 source file

$ pipx run --spec mypy==0.960 mypy --strict script.py
script.py: note: In member "__post_init__" of class "Foo":
script.py:11:9: error: "Foo" has no attribute "b"  [attr-defined]
            self.b = self.a + 1
            ^
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

I expected that mypy would not complain about an assignment just because it shares the same name as an InitVar.

Actual Behavior

mypy warns the user that the attribute does not exists in an assignment expression.

Your Environment

  • Mypy version used: 0.960
  • Mypy command-line flags: --strict
  • Python version used: 3.10
  • Operating system and version: macOS 12.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions