mypy icon indicating copy to clipboard operation
mypy copied to clipboard

no error when implementation of abstract property isn't a property

Open DetachHead opened this issue 3 years ago • 1 comments

from abc import ABC, abstractmethod

class A(ABC):
    @property
    @abstractmethod
    def foo(self) -> str: ...

class B(A):
    def foo(self) -> str:
        return "asdf"

def get_foo(a: A) -> str:
    return a.foo

b = B()

# runtime error - AttributeError: 'function' object has no attribute 'capitalize'
get_foo(b).capitalize()

https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-context%2Cshow-error-codes%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-expr%2Cdisallow-any-explicit%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cno-implicit-optional%2Cno-implicit-reexport%2Clocal-partial-types%2Cstrict-equality%2Cwarn-incomplete-stub%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=65fa8e55221b84e9101e9d5e1f59abbc

DetachHead avatar May 28 '22 05:05 DetachHead

We recently ran into a similar problem. It doesn't even matter if it's an abstract property.

class A:
    @property
    def foo(self) -> str:
        ...

    def get_foo(self) -> str:
        return self.foo


class B(A):
    def foo(self) -> str:
        return "asdf"


b = B()

# runtime error - AttributeError: 'function' object has no attribute 'capitalize'
b.get_foo().capitalize()

# mypy error - error: "Callable[[], str]" has no attribute "capitalize"  [attr-defined]
b.foo.capitalize()

Shouldn't the class definition of B.foo raise a mypy error since it's overriding a property with a method?

alexifm avatar Aug 05 '22 06:08 alexifm

This bug was apparently fixed in mypy 0.990. That version (and all subsequent versions) correctly detect this condition and report it as an error.

erictraut avatar Aug 13 '23 14:08 erictraut