Skip to content

mypy error: property defined is read-only, Callable class had no attribute "setter" #12376

Closed
@John15321

Description

@John15321

Bug Report

mypy is thorwing errors at properties that are properly defined (unless I am mistaken).

Here is my code:

from fs.sshfs import SSHFS


class SMS:
    _USER_DEFAULT: str = "xxx"
    _PASSWORD_DEFAULT: str = "xxx"
    _PORT_DEFAULT: int = 22

    def __init__(
        self,
        host: str,
        user: str = _USER_DEFAULT,
        password: str = _PASSWORD_DEFAULT,
        port: int = _PORT_DEFAULT,
    ):
        self._host: str = ""
        self._user: str = ""
        self._password: str = ""
        self._port: int = 0

        self.host = host
        self.user = user
        self.password = password
        self.port = port

    @property
    def sshfs(self) -> SSHFS:
        return SSHFS(
            host=self.host, user=self.user, passwd=self.password, port=self.port
        )

    @property
    def host(self) -> str:
        return self._host

    @property
    def user(self) -> str:
        return self._user

    @property
    def password(self) -> str:
        return self._password

    @property
    def port(self) -> int:
        return self._port

    @host.setter
    def host(self, host: str) -> None:
        if not isinstance(host, str):
            raise TypeError("Host must be a string!")
        self._host = host

    @user.setter
    def user(self, user: str) -> None:
        if not isinstance(user, str):
            raise TypeError("User should be a string!")
        self._user = user

    @password.setter
    def password(self, password: str) -> None:
        if not isinstance(password, str):
            raise TypeError("Password must be a string!")
        self._password = password

    @port.setter
    def port(self, port: int) -> None:
        if not isinstance(port, int) or port < 21:
            raise TypeError("Port number must be an integer and above 20!")
        self._port = port

To Reproduce

mypy <the pasted code file>

Rsult:

mycodefile.py:55: error: Property "host" defined in "SMS" is read-only
mycodefile.py:56: error: Property "user" defined in "SMS" is read-only
mycodefile.py:57: error: Property "password" defined in "SMS" is read-only
mycodefile.py:58: error: Property "port" defined in "SMS" is read-only
mycodefile.py:117: error: Name "host" already defined on line 73
mycodefile.py:117: error: "Callable[[SMS], str]" has no attribute "setter"
mycodefile.py:135: error: Name "user" already defined on line 84
mycodefile.py:135: error: "Callable[[SMS], str]" has no attribute "setter"
mycodefile.py:153: error: Name "password" already defined on line 95
mycodefile.py:153: error: "Callable[[SMS], str]" has no attribute "setter"
mycodefile.py:171: error: Name "port" already defined on line 106
mycodefile.py:171: error: "Callable[[SMS], int]" has no attribute "setter"
Found 12 errors in 1 file (checked 5 source files)

Your Environment

  • Mypy version used: 0.941
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.8.13
  • Operating system and version: macOS 12.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions