python / python/mypy

Issue initialising dataclass with getters and setters

Open
#9,779 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

When using mypy with a dataclass that includes getters and setters it throws an error. It returns Name 'byr' already defined on line 5.

To Reproduce

To reproduce use mypy with this script:

@dataclass
class Passport:
    byr: int

    @property
    def byr(self) -> int:
        return self._byr

    @byr.setter
    def byr(self, value) -> None:
        if int(value) < 1920 or int(value) > 2002:
            raise Exception("Birth year must be between 1920 and 2002")
        self._byr = int(value)

Expected Behavior

I would have expected it to accept this, as this seems to be a standard way to handle getters and setters.

Your Environment

  • Mypy command-line flags: python -m mypy my.py
  • Python version used: 3.9.0
  • Operating system and version:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided dataclass and run python -m mypy my.py to reproduce the duplicate-name error. Trace how dataclasses and property getters/setters are analyzed, then add a regression test showing that this pattern is accepted without the error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.