diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 0cc2f13e3504..498ca4c77b2c 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) 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