Skip to content

Commit b541af9

Browse files
elazargJukkaL
authored andcommitted
Do not warn on returning any from deferred node (#4295)
Fix #4296. The access to member defined in __init__ causes the current node to be deferred, so mypy assumes expressions are Any but should not warn about it. This is not the only conceivable fix - for example, I'm not sure why mypy should continue checking the function in the first place.
1 parent 952a75e commit b541af9

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2133,7 +2133,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
21332133
if isinstance(typ, AnyType):
21342134
# (Unless you asked to be warned in that case, and the
21352135
# function is not declared to return Any)
2136-
if (self.options.warn_return_any and
2136+
if (self.options.warn_return_any and not self.current_node_deferred and
21372137
not is_proper_subtype(AnyType(TypeOfAny.special_form), return_type)):
21382138
self.msg.incorrectly_returning_any(return_type, s)
21392139
return

test-data/unit/check-warnings.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,15 @@ class Test(object):
197197
return None
198198
[builtins fixtures/list.pyi]
199199
[out]
200+
201+
[case testReturnAnyDeferred]
202+
# flags: --warn-return-any
203+
def foo(a1: A) -> int:
204+
if a1._x:
205+
return 1
206+
n = 1
207+
return n
208+
209+
class A:
210+
def __init__(self, x: int) -> None:
211+
self._x = x

0 commit comments

Comments
 (0)