Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
John15321 opened this issue Mar 18, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@John15321
Copy link

John15321 commented Mar 18, 2022

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
@John15321 John15321 added the bug mypy got something wrong label Mar 18, 2022
@John15321
Copy link
Author

NOTE:

After putting each setter right after its property there are no errors reported!

image

I think this is a bug, please fix it

@sterliakov
Copy link
Contributor

This problem persists since 2016 (duplicate of #1465).

@AlexWaygood
Copy link
Member

Closing as a duplicate of #1465

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants