From c090ca2a55ef2343cc39222ffb6e503432cfe9f4 Mon Sep 17 00:00:00 2001 From: Stas Ilinskiy Date: Thu, 9 Feb 2023 09:16:08 -0800 Subject: [PATCH] [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). --- mypy/traverser.py | 2 +- test-data/unit/check-python38.test | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/mypy/traverser.py b/mypy/traverser.py index 378d44c67f47..038d948522f0 100644 --- a/mypy/traverser.py +++ b/mypy/traverser.py @@ -253,9 +253,9 @@ def visit_yield_expr(self, o: YieldExpr) -> None: o.expr.accept(self) def visit_call_expr(self, o: CallExpr) -> None: + o.callee.accept(self) for a in o.args: a.accept(self) - o.callee.accept(self) if o.analyzed: o.analyzed.accept(self) diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index c8fb1eb5aac8..bb88fcc3cff7 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -573,6 +573,14 @@ def foo() -> None: [x := x + y for y in [1, 2, 3]] [builtins fixtures/dict.pyi] +[case testWalrusUsedBeforeDef] +# flags: --python-version 3.8 +class C: + def f(self, c: 'C') -> None: pass + +(x := C()).f(y) # E: Cannot determine type of "y" # E: Name "y" is used before definition +(y := C()).f(y) + [case testOverloadWithPositionalOnlySelf] # flags: --python-version 3.8 from typing import overload, Optional