Skip to content

Commit 130663b

Browse files
ilevkivskyiJukkaL
authored andcommitted
Properly push scope when checking lambda expressions (#8080)
Fixes #8079. Previously function scope was pushed inconsistently in two branches of `visit_lambda_expr()` (it was only pushed in the second branch by `check_func_item()`). I believe there is still a small inconsistency, although I wasn't able to trigger a corresponding crash, I added a TODO about this.
1 parent a5005f4 commit 130663b

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

mypy/checkexpr.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3236,12 +3236,13 @@ def visit_lambda_expr(self, e: LambdaExpr) -> Type:
32363236
self.chk.return_types.append(AnyType(TypeOfAny.special_form))
32373237
# Type check everything in the body except for the final return
32383238
# statement (it can contain tuple unpacking before return).
3239-
for stmt in e.body.body[:-1]:
3240-
stmt.accept(self.chk)
3241-
# Only type check the return expression, not the return statement.
3242-
# This is important as otherwise the following statements would be
3243-
# considered unreachable. There's no useful type context.
3244-
ret_type = self.accept(e.expr(), allow_none_return=True)
3239+
with self.chk.scope.push_function(e):
3240+
for stmt in e.body.body[:-1]:
3241+
stmt.accept(self.chk)
3242+
# Only type check the return expression, not the return statement.
3243+
# This is important as otherwise the following statements would be
3244+
# considered unreachable. There's no useful type context.
3245+
ret_type = self.accept(e.expr(), allow_none_return=True)
32453246
fallback = self.named_type('builtins.function')
32463247
self.chk.return_types.pop()
32473248
return callable_type(e, fallback, ret_type)
@@ -3250,6 +3251,7 @@ def visit_lambda_expr(self, e: LambdaExpr) -> Type:
32503251
self.chk.return_types.append(inferred_type.ret_type)
32513252
self.chk.check_func_item(e, type_override=type_override)
32523253
if e.expr() not in self.chk.type_map:
3254+
# TODO: return expression must be accepted before exiting function scope.
32533255
self.accept(e.expr(), allow_none_return=True)
32543256
ret_type = self.chk.type_map[e.expr()]
32553257
if isinstance(get_proper_type(ret_type), NoneType):

test-data/unit/check-classes.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6566,3 +6566,12 @@ class A:
65666566

65676567
reveal_type(A().y) # N: Revealed type is 'builtins.int'
65686568
[builtins fixtures/property.pyi]
6569+
6570+
[case testEnclosingScopeLambdaNoCrash]
6571+
class C:
6572+
x = lambda x: x.y.g()
6573+
6574+
[case testEnclosingScopeLambdaNoCrashExplicit]
6575+
from typing import Callable
6576+
class C:
6577+
x: Callable[[C], int] = lambda x: x.y.g() # E: "C" has no attribute "y"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy