Skip to content

Commit 8269952

Browse files
committed
Do not blindly undefer on leaving fuction
1 parent 2831eb1 commit 8269952

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

mypy/checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ def check_func_item(
11101110
"""
11111111
self.dynamic_funcs.append(defn.is_dynamic() and not type_override)
11121112

1113+
enclosing_node_deferred = self.current_node_deferred
11131114
with self.enter_partial_types(is_function=True):
11141115
typ = self.function_type(defn)
11151116
if type_override:
@@ -1121,7 +1122,7 @@ def check_func_item(
11211122
raise RuntimeError("Not supported")
11221123

11231124
self.dynamic_funcs.pop()
1124-
self.current_node_deferred = False
1125+
self.current_node_deferred = enclosing_node_deferred
11251126

11261127
if name == "__exit__":
11271128
self.check__exit__return_type(defn)

test-data/unit/check-inference.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3913,3 +3913,21 @@ x = "abc"
39133913
for x in list[int]():
39143914
reveal_type(x) # N: Revealed type is "builtins.int"
39153915
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
3916+
3917+
[case testVariableDeferredWithNestedFunction]
3918+
from typing import Callable, TypeVar
3919+
3920+
T = TypeVar("T")
3921+
def deco(fn: Callable[[], T]) -> Callable[[], T]: ...
3922+
3923+
@deco
3924+
def f() -> None:
3925+
x = 1
3926+
f() # defer current node
3927+
x = x
3928+
3929+
def nested() -> None:
3930+
...
3931+
3932+
# The type below should not be Any.
3933+
reveal_type(x) # N: Revealed type is "builtins.int"

0 commit comments

Comments
 (0)