Skip to content

Commit c090ca2

Browse files
authored
[backport] [used before def] correctly handle walrus operator (#14646) (#14654)
Fixes #14626. I believe changing the way that we analyze call expression makes sense (first, we analyze the callee, then we analyze the arguments).
1 parent 0c99585 commit c090ca2

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

mypy/traverser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ def visit_yield_expr(self, o: YieldExpr) -> None:
253253
o.expr.accept(self)
254254

255255
def visit_call_expr(self, o: CallExpr) -> None:
256+
o.callee.accept(self)
256257
for a in o.args:
257258
a.accept(self)
258-
o.callee.accept(self)
259259
if o.analyzed:
260260
o.analyzed.accept(self)
261261

test-data/unit/check-python38.test

+8
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,14 @@ def foo() -> None:
573573
[x := x + y for y in [1, 2, 3]]
574574
[builtins fixtures/dict.pyi]
575575

576+
[case testWalrusUsedBeforeDef]
577+
# flags: --python-version 3.8
578+
class C:
579+
def f(self, c: 'C') -> None: pass
580+
581+
(x := C()).f(y) # E: Cannot determine type of "y" # E: Name "y" is used before definition
582+
(y := C()).f(y)
583+
576584
[case testOverloadWithPositionalOnlySelf]
577585
# flags: --python-version 3.8
578586
from typing import overload, Optional

0 commit comments

Comments
 (0)