From bbfad4630c06a941c29b47530f9fdb37baa997c1 Mon Sep 17 00:00:00 2001 From: Lawrence Chan Date: Sat, 26 Sep 2020 20:27:34 -0500 Subject: [PATCH] Store the type for assignment expr (walrus) targets (#9479) Fixes #9054 --- mypy/checkexpr.py | 3 +++ test-data/unit/check-python38.test | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index d459d5952e14..103de804a1f0 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -3778,6 +3778,9 @@ def accept(self, assert typ is not None self.chk.store_type(node, typ) + if isinstance(node, AssignmentExpr) and not has_uninhabited_component(typ): + self.chk.store_type(node.target, typ) + if (self.chk.options.disallow_any_expr and not always_allow_any and not self.chk.is_stub and diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index 78d62ae43ba4..8e013751835f 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -377,3 +377,13 @@ def check_partial_list() -> None: z.append(3) reveal_type(z) # N: Revealed type is 'builtins.list[builtins.int]' [builtins fixtures/list.pyi] + +[case testWalrusExpr] +def func() -> None: + foo = Foo() + if x := foo.x: + pass + +class Foo: + def __init__(self) -> None: + self.x = 123