Skip to content

Commit 35fbfb6

Browse files
ilevkivskyiJukkaL
authored andcommitted
Don't warn about returning Any if it is a proper subtype of the return type (#3473)
Fixes #3472
1 parent 46e41d9 commit 35fbfb6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

mypy/checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,8 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
18741874
if isinstance(typ, AnyType):
18751875
# (Unless you asked to be warned in that case, and the
18761876
# function is not declared to return Any)
1877-
if not isinstance(return_type, AnyType) and self.options.warn_return_any:
1877+
if (not is_proper_subtype(AnyType(), return_type) and
1878+
self.options.warn_return_any):
18781879
self.warn(messages.RETURN_ANY.format(return_type), s)
18791880
return
18801881

test-data/unit/check-warnings.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,20 @@ from typing import Any
165165
def g() -> Any: pass
166166
def f() -> Any: return g()
167167
[out]
168+
169+
[case testOKReturnAnyIfProperSubtype]
170+
# flags: --warn-return-any --strict-optional
171+
from typing import Any, Optional
172+
173+
class Test(object):
174+
175+
def __init__(self) -> None:
176+
self.attr = "foo" # type: Any
177+
178+
def foo(self, do_it: bool) -> Optional[Any]:
179+
if do_it:
180+
return self.attr # Should not warn here
181+
else:
182+
return None
183+
[builtins fixtures/list.pyi]
184+
[out]

0 commit comments

Comments
 (0)