Skip to content

Error on instance property and init-only variable with the same name in a dataclass #12046

@mbraakhekke

Description

@mbraakhekke

Bug Report

Consider the following class definition:

from dataclasses import dataclass, InitVar
@dataclass
class MyClass:
    prop: InitVar[int]
    _prop: int

    def __post_init__(self, prop: int):
        self._prop = prop

    @property
    def prop(self) -> int:
        return self._prop

Mypy reports an error on line 10: Name "prop" already defined on line 4.

Is this a false positive? If not, how can I avoid this short of renaming the "prop" attribute or getter method (or using #typing: ignore)?

Environment

  • Mypy version used: 0.931
  • Python version used: 3.10.1
  • Operating system and version: Windows 10 x64

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions