Skip to content

Commit dfd50fc

Browse files
joejuzlilevkivskyi
authored andcommitted
Specify return types for the return types incompatible with supertype error (#6979)
Fixes #2797
1 parent 7692f56 commit dfd50fc

8 files changed

+22
-21
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ def erase_override(t: Type) -> Type:
15561556
if not is_subtype(erase_override(override.ret_type),
15571557
original.ret_type):
15581558
self.msg.return_type_incompatible_with_supertype(
1559-
name, name_in_super, supertype, node)
1559+
name, name_in_super, supertype, original.ret_type, override.ret_type, node)
15601560
emitted_msg = True
15611561
elif isinstance(override, Overloaded) and isinstance(original, Overloaded):
15621562
# Give a more detailed message in the case where the user is trying to

mypy/messages.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,10 +826,11 @@ def __eq__(self, other: object) -> bool:
826826

827827
def return_type_incompatible_with_supertype(
828828
self, name: str, name_in_supertype: str, supertype: str,
829+
original: Type, override: Type,
829830
context: Context) -> None:
830831
target = self.override_target(name, name_in_supertype, supertype)
831-
self.fail('Return type of "{}" incompatible with {}'
832-
.format(name, target), context)
832+
self.fail('Return type {} of "{}" incompatible with return type {} in {}'
833+
.format(self.format(override), name, self.format(original), target), context)
833834

834835
def override_target(self, name: str, name_in_super: str,
835836
supertype: str) -> str:

test-data/unit/check-abstract.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ class A(I, J):
326326
def f(self, x: str) -> int: pass \
327327
# E: Argument 1 of "f" is incompatible with supertype "I"; supertype defines the argument type as "int"
328328
def g(self, x: str) -> int: pass \
329-
# E: Return type of "g" incompatible with supertype "J"
329+
# E: Return type "int" of "g" incompatible with return type "str" in supertype "J"
330330
def h(self) -> int: pass # Not related to any base class
331331
[out]
332332

@@ -372,7 +372,7 @@ class A(I):
372372
pass
373373
[out]
374374
main:11: error: Argument 1 of "h" is incompatible with supertype "I"; supertype defines the argument type as "I"
375-
main:11: error: Return type of "h" incompatible with supertype "I"
375+
main:11: error: Return type "I" of "h" incompatible with return type "A" in supertype "I"
376376

377377

378378
-- Accessing abstract members
@@ -770,7 +770,7 @@ class A(metaclass=ABCMeta):
770770
def x(self) -> int: pass
771771
class B(A):
772772
@property
773-
def x(self) -> str: pass # E: Return type of "x" incompatible with supertype "A"
773+
def x(self) -> str: pass # E: Return type "str" of "x" incompatible with return type "int" in supertype "A"
774774
b = B()
775775
b.x() # E: "str" not callable
776776
[builtins fixtures/property.pyi]

test-data/unit/check-class-namedtuple.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ class Child(Base):
572572
return self.x
573573
def good_override(self) -> int:
574574
return 0
575-
def bad_override(self) -> str: # E: Return type of "bad_override" incompatible with supertype "Base"
575+
def bad_override(self) -> str: # E: Return type "str" of "bad_override" incompatible with return type "int" in supertype "Base"
576576
return 'incompatible'
577577

578578
def takes_base(base: Base) -> int:

test-data/unit/check-classes.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class B(A):
281281
def h(self, x: A, y: 'B') -> object: pass # Fail
282282
[out]
283283
main:7: error: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "A"
284-
main:9: error: Return type of "h" incompatible with supertype "A"
284+
main:9: error: Return type "object" of "h" incompatible with return type "A" in supertype "A"
285285

286286
[case testEqMethodsOverridingWithNonObjects]
287287
class A:
@@ -328,7 +328,7 @@ class C(B): # with multiple implementations
328328
def f(self) -> B: # Fail
329329
pass
330330
[out]
331-
main:7: error: Return type of "f" incompatible with supertype "B"
331+
main:7: error: Return type "B" of "f" incompatible with return type "C" in supertype "B"
332332

333333
[case testMethodOverridingWithVoidReturnValue]
334334
import typing
@@ -339,7 +339,7 @@ class B(A):
339339
def f(self) -> A: pass # Fail
340340
def g(self) -> None: pass
341341
[out]
342-
main:6: error: Return type of "f" incompatible with supertype "A"
342+
main:6: error: Return type "A" of "f" incompatible with return type "None" in supertype "A"
343343

344344
[case testOverride__new__WithDifferentSignature]
345345
class A:
@@ -2451,7 +2451,7 @@ class C(A):
24512451
class D(A):
24522452
def __iadd__(self, x: 'A') -> 'B': pass
24532453
[out]
2454-
main:6: error: Return type of "__iadd__" incompatible with "__add__" of supertype "A"
2454+
main:6: error: Return type "A" of "__iadd__" incompatible with return type "B" in "__add__" of supertype "A"
24552455
main:8: error: Argument 1 of "__iadd__" is incompatible with "__add__" of supertype "A"; supertype defines the argument type as "A"
24562456
main:8: error: Signatures of "__iadd__" and "__add__" are incompatible
24572457

test-data/unit/check-selftype.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class C(A):
185185
pass
186186

187187
class D(A):
188-
def copy(self: A) -> A: # E: Return type of "copy" incompatible with supertype "A"
188+
def copy(self: A) -> A: # E: Return type "A" of "copy" incompatible with return type "D" in supertype "A"
189189
pass
190190

191191
TB = TypeVar('TB', bound='B', covariant=True)

test-data/unit/check-typevar-values.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ class B(A[str]):
487487
def f(self) -> 'B': pass
488488
class C(A[str]):
489489
@abstractmethod
490-
def f(self) -> int: # E: Return type of "f" incompatible with supertype "A"
490+
def f(self) -> int: # E: Return type "int" of "f" incompatible with return type "A[str]" in supertype "A"
491491
pass
492492
[out]
493493

test-data/unit/fine-grained.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,9 +1063,9 @@ class A:
10631063
[file n.py.3]
10641064
[out]
10651065
==
1066-
main:3: error: Return type of "f" incompatible with supertype "A"
1066+
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "A"
10671067
==
1068-
main:3: error: Return type of "f" incompatible with supertype "A"
1068+
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "A"
10691069

10701070
[case testModifyBaseClassMethodCausingInvalidOverride]
10711071
import m
@@ -1079,7 +1079,7 @@ class A:
10791079
def f(self) -> int: pass
10801080
[out]
10811081
==
1082-
main:3: error: Return type of "f" incompatible with supertype "A"
1082+
main:3: error: Return type "str" of "f" incompatible with return type "int" in supertype "A"
10831083

10841084
[case testAddBaseClassAttributeCausingErrorInSubclass]
10851085
import m
@@ -1985,11 +1985,11 @@ class B:
19851985
class B:
19861986
def foo(self) -> int: return 12
19871987
[out]
1988-
a.py:9: error: Return type of "foo" incompatible with supertype "B"
1988+
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "B"
19891989
==
1990-
a.py:9: error: Return type of "foo" incompatible with supertype "B"
1990+
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "B"
19911991
==
1992-
a.py:9: error: Return type of "foo" incompatible with supertype "B"
1992+
a.py:9: error: Return type "int" of "foo" incompatible with return type "str" in supertype "B"
19931993
==
19941994

19951995
[case testPreviousErrorInMethodSemanal1]
@@ -7449,7 +7449,7 @@ def deco(f: F) -> F:
74497449
[out]
74507450
main:7: error: Unsupported operand types for + ("str" and "int")
74517451
==
7452-
main:5: error: Return type of "m" incompatible with supertype "B"
7452+
main:5: error: Return type "str" of "m" incompatible with return type "int" in supertype "B"
74537453

74547454
[case testLiskovFineVariableClean-only_when_nocache]
74557455
import b
@@ -7553,7 +7553,7 @@ def deco(f: F) -> F:
75537553
pass
75547554
[out]
75557555
==
7556-
main:5: error: Return type of "m" incompatible with supertype "B"
7556+
main:5: error: Return type "str" of "m" incompatible with return type "int" in supertype "B"
75577557

75587558
[case testAddAbstractMethod]
75597559
from b import D

0 commit comments

Comments
 (0)