Skip to content

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

Closed
@scottbelden

Description

@scottbelden

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions