Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not blindly undefer on leaving fuction #18674

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ def check_func_item(
"""
self.dynamic_funcs.append(defn.is_dynamic() and not type_override)

enclosing_node_deferred = self.current_node_deferred
with self.enter_partial_types(is_function=True):
typ = self.function_type(defn)
if type_override:
Expand All @@ -1119,7 +1120,7 @@ def check_func_item(
raise RuntimeError("Not supported")

self.dynamic_funcs.pop()
self.current_node_deferred = False
self.current_node_deferred = enclosing_node_deferred

if name == "__exit__":
self.check__exit__return_type(defn)
Expand Down Expand Up @@ -5341,6 +5342,7 @@ def check_for_untyped_decorator(
self.options.disallow_untyped_decorators
and is_typed_callable(func.type)
and is_untyped_decorator(dec_type)
and not self.current_node_deferred
):
self.msg.typed_function_untyped_decorator(func.name, dec_expr)

Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3945,3 +3945,21 @@ def deco(fn: Callable[[], T]) -> Callable[[], T]: ...

@deco
def defer() -> int: ...

[case testVariableDeferredWithNestedFunction]
from typing import Callable, TypeVar

T = TypeVar("T")
def deco(fn: Callable[[], T]) -> Callable[[], T]: ...

@deco
def f() -> None:
x = 1
f() # defer current node
x = x

def nested() -> None:
...

# The type below should not be Any.
reveal_type(x) # N: Revealed type is "builtins.int"