getting mypy's to handle getters and setters in dataclasses

Viewed 115

I have a dataclass like

@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)

This code runs correctly, but mypy gives the error error: Name 'byr' already defined on line 10. I can change my initialisation to say _byr: int but this changes my codes behaviour, making it fail my unit tests. Specifically, I can no longer write something like Passport(byr=2000), which is required for me.

Any ideas how to solve this? Or is mypy just not designed yet to handle getters and setters in dataclasses?

0 Answers
Related