Skip to content

Commit be5c456

Browse files
OddBlokeilevkivskyi
authored andcommitted
Format types nicely in incorrectly-returning-Any warning (#3910)
This fixes #3899 by using the same formatting as for other messages.
1 parent 32174f6 commit be5c456

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
20152015
# function is not declared to return Any)
20162016
if (self.options.warn_return_any and
20172017
not is_proper_subtype(AnyType(TypeOfAny.special_form), return_type)):
2018-
self.warn(messages.RETURN_ANY.format(return_type), s)
2018+
self.msg.incorrectly_returning_any(return_type, s)
20192019
return
20202020

20212021
# Disallow return expressions in functions declared to return

mypy/messages.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
MISSING_RETURN_STATEMENT = 'Missing return statement'
3030
INVALID_IMPLICIT_RETURN = 'Implicit return in function which does not return'
3131
INCOMPATIBLE_RETURN_VALUE_TYPE = 'Incompatible return value type'
32-
RETURN_ANY = 'Returning Any from function with declared return type "{}"'
3332
RETURN_VALUE_EXPECTED = 'Return value expected'
3433
NO_RETURN_EXPECTED = 'Return statement in function which does not return'
3534
INVALID_EXCEPTION = 'Exception must be derived from BaseException'
@@ -1005,6 +1004,11 @@ def disallowed_any_type(self, typ: Type, context: Context) -> None:
10051004
message = 'Expression type contains "Any" (has type {})'.format(self.format(typ))
10061005
self.fail(message, context)
10071006

1007+
def incorrectly_returning_any(self, typ: Type, context: Context) -> None:
1008+
message = 'Returning Any from function declared to return {}'.format(
1009+
self.format(typ))
1010+
self.warn(message, context)
1011+
10081012
def untyped_decorated_function(self, typ: Type, context: Context) -> None:
10091013
if isinstance(typ, AnyType):
10101014
self.fail("Function is untyped after decorator transformation", context)

test-data/unit/check-warnings.test

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,22 @@ from typing import Any
141141
def g() -> Any: pass
142142
def f() -> int: return g()
143143
[out]
144-
main:4: warning: Returning Any from function with declared return type "builtins.int"
144+
main:4: warning: Returning Any from function declared to return "int"
145+
146+
[case testReturnAnyFromTypedFunctionWithSpecificFormatting]
147+
# flags: --warn-return-any
148+
from typing import Any, Tuple
149+
typ = Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int,
150+
int, int, int, int, int, int, int, int, int, int, int, int, int,
151+
int, int, int, int, int, int, int, int, int, int, int, int, int,
152+
int, int, int, int, int, int, int, int, int, int, int, int, int,
153+
int, int, int, int, int, int, int, int, int, int, int, int, int,
154+
int, int, int, int, int, int, int, int, int, int, int, int, int,
155+
int, int, int, int, int, int, int, int, int, int, int, int, int]
156+
def g() -> Any: pass
157+
def f() -> typ: return g()
158+
[out]
159+
main:11: warning: Returning Any from function declared to return <tuple: 91 items>
145160

146161
[case testReturnAnySilencedFromTypedFunction]
147162
# flags: --warn-return-any

0 commit comments

Comments
 (0)