mypy
mypy copied to clipboard
For incompatible attribute assignment, show class that defines attribute
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
working on this.
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.