Skip to content

Commit 27417ba

Browse files
authored
Do not blindly undefer on leaving fuction (#18674)
Fixes #16496 for real. The fix is trivial, just save and restore the previous value.
1 parent 5fcca77 commit 27417ba

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

mypy/checker.py

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

1111+
enclosing_node_deferred = self.current_node_deferred
11111112
with self.enter_partial_types(is_function=True):
11121113
typ = self.function_type(defn)
11131114
if type_override:
@@ -1119,7 +1120,7 @@ def check_func_item(
11191120
raise RuntimeError("Not supported")
11201121

11211122
self.dynamic_funcs.pop()
1122-
self.current_node_deferred = False
1123+
self.current_node_deferred = enclosing_node_deferred
11231124

11241125
if name == "__exit__":
11251126
self.check__exit__return_type(defn)
@@ -5341,6 +5342,7 @@ def check_for_untyped_decorator(
53415342
self.options.disallow_untyped_decorators
53425343
and is_typed_callable(func.type)
53435344
and is_untyped_decorator(dec_type)
5345+
and not self.current_node_deferred
53445346
):
53455347
self.msg.typed_function_untyped_decorator(func.name, dec_expr)
53465348

test-data/unit/check-inference.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3945,3 +3945,21 @@ def deco(fn: Callable[[], T]) -> Callable[[], T]: ...
39453945

39463946
@deco
39473947
def defer() -> int: ...
3948+
3949+
[case testVariableDeferredWithNestedFunction]
3950+
from typing import Callable, TypeVar
3951+
3952+
T = TypeVar("T")
3953+
def deco(fn: Callable[[], T]) -> Callable[[], T]: ...
3954+
3955+
@deco
3956+
def f() -> None:
3957+
x = 1
3958+
f() # defer current node
3959+
x = x
3960+
3961+
def nested() -> None:
3962+
...
3963+
3964+
# The type below should not be Any.
3965+
reveal_type(x) # N: Revealed type is "builtins.int"

0 commit comments

Comments
 (0)