Skip to content

Format types nicely in incorrectly-returning-Any warning #3910

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
Sep 2, 2017
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 @@ -2015,7 +2015,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
# function is not declared to return Any)
if (self.options.warn_return_any and
not is_proper_subtype(AnyType(TypeOfAny.special_form), return_type)):
self.warn(messages.RETURN_ANY.format(return_type), s)
self.msg.incorrectly_returning_any(return_type, s)
return

# Disallow return expressions in functions declared to return
Expand Down
6 changes: 5 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
MISSING_RETURN_STATEMENT = 'Missing return statement'
INVALID_IMPLICIT_RETURN = 'Implicit return in function which does not return'
INCOMPATIBLE_RETURN_VALUE_TYPE = 'Incompatible return value type'
RETURN_ANY = 'Returning Any from function with declared return type "{}"'
RETURN_VALUE_EXPECTED = 'Return value expected'
NO_RETURN_EXPECTED = 'Return statement in function which does not return'
INVALID_EXCEPTION = 'Exception must be derived from BaseException'
Expand Down Expand Up @@ -1005,6 +1004,11 @@ def disallowed_any_type(self, typ: Type, context: Context) -> None:
message = 'Expression type contains "Any" (has type {})'.format(self.format(typ))
self.fail(message, context)

def incorrectly_returning_any(self, typ: Type, context: Context) -> None:
message = 'Returning Any from function declared to return {}'.format(
self.format(typ))
self.warn(message, context)

def untyped_decorated_function(self, typ: Type, context: Context) -> None:
if isinstance(typ, AnyType):
self.fail("Function is untyped after decorator transformation", context)
Expand Down
17 changes: 16 additions & 1 deletion test-data/unit/check-warnings.test
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,22 @@ from typing import Any
def g() -> Any: pass
def f() -> int: return g()
[out]
main:4: warning: Returning Any from function with declared return type "builtins.int"
main:4: warning: Returning Any from function declared to return "int"

[case testReturnAnyFromTypedFunctionWithSpecificFormatting]
# flags: --warn-return-any
from typing import Any, Tuple
typ = Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int, int, int]
def g() -> Any: pass
def f() -> typ: return g()
[out]
main:11: warning: Returning Any from function declared to return <tuple: 91 items>

[case testReturnAnySilencedFromTypedFunction]
# flags: --warn-return-any
Expand Down