Skip to content

Commit

Permalink
Store the type for assignment expr (walrus) targets (#9479)
Browse files Browse the repository at this point in the history
Fixes #9054
  • Loading branch information
llchan committed Sep 27, 2020
1 parent f668117 commit 0b4a2c9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mypy/checkexpr.py
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-python38.test
Expand Up @@ -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

0 comments on commit 0b4a2c9

Please sign in to comment.