From 62f054c79bded0a7276f0d920732d34203b54463 Mon Sep 17 00:00:00 2001 From: Lawrence Chan Date: Thu, 24 Sep 2020 17:42:37 -0500 Subject: [PATCH 1/3] Store the type for assignment expr targets Fixes #9054 --- mypy/checkexpr.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 0cc2f13e3504..c618ed857067 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -3788,6 +3788,9 @@ def accept(self, assert typ is not None self.chk.store_type(node, typ) + if isinstance(node, AssignmentExpr): + 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 From c20c346d2fccceb73ba983037a632d8a324d2a1c Mon Sep 17 00:00:00 2001 From: Lawrence Chan Date: Fri, 25 Sep 2020 15:02:55 -0500 Subject: [PATCH 2/3] Add another walrus expr test --- test-data/unit/check-python38.test | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 From 0d3cfdd6a54624b8c0138c93b9e9931638aa6f57 Mon Sep 17 00:00:00 2001 From: Lawrence Chan Date: Fri, 25 Sep 2020 15:03:48 -0500 Subject: [PATCH 3/3] Only apply walrus type propagation if inhabited --- mypy/checkexpr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c618ed857067..498ca4c77b2c 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -3788,7 +3788,7 @@ def accept(self, assert typ is not None self.chk.store_type(node, typ) - if isinstance(node, AssignmentExpr): + 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