8000 Store the type for assignment expr (walrus) targets (#9479) · python/mypy@bbfad46 · GitHub
[go: up one dir, main page]

Skip to content

Commit bbfad46

Browse files
llchanJukkaL
authored andcommitted
Store the type for assignment expr (walrus) targets (#9479)
Fixes #9054
1 parent 498769b commit bbfad46

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
@@ -3778,6 +3778,9 @@ def accept(self,
37783778
assert typ is not None
37793779
self.chk.store_type(node, typ)
37803780

3781+
if isinstance(node, AssignmentExpr) and not has_uninhabited_component(typ):
3782+
self.chk.store_type(node.target, typ)
3783+
37813784
if (self.chk.options.disallow_any_expr and
37823785
not always_allow_any and
37833786
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)
0