From 4bf402836448baed4e993772ff8a02b49f8e119f Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Tue, 30 May 2017 10:32:14 +0200 Subject: [PATCH] Don't warn about returning Any if it is a proper subtype of the return type --- mypy/checker.py | 3 ++- test-data/unit/check-warnings.test | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mypy/checker.py b/mypy/checker.py index 56ff993ea694..a492b6e035dc 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1874,7 +1874,8 @@ def check_return_stmt(self, s: ReturnStmt) -> None: if isinstance(typ, AnyType): # (Unless you asked to be warned in that case, and the # function is not declared to return Any) - if not isinstance(return_type, AnyType) and self.options.warn_return_any: + if (not is_proper_subtype(AnyType(), return_type) and + self.options.warn_return_any): self.warn(messages.RETURN_ANY.format(return_type), s) return diff --git a/test-data/unit/check-warnings.test b/test-data/unit/check-warnings.test index 2f2d592b13fd..c95baec1cc93 100644 --- a/test-data/unit/check-warnings.test +++ b/test-data/unit/check-warnings.test @@ -165,3 +165,20 @@ from typing import Any def g() -> Any: pass def f() -> Any: return g() [out] + +[case testOKReturnAnyIfProperSubtype] +# flags: --warn-return-any --strict-optional +from typing import Any, Optional + +class Test(object): + + def __init__(self) -> None: + self.attr = "foo" # type: Any + + def foo(self, do_it: bool) -> Optional[Any]: + if do_it: + return self.attr # Should not warn here + else: + return None +[builtins fixtures/list.pyi] +[out]