mypy icon indicating copy to clipboard operation
mypy copied to clipboard

For incompatible attribute assignment, show class that defines attribute

Open JukkaL opened this issue 8 years ago • 1 comments

It would be nice to display the class that defines the attribute when we assign an incompatible value to an attribute:

class A:
    a = 1

class B(A):
    a = ''  # Show that 'a' was defined in 'A' (in addition to the current message)

class C(A): pass

c = C()
c.a = ''   # Again, maybe show that 'a' was defined in 'A'

Maybe also show the file and line on which the attribute was originally defined. However, we probably don't want these verbose messages in the body of a class if the attribute definition is in the same class, as the error is probably clear enough based on immediate context:

class A:
    a = 1

    def f(self) -> None:
        self.a = ''   # The current message is probably just fine, as the context is clear

JukkaL avatar Nov 28 '16 12:11 JukkaL

working on this.

ucodery avatar May 06 '19 15:05 ucodery

This is still open. #6806 can be used for context, though it was closed in an incomplete state (and is now quite old). Marking this as "good second issue" instead of "good first issue" since this has been open for quite a long time and apparently the implementation is not quite trivial.

JukkaL avatar Apr 22 '23 13:04 JukkaL