Skip to content

Display FQN for imported base classes in errors about incompatible ov… #19115

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

Merged
merged 3 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2264,7 +2264,7 @@ def check_method_override_for_base_with_name(
original_type,
defn.name,
name,
base.name,
base.name if base.module_name == self.tree.fullname else base.fullname,
original_class_or_static,
override_class_or_static,
context,
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ class A:
def __eq__(self, other: A) -> bool: pass # Fail
[builtins fixtures/plugin_attrs.pyi]
[out]
main:2: error: Argument 1 of "__eq__" is incompatible with supertype "object"; supertype defines the argument type as "object"
main:2: error: Argument 1 of "__eq__" is incompatible with supertype "builtins.object"; supertype defines the argument type as "object"
main:2: note: This violates the Liskov substitution principle
main:2: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
main:2: note: It is recommended for "__eq__" to work with arbitrary objects, for example:
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -3592,6 +3592,28 @@ class Bar(Foo):
def foo(self, value: Union[int, str]) -> Union[int, str]:
return super().foo(value) # E: Call to abstract method "foo" of "Foo" with trivial body via super() is unsafe

[case fullNamesOfImportedBaseClassesDisplayed]
from a import A

class B(A):
def f(self, x: str) -> None: # E: Argument 1 of "f" is incompatible with supertype "a.A"; supertype defines the argument type as "int" \
# N: This violates the Liskov substitution principle \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
...
def g(self, x: str) -> None: # E: Signature of "g" incompatible with supertype "a.A" \
# N: Superclass: \
# N: def g(self) -> None \
# N: Subclass: \
# N: def g(self, x: str) -> None
...

[file a.py]
class A:
def f(self, x: int) -> None:
...
def g(self) -> None:
...

[case testBoundMethodsAssignedInClassBody]
from typing import Callable

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -3206,13 +3206,13 @@ class Bar(Foo):
def frobnicate(self, *args: int) -> None: pass # type: ignore[override] # I know
[builtins fixtures/dict.pyi]
[out1]
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo"
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "a.Foo"
tmp/b.py:3: note: Superclass:
tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any
tmp/b.py:3: note: Subclass:
tmp/b.py:3: note: def frobnicate(self) -> None
[out2]
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo"
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "a.Foo"
tmp/b.py:3: note: Superclass:
tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any
tmp/b.py:3: note: Subclass:
Expand Down
24 changes: 12 additions & 12 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,9 @@ class A:
[file n.py.3]
[out]
==
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "A"
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "m.A"
==
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "A"
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "m.A"

[case testModifyBaseClassMethodCausingInvalidOverride]
import m
Expand All @@ -1067,7 +1067,7 @@ class A:
def f(self) -> int: pass
[out]
==
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "A"
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "m.A"

[case testAddBaseClassAttributeCausingErrorInSubclass]
import m
Expand Down Expand Up @@ -1974,11 +1974,11 @@ class B:
class B:
def foo(self) -> int: return 12
[out]
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "B"
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "b.B"
==
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "B"
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "b.B"
==
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "B"
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "b.B"
==

[case testPreviousErrorInMethodSemanal1]
Expand Down Expand Up @@ -7337,7 +7337,7 @@ class Parent:
def f(self, arg: Any) -> Any: ...
[out]
==
main:4: error: Signature of "f" incompatible with supertype "Parent"
main:4: error: Signature of "f" incompatible with supertype "b.Parent"
main:4: note: Superclass:
main:4: note: @overload
main:4: note: def f(self, arg: int) -> int
Expand Down Expand Up @@ -7380,7 +7380,7 @@ class Parent:
def f(self, arg: Any) -> Any: ...
[out]
==
main:4: error: Signature of "f" incompatible with supertype "Parent"
main:4: error: Signature of "f" incompatible with supertype "b.Parent"
main:4: note: Superclass:
main:4: note: @overload
main:4: note: def f(self, arg: int) -> int
Expand Down Expand Up @@ -7765,7 +7765,7 @@ def deco(f: F) -> F:
[out]
main:7: error: Unsupported operand types for + ("str" and "int")
==
main:5: error: Return type "str" of "m" incompatible with return type "int" in supertype "B"
main:5: error: Return type "str" of "m" incompatible with return type "int" in supertype "b.B"

[case testLiskovFineVariableClean-only_when_nocache]
import b
Expand Down Expand Up @@ -7870,7 +7870,7 @@ def deco(f: F) -> F:
pass
[out]
==
main:5: error: Return type "str" of "m" incompatible with return type "int" in supertype "B"
main:5: error: Return type "str" of "m" incompatible with return type "int" in supertype "b.B"

[case testAddAbstractMethod]
from b import D
Expand Down Expand Up @@ -8518,7 +8518,7 @@ class D:
==
==
a.py:3: error: Cannot override final attribute "meth" (previously declared in base class "C")
a.py:3: error: Signature of "meth" incompatible with supertype "C"
a.py:3: error: Signature of "meth" incompatible with supertype "c.C"
a.py:3: note: Superclass:
a.py:3: note: @overload
a.py:3: note: def meth(self, x: int) -> int
Expand Down Expand Up @@ -8565,7 +8565,7 @@ class D:
==
==
a.py:3: error: Cannot override final attribute "meth" (previously declared in base class "C")
a.py:3: error: Signature of "meth" incompatible with supertype "C"
a.py:3: error: Signature of "meth" incompatible with supertype "c.C"
a.py:3: note: Superclass:
a.py:3: note: @overload
a.py:3: note: def meth(x: int) -> int
Expand Down