Skip to content

Show more context about the base class on incompatible override #19112

Closed
@JukkaL

Description

@JukkaL

Feature

When an override is incompatible with the base class, mypy only shows the short name of the base class (e.g. MyClass). Change that to show the full name (e.g. pkg.mod.MyClass) when the base class is defined in a different module.

Example where this would make a difference:

# a.py
class A:
    def f(self, x: int) -> None:
        ...
    def g(self) -> None:
        ...
# b.py
from a import A

class B(A):
    def f(self, x: str) -> None:
        ...
    def g(self, x: str) -> None:
        ...

This is the current output:

b.py:4: error: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int"  [override]
b.py:4: note: This violates the Liskov substitution principle
b.py:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
b.py:6: error: Signature of "g" incompatible with supertype "A"  [override]
b.py:6: note:      Superclass:
b.py:6: note:          def g(self) -> None
b.py:6: note:      Subclass:
b.py:6: note:          def g(self, x: str) -> None

The desired output would be ... with supertype "a.A" ... instead of ... with supertype "A" ....

Pitch

There can be a complex class hierarchy, and different base classes in the MRO can be in different modules. Currently finding the base class of the method is inconvenient, especially for somebody not using an IDE that allows them to quickly look it up. Also, it's not uncommon to have multiple classes with the same short name, so the current message is ambiguous.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions