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

Confusing error if property setter function name is different than getter function name #4234

Closed
scottbelden opened this issue Nov 10, 2017 · 2 comments

Comments

@scottbelden
Copy link

The following code produces a confusing error:

class Foo:
    def __init__(self):
        self._foo = None

    @property
    def foo(self):
        return self._foo

    @foo.setter
    def bar(self, value):
        self._foo = value

Error:

foo.py:9: error: "Callable[[Any], Any]" has no attribute "setter"

I think this is related to #1465 but slightly different. Here my getter and setter are next to each other, but the function name is different. I'm not saying that having different function names is good practice (I meant for them to be the same, but I had a typo), but technically it is valid code and shouldn't produce an error:

>>> import foo
>>> f = foo.Foo()
>>> f.foo
>>> f.bar = 1
>>> f.foo
1

I like the fact that mypy accidentally caught this typo, but I don't think it should have.

@gvanrossum
Copy link
Member

What's technically valid and what mypy should allow are two different things. So if this is bad style I like that mypy caught it. If you really want your code like that all you have to do is add # type: ignore to the line with the error.

@scottbelden
Copy link
Author

I agree. I guess I would just hope that it might be possible for the error message to be a little more clear that the problem is in the function name.

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

No branches or pull requests

2 participants