python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.17k stars 2.77k forks source link

Property setter not accepted if not next to getter #1465

Open mihneagiurgea opened 8 years ago

mihneagiurgea commented 8 years ago

The following code incorrectly raises an error:

class Config(object):

    @property
    def my_proprty(self):
        return None

    def _other(self):
        pass

    @my_proprty.setter
    def my_proprty(self, val):
        pass

Error:

mypytest.py: note: In class "Config":
mypytest.py:12: error: Callable[[Any], Any] has no attribute "setter"

Commenting out the _other method will fix the error.

ilevkivskyi commented 7 years ago

1713 has another example and some ideas.

levsa commented 7 years ago

What is the status of this issue? #1713 is closed as duplicate of this issue.

JukkaL commented 7 years ago

We don't have immediate plans for fix this issue, but we are happy to receive a PR.

levsa commented 7 years ago

I had missed that moving the setter next to getter solves the problem, which is good enough. Thanks for the update!

dacut commented 6 years ago

This also affects subclasses that override properties, e.g.:

class Parent(object):
    @property
    def foo(self):
        return "parent_foo"

class Child(Parent):
    @Parent.foo.getter
    def foo(self):
        return "child_foo"

mypy returns a "Callable[[Any], Any]" has no attribute "getter" error here. In this case, it's obviously not possible to move the two methods to be next to each other.

hatal175 commented 3 years ago

So I've been looking at this. The issue is basically the OverloadedFuncDef unification logic in fastparse. Currently it basically unites all decorated consecutive functions with the same name. That is incorrect for everything that is not the overload decorator. However, due to its location it is unaware of name resolving. Meaning that if if I import overload as a different name, in order to correctly create OverloadedFuncDef, it has to err on the side of caution and apply the logic anyway.

I'm not sure how to handle this - the visitor logic depends on OverloadedFuncDef created in tree parsing but it obviously can't do this right. Do you have an idea where to move this logic? If we want to not change too much, we need somewhere that can resolve which decorator is used.

hatal175 commented 3 years ago

Maybe unite everything but add function indices? Then we can figure everything out in the semantic analysis phase.

John15321 commented 2 years ago

This problem seems to be persistent and still confuses people a lot maybe it's time to fix it?

samskiter commented 1 year ago

This also affects subclasses that override properties, e.g.:

class Parent(object):
    @property
    def foo(self):
        return "parent_foo"

class Child(Parent):
    @Parent.foo.getter
    def foo(self):
        return "child_foo"

mypy returns a "Callable[[Any], Any]" has no attribute "getter" error here. In this case, it's obviously not possible to move the two methods to be next to each other.

Thanks for posting this - also struggling with this. Is there any known workaround? Specifically I wanted to override a property setter.

asarkar commented 11 months ago

"No attribute" along with "Untyped decorator" error.

class Cell(ABC):
    def __init__(self, value: int | None = None):
        ...

    @property
    def value(self) -> int | None:
        return self._value

class InputCell(Cell):
    @Cell.value.setter
    def value(self, value: int) -> None:
        ...

Running mypy with --strict generates:

react.py:28: error: "Callable[[Cell], int | None]" has no attribute "setter"  [attr-defined]
react.py:28: error: Untyped decorator makes function "value" untyped  [misc]

Line 28 happens to be the @Cell.value.setter.