Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JukkaL opened this issue May 19, 2025 · 1 comment · May be fixed by #19115
Open

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

JukkaL opened this issue May 19, 2025 · 1 comment · May be fixed by #19115

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented May 19, 2025

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.

@east825
Copy link

east825 commented May 19, 2025

I'm going to look at this at PyCon US 2025 sprints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants