Skip to content

Commit 0b4a2c9

Browse files
authored
Store the type for assignment expr (walrus) targets (#9479)
Fixes #9054
1 parent f668117 commit 0b4a2c9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

mypy/checkexpr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3788,6 +3788,9 @@ def accept(self,
37883788
assert typ is not None
37893789
self.chk.store_type(node, typ)
37903790

3791+
if isinstance(node, AssignmentExpr) and not has_uninhabited_component(typ):
3792+
self.chk.store_type(node.target, typ)
3793+
37913794
if (self.chk.options.disallow_any_expr and
37923795
not always_allow_any and
37933796
not self.chk.is_stub and

test-data/unit/check-python38.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,13 @@ def check_partial_list() -> None:
377377
z.append(3)
378378
reveal_type(z) # N: Revealed type is 'builtins.list[builtins.int]'
379379
[builtins fixtures/list.pyi]
380+
381+
[case testWalrusExpr]
382+
def func() -> None:
383+
foo = Foo()
384+
if x := foo.x:
385+
pass
386+
387+
class Foo:
388+
def __init__(self) -> None:
389+
self.x = 123

0 commit comments

Comments
 (0)