Skip to content

Commit 325f776

Browse files
authored
Display FQN for imported base classes in errors about incompatible overrides (#19115)
Fixes #19112
1 parent a573a40 commit 325f776

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,7 @@ def check_method_override_for_base_with_name(
22852285
original_type,
22862286
defn.name,
22872287
name,
2288-
base.name,
2288+
base.name if base.module_name == self.tree.fullname else base.fullname,
22892289
original_class_or_static,
22902290
override_class_or_static,
22912291
context,

test-data/unit/check-classes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ class A:
397397
def __eq__(self, other: A) -> bool: pass # Fail
398398
[builtins fixtures/plugin_attrs.pyi]
399399
[out]
400-
main:2: error: Argument 1 of "__eq__" is incompatible with supertype "object"; supertype defines the argument type as "object"
400+
main:2: error: Argument 1 of "__eq__" is incompatible with supertype "builtins.object"; supertype defines the argument type as "object"
401401
main:2: note: This violates the Liskov substitution principle
402402
main:2: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
403403
main:2: note: It is recommended for "__eq__" to work with arbitrary objects, for example:

test-data/unit/check-functions.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,6 +3592,28 @@ class Bar(Foo):
35923592
def foo(self, value: Union[int, str]) -> Union[int, str]:
35933593
return super().foo(value) # E: Call to abstract method "foo" of "Foo" with trivial body via super() is unsafe
35943594

3595+
[case fullNamesOfImportedBaseClassesDisplayed]
3596+
from a import A
3597+
3598+
class B(A):
3599+
def f(self, x: str) -> None: # E: Argument 1 of "f" is incompatible with supertype "a.A"; supertype defines the argument type as "int" \
3600+
# N: This violates the Liskov substitution principle \
3601+
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
3602+
...
3603+
def g(self, x: str) -> None: # E: Signature of "g" incompatible with supertype "a.A" \
3604+
# N: Superclass: \
3605+
# N: def g(self) -> None \
3606+
# N: Subclass: \
3607+
# N: def g(self, x: str) -> None
3608+
...
3609+
3610+
[file a.py]
3611+
class A:
3612+
def f(self, x: int) -> None:
3613+
...
3614+
def g(self) -> None:
3615+
...
3616+
35953617
[case testBoundMethodsAssignedInClassBody]
35963618
from typing import Callable
35973619

test-data/unit/check-modules.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,13 +3206,13 @@ class Bar(Foo):
32063206
def frobnicate(self, *args: int) -> None: pass # type: ignore[override] # I know
32073207
[builtins fixtures/dict.pyi]
32083208
[out1]
3209-
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo"
3209+
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "a.Foo"
32103210
tmp/b.py:3: note: Superclass:
32113211
tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any
32123212
tmp/b.py:3: note: Subclass:
32133213
tmp/b.py:3: note: def frobnicate(self) -> None
32143214
[out2]
3215-
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo"
3215+
tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "a.Foo"
32163216
tmp/b.py:3: note: Superclass:
32173217
tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any
32183218
tmp/b.py:3: note: Subclass:

test-data/unit/fine-grained.test

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,9 +1051,9 @@ class A:
10511051
[file n.py.3]
10521052
[out]
10531053
==
1054-
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "A"
1054+
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "m.A"
10551055
==
1056-
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "A"
1056+
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "m.A"
10571057

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

10721072
[case testAddBaseClassAttributeCausingErrorInSubclass]
10731073
import m
@@ -1974,11 +1974,11 @@ class B:
19741974
class B:
19751975
def foo(self) -> int: return 12
19761976
[out]
1977-
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "B"
1977+
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "b.B"
19781978
==
1979-
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "B"
1979+
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "b.B"
19801980
==
1981-
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "B"
1981+
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "b.B"
19821982
==
19831983

19841984
[case testPreviousErrorInMethodSemanal1]
@@ -7337,7 +7337,7 @@ class Parent:
73377337
def f(self, arg: Any) -> Any: ...
73387338
[out]
73397339
==
7340-
main:4: error: Signature of "f" incompatible with supertype "Parent"
7340+
main:4: error: Signature of "f" incompatible with supertype "b.Parent"
73417341
main:4: note: Superclass:
73427342
main:4: note: @overload
73437343
main:4: note: def f(self, arg: int) -> int
@@ -7380,7 +7380,7 @@ class Parent:
73807380
def f(self, arg: Any) -> Any: ...
73817381
[out]
73827382
==
7383-
main:4: error: Signature of "f" incompatible with supertype "Parent"
7383+
main:4: error: Signature of "f" incompatible with supertype "b.Parent"
73847384
main:4: note: Superclass:
73857385
main:4: note: @overload
73867386
main:4: note: def f(self, arg: int) -> int
@@ -7765,7 +7765,7 @@ def deco(f: F) -> F:
77657765
[out]
77667766
main:7: error: Unsupported operand types for + ("str" and "int")
77677767
==
7768-
main:5: error: Return type "str" of "m" incompatible with return type "int" in supertype "B"
7768+
main:5: error: Return type "str" of "m" incompatible with return type "int" in supertype "b.B"
77697769

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

78757875
[case testAddAbstractMethod]
78767876
from b import D
@@ -8518,7 +8518,7 @@ class D:
85188518
==
85198519
==
85208520
a.py:3: error: Cannot override final attribute "meth" (previously declared in base class "C")
8521-
a.py:3: error: Signature of "meth" incompatible with supertype "C"
8521+
a.py:3: error: Signature of "meth" incompatible with supertype "c.C"
85228522
a.py:3: note: Superclass:
85238523
a.py:3: note: @overload
85248524
a.py:3: note: def meth(self, x: int) -> int
@@ -8565,7 +8565,7 @@ class D:
85658565
==
85668566
==
85678567
a.py:3: error: Cannot override final attribute "meth" (previously declared in base class "C")
8568-
a.py:3: error: Signature of "meth" incompatible with supertype "C"
8568+
a.py:3: error: Signature of "meth" incompatible with supertype "c.C"
85698569
a.py:3: note: Superclass:
85708570
a.py:3: note: @overload
85718571
a.py:3: note: def meth(x: int) -> int

0 commit comments

Comments
 (0)