Skip to content

Always reset binder when checking deferred nodes #17643

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

Merged
merged 1 commit into from
Aug 4, 2024
Merged
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
9 changes: 5 additions & 4 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,11 @@ def check_partial(self, node: DeferredNodeType | FineGrainedDeferredNodeType) ->
self.check_top_level(node)
else:
self.recurse_into_functions = True
if isinstance(node, LambdaExpr):
self.expr_checker.accept(node)
else:
self.accept(node)
with self.binder.top_frame_context():
if isinstance(node, LambdaExpr):
self.expr_checker.accept(node)
else:
self.accept(node)

def check_top_level(self, node: MypyFile) -> None:
"""Check only the top-level of a module, skipping function definitions."""
Expand Down
37 changes: 37 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -3409,3 +3409,40 @@ for factory in (
reveal_type(factory) # N: Revealed type is "def () -> Union[builtins.str, None]"
var = factory()
[builtins fixtures/tuple.pyi]

[case testLambdaInDeferredDecoratorNoCrash]
def foo(x):
pass

class Bar:
def baz(self, x):
pass

class Qux(Bar):
@foo(lambda x: None)
def baz(self, x) -> None:
pass
[builtins fixtures/tuple.pyi]

[case testGeneratorInDeferredDecoratorNoCrash]
from typing import Protocol, TypeVar

T = TypeVar("T", covariant=True)

class SupportsNext(Protocol[T]):
def __next__(self) -> T: ...

def next(i: SupportsNext[T]) -> T: ...

def foo(x):
pass

class Bar:
def baz(self, x):
pass

class Qux(Bar):
@next(f for f in [foo])
def baz(self, x) -> None:
pass
[builtins fixtures/tuple.pyi]
Loading