From 3285d21193f976509748be26363ea526056e4735 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Thu, 31 Aug 2017 16:30:14 +0100 Subject: [PATCH 1/3] Format types nicely in incorrectly-returning-Any warning --- mypy/checker.py | 2 +- mypy/messages.py | 6 +++++- test-data/unit/check-warnings.test | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index 2e7943fe48e6..3f1ae83366a6 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -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 diff --git a/mypy/messages.py b/mypy/messages.py index d02b12a4cbde..b8348d7ebf77 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -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' @@ -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 with declared return type {}'.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) diff --git a/test-data/unit/check-warnings.test b/test-data/unit/check-warnings.test index c95baec1cc93..d9902ccf2d9d 100644 --- a/test-data/unit/check-warnings.test +++ b/test-data/unit/check-warnings.test @@ -141,7 +141,7 @@ 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 with declared return type "int" [case testReturnAnySilencedFromTypedFunction] # flags: --warn-return-any From 3c82fe2b1a72121a1b3e2be8c6e04a8353994cdc Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Sat, 2 Sep 2017 11:15:15 +0100 Subject: [PATCH 2/3] Improve wording of error message --- mypy/messages.py | 2 +- test-data/unit/check-warnings.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index b8348d7ebf77..4867b8940d68 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -1005,7 +1005,7 @@ def disallowed_any_type(self, typ: Type, context: Context) -> None: self.fail(message, context) def incorrectly_returning_any(self, typ: Type, context: Context) -> None: - message = 'Returning Any from function with declared return type {}'.format( + message = 'Returning Any from function declared to return {}'.format( self.format(typ)) self.warn(message, context) diff --git a/test-data/unit/check-warnings.test b/test-data/unit/check-warnings.test index d9902ccf2d9d..df9be0db11f0 100644 --- a/test-data/unit/check-warnings.test +++ b/test-data/unit/check-warnings.test @@ -141,7 +141,7 @@ 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 "int" +main:4: warning: Returning Any from function declared to return "int" [case testReturnAnySilencedFromTypedFunction] # flags: --warn-return-any From 737e18172e1ef36abfbe2f40dd881dea9a26d60a Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Sat, 2 Sep 2017 11:15:37 +0100 Subject: [PATCH 3/3] Add test for irregularly formatted types in error message --- test-data/unit/check-warnings.test | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test-data/unit/check-warnings.test b/test-data/unit/check-warnings.test index df9be0db11f0..ef2f68386917 100644 --- a/test-data/unit/check-warnings.test +++ b/test-data/unit/check-warnings.test @@ -143,6 +143,21 @@ def f() -> int: return g() [out] 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 + [case testReturnAnySilencedFromTypedFunction] # flags: --warn-return-any from typing import Any