Skip to content

Commit 5c9cbc7

Browse files
committed
More verbose error message on invalid return value
1 parent fc32df8 commit 5c9cbc7

File tree

8 files changed

+24
-23
lines changed

8 files changed

+24
-23
lines changed

mypy/checker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ def visit_return_stmt(self, s: ReturnStmt) -> Type:
11921192
self.check_subtype(
11931193
typ, self.return_types[-1], s,
11941194
messages.INCOMPATIBLE_RETURN_VALUE_TYPE
1195+
+ ": expected {}, got {}".format(self.return_types[-1], typ)
11951196
)
11961197
else:
11971198
# Return without a value. It's valid in a generator function.

mypy/test/data/check-basic.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class A: pass
193193
class B: pass
194194
[out]
195195
main: In function "f":
196-
main, line 3: Incompatible return value type
196+
main, line 3: Incompatible return value type: expected __main__.A, got __main__.B
197197

198198
[case testTopLevelContextAndInvalidReturn]
199199
import typing
@@ -204,7 +204,7 @@ class A: pass
204204
class B: pass
205205
[out]
206206
main: In function "f":
207-
main, line 3: Incompatible return value type
207+
main, line 3: Incompatible return value type: expected __main__.A, got __main__.B
208208
main: At top level:
209209
main, line 4: Incompatible types in assignment (expression has type "B", variable has type "A")
210210

@@ -313,7 +313,7 @@ def f(x): # type: (int) -> str
313313
f('')
314314
[out]
315315
main: In function "f":
316-
main, line 2: Incompatible return value type
316+
main, line 2: Incompatible return value type: expected builtins.str, got builtins.int
317317
main: At top level:
318318
main, line 3: Argument 1 to "f" has incompatible type "str"; expected "int"
319319

@@ -327,7 +327,7 @@ A().f('') # Fail
327327
[out]
328328
main: In member "f" of class "A":
329329
main, line 4: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
330-
main, line 5: Incompatible return value type
330+
main, line 5: Incompatible return value type: expected builtins.str, got builtins.int
331331
main: At top level:
332332
main, line 6: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
333333

mypy/test/data/check-classes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class A:
9696
class B: pass
9797
[out]
9898
main: In member "f" of class "A":
99-
main, line 4: Incompatible return value type
99+
main, line 4: Incompatible return value type: expected __main__.A, got __main__.B
100100

101101
[case testSelfArgument]
102102
import typing

mypy/test/data/check-expressions.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ f = lambda: ''.x
828828
f = lambda: ''
829829
[out]
830830
main, line 3: "str" has no attribute "x"
831-
main, line 4: Incompatible return value type
831+
main, line 4: Incompatible return value type: expected builtins.int, got builtins.str
832832
main, line 4: Incompatible types in assignment (expression has type , variable has type )
833833

834834

mypy/test/data/check-functions.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,9 @@ class A: pass
424424
class B: pass
425425
[out]
426426
main: In function "g":
427-
main, line 4: Incompatible return value type
427+
main, line 4: Incompatible return value type: expected __main__.B, got __main__.A
428428
main: In function "f":
429-
main, line 6: Incompatible return value type
429+
main, line 6: Incompatible return value type: expected __main__.A, got __main__.B
430430

431431
[case testDynamicallyTypedNestedFunction]
432432
import typing

mypy/test/data/check-generics.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class p(Generic[T, S]):
476476
def f(s: S, t: T) -> p[T, A]:
477477
a = t # type: S # E: Incompatible types in assignment (expression has type "T", variable has type "S")
478478
s = t # E: Incompatible types in assignment (expression has type "T", variable has type "S")
479-
return p[S, A](s, A()) # E: Incompatible return value type
479+
return p[S, A](s, A()) # E: Incompatible return value type: expected __main__.p[T`-2, __main__.A], got __main__.p[S`-1, __main__.A*]
480480
b = t # type: T
481481
c = s # type: S
482482
return p[T, A](t, A())
@@ -492,8 +492,8 @@ class p(Generic[T, S]):
492492
class A(Generic[T]):
493493
def f(self, s: S, t: T) -> p[S, T]:
494494
s = t # E: Incompatible types in assignment (expression has type "T", variable has type "S")
495-
return p[S, S](s, s) # E: Incompatible return value type
496-
return p[T, T](t, t) # E: Incompatible return value type
495+
return p[S, S](s, s) # E: Incompatible return value type: expected __main__.p[S`-1, T`1], got __main__.p[S`-1, S`-1]
496+
return p[T, T](t, t) # E: Incompatible return value type: expected __main__.p[S`-1, T`1], got __main__.p[T`1, T`1]
497497
t = t
498498
s = s
499499
return p[S, T](s, t)

mypy/test/data/check-statements.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class B:
1515
pass
1616
[out]
1717
main: In function "f":
18-
main, line 4: Incompatible return value type
18+
main, line 4: Incompatible return value type: expected __main__.A, got __main__.B
1919
main: In function "g":
20-
main, line 6: Incompatible return value type
20+
main, line 6: Incompatible return value type: expected __main__.B, got __main__.A
2121

2222
[case testReturnSubtype]
2323
import typing
@@ -31,7 +31,7 @@ class B(A):
3131
pass
3232
[out]
3333
main: In function "f":
34-
main, line 3: Incompatible return value type
34+
main, line 3: Incompatible return value type: expected __main__.B, got __main__.A
3535

3636
[case testReturnWithoutAValue]
3737
import typing

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def g(x: AB) -> AB:
7070
return x.g() # Error
7171
[out]
7272
main: In function "f":
73-
main, line 10: Incompatible return value type
73+
main, line 10: Incompatible return value type: expected __main__.B*, got __main__.A
7474
main: In function "g":
75-
main, line 12: Incompatible return value type
75+
main, line 12: Incompatible return value type: expected __main__.A*, got __main__.B
7676

7777
[case testTypeInferenceAndTypeVarValues]
7878
from typing import typevar
@@ -88,7 +88,7 @@ def f(x: AB) -> AB:
8888
if y:
8989
return y.f()
9090
else:
91-
return y.g() # E: Incompatible return value type
91+
return y.g() # E: Incompatible return value type: expected __main__.A*, got __main__.B
9292
[out]
9393
main: In function "f":
9494

@@ -98,7 +98,7 @@ T = typevar('T', values=(int, str))
9898
def f(x: T) -> List[T]:
9999
return List[T]()
100100
def g(x: T) -> List[T]:
101-
return List[int]() # E: Incompatible return value type
101+
return List[int]() # E: Incompatible return value type: expected builtins.list[builtins.str*], got builtins.list[builtins.int*]
102102
[builtins fixtures/list.py]
103103
[out]
104104
main: In function "g":
@@ -127,7 +127,7 @@ def g(x: T) -> T:
127127
return ''
128128
def h(x: T) -> T:
129129
if isinstance(x, int):
130-
return '' # E: Incompatible return value type
130+
return '' # E: Incompatible return value type: expected builtins.int*, got builtins.str
131131
return x
132132
[builtins fixtures/isinstance.py]
133133
[out]
@@ -143,9 +143,9 @@ def f(x: T) -> T:
143143
return ''
144144
def g(x: T) -> T:
145145
if isinstance(x, int):
146-
return '' # E: Incompatible return value type
146+
return '' # E: Incompatible return value type: expected builtins.int*, got builtins.str
147147
else:
148-
return 2 # E: Incompatible return value type
148+
return 2 # E: Incompatible return value type: expected builtins.str*, got builtins.int
149149
return x
150150
[builtins fixtures/isinstance.py]
151151
[out]
@@ -170,7 +170,7 @@ def f(x: T) -> T:
170170
y = 1
171171
else:
172172
y = object()
173-
return y # E: Incompatible return value type
173+
return y # E: Incompatible return value type: expected builtins.str*, got builtins.object
174174
[builtins fixtures/isinstance.py]
175175
[out]
176176
main: In function "f":
@@ -183,7 +183,7 @@ def f(x: T) -> T:
183183
y = object()
184184
else:
185185
y = ''
186-
return y # E: Incompatible return value type
186+
return y # E: Incompatible return value type: expected builtins.int*, got builtins.object
187187
[builtins fixtures/isinstance.py]
188188
[out]
189189
main: In function "f":

0 commit comments

Comments
 (0)